diff --git a/CIRCUITPY/.vscode/settings.json b/CIRCUITPY/.vscode/settings.json index d1e24ed..641b76d 100755 --- a/CIRCUITPY/.vscode/settings.json +++ b/CIRCUITPY/.vscode/settings.json @@ -7,7 +7,7 @@ "python.analysis.extraPaths": [ "/Users/travis.shears/.vscode/extensions/joedevivo.vscode-circuitpython-0.1.20-darwin-arm64/boards/0x239A/0x80F4", "/Users/travis.shears/.vscode/extensions/joedevivo.vscode-circuitpython-0.1.20-darwin-arm64/stubs", - "/Users/travis.shears/Library/Application Support/Code/User/globalStorage/joedevivo.vscode-circuitpython/bundle/20230808/adafruit-circuitpython-bundle-py-20230808/lib" + "/Users/travis.shears/Library/Application Support/Code/User/globalStorage/joedevivo.vscode-circuitpython/bundle/20230815/adafruit-circuitpython-bundle-py-20230815/lib" ], "circuitpython.board.version": "8.2.0", "circuitpython.board.vid": "0x239A", diff --git a/CIRCUITPY/code.py b/CIRCUITPY/code.py index a6aa7b8..5cb4c13 100755 --- a/CIRCUITPY/code.py +++ b/CIRCUITPY/code.py @@ -1,5 +1,4 @@ import time -import supervisor from weather_station.pubsub import PubSub from weather_station.timer import Timer from weather_station.bme280 import BME280 @@ -10,28 +9,19 @@ airlift = AirLift(pubsub) bme280 = BME280(pubsub) timers = [ Timer(pubsub, 120), - Timer(pubsub, 86400), ] -board_reload_calls_count = 0 -def board_reload(_): - global board_reload_calls_count - if board_reload_calls_count > 0: - print("resetting board in 10 sec") - time.sleep(10) - supervisor.reload() - board_reload_calls_count += 1 -pubsub.subscribe('tick 86400', board_reload) - - while True: try: + airlift.mqtt_client.loop() for timer in timers: t = time.time() timer.tick(t) - except Exception as e: - # raise # for debugging - print("encountered problem", e) - time.sleep(60) - supervisor.reload() - + except (ValueError, RuntimeError, ConnectionError) as e: + print("Ran into problem\n", e) + airlift.esp.reset() + time.sleep(5) + airlift.connect() + airlift.mqtt_client.reconnect() + time.sleep(5) + continue diff --git a/CIRCUITPY/weather_station/airlift.py b/CIRCUITPY/weather_station/airlift.py index f0f6e9b..89ba73a 100755 --- a/CIRCUITPY/weather_station/airlift.py +++ b/CIRCUITPY/weather_station/airlift.py @@ -13,23 +13,20 @@ class AirLift(): self.wifi_pass = os.getenv("WIFI_PASSWORD") self._setup() pubsub.subscribe("mqtt_pub", self._publish) + def _publish(self, body): self.mqtt_client.publish(body['topic'], body['msg']) - # print(body['msg']) - def reset(self): - print("Resetting AIRLIFT") - self.esp.reset() - time.sleep(5) - self._connect() - def _connect(self): + def connect(self): while not self.esp.is_connected: try: print(f"Trying to connect to {self.ssid}") self.esp.connect_AP(self.ssid, self.wifi_pass) - except ConnectionError as e: - print("Could not connect to AP, sleeping for 10 sec then trying again\n", e) - time.sleep(10) + except (RuntimeError, ConnectionError) as e: + print("Ran into problem connecting to AP", e) + time.sleep(5) + self.esp.reset() + time.sleep(5) continue print("Connected to", str(self.esp.ssid, "utf-8"), "\tRSSI:", self.esp.rssi) @@ -43,7 +40,7 @@ class AirLift(): print("ESP32 found and in idle mode") print("Firmware vers.", self.esp.firmware_version) print("MAC addr:", [hex(i) for i in self.esp.MAC_address]) - self._connect() + self.connect() # MQTT inspiration from https://docs.circuitpython.org/projects/minimqtt/en/latest/examples.html def connect(mqtt_client, userdata, flags, rc): @@ -71,6 +68,7 @@ class AirLift(): broker=os.getenv("MQTT_HOST"), username=os.getenv("MQTT_USER"), password=os.getenv("MQTT_PASSWORD"), + client_id='balcony_weather_station' ) self.mqtt_client.on_connect = connect