Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover from failed Wi-Fi connections #56

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions software/inspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ def __init__(self):
self.last_trigger = utime.ticks_ms() - self.config.get('TimeBetweenTriggers')
self.last_update = utime.ticks_ms()

self.stream = None
if self.config.get('AccessPoint'):
self.stream = inspec_stream("AccessPoint", self.config.get('AccessPointName'), self.config.get('AccessPointPassword'))

if self.config.get('WiFi'):
self.stream = inspec_stream("Station", self.config.get('WiFiNetworkName'), self.config.get('WiFiKey'))

self.init_stream()

def configure_sensor(self):
sensor.reset()
sensor.set_hmirror(True if self.config.get('HorizontalMirror') else False)
Expand Down Expand Up @@ -118,11 +113,8 @@ def monitor(self):
self.extra_fb.replace(self.img)

self.face.detect(self.img)

if self.config.get('AccessPoint') or self.config.get('WiFi'):
self.manage_stream()

self.detect()
self.send_stream()
self.led.process()

if (utime.ticks_ms() - self.last_update > 128):
Expand All @@ -143,16 +135,6 @@ def monitor(self):
if str(e) == "IDE interrupt":
break

def manage_stream(self):
if not self.stream.connected:
self.stream.start_server()
if self.stream.error:
self.stream.error = None
self.comms.send_data("ip", self.stream.ip)
else:
self.stream.send_image(self.img)


def ble_message_received(self, message):
print("ble message", message)

Expand All @@ -177,17 +159,14 @@ def ble_message_received(self, message):
message = message.replace("update.setting.", "")
setting, value = message.split(':')

if setting == "AccessPoint" and value == "1" and self.stream == None:
self.stream = inspec_stream("AccessPoint", self.config.get('AccessPointName'), self.config.get('AccessPointPassword'))
self.comms.send_data("ip", self.stream.ip)

if setting == "WiFi" and value == "1" and self.stream == None:
self.stream = inspec_stream("Station", self.config.get('WiFiNetworkName'), self.config.get('WiFiKey'))
self.comms.send_data("ip", self.stream.ip)

self.config.set(setting, value)
self.config.save()

if (setting == "AccessPoint" or setting == "WiFi") and value == "1" and self.stream == None:
self.init_stream()
if self.stream != None:
self.comms.send_data("ip", self.stream.ip)

if self.config.is_sensor_setting(setting):
self.configure_sensor()

Expand Down Expand Up @@ -246,3 +225,30 @@ def trigger(self):

if self.stream == None or not self.stream.connected:
self.comms.send_image(self.img)

def init_stream(self):
try:
if self.config.get('AccessPoint'):
self.stream = inspec_stream("AccessPoint", self.config.get('AccessPointName'), self.config.get('AccessPointPassword'))

if self.config.get('WiFi'):
self.stream = inspec_stream("Station", self.config.get('WiFiNetworkName'), self.config.get('WiFiKey'))

except Exception as e:
print("init_stream error: ", str(e))
self.stream = None

def send_stream(self):
if self.stream == None:
return

if not self.config.get('AccessPoint') and not self.config.get('WiFi'):
return

if not self.stream.connected:
self.stream.start_server()
if self.stream.error:
self.stream.error = None
self.comms.send_data("ip", self.stream.ip)
else:
self.stream.send_image(self.img)
4 changes: 4 additions & 0 deletions software/wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ def connect_network(self, ssid, password):
self.wlan.active(True)
self.wlan.connect(ssid=ssid, key=password)

attempts = 0
while not self.wlan.isconnected():
print("Connecting to " + ssid)
time.sleep_ms(1000)
attempts = attempts + 1
if attempts > 10:
raise Exception("Failed to connect to " + ssid)

def start_server(self):
try:
Expand Down