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