WLAN issue: wlan auto-connects upon entering function body

  • I have a Pycom Lopy 4 that will recieve sensor readings from another MCU and structure the received data before sending it on to an MQTT server. To achieved that, I initialised the WLAN mode of my LoPy4 to 'STA_AP'.

    However, I realised that when i attempted a wlan.disconnect(), and my code enters a function block to deal with the data, wlan.isconnected() returns True again! I know this because I placed a few print statements within the function body to check for connectivity. Below is my code:

    import usocket
    from network import WLAN
    import ustruct
    from mqtt import MQTTClient
    import machine
    sensor_dict = {
        0x77 : "bme280"
        # Add more sensors here #
    wlan = WLAN()
    ## Set static IP address for server
    wlan.ifconfig(id = 1, config=('', '', '', ''))
    ## Set WLAN mode to STA_AP (station & access point)
    print("Setting up wlan access point...")
    wlan.init(mode = WLAN.STA_AP, ssid = 'server_layer1', auth = (WLAN.WPA2, 'Password123'), channel=7, antenna=WLAN.INT_ANT)
    print("wlan mode set up: with WPA2 auth\n")
    ## -----------------------------This is the function block---------------------------------- ##
    def send_data(data_raw):
        print("Unpacking raw data...")
        data = ustruct.unpack('lifff', data_raw)
        loc, sensor_type, temp, press, hum = data
        if wlan.isconnected():
        ## Connect to internet
        wlan.connect("Name", auth=(WLAN.WPA2, "hahahahaha"), timeout=5000)
        if wlan.isconnected():
        while not wlan.isconnected():
        print("Connected to WiFi\n")
        print("Sending data up to MQTT server...")
        client = MQTTClient("xxx123", "io.adafruit.com",user="yoplo", password="xxx123", port=1883)
        client.publish(topic="yoplo/feeds/{}/{}/temp".format(loc, sensor_dict[sensor_type]), msg=temp, retain = True)
    ## ---------------------------------End of function block------------------------------------- ##
    UDP_IP = ""
    UDP_PORT = 6006
    ## Set up UDP socket and bind
    sock = usocket.socket(usocket.AF_INET,usocket.SOCK_DGRAM) ## UDP
    sock.bind((UDP_IP, UDP_PORT))
    print('Socket binded.')
    data_raw, addr = sock.recvfrom(24) ## buffer size is 24 bytes
    ## -----------------------------------ran wlan.disconnect() here--------------------------------##
    if wlan.isconnected():

    My output was:

    Setting up wlan access point...
    wlan mode set up: with WPA2 auth
    Socket binded.
    raw data payload: b'q\x01\x00\x00w\x00\x00\x00\xe1z\xd0Aq\xedzD\x14\xae^B'
    source:  ('', 49258)
    Unpacking raw data...
    Connected to WiFi
    Sending data up to MQTT server...
    Traceback (most recent call last):
      File "main.py", line 65, in <module>
      File "main.py", line 43, in send_data
      File "/flash/lib/mqtt.py", line 66, in connect
    OSError: [Errno 113] EHOSTUNREACH
    Pycom MicroPython 1.18.2.r6 [v1.8.6-849-a210e85] on 2019-05-13; LoPy4 with ESP32
    Type "help()" for more information.

    'connected1' and 'connected3' were printed, although 'connected2' was not printed. Not sure why, appreciate any assistance!

Log in to reply

Pycom on Twitter