-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (52 loc) · 1.54 KB
/
main.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
from machine import Pin, I2C
import sys
import select
import time
import json
from mcp9808 import MCP9808
# Built-in led for heartbeat.
led = Pin(25, Pin.OUT)
# Pin numbers depend on where it is plugged in.
relays = {
'relay_0': Pin(20, Pin.OUT),
'relay_1': Pin(21, Pin.OUT)
}
# I2C pin numbers shouldn't need to change if using Pico.
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
temp_sensor = MCP9808(i2c)
def handle_input():
data = dict()
if select.select([sys.stdin], [], [], 0)[0]:
# Read the stdin, which is mapped to serial for usb.
line = sys.stdin.readline()
try:
# Try to parse json.
data = json.loads(line)
except Exception as e:
# Otherwise just send raw.
data['input'] = line
return data
while True:
# Get user commands.
serial_input = handle_input()
output = dict(
temp_c=temp_sensor.get_temp(),
time=time.time(),
serial_input=serial_input
)
# Check input for relay commands.
for relay_name, relay in relays.items():
try:
# See if we have a working command for relay.
cmd = getattr(relay, serial_input[relay_name])
cmd()
except (KeyError, AttributeError) as e:
pass
finally:
# Get current value for output.
output[relay_name] = bool(relay.value())
# Send to stdout, which maps to serial for usb.
print(json.dumps(output))
# Heartbeat
led.toggle()
time.sleep(1)