11import socket
2+ from sys import argv
23
34PH803W_DEFAULT_TCP_PORT = 12416
45PH803W_PING_INTERVAL = 4000
78
89
910class Device (object ):
10- def __init__ (self ):
11- #self.result = {}
12-
13- self .socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM , socket .IPPROTO_UDP )
14- # Enable broadcasting mode
15- self .socket .setsockopt (socket .SOL_SOCKET , socket .SO_BROADCAST , 1 )
16- # Set a timeout so the socket does not block
17- # indefinitely when trying to receive data.
18- self .socket .settimeout (1 )
11+ def __init__ (self , host ):
12+ self .result = {}
13+ self .host = host
14+ self .socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
15+
16+ def run (self ):
17+ self .loop = True
18+ self .socket .connect ((self .host , PH803W_DEFAULT_TCP_PORT ))
19+
20+ data = bytes .fromhex ('0000000303000006' )
21+ self .socket .sendall (data )
22+ response = self .socket .recv (1024 )
23+ passcode_lenth = response [9 ]
24+ passcode_raw = response [10 : 10 + passcode_lenth ]
25+ passcode = passcode_raw .decode ("utf-8" )
26+
27+ data = bytes .fromhex ('000000030f00000800' ) + passcode_lenth .to_bytes (1 , 'little' ) + passcode_raw
28+ self .socket .sendall (data )
29+ response = self .socket .recv (1024 )
30+ if response [8 ] != 0 :
31+ print ('Error connecting' )
32+
33+ # Connection established, from now on some cyclig bahavior
34+ data = bytes .fromhex ('000000030400009002' )
35+ self .socket .sendall (data )
36+ empty_counter = 0
37+ data = bytes .fromhex ('0000000303000015' )
38+ while self .loop and empty_counter < 10 :
39+ response = self .socket .recv (1024 )
40+ if len (response ) == 0 :
41+ empty_counter += 1
42+ continue
43+ empty_counter = 0
44+ print (response )
45+ if len (response ) == 18 :
46+ flag1 = response [8 ]
47+ if flag1 & 0b0000_0100 :
48+ print ('In water' )
49+ flag2 = response [9 ]
50+ if flag2 & 0b0000_0010 :
51+ print ('ORP on' )
52+ if flag2 & 0b0000_0001 :
53+ print ('PH on' )
54+ #state_raw = response[8 : 9]
55+ ph_raw = response [10 : 12 ]
56+ ph = int .from_bytes (ph_raw , 'big' ) * 0.01
57+ redox_raw = response [12 : 14 ]
58+ redox = int .from_bytes (redox_raw , 'big' ) - 2000
59+ unknown1_raw = response [14 : 16 ]
60+ unknown1 = int .from_bytes (unknown1_raw , 'big' )
61+ unknown2_raw = response [15 : 18 ]
62+ unknown2 = int .from_bytes (unknown2_raw , 'big' )
63+ print ('pH: %s, Redox: %s, U1: %s, U2: %s' % (ph , redox , unknown1 , unknown2 ))
64+
65+
66+
67+ self .socket .sendall (data )
68+ response = self .socket .recv (1024 )
69+ #print(response)
70+
71+
72+ pass
73+ #self.socket.bind((host, PH803W_DEFAULT_TCP_PORT))
74+ #self.socket.listen()
75+ #conn, addr = self.socket.accept()
76+ #with conn:
77+ # print('Connected to: %s' % addr)
78+ # while True:
79+ # data = conn.recv(1024)
80+ # if not data:
81+ # break
82+ # conn.sendall(data)
83+
84+ def abort (self ):
85+ self .loop = False
86+
87+ def close (self ):
88+ self .socket .close ()
89+
90+ def get_result (self ):
91+ return str (self .result )
92+
93+ def __enter__ (self ):
94+ self .run ()
95+ return self
96+
97+ def __exit__ (self , type , value , traceback ):
98+ self .socket .close ()
99+
100+
101+ if __name__ == '__main__' :
102+ with Device ('192.168.1.89' ) as d :
103+ print (d .get_result ())
0 commit comments