-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEthRly.inc.php
More file actions
89 lines (66 loc) · 1.74 KB
/
EthRly.inc.php
File metadata and controls
89 lines (66 loc) · 1.74 KB
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
<?php
class EthRly {
protected $ip;
protected $port;
protected $socket;
public function __construct($ip, $port) {
$this->ip = $ip;
$this->port = $port;
}
public function __destruct() {
fclose($this->socket);
}
public function connect() {
$this->socket = fsockopen($this->ip, $this->port, $errno, $errstr, 30);
socket_set_timeout($this->socket, 1);
if (!$this->socket) {
}
}
protected function write ($command) {
$out = pack('C', $command);
$ret = fwrite($this->socket, $out);
$res = fread($this->socket,1);
$res = ord($res);
$ret = fwrite($this->socket, "\x00");
return $res;
}
public function turnRelayOn ($relay_num) {
$command = 0x64 + $relay_num;
$res = $this->write($command);
return true;
}
public function turnRelayOff ($relay_num) {
$command = 0x6E + $relay_num;
$res = $this->write($command);
return true;
}
public function turnAllRelayOn() {
$res = $this->write(0x64);
return true;
}
public function turnAllRelayOff() {
$res = $this->write(0x6E);
return true;
}
public function getRelayStatus() {
$res = $this->write(0x5B);
$status = array();
$status[1] = $res & 0b00000001;
$status[2] = ($res & 0b00000010) >> 1;
$status[3] = ($res & 0b00000100) >> 2;
$status[4] = ($res & 0b00001000) >> 3;
$status[5] = ($res & 0b00010000) >> 4;
$status[6] = ($res & 0b00100000) >> 5;
$status[7] = ($res & 0b01000000) >> 6;
$status[8] = ($res & 0b10000000) >> 7;
return $status;
}
public function getBoardVersion() {
$res = $this->write(0x5A);
return $res;
}
public function getInputVoltage() {
$res = floatval($this->write(0x5D))/10.0;
return $res;
}
}