25
25
26
26
MQTT_active = config .get ('MQTT' , 'MQTT_active' )
27
27
MQTT_broker = config .get ('MQTT' , 'MQTT_broker' )
28
- MQTT_port = int (config .get ('MQTT' , 'MQTT_port' ))
29
- MQTT_username = config .get ('MQTT' , 'MQTT_username' )
30
- MQTT_password = config .get ('MQTT' , 'MQTT_password' )
31
28
32
29
start_time = time .time ()
33
30
up_time = time .time ()
36
33
errors_no = 0 # used to count no of errors and to calculate %
37
34
errors = 'false'
38
35
39
- print ('PytesSerial build: v0.3.0_20230123 ' )
36
+ print ('PytesSerial build: v0.2.3_20230114 ' )
40
37
41
38
# ------------------------functions area----------------------------
42
39
def log (str ) :
@@ -48,32 +45,23 @@ def log (str) :
48
45
return
49
46
except Exception as e :
50
47
print ("Errorhandling: double error in EventLog" , e )
51
-
48
+
52
49
def parsing_serial ():
53
- global errors
54
- volt_st = None # initiate non critical variable to ensure various firmaware combatibility
55
- current_st = None
56
- temp_st = None
57
- coul_st = None
58
- soh_st = None
59
- heater_st = None
60
- bat_events = None
61
- power_events = None
62
- sys_events = None
63
-
64
- try :
65
- if ser .is_open != True :
66
- ser .open ()
67
- print ('...serial opened' )
68
-
69
- for power in range (1 , powers + 1 ): # do the loop for each battery
70
- line_str = "" # clear line_str
71
- line = "" # clear line
72
- power_bytes = bytes (str (power ), 'ascii' ) # convert to bytes
73
- ser .write (b'pwr ' + power_bytes + b'\n ' ) # write on serial port 'pwr x' command
50
+ global errors
51
+ if ser .is_open != True :
52
+ ser .open ()
53
+ time .sleep (0.2 )
54
+ print ('...serial opened' )
55
+ for power in range (1 , powers + 1 ): # do the loop for each battery
56
+ try :
57
+ # parsing pwr x commmand - reading power bank x
58
+ line_str = "" # clear line_str
59
+ line = "" # clear line
60
+ power_bytes = bytes (str (power ), 'ascii' ) # convert to bytes
61
+ ser .write (b'pwr ' + power_bytes + b'\n ' ) # write on serial port 'pwr x' command
74
62
ser .flush ()
75
63
76
- time .sleep (0.5 ) # calm down a bit ...
64
+ time .sleep (0.2 ) # calm down a bit ...
77
65
buffer = ser .in_waiting
78
66
print ('...writing complete ' , 'in buffer:' , buffer )
79
67
@@ -85,12 +73,12 @@ def parsing_serial():
85
73
print ('...suspicious data set, trying again' )
86
74
errors = 'true'
87
75
88
- log ('*' + str (errors_no )+ '*' + str (buffer )+ '**>' + str (line )) # [DPO] for debug purpose
76
+ log ('*' + str (errors_no )+ '*' + str (buffer )+ '**>' + str (line )) # [DPO] for debug purpose remark the line
89
77
90
78
if ser .is_open == True :
91
79
ser .close ()
92
80
print ('...serial closed' )
93
- return # do not move forward if no end of the parsing group detected
81
+ return # do not move forward if no end of the parsing group detected
94
82
95
83
if line_str [1 :18 ] == 'Voltage :' : voltage = int (line_str [19 :27 ])/ 1000
96
84
if line_str [1 :18 ] == 'Current :' : current = int (line_str [19 :27 ])/ 1000
@@ -105,7 +93,7 @@ def parsing_serial():
105
93
if line_str [1 :18 ] == 'Heater Status :' : heater_st = line_str [19 :27 ]
106
94
if line_str [1 :18 ] == 'Bat Events :' : bat_events = int (line_str [19 :27 ],16 )
107
95
if line_str [1 :18 ] == 'Power Events :' : power_events = int (line_str [19 :27 ],16 )
108
- if line_str [1 :18 ] == 'System Fault :' : sys_events = int (line_str [19 :27 ],16 )
96
+ if line_str [1 :18 ] == 'System Fault :' : sys_events = int (line_str [19 :27 ],16 )
109
97
if line_str [1 :18 ] == 'Command completed' :
110
98
break
111
99
@@ -144,62 +132,28 @@ def parsing_serial():
144
132
'sys_events' : sys_events }
145
133
146
134
pwr .append (pwr_array )
135
+
136
+ except Exception as e :
137
+ print ("...serial parsing error: " + str (e ))
138
+ errors = 'true'
139
+
140
+ log ('*' + str (errors_no )+ '*' + str (buffer )+ '**>' + str (line )) # [DPO] for debug purpose remark the line
147
141
148
- statistics ()
149
-
150
- print ('...serial parsing: ok' )
151
-
152
- except Exception as e :
153
- print ("...serial parsing error: " + str (e ))
154
- errors = 'true'
155
- if ser .is_open == True :
156
- ser .close ()
157
- print ('...serial closed' )
158
- return
159
-
160
- log ('*' + str (errors_no )+ '*' + str (buffer )+ '**>' + str (line )) # [DPO] for debug purpose
142
+ if ser .is_open == True :
143
+ ser .close ()
144
+ print ('...serial closed' )
145
+ return
161
146
162
- def statistics ():
163
- global sys_voltage
164
- global sys_current
165
- global sys_soc
166
- global sys_temp
167
- global sys_basic_st
168
- sys_voltage = 0
169
- sys_current = 0
170
- sys_soc = 0
171
- sys_temp = 0
172
- sys_basic_st = ""
173
-
174
- for power in range (1 , powers + 1 ):
175
- sys_voltage = sys_voltage + pwr [power - 1 ]['voltage' ] # vontage will be the average of all batteries
176
- sys_current = round ((sys_current + pwr [power - 1 ]['current' ]),3 ) # current will be sum of all banks
177
- sys_soc = sys_soc + pwr [power - 1 ]['soc' ] # soc will be the average of all batteries
178
- sys_temp = sys_temp + pwr [power - 1 ]['temperature' ] # temperature will be the average of all batteries
179
-
180
- sys_voltage = round ((sys_voltage / powers ), 3 )
181
- sys_soc = int (sys_soc / powers )
182
- sys_basic_st = pwr [0 ]['basic_st' ] # status will be the master status
183
- sys_temp = round ((sys_temp / powers ), 1 )
147
+ print ('...serial parsing: ok' )
184
148
185
149
def json_serialize ():
186
150
global errors
187
151
global json_data
188
152
try :
189
- json_data = {'relay_local_time' :TimeStamp ,
190
- 'serial_uptime' :uptime ,
191
- 'powers' : powers ,
192
- 'voltage' : sys_voltage ,
193
- 'current' : sys_current ,
194
- 'temperature' : sys_temp ,
195
- 'soc' : sys_soc ,
196
- 'basic_st' : sys_basic_st ,
197
- 'pytes' :pwr }
198
-
153
+ json_data = {'relay_local_time' :TimeStamp , 'serial_uptime' :uptime , 'pytes' :pwr }
199
154
with open (output_path + 'pytes_status.json' , 'w' ) as outfile :
200
155
json .dump (json_data , outfile )
201
- print ('...json creation: ok' )
202
-
156
+ print ('...json creation: ok' )
203
157
except Exception as e :
204
158
print ('...json serailization error: ' + str (e ))
205
159
errors = 'true'
@@ -254,39 +208,13 @@ def maria_db():
254
208
255
209
def mqtt_discovery ():
256
210
try :
257
- MQTT_auth = None
258
- if len (MQTT_username ) > 0 :
259
- MQTT_auth = { 'username' : MQTT_username , 'password' : MQTT_password }
260
211
msg = {}
261
212
config = 1
262
213
names = ["pytes_current" , "pytes_voltage" , "pytes_temperature" , "pytes_soc" , "pytes_status" ]
263
214
ids = ["current" , "voltage" , "temperature" , "soc" , "basic_st" ] #do not change the prefix "pytes_"
264
215
dev_cla = ["current" , "voltage" , "temperature" , "battery" ,"None" ]
265
216
unit_of_meas = ["A" ,"v" ,"°C" , "%" ,"" ]
266
217
267
- # define system sensors
268
- for n in range (5 ):
269
- state_topic = "homeassistant/sensor/pytes/" + str (config )+ "/config"
270
- msg ["name" ] = names [n ]
271
- msg ["stat_t" ] = "homeassistant/sensor/pytes/state"
272
- msg ["uniq_id" ] = "pytes_" + ids [n ]
273
- if dev_cla [n ] != "None" :
274
- msg ["dev_cla" ] = dev_cla [n ]
275
- msg ["unit_of_meas" ] = unit_of_meas [n ]
276
- msg ["val_tpl" ] = "{{ value_json." + ids [n ]+ "}}"
277
- msg ["dev" ] = {"identifiers" : ["pytes" ],"manufacturer" : "PYTES" ,"model" : "E-Box48100R" ,"name" : "pytes_ebox" ,"sw_version" : "1.0" }
278
-
279
- message = json .dumps (msg )
280
- publish .single (state_topic , message , hostname = MQTT_broker , port = MQTT_port , auth = MQTT_auth , qos = 0 , retain = True )
281
-
282
- b = "...mqtt auto discovery initialization :" + str (round (config / (5 * powers + 5 )* 100 )) + " %"
283
- print (b , end = "\r " )
284
-
285
- msg = {}
286
- config = config + 1
287
- time .sleep (2 )
288
-
289
- # define individual batteries sensors
290
218
for power in range (1 , powers + 1 ):
291
219
for n in range (5 ):
292
220
state_topic = "homeassistant/sensor/pytes/" + str (config )+ "/config"
@@ -300,28 +228,23 @@ def mqtt_discovery():
300
228
msg ["dev" ] = {"identifiers" : ["pytes" ],"manufacturer" : "PYTES" ,"model" : "E-Box48100R" ,"name" : "pytes_ebox" ,"sw_version" : "1.0" }
301
229
302
230
message = json .dumps (msg )
303
- publish .single (state_topic , message , hostname = MQTT_broker , port = MQTT_port , auth = MQTT_auth , qos = 0 , retain = True )
231
+ publish .single (state_topic , message , hostname = MQTT_broker , qos = 0 , retain = True )
304
232
305
- b = "...mqtt auto discovery initialization :" + str (round (config / (5 * powers + 5 )* 100 )) + " %"
306
- print (b , end = "\r " )
307
-
308
233
msg = {}
309
234
config = config + 1
235
+ b = "...mqtt auto discovery initialization" + "." * config
236
+ print (b , end = "\r " )
310
237
time .sleep (2 )
311
-
312
238
print ("...mqtt auto discovery initialization completed" )
313
239
314
240
except Exception as e :
315
241
print ('...mqtt_discovery failed' + str (e ))
316
242
317
243
def mqtt_publish ():
318
244
try :
319
- MQTT_auth = None
320
- if len (MQTT_username ) > 0 :
321
- MQTT_auth = { 'username' : MQTT_username , 'password' : MQTT_password }
322
245
state_topic = "homeassistant/sensor/pytes/state"
323
246
message = json .dumps (json_data )
324
- publish .single (state_topic , message , hostname = MQTT_broker , port = MQTT_port , auth = MQTT_auth )
247
+ publish .single (state_topic , message , hostname = MQTT_broker )
325
248
print ('...mqtt publish : ok' )
326
249
327
250
except Exception as e :
0 commit comments