try and fix reliability issues

This commit is contained in:
Travis Shears 2023-08-15 21:22:13 +02:00
parent b4d6e97b46
commit f5cd4501fe
Signed by: travisshears
GPG key ID: D4C2E4DFAB8BABF8
3 changed files with 19 additions and 31 deletions

View file

@ -7,7 +7,7 @@
"python.analysis.extraPaths": [ "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/boards/0x239A/0x80F4",
"/Users/travis.shears/.vscode/extensions/joedevivo.vscode-circuitpython-0.1.20-darwin-arm64/stubs", "/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.version": "8.2.0",
"circuitpython.board.vid": "0x239A", "circuitpython.board.vid": "0x239A",

View file

@ -1,5 +1,4 @@
import time import time
import supervisor
from weather_station.pubsub import PubSub from weather_station.pubsub import PubSub
from weather_station.timer import Timer from weather_station.timer import Timer
from weather_station.bme280 import BME280 from weather_station.bme280 import BME280
@ -10,28 +9,19 @@ airlift = AirLift(pubsub)
bme280 = BME280(pubsub) bme280 = BME280(pubsub)
timers = [ timers = [
Timer(pubsub, 120), 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: while True:
try: try:
airlift.mqtt_client.loop()
for timer in timers: for timer in timers:
t = time.time() t = time.time()
timer.tick(t) timer.tick(t)
except Exception as e: except (ValueError, RuntimeError, ConnectionError) as e:
# raise # for debugging print("Ran into problem\n", e)
print("encountered problem", e) airlift.esp.reset()
time.sleep(60) time.sleep(5)
supervisor.reload() airlift.connect()
airlift.mqtt_client.reconnect()
time.sleep(5)
continue

View file

@ -13,23 +13,20 @@ class AirLift():
self.wifi_pass = os.getenv("WIFI_PASSWORD") self.wifi_pass = os.getenv("WIFI_PASSWORD")
self._setup() self._setup()
pubsub.subscribe("mqtt_pub", self._publish) pubsub.subscribe("mqtt_pub", self._publish)
def _publish(self, body): def _publish(self, body):
self.mqtt_client.publish(body['topic'], body['msg']) 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: while not self.esp.is_connected:
try: try:
print(f"Trying to connect to {self.ssid}") print(f"Trying to connect to {self.ssid}")
self.esp.connect_AP(self.ssid, self.wifi_pass) self.esp.connect_AP(self.ssid, self.wifi_pass)
except ConnectionError as e: except (RuntimeError, ConnectionError) as e:
print("Could not connect to AP, sleeping for 10 sec then trying again\n", e) print("Ran into problem connecting to AP", e)
time.sleep(10) time.sleep(5)
self.esp.reset()
time.sleep(5)
continue continue
print("Connected to", str(self.esp.ssid, "utf-8"), "\tRSSI:", self.esp.rssi) 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("ESP32 found and in idle mode")
print("Firmware vers.", self.esp.firmware_version) print("Firmware vers.", self.esp.firmware_version)
print("MAC addr:", [hex(i) for i in self.esp.MAC_address]) 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 # MQTT inspiration from https://docs.circuitpython.org/projects/minimqtt/en/latest/examples.html
def connect(mqtt_client, userdata, flags, rc): def connect(mqtt_client, userdata, flags, rc):
@ -71,6 +68,7 @@ class AirLift():
broker=os.getenv("MQTT_HOST"), broker=os.getenv("MQTT_HOST"),
username=os.getenv("MQTT_USER"), username=os.getenv("MQTT_USER"),
password=os.getenv("MQTT_PASSWORD"), password=os.getenv("MQTT_PASSWORD"),
client_id='balcony_weather_station'
) )
self.mqtt_client.on_connect = connect self.mqtt_client.on_connect = connect