1
- import utime
2
- from machine import ADC , Pin , disable_irq , enable_irq
1
+ from machine import ADC , Pin
3
2
from AnalogSensor import AnalogSensor
4
3
from Pump import Pump
5
4
import ujson
6
5
import sys
7
6
import select
8
7
import micropython
9
8
9
+ from Relay import Relay
10
+
10
11
micropython .alloc_emergency_exception_buf (100 )
11
12
12
13
led = Pin (25 , Pin .OUT )
@@ -39,6 +40,9 @@ def init():
39
40
elif v ['type' ] == 'pump' :
40
41
pump = Pump (v )
41
42
io_components [str (pump .index )] = pump
43
+ elif v ['type' ] == 'relay' :
44
+ relay = Relay (v )
45
+ io_components [str (relay .index )] = relay
42
46
except Exception as e :
43
47
print (e )
44
48
config = {}
@@ -50,32 +54,39 @@ def loop():
50
54
global poll , io_components , led , serial_no
51
55
command = ''
52
56
try :
53
- command = str (poll .poll (200 )[0 ][0 ].read (4 ))
57
+ command = (str (poll .poll (200 )[0 ][0 ].readline ())).strip ()
58
+ command_json = ujson .loads (command )
54
59
except Exception as e :
55
- command = ''
60
+ command_json = {}
56
61
finally :
57
- if command == 'data' :
58
- channel = str (poll .poll ()[0 ][0 ].read (1 ))
59
- print (io_components [str (channel )].response_dict )
60
- elif command == 'init' :
61
- print (serial_no )
62
- new_config = (str (poll .poll ()[0 ][0 ].readline ())).strip ()
63
- try :
64
- with open ("""/config.json""" , 'w' ) as data_file :
65
- ujson .dump (ujson .loads (new_config ), data_file )
66
- except Exception as e :
67
- print (e )
68
- io_components = init ()
69
- elif command == 'stat' :
70
- channel = str (poll .poll ()[0 ][0 ].read (1 ))
71
- state = (str (poll .poll ()[0 ][0 ].readline ())).strip ()
72
- print (str (io_components [channel ].set_state (state )))
73
-
74
- for sensor in io_components .values ():
75
- if sensor .type == 'sensor' :
76
- sensor .add_value ()
77
- sensor .update_response_dict ()
78
- led .toggle ()
62
+ try :
63
+ if 'command' in command_json .keys ():
64
+ command = command_json ['command' ]
65
+ if command == 'data' :
66
+ channel = command_json ['channel' ]
67
+ print (io_components [str (channel )].response_dict )
68
+ elif command == 'serial' :
69
+ print (serial_no )
70
+ elif command == 'init' :
71
+ new_config = command_json ['config' ]
72
+ try :
73
+ with open ("""/config.json""" , 'w' ) as data_file :
74
+ ujson .dump (new_config , data_file )
75
+ except Exception as e :
76
+ print (e )
77
+ io_components = init ()
78
+ elif command == 'state' :
79
+ channel = command_json ['channel' ]
80
+ state = command_json ['state' ]
81
+ print (str (io_components [channel ].set_state (state )))
82
+ except Exception as e :
83
+ print (e )
84
+ finally :
85
+ for sensor in io_components .values ():
86
+ if sensor .type == 'sensor' :
87
+ sensor .add_value ()
88
+ sensor .update_response_dict ()
89
+ led .toggle ()
79
90
80
91
81
92
io_components = init ()
0 commit comments