forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_C022_ESPNow.py
1470 lines (1401 loc) · 46.5 KB
/
_C022_ESPNow.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#############################################################################
##################### ESPNow controller for RPIEasy #########################
#############################################################################
#
# This controller is able to send and receive data through ESPNow using a
# serially connected ESP8266.
#
# Copyright (C) 2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import controller
import misc
import rpieGlobals
import time
from rpieTime import *
import webserver
import commands
import Settings
from datetime import datetime
import lib.lib_p2pbuffer as p2pbuffer
import threading
import serial
import lib.lib_serial as rpiSerial
CAPABILITY_BYTE = (1+2) # send and receive
class Controller(controller.ControllerProto):
CONTROLLER_ID = 22
CONTROLLER_NAME = "ESPNow (EXPERIMENTAL)"
def __init__(self,controllerindex):
controller.ControllerProto.__init__(self,controllerindex)
self.usesID = True
self.onmsgcallbacksupported = False # use direct set_value() instead of generic callback to make sure that values setted anyway
self.controllerport = 1
self.lastsysinfo = 0
self.sysinfoperiod = 180 # seconds
self.timer30s = True
self.defaultunit = 0
self.enablesend = True
self.bsize = 8
self.sbit = 1
self.baud = 115200
self.bgproc = None
self.serdev = None
self.timeout = 0.001
self.maxexpecteddata = 250
self.port = ""
self.mac = ""
self.wchan = 1
self.connected = False
def calctimeout(self):
try:
if self.baud<50:
self.baud = 50
except:
self.baud = 50
if self.maxexpecteddata>4096:# Linux serial buffer is fixed max 4096 bytes
self.maxexpecteddata=4096
if self.maxexpecteddata<1:
self.maxexpecteddata=1
self.timeout = (self.bsize+self.sbit)*self.maxexpecteddata/self.baud
def controller_init(self,enablecontroller=None):
if enablecontroller != None:
self.enabled = enablecontroller
self.initialized = False
try:
if str(self.port)!="0" and str(self.port).strip()!="" and self.baud != 0:
self.initialized = False
if self.enabled:
if int(Settings.Settings["Unit"])>0:
self.controllerport = Settings.Settings["Unit"]
self.calctimeout()
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Try to init serial "+str(self.port)+" speed "+str(self.baud))
self.enabled = False
self.connect()
time.sleep(3)
self.enabled = True
if self.connected:
self.initialized = True
misc.shadowlogenabled = True
self.bgproc = threading.Thread(target=self.bgreceiver)
self.bgproc.daemon = True
self.bgproc.start()
time.sleep(2)
mode = self.changemode(0)
if mode!=0:
mode = self.changemode(0)
if mode!=0:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Serial ESPNow GW adapter is not compatible!")
self.initialized = False
self.disconnect()
if self.connected:
if int(self.controllerport)>0:
self.serialcommand("unit,"+str(self.controllerport))
time.sleep(0.2)
self.serialcommand("espnow,dest,"+str(self.defaultunit))
time.sleep(0.2)
self.serialcommand("espnow,chan,"+str(self.wchan))
time.sleep(0.2)
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Serial ESPNow GW initialized")
self.serialcommand("settings")
time.sleep(0.5)
scmdarr = getlastseriallogs(3)
sreading = ""
for i in range(len(scmdarr)):
if "MAC" in scmdarr[i]:
sreading = scmdarr[1].replace("SERIAL: ","").strip()
break
if "MAC" in sreading:
ms = sreading.split(":")
sreading = ""
for i in range(1,len(ms)):
sreading += ms[i].strip()
if i!=len(ms)-1:
sreading += ":"
self.mac = sreading.strip()
else:
try:
defdev = Settings.NetMan.getprimarydevice()
except:
defdev = -1
if defdev != -1:
self.mac = Settings.NetworkDevices[defdev].mac
else:
self.mac = "00:00:00:00:00:00"
# print(self.mac)
else:
self.baud = 0
try:
self.serdev.close() # close in case if already opened by ourself
except:
pass
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,str(e))
return True
def connect(self):
self.connected = False
self.lastsysinfo = 0
try:
self.serdev.close() # close in case if already opened by ourself
except:
pass
try:
self.serdev = rpiSerial.SerialPort(self.port,self.baud,ptimeout=self.timeout,pbytesize=self.bsize,pstopbits=self.sbit)
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Serial connected "+str(self.port))
self.connected = True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Serial failed "+str(e))
try:
self.connected = self.serdev.isopened()
except Exception as e:
self.connected = False
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Open failed "+str(e))
def changemode(self,modenum):
result = -1
self.serialcommand("espnow,mode,"+str(modenum)) # TRY TO SET MODE
time.sleep(0.5)
scmdarr = getlastseriallogs(3)
saddr = 0
if len(scmdarr)>2:
saddr = 2
elif len(scmdarr)>1:
saddr = 1
if saddr>0:
if "espnow,mode" in scmdarr[saddr]:
result = scmdarr[saddr-1].replace("SERIAL: ","").strip()
try:
result = int(result)
except:
result = -1
return result
def disconnect(self):
self.connected = False
try:
self.serdev.close() # close in case if already opened by ourself
except:
pass
def webform_load(self):
webserver.addFormNote("IP and Port parameter is not used!")
try:
choice1 = self.port
options = rpiSerial.serial_portlist()
if len(options)>0:
webserver.addHtml("<tr><td>Serial Device:<td>")
webserver.addSelector_Head("ser_addr",False)
for o in range(len(options)):
webserver.addSelector_Item(options[o],options[o],(str(options[o])==str(choice1)),False)
webserver.addSelector_Foot()
webserver.addFormNote("For RPI use 'raspi-config' tool: 5- Interfacing Options-P6 Serial- (Kernel logging disabled + serial port hardware enabled) before enable this plugin")
webserver.addFormNumericBox("Baudrate","ser_spd",self.baud,50,4000000)
webserver.addFormNote("Generic values: 9600, 19200, 38400, 57600, 115200")
# webserver.addFormCheckBox("Enable Sending","sender",self.enablesend)
webserver.addFormNumericBox("Default destination node index","defaultnode",self.defaultunit,0,255)
webserver.addFormNote("Default node index for data sending")
webserver.addFormNote("Detected gateway MAC address "+str(self.mac))
options = []
optionvalues = []
for i in range(1,14):
options.append(str(i))
optionvalues.append(i)
webserver.addFormSelector("Wifi channel","wchannel",len(options),options,optionvalues,None,self.wchan)
webserver.addFormNote("Set the same wifi channel at all nodes!")
webserver.addWideButton("espnow","ESPNow endpoint management","")
else:
webserver.addFormNote("No serial ports found")
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"ESPNow GW init error: "+str(e))
return True
def webform_save(self,params):
try:
self.port = str(webserver.arg("ser_addr",params))
self.baud = int(webserver.arg("ser_spd",params))
self.wchan = int(webserver.arg("wchannel",params))
self.defaultunit = int(webserver.arg("defaultnode",params))
# self.enablesend = (webserver.arg("sender",params)=="on")
except Exception as e:
self.baud = 0
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"ESPNow parameter save: "+str(e))
self.calctimeout()
return True
def nodesort(self,item):
v = 0
try:
v = int(item["unitno"])
except:
v = 0
return v
def bgreceiver(self):
if self.initialized:
while self.enabled:
if self.serdev is not None:
# tt = rpieTime.millis()
try:
while self.serdev.available()>0:
reading = []
fc = self.serdev.read(1)
if int(fc[0])!=255:
reading.append(chr(fc[0]))
breading = self.serdev.readline().decode('utf-8')
else:
reading.append(fc[0])
breading = list(self.serdev.read(self.maxexpecteddata))
reading.extend(breading)
if len(reading)>0:
if int(fc[0])==255:
self.pkt_receiver(bytes(reading))
else:
sstr = ''.join(reading).strip()
if sstr != "":
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"SERIAL: "+sstr)
except Exception as e:
time.sleep(0.2)
time.sleep(0.005)
try:
self.serdev.close()
except:
pass
def pkt_receiver(self,payload):
if self.enabled:
while len(payload)>0:
dp = p2pbuffer.data_packet()
dp.buffer = payload
dp.decode()
# print(dp.pktlen," T:",dp.pkgtype," FL:",len(payload)," PL:",dp.pktlen)
# print("DATA ARRIVED ",payload,dp.buffer)
if int(dp.pkgtype)!=0:
if dp.pkgtype==1:
if int(dp.infopacket["unitno"]) == int(Settings.Settings["Unit"]): # skip own messages
return False
un = getunitordfromnum(dp.infopacket["unitno"]) # process incoming alive reports
if un==-1:
# CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive
Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.infopacket["unitno"],"name":dp.infopacket["name"],"build":dp.infopacket["build"],"type":dp.infopacket["type"],"mac":dp.infopacket["mac"],"lastseen":datetime.now(),"lastrssi":"","cap":dp.infopacket["cap"]})
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"New ESPNOW unit discovered: "+str(dp.infopacket["unitno"])+" "+str(dp.infopacket["name"]))
Settings.p2plist.sort(reverse=False,key=self.nodesort)
else:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Unit alive: "+str(dp.infopacket["unitno"]))
if Settings.p2plist[un]["type"]==0:
Settings.p2plist[un]["name"] = dp.infopacket["name"]
Settings.p2plist[un]["build"] = dp.infopacket["build"]
Settings.p2plist[un]["type"] = dp.infopacket["type"]
Settings.p2plist[un]["mac"] = dp.infopacket["mac"]
Settings.p2plist[un]["cap"] = dp.infopacket["cap"]
Settings.p2plist[un]["lastseen"] = datetime.now()
elif dp.pkgtype==5: # process incoming data
if int(dp.sensordata["sunit"])==int(Settings.Settings["Unit"]):
return False
un = getunitordfromnum(dp.sensordata["sunit"])
if un>-1: # refresh lastseen data
Settings.p2plist[un]["lastseen"] = datetime.now()
else:
Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.sensordata["sunit"],"name":"","build":0,"type":0,"mac":"","lastseen":datetime.now(),"lastrssi":"","cap":1})
if (int(Settings.Settings["Unit"])==int(dp.sensordata["dunit"])) or (0==int(dp.sensordata["dunit"])): # process only if we are the destination or broadcast
ltaskindex = -1
for x in range(0,len(Settings.Tasks)): # check if the sent IDX already exists?
try:
if (type(Settings.Tasks[x]) is not bool and Settings.Tasks[x]):
if Settings.Tasks[x].controlleridx[self.controllerindex]==int(dp.sensordata["idx"]):
ltaskindex = x
break
except Exception as e:
print(e)
dvaluecount = int(dp.sensordata["valuecount"])
if rpieGlobals.VARS_PER_TASK<dvaluecount: # avoid possible buffer overflow
dvaluecount = rpieGlobals.VARS_PER_TASK
if ltaskindex < 0: # create new task if necessarry
devtype = int(dp.sensordata["pluginid"])
m = False
try:
for y in range(len(rpieGlobals.deviceselector)):
if int(rpieGlobals.deviceselector[y][1]) == devtype:
m = __import__(rpieGlobals.deviceselector[y][0])
break
except:
m = False
TempEvent = None
if m:
try:
TempEvent = m.Plugin(-1)
except:
TempEvent = None
if True:
ltaskindex = -1
for x in range(0,len(Settings.Tasks)): # check if there are free TaskIndex slot exists
try:
if (type(Settings.Tasks[x]) is bool):
if Settings.Tasks[x]==False:
ltaskindex = x
break
except:
pass
devtype = 33 # dummy device
m = False
try:
for y in range(len(rpieGlobals.deviceselector)):
if int(rpieGlobals.deviceselector[y][1]) == devtype:
m = __import__(rpieGlobals.deviceselector[y][0])
break
except:
m = False
if m:
if ltaskindex<0:
ltaskindex = len(Settings.Tasks)
try:
Settings.Tasks[ltaskindex] = m.Plugin(ltaskindex)
except:
ltaskindex = len(Settings.Tasks)
Settings.Tasks.append(m.Plugin(ltaskindex)) # add a new device
Settings.Tasks[ltaskindex].plugin_init(True)
Settings.Tasks[ltaskindex].remotefeed = int(dp.sensordata["sunit"]) # True # Mark that this task accepts incoming data updates!
Settings.Tasks[ltaskindex].enabled = True
Settings.Tasks[ltaskindex].interval = 0
Settings.Tasks[ltaskindex].senddataenabled[self.controllerindex]=True
Settings.Tasks[ltaskindex].controlleridx[self.controllerindex]=int(dp.sensordata["idx"])
if TempEvent is not None:
Settings.Tasks[ltaskindex].taskname = TempEvent.PLUGIN_NAME.replace(" ","")
for v in range(dvaluecount):
Settings.Tasks[ltaskindex].valuenames[v] = TempEvent.valuenames[v]
Settings.Tasks[ltaskindex].taskdevicepluginconfig[0] = TempEvent.vtype
Settings.Tasks[ltaskindex].vtype = TempEvent.vtype
else:
Settings.Tasks[ltaskindex].taskname = Settings.Tasks[ltaskindex].PLUGIN_NAME.replace(" ","")
Settings.Tasks[ltaskindex].valuecount = dvaluecount
Settings.savetasks()
if ltaskindex<0:
return False
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Sensordata update arrived from unit "+str(dp.sensordata["sunit"])) # save received values
if Settings.Tasks[ltaskindex].remotefeed:
for v in range(dvaluecount):
Settings.Tasks[ltaskindex].set_value(v+1,dp.sensordata["values"][v],False)
Settings.Tasks[ltaskindex].plugin_senddata()
elif dp.pkgtype==7: # process incoming command
if int(dp.cmdpacket["sunit"])==int(Settings.Settings["Unit"]):
return False
un = getunitordfromnum(dp.cmdpacket["sunit"])
if un>-1: # refresh lastseen data
Settings.p2plist[un]["lastseen"] = datetime.now()
else:
Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.cmdpacket["sunit"],"name":"","build":0,"type":0,"mac":"","lastseen":datetime.now(),"lastrssi":"","cap":1})
if (int(Settings.Settings["Unit"])==int(dp.cmdpacket["dunit"])) or (0==int(dp.cmdpacket["dunit"])): # process only if we are the destination or broadcast
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"Command arrived from "+str(dp.cmdpacket["sunit"]))
commands.doExecuteCommand(dp.cmdpacket["cmdline"],True)
elif dp.pkgtype==8: # process incoming text/log
if int(dp.cmdpacket["sunit"])==int(Settings.Settings["Unit"]):
return False
un = getunitordfromnum(dp.cmdpacket["sunit"])
if un>-1: # refresh lastseen data
Settings.p2plist[un]["lastseen"] = datetime.now()
else:
Settings.p2plist.append({"protocol":"ESPNOW","unitno":dp.cmdpacket["sunit"],"name":"","build":0,"type":0,"mac":"","lastseen":datetime.now(),"lastrssi":"","cap":1})
if (int(Settings.Settings["Unit"])==int(dp.cmdpacket["dunit"])) or (0==int(dp.cmdpacket["dunit"])): # process only if we are the destination or broadcast
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"ESPNOW: "+str(dp.cmdpacket["cmdline"]))
payload = payload[dp.pktlen:-1]
else:
payload = payload[1:-1]
def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,changedvalue=-1): # called by plugin
if self.enabled and self.initialized and self.enablesend and tasknum is not None:
if int(idx)>0:
if int(Settings.Tasks[tasknum].remotefeed) < 1: # do not republish received values
dp2 = p2pbuffer.data_packet()
dp2.sensordata["sunit"] = Settings.Settings["Unit"]
dp2.sensordata["dunit"] = self.defaultunit
dp2.sensordata["idx"] = idx
if tasknum>-1:
dp2.sensordata["pluginid"] = Settings.Tasks[tasknum].pluginid
else:
dp2.sensordata["pluginid"] = 33
dp2.sensordata["valuecount"] = Settings.Tasks[tasknum].valuecount
for u in range(Settings.Tasks[tasknum].valuecount):
dp2.sensordata["values"][u] = Settings.Tasks[tasknum].uservar[u]
dp2.encode(5)
# print(dp2.buffer) # debug
if self.serdev is not None:
try:
self.serdev.write(dp2.buffer)
except Exception as e:
print(e)
def sendcommand(self,unitno,commandstr):
if self.enabled and self.initialized and self.enablesend:
dpc = p2pbuffer.data_packet()
dpc.cmdpacket["sunit"] = Settings.Settings["Unit"]
dpc.cmdpacket["dunit"] = unitno
# print("CMD:",commandstr)
dpc.cmdpacket["cmdline"] = commandstr
dpc.encode(7)
# print("RPIEasy buffer:",dpc.buffer) # debug
if self.serdev is not None:
try:
self.serdev.write(dpc.buffer)
except Exception as e:
print(e)
def timer_thirty_second(self):
if self.enabled and self.initialized and ((time.time()-self.lastsysinfo) >self.sysinfoperiod):
self.sendsysinfo()
def sendsysinfo(self):
if self.enabled and self.initialized and self.enablesend:
dp = p2pbuffer.data_packet()
dp.infopacket["mac"] = self.mac
dp.infopacket["unitno"] = int(Settings.Settings["Unit"])
dp.infopacket["build"] = int(rpieGlobals.BUILD)
dp.infopacket["name"] = Settings.Settings["Name"]
dp.infopacket["type"] = int(rpieGlobals.NODE_TYPE_ID_RPI_EASY_STD)
# CAPABILITIES byte: first bit 1 if able to send, second bit 1 if able to receive
dp.infopacket["cap"] = int(CAPABILITY_BYTE)
dp.encode(1)
# print(dp.buffer) # debug
self.lastsysinfo = time.time()
return True
return False
def serialcommand(self,cmd):
if self.serdev is not None:
try:
while self.serdev.available()>0:
reading = self.serdev.readline()
self.serdev.write(cmd+'\n')
except Exception as e:
pass
# Helper functions
def getunitordfromnum(unitno):
for n in range(len(Settings.p2plist)):
if int(Settings.p2plist[n]["unitno"]) == int(unitno) and str(Settings.p2plist[n]["protocol"]) == "ESPNOW":
return n
return -1
def getlastseriallogs(num=3):
scmdarr = []
for i in reversed(range(len(misc.ShadowLog))):
if len(scmdarr)>=num:
break
if misc.ShadowLog[i]["lvl"]== rpieGlobals.LOG_LEVEL_DEBUG:
if "SERIAL:" in misc.ShadowLog[i]["l"]:
scmdarr.append(misc.ShadowLog[i]["l"].replace("SERIAL:","").strip())
return scmdarr
def getlastespnowlogs(num=1):
scmdarr = []
for i in reversed(range(len(misc.ShadowLog))):
if len(scmdarr)>=num:
break
if misc.ShadowLog[i]["lvl"]== rpieGlobals.LOG_LEVEL_DEBUG:
if "ESPNOW:" in misc.ShadowLog[i]["l"]:
scmdarr.append(misc.ShadowLog[i]["l"].replace("ESPNOW:","").strip())
return scmdarr
############################
## WEBSERVER PART FOLLOWS ##
############################
@webserver.WebServer.route('/espnow')
def handle_espnow(self):
try:
webserver.TXBuffer=""
if self.type == "GET":
responsearr = self.get
else:
responsearr = self.post
try:
managenode = webserver.arg('nodenum',responsearr)
except:
managenode = ""
try:
tasknum = int(webserver.arg('tasknum',responsearr))
except:
tasknum = 0
taskmode = False
i2cmode = False
for i in range(1,48):
try:
if webserver.arg('del'+str(i),responsearr) != '':
if str(managenode)=="local":
commands.doExecuteCommand("serialcommand,taskclear,"+str(i),False)
taskmode = True
break
elif str(managenode)!="":
commands.doExecuteCommand("espnowcommand,"+str(managenode)+",taskclear,"+str(i),False)
taskmode = True
time.sleep(1)
break
except:
pass
if int(tasknum)>0:
taskc = ["Conf",tasknum,0,0,0,0,0,0,0,0,0,0]
taska = ["Task",tasknum,0,-1,-1,-1,0,0,0]
wv = -1
try:
wv = int(webserver.arg('pluginid',responsearr))
except:
wv = -999
if wv > -999:
taska[2] = wv
for i in range(1,4):
try:
wv = int(webserver.arg('pin'+str(i),responsearr))
except:
wv = -999
if wv>-999:
taska[2+i]=wv
try:
wv = int(webserver.arg('interval',responsearr))
except:
wv = -999
if wv>-999:
taska[7] = wv
try:
wv = int(webserver.arg('idx',responsearr))
except:
wv = -999
if wv>-999:
taska[8] = wv
try:
wv = int(webserver.arg('port',responsearr))
except:
wv = -999
if wv>-999:
taska[6] = wv
try:
wv = webserver.arg('pullup',responsearr)
if str(wv)=="on":
wv = 1
else:
wv = 0
except:
wv = 0
if wv>-1:
taskc[2] = wv
try:
wv = 0
wv = webserver.arg('inverse',responsearr)
if str(wv)=="on" or str(wv)=="1":
wv = 1
else:
wv = 0
except:
wv = 0
if wv>-1:
taskc[3] = wv
for i in range(0,8):
try:
wv = webserver.arg('c'+str(i),responsearr)
wv = int(wv)
except:
wv = -1
if str(wv)=="on" or str(wv)=="1":
wv = 1
else:
wv = 0
if wv>-1:
taskc[4+i]=wv
if str(managenode)=="local":
tcmd = "serialcommand,"
else:
tcmd = "espnowcommand,"+str(managenode)+","
tcmdc = tcmd + "espnow,taskconf"
tcmda = tcmd + "espnow,taskadd"
for i in range(1,len(taskc)):
tcmdc += ","+str(taskc[i])
for i in range(1,len(taska)):
tcmda += ","+str(taska[i])
commands.doExecuteCommand(tcmdc,False)
time.sleep(1)
commands.doExecuteCommand(tcmda,False)
# print(tcmdc)
# print(tcmda)
taskmode = True
elif taskmode==False:
for i in range(1,48):
try:
if webserver.arg('add'+str(i),responsearr) != '':
tasknum = i
taskmode = True
taskc = ["Conf",i,0,0,0,0,0,0,0,0,0,0]
taska = ["Task",i,0,-1,-1,-1,0,0,0]
break
except:
pass
if taskmode:
pass
elif ( (webserver.arg('savelocal',responsearr) != '') or (webserver.arg('savenode',responsearr) != '') ):
try:
workmode = int(webserver.arg('workmode',responsearr))
except:
workmode = 0
try:
nodenum = int(webserver.arg('nnodenum',responsearr))
except:
nodenum = 0
try:
dnodenum = int(webserver.arg('dnodenum',responsearr))
except:
dnodenum = 0
try:
name = str(webserver.arg('name',responsearr))
except:
name = ""
try:
wchannel = int(webserver.arg('wchannel',responsearr))
except:
wchannel = 1
try:
deepsleep = int(webserver.arg('deepsleep',responsearr))
except:
deepsleep = 1
if str(managenode)=="local":
tcmd = "serialcommand,"
dt = 0.5
elif str(managenode)!="":
tcmd = "espnowcommand,"+str(managenode)+","
dt = 2
commands.doExecuteCommand(tcmd+"espnow,mode,"+str(workmode),False)
time.sleep(dt)
commands.doExecuteCommand(tcmd+"espnow,name,"+str(name),False)
time.sleep(dt)
commands.doExecuteCommand(tcmd+"espnow,deepsleep,"+str(deepsleep),False)
time.sleep(dt)
commands.doExecuteCommand(tcmd+"espnow,chan,"+str(wchannel),False)
time.sleep(dt)
commands.doExecuteCommand(tcmd+"espnow,dest,"+str(dnodenum),False)
time.sleep(dt)
commands.doExecuteCommand(tcmd+"unit,"+str(nodenum),False)
time.sleep(dt)
if str(managenode)=="local":
pass
elif str(managenode)!="":
commands.doExecuteCommand(tcmd+"reboot",False) # just for sure reboot old node
commands.doExecuteCommand("espnowcommand,"+str(nodenum)+",reboot",False) # reboot new node number
time.sleep(dt*3)
managenode = "" # return to main page instead of re-request all settings
elif webserver.arg('submit',responsearr) != '':
pass
elif webserver.arg('search',responsearr) != '':
commands.doExecuteCommand("espnowcommand,0,espnow,sendinfo",False) # broadcast command to every node
managenode = ""
elif webserver.arg('reboot',responsearr) != '':
if str(managenode)!="local" and str(managenode)!="":
commands.doExecuteCommand("espnowcommand,"+str(managenode)+",reboot",False) # reboot selected node
else:
commands.doExecuteCommand("serialcommand,reboot",False) # reboot selected node
managenode = ""
elif webserver.arg('i2c',responsearr) != '':
i2cmode = True
elif webserver.arg('tasks',responsearr) != '':
taskmode = True
elif webserver.arg('time',responsearr) != '':
if str(managenode)=="local":
commands.doExecuteCommand("serialcommand,espnow,setdate,"+datetime.now().strftime('%Y,%m,%d,%H,%M,%S'),False)
elif str(managenode)!="":
commands.doExecuteCommand("espnowcommand,"+str(managenode)+",espnow,setdate,"+datetime.now().strftime('%Y,%m,%d,%H,%M,%S'),False)
managenode = ""
webserver.sendHeadandTail("TmplStd",webserver._HEAD)
if i2cmode:
webserver.TXBuffer += "<form name='frmadd' method='post'><table class='normal'>"
webserver.addFormHeader("I2C scan on Node "+str(managenode))
if str(managenode)=="local":
commands.doExecuteCommand("serialcommand,i2cscanner",False)
time.sleep(1)
elif str(managenode)!="":
commands.doExecuteCommand("espnowcommand,"+str(managenode)+",i2cscanner",False)
time.sleep(3)
scmdarr = []
for i in reversed(range(len(misc.ShadowLog))):
if len(scmdarr)>=30:
break
if "i2cscanner" in misc.ShadowLog[i]["l"]:
break
if misc.ShadowLog[i]["lvl"]== rpieGlobals.LOG_LEVEL_DEBUG:
if "I2C :" in misc.ShadowLog[i]["l"]:
scmdarr.append(misc.ShadowLog[i]["l"].replace("ESPNOW:","").replace("SERIAL:","").strip())
webserver.TXBuffer += "<TR><TD colspan='2'><textarea readonly rows='10' wrap='on'>"
for i in range(len(scmdarr)):
webserver.TXBuffer += scmdarr[i]+" "
webserver.TXBuffer += "</textarea><br>"
webserver.addButton("espnow","Back")
webserver.TXBuffer += "</table></form>"
webserver.sendHeadandTail("TmplStd",webserver._TAIL)
return webserver.TXBuffer
elif taskmode and int(tasknum)>0: # display plugin selection
webserver.TXBuffer += "<form name='frmadd' method='post'><table class='normal'>"
try:
if ("," in taska):
ts = str(taska).split(",")
tasknum2 = int(ts[1])
else:
tasknum2 = int(taska[1])
except:
tasknum2 = int(tasknum)
webserver.addFormHeader("ESPNow add new task "+str(tasknum2)+" on Node "+str(managenode))
displaytask(taska,taskc)
webserver.addHtml("<tr><td><input type='hidden' name='nodenum' value='"+str(managenode)+"'><input type='hidden' name='tasknum' value='"+str(tasknum2)+"'>")
webserver.addSubmitButton("Save task", "savetask")
webserver.addButton("espnow","Back")
webserver.TXBuffer += "</table></form>"
webserver.sendHeadandTail("TmplStd",webserver._TAIL)
return webserver.TXBuffer
elif taskmode and str(managenode)!="": # display tasklist
webserver.TXBuffer += "<form name='frmtask' method='post'><table class='normal'>"
webserver.addFormHeader("ESPNow tasklist")
tasks = []
confs = []
if str(managenode)=="local": #serial node
webserver.addFormHeader("Serial node")
commands.doExecuteCommand("serialcommand,espnow,tasklist",False)
time.sleep(3)
scmdarr = getlastseriallogs(30)
else: # espnow node
webserver.addFormHeader("Node: "+str(managenode))
commands.doExecuteCommand("espnowcommand,"+str(managenode)+",espnow,tasklist",False)
time.sleep(3)
scmdarr = []
for i in reversed(range(len(misc.ShadowLog))):
if len(scmdarr)>=30:
break
if "espnow,tasklist" in misc.ShadowLog[i]["l"]:
break
if misc.ShadowLog[i]["lvl"]== rpieGlobals.LOG_LEVEL_DEBUG:
if "ESPNOW:" in misc.ShadowLog[i]["l"]:
scmdarr.append(misc.ShadowLog[i]["l"].replace("ESPNOW:","").strip())
if len(scmdarr)>0:
for i in range(len(scmdarr)):
if ",tasklist" in scmdarr[i]:
break
if "Task," in scmdarr[i]:
tasks.append(scmdarr[i])
elif "Conf," in scmdarr[i]:
confs.append(scmdarr[i])
tasknum = 0
tasknum2 = tasknum
if len(tasks)>0:
for i in reversed(range(len(tasks))):
tasknum = len(tasks)-i
try:
if ("," in tasks[i]):
ts = str(tasks[i]).split(",")
tasknum2 = int(ts[1])
else:
tasknum2 = int(tasks[i][1])
except:
tasknum2 = int(tasknum)
webserver.addFormSubHeader("Task "+str(tasknum2))
webserver.addSubmitButton("Delete task "+str(tasknum2), "del"+str(tasknum2))
displaytask(tasks[i],confs[i])
webserver.addFormSeparator(2)
webserver.addHtml("<tr><td><input type='hidden' name='nodenum' value='"+str(managenode)+"'>")
webserver.addSubmitButton("Add new task "+str(tasknum2+1), "add"+str(tasknum2+1))
webserver.addButton("espnow","Back")
webserver.TXBuffer += "</table></form>"
webserver.sendHeadandTail("TmplStd",webserver._TAIL)
return webserver.TXBuffer
elif str(managenode)=="": # display node list
webserver.addFormHeader("ESPNow node list")
webserver.TXBuffer += "<form name='frmespnow' method='post'><table class='multirow'><TR><TH>Select<TH>P2P node number<TH>Name<TH>Build<TH>Type<TH>MAC<TH>Last seen<TH>Capabilities"
webserver.TXBuffer += "<TR>"
webserver.TXBuffer += "<td><input type='radio' name='nodenum' value='local'>"
webserver.TXBuffer +="<TD>serial<TD>local node<TD></TR>"
if len(Settings.p2plist)>0:
for n in Settings.p2plist:
if str(n["protocol"]) == "ESPNOW":
webserver.TXBuffer += "<TR>"
webserver.TXBuffer += "<td><input type='radio' name='nodenum' value='"+ str(n["unitno"])+"'>"
webserver.TXBuffer +="<TD>Unit "+str(n["unitno"])+"<TD>"+str(n["name"])+"<TD>"+str(n["build"])+"<TD>"
ntype = "Unknown"
if int(n["type"])==rpieGlobals.NODE_TYPE_ID_ESP_EASY_STD:
ntype = "ESP Easy"
elif int(n["type"])==rpieGlobals.NODE_TYPE_ID_ESP_EASYM_STD:
ntype = "ESP Easy Mega"
elif int(n["type"])==rpieGlobals.NODE_TYPE_ID_ESP_EASY32_STD:
ntype = "ESP Easy32"
elif int(n["type"])==rpieGlobals.NODE_TYPE_ID_ARDUINO_EASY_STD:
ntype = "Arduino Easy"
elif int(n["type"])==rpieGlobals.NODE_TYPE_ID_NANO_EASY_STD:
ntype = "Nano Easy"
elif int(n["type"])==rpieGlobals.NODE_TYPE_ID_RPI_EASY_STD:
ntype = "RPI Easy"
elif int(n["type"])==rpieGlobals.NODE_TYPE_ID_ATMEGA_EASY_LORA:
ntype = "LoRa32u4"
webserver.TXBuffer += ntype
webserver.TXBuffer += "<TD>"+str(n["mac"])
ldt = n["lastseen"]
lstr = ""
try:
lstr = ldt.strftime('%Y-%m-%d %H:%M:%S')
except:
lstr = str(ldt)
webserver.TXBuffer += "<TD>"+lstr
wm = int(n["cap"])
wms = ""
if (wm & 1)==1:
wms = "SEND "
if (wm & 2)==2:
wms += "RECEIVE "
webserver.TXBuffer += "<TD>"+wms
webserver.TXBuffer += "</table><br>"
webserver.addSubmitButton("Manage selected node", "submit")
webserver.addSubmitButton("Tasks on selected node", "tasks")
webserver.addSubmitButton("I2C scan selected node", "i2c")
webserver.addSubmitButton("Reboot selected node", "reboot")
webserver.addSubmitButton("Set date and time on selected node", "time")
webserver.TXBuffer += "<P>"
webserver.addSubmitButton("Request all nodes to advertise itself", "search")
webserver.TXBuffer += "<P>"
webserver.addButton("espnow","Refresh")
webserver.TXBuffer += "</form>"
elif str(managenode)=="local": # display local settings page
webserver.addFormHeader("Serial node management")
webserver.TXBuffer += "<form name='manespnow' method='post'><table class='multirow'>"
modenum = -1
for i in range(3):
commands.doExecuteCommand("serialcommand,espnow,mode",False)
time.sleep(0.5)
scmdarr = getlastseriallogs(3)
if len(scmdarr)>2:
if ",mode" in scmdarr[2]:
try:
modenum = int(scmdarr[1])
break
except:
modenum = -1
if modenum!=-1: # valid number, process
options = ["Gateway","Send only (remote config wont work!)","Receive only","Send&Receive"]
optionvalues = [0,1,2,3]
webserver.addFormSelector("Working mode","workmode",len(options),options,optionvalues,None,modenum)
nodenum = -1
commands.doExecuteCommand("serialcommand,unit",False)
time.sleep(1)
scmdarr = getlastseriallogs(3)
if len(scmdarr)>2:
if "unit" in scmdarr[2]:
nodenum = scmdarr[1].replace("SERIAL: ","").strip()
try:
nodenum = int(nodenum)
except:
nodenum = -1
if nodenum!=-1:
webserver.addFormNumericBox("Unit number","nnodenum",nodenum,1,254)
dnodenum = -1
commands.doExecuteCommand("serialcommand,espnow,dest",False)
time.sleep(1)
scmdarr = getlastseriallogs(3)
if len(scmdarr)>2:
if ",dest" in scmdarr[2]:
dnodenum = scmdarr[1].replace("SERIAL: ","").strip()
try:
dnodenum = int(dnodenum)
except:
dnodenum = -1
webserver.addFormNumericBox("Destination node number","dnodenum",dnodenum,0,254)
sdat = ""
commands.doExecuteCommand("serialcommand,espnow,name",False)
time.sleep(1)
scmdarr = getlastseriallogs(3)
if len(scmdarr)>2:
if ",name" in scmdarr[2]:
sdat = scmdarr[1].replace("SERIAL: ","").strip()
webserver.addFormTextBox("Unit name","name",sdat,25)
wchan = -1
commands.doExecuteCommand("serialcommand,espnow,chan",False)
time.sleep(1)
scmdarr = getlastseriallogs(3)
if len(scmdarr)>2:
if ",chan" in scmdarr[2]:
wchan = scmdarr[1].replace("SERIAL: ","").strip()
try:
wchan = int(wchan)
except:
wchan = -1
if wchan!=-1:
options = []
optionvalues = []
for i in range(1,14):
options.append(str(i))
optionvalues.append(i)
webserver.addFormSelector("Wifi channel","wchannel",len(options),options,optionvalues,None,wchan)
ds = -1
commands.doExecuteCommand("serialcommand,espnow,deepsleep",False)
time.sleep(1)
scmdarr = getlastseriallogs(3)
if len(scmdarr)>2:
if ",deep" in scmdarr[2]:
ds = scmdarr[1].replace("SERIAL: ","").strip()
try:
ds = int(ds)
except:
ds = -1
if ds != -1:
webserver.addFormNumericBox("DeepSleep timeout","deepsleep",ds,0,4294)
webserver.addUnit("s")
webserver.addFormNote("0 means disabled state,1-4294 means deep sleep timout in sec")
webserver.addHtml("<input type='hidden' name='nodenum' value='"+str(managenode)+"'>")
webserver.addSubmitButton("Save settings", "savelocal")
webserver.addButton("espnow","Back")
webserver.TXBuffer += "</form>"
# print(scmdarr,modenum)
elif str(managenode)!="": # display remote settings page
webserver.addFormHeader("Node management")
# commands.doExecuteCommand("espnowcommand,"+str(managenode)+",espnow,name",False)
webserver.TXBuffer += "<form name='manespnow' method='post'><table class='multirow'>"
modenum = -1
for i in range(3):
commands.doExecuteCommand("espnowcommand,"+str(managenode)+",espnow,mode",False)
time.sleep(1)
scmdarr = getlastespnowlogs(1)
if len(scmdarr)>0:
try:
modenum = int(scmdarr[0])
break
except: