[w600] socket.connect() hangs
Posted: Tue May 18, 2021 12:55 pm
My program on the Air602 running MP 1.10 calls a remote service over WiFi. socket.connect() hangs at random times maybe once a day. The code is:
The log reads:
Code: Select all
import network
import utime
from machine import Pin
import usocket
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
sid = '...'
password = '...'
wlan.connect(sid, password)
utime.sleep(10)
log_contents = ''
def pin_is_grounded():
for i in [Pin.PB_09, Pin.PB_10, Pin.PB_11, Pin.PB_12]:
pin = Pin(i, Pin.IN, Pin.PULL_UP)
if pin.value() == 0:
return True
return False
def log(message):
global log_contents
log_contents += message + '\r'
log_contents = log_contents[-3000:] # purge old messages
with open('error.log', 'w') as f:
f.write(log_contents)
while True:
if pin_is_grounded():
quit()
try:
address = ('192.168.0.4', 18552)
s = usocket.socket()
log('connecting')
s.connect(address)
log('sending')
s.send('garage is open')
log('sent')
s.close()
except BaseException as e:
log('exception: {}'.format(str(e)))
utime.sleep(10) # seconds
Is there a way to make connect() asynchronous or time out?...connecting
sending
sent
connecting
sending
sent
connecting