From 2c3711a91d2cfea8bb3f1c460d6f9a41f2829a18 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Sun, 26 May 2019 14:16:57 +0200 Subject: [PATCH] update autoreconnect --- config.json | 6 +++--- main.py | 40 ++++++++++++++++++++++++---------------- uwebsockets.py | 1 + 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/config.json b/config.json index 264969b..134dfb2 100755 --- a/config.json +++ b/config.json @@ -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, diff --git a/main.py b/main.py index cc3dcd9..8a8720d 100755 --- a/main.py +++ b/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") - # Re initiate the 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() diff --git a/uwebsockets.py b/uwebsockets.py index 0b7dd41..b55f57f 100755 --- a/uwebsockets.py +++ b/uwebsockets.py @@ -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])