update autoreconnect
This commit is contained in:
parent
c0a5dbbf55
commit
2c3711a91d
@ -1,9 +1,9 @@
|
||||
{
|
||||
"status_led_pin": 22,
|
||||
"status_led_pin": 5,
|
||||
"battery_coeff": 2.25,
|
||||
"sumo_id": "xxxxxxxx",
|
||||
"firmware_timestamp": "2019.05.07 21:23:00",
|
||||
"firmware_version": "0.5.0",
|
||||
"firmware_timestamp": "2019.05.12 17:04:00",
|
||||
"firmware_version": "0.5.1",
|
||||
"left_servo_tuning": 33,
|
||||
"right_servo_tuning": 33,
|
||||
"ultrasonic_distance": 40,
|
||||
|
38
main.py
38
main.py
@ -21,20 +21,26 @@ def step():
|
||||
|
||||
# The WebSocket processing thread
|
||||
def ws_handler():
|
||||
global conn
|
||||
global conn, watchdog_counter
|
||||
|
||||
while True:
|
||||
# When WiFi has just been reconnected
|
||||
if wlan.isconnected() and not sumorobot.is_wifi_connected:
|
||||
print("main.py reconnected to Wi-Fi")
|
||||
# Stop blinking status LED
|
||||
timer.deinit()
|
||||
# Turn status LED to steady ON
|
||||
sumorobot.set_led(STATUS, True)
|
||||
sumorobot.is_wifi_connected = True
|
||||
# When WiFi has just been disconnected
|
||||
elif not wlan.isconnected() and sumorobot.is_wifi_connected:
|
||||
print("main.py lost Wi-Fi connection, reconnecting...")
|
||||
print("main.py lost Wi-Fi, reconnecting to Wi-Fi")
|
||||
# Reinitiate the Wi-Fi connection
|
||||
wlan.connect(ssid, config["wifis"][ssid])
|
||||
# Turn OFF status LED
|
||||
sumorobot.set_led(STATUS, False)
|
||||
sumorobot.is_wifi_connected = False
|
||||
# Start bliking status LED
|
||||
timer.init(period=2000, mode=Timer.PERIODIC, callback=sumorobot.toggle_led)
|
||||
elif not wlan.isconnected():
|
||||
# Continue to wait for a WiFi connection
|
||||
@ -44,11 +50,14 @@ def ws_handler():
|
||||
try: # Try to read from the WebSocket
|
||||
data = conn.recv()
|
||||
except: # Socket timeout, no data received
|
||||
# TODO: implement watchdog
|
||||
# Try reconnecting to the websocket if Wi-Fi is connected
|
||||
if wlan.isconnected():
|
||||
print("main.py WebSocket error, reconnecting")
|
||||
# Increment watchdog counter
|
||||
watchdog_counter += 1
|
||||
# When Wi-Fi is connected and X-th exception happened
|
||||
# Try reconnecting to the WebSocket server
|
||||
if wlan.isconnected() and watchdog_counter == 3:
|
||||
print("main.py WebSocket timeout, reconnecting")
|
||||
conn = uwebsockets.connect(uri)
|
||||
watchdog_counter = 0
|
||||
# Continue to try to read data
|
||||
continue
|
||||
|
||||
@ -124,26 +133,25 @@ if 'code.py' in os.listdir():
|
||||
# Start the code processing thread
|
||||
_thread.start_new_thread(step, ())
|
||||
|
||||
# Wifi connection counter
|
||||
wifi_connection_counter = 0
|
||||
# Wifi watchdog counter
|
||||
watchdog_counter = 0
|
||||
# Wait for WiFi to get connected
|
||||
while not wlan.isconnected():
|
||||
sleep_ms(100)
|
||||
wifi_connection_counter += 1
|
||||
watchdog_counter += 1
|
||||
# When Wi-Fi didn't connect in X seconds
|
||||
if wifi_connection_counter == 30:
|
||||
print("main.py reinitiating Wi-Fi connection")
|
||||
if watchdog_counter == 30:
|
||||
print("main.py reconnecting to Wi-Fi")
|
||||
# Reinitiate the Wi-Fi connection
|
||||
wlan.connect(ssid, config["wifis"][ssid])
|
||||
wifi_connection_counter = 0
|
||||
|
||||
# Restart watchdog counter
|
||||
watchdog_counter = 0
|
||||
|
||||
# Connect to the websocket
|
||||
uri = "ws://%s/p2p/sumo-%s/browser/" % (config['sumo_server'], config['sumo_id'])
|
||||
conn = uwebsockets.connect(uri)
|
||||
|
||||
# Set X seconds timeout for socket reads
|
||||
conn.settimeout(1)
|
||||
|
||||
# Stop bootup blinking
|
||||
timer.deinit()
|
||||
|
||||
|
@ -204,6 +204,7 @@ def connect(uri):
|
||||
|
||||
# Connect the socket
|
||||
sock = socket.socket()
|
||||
sock.settimeout(1)
|
||||
addr = socket.getaddrinfo(uri.hostname, uri.port)
|
||||
sock.connect(addr[0][4])
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user