Skip to content

Commit 6faa048

Browse files
committed
Fixed out of sync issues. Added relay code.
1 parent 4eb6aef commit 6faa048

File tree

5 files changed

+82
-56
lines changed

5 files changed

+82
-56
lines changed

Pump/Pump.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def __init__(self, data):
2020
"state": "off"}
2121

2222
def set_state(self, state):
23-
if state == "on":
24-
self.turn_on()
25-
elif state == "off":
26-
self.turn_off()
23+
if self.state != state:
24+
if state == "on":
25+
self.turn_on()
26+
elif state == "off":
27+
self.turn_off()
2728

2829
return self.response_dict
2930

Relay.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from machine import Pin
2+
import ujson
3+
4+
5+
class Relay:
6+
7+
def __init__(self, data):
8+
self.index = str(data["index"])
9+
self.serial_no = data["serial_no"]
10+
self.gpio = Pin(int(data["gpio"]), Pin.OUT)
11+
self.gpio.off()
12+
self.type = data["type"]
13+
self.state = "off"
14+
15+
self.response_dict = {"serial_no": str(self.serial_no), "index": str(self.index), "type": self.type,
16+
"state": "off"}
17+
18+
def set_state(self, state):
19+
if self.state != state:
20+
if state == "on":
21+
self.turn_on()
22+
elif state == "off":
23+
self.turn_off()
24+
25+
return self.response_dict
26+
27+
def turn_on(self):
28+
self.gpio.on()
29+
self.state = "on"
30+
self.update_response_dict("on")
31+
32+
def turn_off(self):
33+
self.gpio.off()
34+
self.state = "off"
35+
self.update_response_dict("off")
36+
37+
def update_response_dict(self, state):
38+
self.response_dict["state"] = state

config.json

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
11
{
2-
"name": "Water Level",
3-
"serial_no": "04142a53-39c8-4612-8642-f1536dd29e5c",
4-
"pi_gpio_interrupt": 2,
5-
"pi_pin_interrupt": 3,
6-
"no_channels": 3,
7-
"channels": {
8-
"0": {
9-
"serial_no": "17336c2e-e610-421f-a7b2-79bcc7c0779e",
10-
"type": "etape",
11-
"index": 0,
12-
"gpio": 26
13-
},
14-
"1": {
15-
"serial_no": "cfb01d39-8571-4532-afcd-29068ccfdc09",
16-
"type": "optical",
17-
"index": 1,
18-
"gpio": 27
19-
},
20-
"2": {
21-
"serial_no": "8a4f4ce1-11d1-48dc-8907-9a0f46de743e",
22-
"type": "optical",
23-
"index": 2,
24-
"gpio": 28
25-
}
26-
}
2+
"serial_no": "8a33fafb-5012-40f3-be1b-e450741ecd14"
273
}

main.py

+37-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import utime
2-
from machine import ADC, Pin, disable_irq, enable_irq
1+
from machine import ADC, Pin
32
from AnalogSensor import AnalogSensor
43
from Pump import Pump
54
import ujson
65
import sys
76
import select
87
import micropython
98

9+
from Relay import Relay
10+
1011
micropython.alloc_emergency_exception_buf(100)
1112

1213
led = Pin(25, Pin.OUT)
@@ -39,6 +40,9 @@ def init():
3940
elif v['type'] == 'pump':
4041
pump = Pump(v)
4142
io_components[str(pump.index)] = pump
43+
elif v['type'] == 'relay':
44+
relay = Relay(v)
45+
io_components[str(relay.index)] = relay
4246
except Exception as e:
4347
print(e)
4448
config = {}
@@ -50,32 +54,39 @@ def loop():
5054
global poll, io_components, led, serial_no
5155
command = ''
5256
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)
5459
except Exception as e:
55-
command = ''
60+
command_json = {}
5661
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()
7990

8091

8192
io_components = init()

serial_no.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
{ "serial_no": "ecd3b04a-7c27-4553-a9f9-4de3f31065a0"
1+
{ "serial_no": "8a33fafb-5012-40f3-be1b-e450741ecd14"
22
}

0 commit comments

Comments
 (0)