Skip to content

Commit ac89484

Browse files
committed
Code for pi pico micro controllers to integrate with "OpenHub" code, probably do not need all the configs, but we're trying to get organized
0 parents  commit ac89484

File tree

15 files changed

+793
-0
lines changed

15 files changed

+793
-0
lines changed

AnalogSensor.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from machine import ADC
2+
import ujson
3+
4+
5+
class AnalogSensor:
6+
7+
def __init__(self, data):
8+
self.index = data["index"]
9+
self.serial_no = data["serial_no"]
10+
self.gpio = data["gpio"]
11+
self.type = data["type"]
12+
self.window = 20
13+
self.current_size = 0.0
14+
self.values = []
15+
self.adc = ADC(self.gpio)
16+
self.response_dict = {"serial_no": str(self.serial_no), "index": str(self.index), "type": self.type}
17+
bin = ujson.dumps(self.response_dict) + "\n"
18+
self.response_dict_binary = bytes(bin.encode('ascii'))
19+
20+
def add_value(self):
21+
if self.current_size >= self.window:
22+
self.values.pop(0)
23+
24+
self.values.append(self.adc.read_u16())
25+
26+
if self.current_size < self.window:
27+
self.current_size = self.current_size + 1.0
28+
29+
def get_average_value(self):
30+
x = 0.0
31+
for element in self.values:
32+
x = x + element
33+
return x / self.current_size
34+
35+
def update_response_dict(self):
36+
self.response_dict["values"] = str(self.values)
37+
self.response_dict["averaged"] = str(self.get_average_value())
38+
bin = ujson.dumps(self.response_dict) + "\n"
39+
self.response_dict_binary = bytes(bin.encode('ascii'))
40+
return self.response_dict

DehumidifierPumps/config.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Dehumidifier Pumps",
3+
"serial_no": "a8b8f514-fb2d-41b7-95ad-16abeecdee29",
4+
"no_channels": 2,
5+
"pi_gpio_interrupt": 3,
6+
"pi_pin_interrupt": 5,
7+
"channels": {
8+
"0": {
9+
"serial_no": "5968592d-1781-4176-b43f-04a15862a0a9",
10+
"type": "pump",
11+
"index": 0,
12+
"en": 0,
13+
"gpio0": 27,
14+
"gpio1": 28
15+
},
16+
"1": {
17+
"serial_no": "accf4598-8f75-4895-812f-40c3a069f2ad",
18+
"type": "pump",
19+
"index": 1,
20+
"en": 1,
21+
"gpio0": 20,
22+
"gpio1": 21
23+
}
24+
}
25+
}

Hub.py

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env python3
2+
import RPi.GPIO as GPIO
3+
4+
GPIO.setmode(GPIO.BCM)
5+
from serial import Serial
6+
import time
7+
8+
import json
9+
10+
ser17 = Serial('/dev/ttyACM0', 9600, timeout=1)
11+
ser2 = Serial('/dev/ttyACM1', 9600, timeout=1)
12+
ser3 = Serial('/dev/ttyACM2', 9600, timeout=1)
13+
14+
interrupt17 = 17
15+
GPIO.setup(interrupt17, GPIO.OUT)
16+
17+
interrupt2 = 2
18+
GPIO.setup(interrupt2, GPIO.OUT)
19+
20+
ser17.flush()
21+
ser2.flush()
22+
while True:
23+
for interrupt, ser in [(interrupt2, ser2), (interrupt17, ser17)]:
24+
for x in [0, 1, 2]:
25+
GPIO.output(interrupt, GPIO.HIGH)
26+
command = 'data' + str(x) + "\n"
27+
ser.write(bytes(command.encode('utf-8')))
28+
pico_data = ser.readline()
29+
pico_data1 = pico_data.decode("utf-8", "ignore")
30+
sensor_response = pico_data1[:-2]
31+
print(sensor_response)
32+
GPIO.output(interrupt, GPIO.LOW)
33+
time.sleep(10)
34+
ser17.flush()
35+
ser2.flush()
36+
37+
data = json.loads(sensor_response)
38+
print("Index: " + str(data["index"]))
39+
print("Average Output: " + str(data["averaged"]))
40+
41+
import RPi.GPIO as GPIO
42+
43+
GPIO.setmode(GPIO.BCM)
44+
from serial import Serial
45+
import time
46+
47+
import json
48+
49+
ser17 = Serial('/dev/ttyACM0', 9600, timeout=1)
50+
ser2 = Serial('/dev/ttyACM1', 9600, timeout=1)
51+
ser3 = Serial('/dev/ttyACM2', 9600, timeout=1)
52+
53+
interrupt17 = 17
54+
GPIO.setup(interrupt17, GPIO.OUT)
55+
56+
interrupt2 = 2
57+
GPIO.setup(interrupt2, GPIO.OUT)
58+
59+
ser17.flush()
60+
ser2.flush()
61+
GPIO.output(interrupt2, GPIO.HIGH)
62+
command = 'init' + "\n"
63+
ser2.write(bytes(command.encode('utf-8')))
64+
pico_data = ser2.readline()
65+
pico_data1 = pico_data.decode("utf-8", "ignore")
66+
sensor_response = pico_data1[:-2]
67+
print(sensor_response)
68+
GPIO.output(interrupt2, GPIO.LOW)
69+
70+
import RPi.GPIO as GPIO
71+
72+
GPIO.setmode(GPIO.BCM)
73+
from serial import Serial
74+
import time
75+
76+
ser3 = Serial('/dev/ttyACM2', 9600, timeout=1)
77+
interrupt3 = 3
78+
GPIO.setup(interrupt3, GPIO.OUT)
79+
GPIO.output(interrupt3, GPIO.HIGH)
80+
command = "0on" + "\n"
81+
ser3.write(command.encode('utf-8'))
82+
pico_data = ser3.readline()
83+
pico_data1 = pico_data.decode("utf-8", "ignore")
84+
sensor_response = pico_data1[:-2]
85+
print(sensor_response)
86+
GPIO.output(interrupt3, GPIO.LOW)
87+
time.sleep(1)
88+
GPIO.output(interrupt3, GPIO.HIGH)
89+
command = "0off " + "\n"
90+
ser3.write(command.encode('utf-8'))
91+
pico_data = ser3.readline()
92+
pico_data1 = pico_data.decode("utf-8", "ignore")
93+
sensor_response = pico_data1[:-2]
94+
print(sensor_response)
95+
GPIO.output(interrupt3, GPIO.LOW)
96+
97+
import RPi.GPIO as GPIO
98+
99+
GPIO.setmode(GPIO.BCM)
100+
from serial import Serial
101+
import time
102+
103+
ser3 = Serial('/dev/ttyACM0', 9600, timeout=1)
104+
105+
interrupt3 = 3
106+
107+
GPIO.setup(interrupt3, GPIO.OUT)
108+
GPIO.output(interrupt3, GPIO.HIGH)
109+
110+
command = "stat1off" + "\n"
111+
# command = "init"
112+
ser3.write(command.encode('utf-8'))
113+
pico_data = ser3.readline()
114+
pico_data1 = pico_data
115+
sensor_response = pico_data1[:-2]
116+
print(sensor_response)
117+
118+
GPIO.output(interrupt3, GPIO.LOW)
119+
120+
import RPi.GPIO as GPIO
121+
122+
GPIO.setmode(GPIO.BCM)
123+
from serial import Serial
124+
import time
125+
126+
ser3 = Serial('/dev/ttyACM1', 9600, timeout=1)
127+
128+
interrupt3 = 17
129+
130+
GPIO.setup(interrupt3, GPIO.OUT)
131+
GPIO.output(interrupt3, GPIO.HIGH)
132+
133+
command = 'init'+'\n'
134+
ser3.write(command.encode('utf-8'))
135+
pico_data = ser3.readline()
136+
pico_data1 = pico_data
137+
sensor_response = pico_data1[:-2]
138+
print(sensor_response)
139+
140+
GPIO.output(interrupt3, GPIO.LOW)

