126 lines
3.5 KiB
Python
126 lines
3.5 KiB
Python
import json
|
|
import machine
|
|
import asyncio
|
|
import ntptime
|
|
import select
|
|
import time
|
|
import sys
|
|
|
|
import lib.TMP117
|
|
import lib.mqtt_as
|
|
|
|
import event_loops
|
|
|
|
LED = machine.Pin("LED", machine.Pin.OUT)
|
|
LED.on()
|
|
|
|
def search_dict_list(dictionary, attribute, value):
|
|
"""Returns first instance where attribute = value in list of dictionaries"""
|
|
return [item for item in dictionary if item[attribute] == value][0]
|
|
|
|
# First, load correct config
|
|
with open("global_config.json") as f:
|
|
jdata = json.load(f)
|
|
|
|
uuid = int.from_bytes(machine.unique_id(), "big")
|
|
|
|
config = search_dict_list(jdata, "micro_id", uuid)
|
|
|
|
# Setup i2c buses
|
|
i2c_buses = []
|
|
for bus_config in config["i2c_buses"]:
|
|
bus = {
|
|
"name": bus_config["name"]
|
|
}
|
|
if bus_config["hard_i2c"]:
|
|
bus["obj"] = machine.I2C(
|
|
bus_config["hard_i2c_bus"],
|
|
scl = machine.Pin(bus_config["scl"]),
|
|
sda = machine.Pin(bus_config["sda"]),
|
|
freq = bus_config["freq"])
|
|
else:
|
|
bus["obj"] = machine.SoftI2C(
|
|
scl = machine.Pin(bus_config["scl"]),
|
|
sda = machine.Pin(bus_config["sda"]),
|
|
freq = bus_config["freq"])
|
|
|
|
i2c_buses.append(bus)
|
|
|
|
# Setup sensors
|
|
sensors = []
|
|
for sensor_config in config["sensors"]:
|
|
|
|
i2c_conn = search_dict_list(i2c_buses, "name", sensor_config["i2c_name"])["obj"]
|
|
sensor = lib.TMP117.TMP117(
|
|
name = sensor_config["name"],
|
|
i2c = i2c_conn,
|
|
i2c_addr = sensor_config["i2c_addr"],
|
|
config_int = sensor_config["config"]
|
|
)
|
|
sensors.append(sensor)
|
|
|
|
print(uuid)
|
|
print(i2c_buses)
|
|
print(sensors)
|
|
|
|
|
|
|
|
temperature_topic = config["temperature_topic"]
|
|
inbound_topic = config["inbound_topic"]
|
|
# Setup communication
|
|
if config["wifi_or_usb"] == "wifi":
|
|
# Setup MQTT Client
|
|
mqtt_config = lib.mqtt_as.config
|
|
mqtt_config["ssid"] = config["network_settings"]["ssid"]
|
|
mqtt_config["wifi_pw"] = config["network_settings"]["wifi_pw"]
|
|
mqtt_config["server"] = config["network_settings"]["mqtt_server"]
|
|
mqtt_config["port"] = config["network_settings"]["mqtt_port"]
|
|
mqtt_config["queue_len"] = config["network_settings"]["mqtt_queue_length"]
|
|
|
|
mqtt_client = lib.mqtt_as.MQTTClient(mqtt_config)
|
|
|
|
# Connect to MQTT broker and sync clock via NTP server
|
|
while not mqtt_client.isconnected():
|
|
try:
|
|
asyncio.run(mqtt_client.connect())
|
|
ntptime.settime()
|
|
except OSError as E:
|
|
print(f"Connection failed. ERROR: {E}. Retrying.")
|
|
|
|
LED.off()
|
|
try:
|
|
asyncio.run(event_loops.main_wifi(uuid, sensors, mqtt_client, temperature_topic, inbound_topic))
|
|
finally:
|
|
mqtt_client.close()
|
|
LED.on()
|
|
asyncio.new_event_loop()
|
|
|
|
|
|
elif config["wifi_or_usb"] == "usb":
|
|
while True:
|
|
poll_obj = select.poll()
|
|
poll_obj.register(sys.stdin, select.POLLIN)
|
|
|
|
t1 = time.ticks_ms()
|
|
sys.stdout.write("REQUEST_TIME\n")
|
|
|
|
poll_results = poll_obj.poll(100)
|
|
if poll_results:
|
|
timestamp_ms = int(sys.stdin.readline())
|
|
print(f"Recieved {timestamp_ms}")
|
|
t2 = time.ticks_ms()
|
|
t = time.gmtime(timestamp_ms//1000)
|
|
machine.RTC().datetime((t[0], t[1], t[2], t[6], t[3], t[4], t[5], 0))
|
|
print(f"Time: {time.time(), time.gmtime()}, took {time.ticks_diff(t2, t1)} ms latency")
|
|
break
|
|
else:
|
|
time.sleep(5)
|
|
|
|
LED.off()
|
|
try:
|
|
asyncio.run(event_loops.main_wired(uuid, sensors, temperature_topic, inbound_topic))
|
|
finally:
|
|
LED.on()
|
|
asyncio.new_event_loop()
|
|
|