-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnotladen_aus.php
125 lines (121 loc) · 3.23 KB
/
notladen_aus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/php
<?php
// Activates battery emergency charging from grid
$moxa_ip = "192.168.x.y"; //IP of USR-TCP304
$moxa_port = xxxxx; //Infinisolar 10K
$moxa_timeout = 10;
$debug = 0;
$fp = @fsockopen($moxa_ip, $moxa_port, $errno, $errstr, $moxa_timeout);
fwrite($fp,"^P005HECS".chr(0x0d));
$antw = parse_antw();
if ($debug) {
echo "HECS:\n";
print_r($antw);
}
if ($antw[2]=="1"){
$acladen = "ein";
} else
{
$acladen = "aus";
}
echo "HECS sagt, AC-Laden ist $acladen\n";
// BATS command
fwrite($fp, "^P005BATS".chr(0x0d));
$antw = parse_antw();
if ( is_array($antw) and count($antw) >= 10 ) {
if ($debug) print_r($antw);
$bats_chg = ((int)substr($antw[0],5,4)/10);
$bats_acchg = $antw[16]/10; // Max. AC charging current
}
echo "BATS sagt, Ladestrom ist aktuell ".$bats_chg."A und Max.AC-Ladestrom ".$bats_acchg."A\n";
sleep(1);
echo "**** Jetzt schalten wir NOTLADEN AUS! ****\n";
//$check = cal_crc_half("^S005EDB1");
fwrite($fp, "^S005EDB0".chr(0x0d));
$antw = parse_antw();
echo "ANTWORT von S005EDB: ";
if ($debug) print_r($antw);
if($antw[0] != "^1" ) {
echo "FEHLER!\n";
} else {
echo "OK!\n";
}
if ($debug) print_r($antw);
fwrite($fp,"^P005HECS".chr(0x0d));
$antw = parse_antw();
if ($debug) {
echo "**** HECS:\n";
print_r($antw);
}
if ($antw[2]=="1"){
$acladen = "ein";
} else
{
$acladen = "aus";
}
echo "**** HECS sagt, AC-Laden ist jetzt $acladen\n";
fwrite($fp, "^S011MUCHGC0010".chr(0x0d));
$antw = parse_antw();
echo "ANTWORT von MUCHGC: ";
if ($debug) print_r($antw);
if($antw[0] != "^1" ) {
echo "FEHLER!\n";
} else {
echo "OK!\n";
}
fwrite($fp, "^S010MCHGC0010".chr(0x0d));
$antw = parse_antw();
echo "ANTWORT von MCHGC: ";
if ($debug) print_r($antw);
if($antw[0] != "^1" ) {
echo "FEHLER!\n";
} else {
echo "OK!\n";
}
// BATS command
fwrite($fp, "^P005BATS".chr(0x0d));
$antw = parse_antw();
if ( is_array($antw) and count($antw) >= 10 ) {
if ($debug) print_r($antw);
$bats_chg = ((int)substr($antw[0],5,4)/10);
$bats_acchg = $antw[16]/10; // Max. AC charging current
}
echo "BATS sagt, Ladestrom ist jetzt aktuell ".$bats_chg."A und Max.AC-Ladestrom ".$bats_acchg."A\n";
fclose($fp);
//neede functions
function hex2str($hex) {
$str = '';
for($i=0;$i<strlen($hex);$i+=2) $str .= chr(hexdec(substr($hex,$i,2)));
return $str;
}
function cal_crc_half($pin)
{
$sum = 0;
for($i = 0; $i < strlen($pin); $i++)
{
$sum += ord($pin[$i]);
}
$sum = $sum % 256;
if(strlen($sum)==2) $sum="0".$sum;
if(strlen($sum)==1) $sum="00".$sum;
return $sum;
}
function parse_antw()
{
global $fp, $debug;
$byte="";
$s="";
if($fp){
while( $fp && ($s != chr(13)) )
{
$s=fgetc($fp);
If($s === false) return $byte;
$byte=$byte.$s;
}
}
else exit(7);
$byte_ok = substr($byte,0,-3);
$antw_werte = explode(",",$byte_ok);
return($antw_werte);
}
?>