[w600] socket.connect() hangs

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
cdude1990
Posts: 4
Joined: Fri Jul 17, 2020 2:56 pm

[w600] socket.connect() hangs

Post by cdude1990 » 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:

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
The log reads:
...connecting
sending
sent
connecting
sending
sent
connecting
Is there a way to make connect() asynchronous or time out?

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: [w600] socket.connect() hangs

Post by SpotlightKid » Wed May 19, 2021 10:23 pm

Not sure whether one of the methods described in the docs is supported by your port / socket implementation:

http://docs.micropython.org/en/latest/l ... settimeout

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: [w600] socket.connect() hangs

Post by davef » Wed May 19, 2021 10:47 pm

My understanding is that connect() is asynchronous and that is a problem. Thanks to Roberthh you need to use a while loop with a small delay, until is really connects. Sometimes it would just keep trying to connect so I added a counter to try 5 times then do a machine.reset().

On an ESP32 I just about always see it connect on the 2nd try.

Code: Select all

    if not sta_if.isconnected():
        print('connecting to hotspot...')
        sta_if.active(True)
        sta_if.ifconfig(('192.168.10.97', '255.255.255.0', '192.168.10.1', '8.8.8.8'))
        sta_if.connect('HUAWEI-E8231-c4fd', 'xxx')


        while (count < 5):
            count += 1

            status = sta_if.status()

            try:
                with open('errors.txt', 'a') as outfile:
                    outfile.write('connect status = ' + str(status) + '\n')
            except OSError:
                pass

            if (sta_if.isconnected()):
                count = 0
                break

            print('.', end = '')
            utime.sleep(1)

    if (count == 5):
        count = 0

        try:
            with open('errors.txt', 'a') as outfile:
                outfile.write('did NOT connect to internet' + '\n')
        except OSError:
            pass

        machine.reset()


    print(' network config:', sta_if.ifconfig())
I log the status and any errors for debugging.

Good luck!

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: [w600] socket.connect() hangs

Post by SpotlightKid » Wed May 19, 2021 10:52 pm

The original poster was talking about socket.connect(), not network.WLAN().connect().

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: [w600] socket.connect() hangs

Post by davef » Thu May 20, 2021 12:32 am

Oops. Just saw he was connecting to WiFi at the top of the script and jumped to the wrong conclusion.

Post Reply