Pump.py

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

Pump/Pump.py

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

Pump/config.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Dehumidifier Pumps",
3+
"serial_no": "a8b8f514-fb2d-41b7-95ad-16abeecdee29",
4+
"no_channels": 2,
5+
"pi_gpio_interrupt": 3,
6+
"pi_pin_interrupt": 5,
7+
"channels": {
8+
"0": {
9+
"serial_no": "5968592d-1781-4176-b43f-04a15862a0a9",
10+
"type": "pump",
11+
"index": 0,
12+
"en": 0,
13+
"gpio0": 27,
14+
"gpio1": 28
15+
},
16+
"1": {
17+
"serial_no": "accf4598-8f75-4895-812f-40c3a069f2ad",
18+
"type": "pump",
19+
"index": 1,
20+
"en": 1,
21+
"gpio0": 20,
22+
"gpio1": 21
23+
}
24+
}
25+
}

Pump/main.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import utime
2+
from machine import Timer, Pin, lightsleep
3+
import ujson
4+
import sys
5+
import select
6+
from Pump import Pump
7+
import micropython
8+
9+
micropython.alloc_emergency_exception_buf(100)
10+
11+
12+
with open("""/config.json""") as data_file:
13+
config = ujson.load(data_file)
14+
15+
serial_no = config["serial_no"]
16+
num_pumps = config["no_channels"]
17+
18+
pumps = {}
19+
20+
for v in config["channels"].values():
21+
pump = Pump(v)
22+
pumps[str(pump.index)] = pump
23+
24+
led = Pin(25, Pin.OUT)
25+
led.on()
26+
27+
p1 = Pin(5, Pin.IN, pull=Pin.PULL_DOWN)
28+
29+
poll = select.poll()
30+
poll.register(sys.stdin, select.POLLIN)
31+
32+
33+
def update_led():
34+
led.on()
35+
if list(pumps.values())[0].state == "on" or list(pumps.values())[1].state == "on":
36+
led.on()
37+
else:
38+
utime.sleep_ms(300)
39+
led.off()
40+
41+
42+
command = '12345'
43+
state = "off"
44+
channel = '-1'
45+
46+
47+
def interrupt_handler():
48+
global command, poll, channel, config, state, pumps
49+
led.on()
50+
51+
command = str(poll.poll()[0][0].read(4))
52+
if command == 'init':
53+
print(str(config))
54+
55+
elif command == 'stat':
56+
channel = str(poll.poll()[0][0].read(1))
57+
state = (str(poll.poll()[0][0].readline())).strip()
58+
print(str(pumps[channel].set_state(state)))
59+
# print("interrupt")
60+
# print(str(poll.poll()[0][0].read(1)))
61+
# print(str(poll.poll()[0][0].read(3)))
62+
# print(pumps[(str(poll.poll()[0][0].read(1)).strip())].set_state((str(poll.poll()[0][0].readline())).strip()))
63+
update_led()
64+
65+
66+
lbo = p1.irq(handler=lambda pin: interrupt_handler(), trigger=Pin.IRQ_RISING)
67+
68+
timer = Timer()
69+
70+
timer.init(period=2000, mode=Timer.PERIODIC, callback=lambda k: update_led())
71+
72+
lightsleep()

0 commit comments

Comments
 (0)