|
1 | 1 | import utime
|
2 | 2 | from machine import ADC, Pin, disable_irq, enable_irq
|
3 | 3 | from AnalogSensor import AnalogSensor
|
| 4 | +from Pump import Pump |
4 | 5 | import ujson
|
5 | 6 | import sys
|
6 | 7 | import select
|
7 | 8 | import micropython
|
8 | 9 |
|
9 | 10 | micropython.alloc_emergency_exception_buf(100)
|
10 | 11 |
|
11 |
| -config = {} |
12 |
| - |
13 |
| -with open("""/config.json""") as data_file: |
14 |
| - config = ujson.load(data_file) |
15 |
| - |
16 |
| -serial_no = config["serial_no"] |
17 |
| -num_sensors = config["no_channels"] |
18 |
| - |
19 |
| -analog_sensors = {} |
20 |
| - |
21 |
| -for v in config["channels"].values(): |
22 |
| - analog_sensor = AnalogSensor(v) |
23 |
| - analog_sensors[str(analog_sensor.index)] = analog_sensor |
24 |
| - |
25 | 12 | led = Pin(25, Pin.OUT)
|
26 | 13 | led.on()
|
27 | 14 |
|
28 |
| -p1 = Pin(5, Pin.IN, pull=Pin.PULL_DOWN) |
| 15 | +with open("""/serial_no.json""") as serial_json_file: |
| 16 | + serial_json = ujson.load(serial_json_file) |
29 | 17 |
|
30 |
| -# command = '12345' |
31 |
| -# channel = '-1' |
| 18 | +serial_no = serial_json["serial_no"] |
32 | 19 |
|
33 | 20 | poll = select.poll()
|
34 | 21 | poll.register(sys.stdin, select.POLLIN)
|
35 |
| -interrupt = False |
36 | 22 |
|
| 23 | +config = {} |
37 | 24 |
|
38 |
| -def interrupt_handler(): |
39 |
| - global interrupt |
40 |
| - led.on() |
41 |
| - interrupt = True |
42 | 25 |
|
| 26 | +def init(): |
| 27 | + global config, io_components |
| 28 | + io_components = {} |
43 | 29 |
|
44 |
| -# lbo = p1.irq(handler=lambda pin: interrupt_handler(), trigger=Pin.IRQ_RISING) |
| 30 | + try: |
| 31 | + |
| 32 | + with open("""/config.json""") as data_file: |
| 33 | + config = ujson.load(data_file) |
| 34 | + |
| 35 | + for v in config["channels"]: |
| 36 | + if v['type'] == 'sensor': |
| 37 | + analog_sensor = AnalogSensor(v) |
| 38 | + io_components[str(analog_sensor.index)] = analog_sensor |
| 39 | + elif v['type'] == 'pump': |
| 40 | + pump = Pump(v) |
| 41 | + io_components[str(pump.index)] = pump |
| 42 | + except Exception as e: |
| 43 | + print(e) |
| 44 | + config = {} |
| 45 | + |
| 46 | + return io_components |
45 | 47 |
|
46 | 48 |
|
47 | 49 | def loop():
|
48 |
| - global poll |
| 50 | + global poll, io_components, led, serial_no |
| 51 | + command = '' |
49 | 52 | try:
|
50 | 53 | command = str(poll.poll(200)[0][0].read(4))
|
51 | 54 | except Exception as e:
|
52 | 55 | command = ''
|
53 | 56 | finally:
|
54 | 57 | if command == 'data':
|
55 | 58 | channel = str(poll.poll()[0][0].read(1))
|
56 |
| - print(analog_sensors[channel].response_dict) |
57 |
| - if command == 'init': |
58 |
| - print(config) |
| 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() |
59 | 79 |
|
60 |
| - for sensor in analog_sensors.values(): |
61 |
| - sensor.add_value() |
62 |
| - sensor.update_response_dict() |
63 |
| - led.toggle() |
64 |
| - # utime.sleep(.3) |
65 | 80 |
|
| 81 | +io_components = init() |
66 | 82 |
|
67 | 83 | while True:
|
68 | 84 | loop()
|
69 |
| - |
70 |
| -# timer.init(period=500, mode=Timer.PERIODIC, callback=lambda k: loop()) |
|
0 commit comments