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

@ -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