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=('192.168.0.104', '255.255.255.0', '192.168.0.1', '192.168.0.1')) ## 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(): print("connected1") ## Connect to internet wlan.connect("Name", auth=(WLAN.WPA2, "hahahahaha"), timeout=5000) if wlan.isconnected(): print("connected3") while not wlan.isconnected(): machine.idle() 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.connect() client.publish(topic="yoplo/feeds/{}/{}/temp".format(loc, sensor_dict[sensor_type]), msg=temp, retain = True) ## ---------------------------------End of function block------------------------------------- ## UDP_IP = "0.0.0.0" 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--------------------------------## wlan.disconnect() if wlan.isconnected(): print("connected2") send_data(data_raw) machine.deepsleep(10)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: ('192.168.0.105', 49258) connected2 Unpacking raw data... connected1 connected3 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!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login