OSError: Network card not available for Wifi (pycom4)
-
Hey,
For study i have setup an MQTT Broker & we have to connect to this broker with a pycom4.
Currently everytime i connect to my wifi in boot.py (This works fine).
Then when the MQTT Client tries to connect or a urequest is done, it throws the error: OSError: Network card not available.Why is it saying this, when it has connected to wifi with no issue??
Setting up wifi...Setting up wifi... (Wlan-piot) Wifi has been setup! (192.168.32.25) ('192.168.32.25', '255.255.255.0', '192.168.32.134', '192.168.32.134')
Error:
RTC Set from NTP to UTC: (1970, 1, 1, 0, 0, 9, 602235, None) Traceback (most recent call last): File "main.py", line 32, in <module> File "/flash/lib/mqtt.py", line 56, in connect OSError: Network card not available
from network import WLAN (Line 3) import machine import ENV #Wifi setup print("Setting up wifi... (" + ENV.SSID + ")") wlan = WLAN(mode=WLAN.STA) if ENV.STATIC_IP: wlan.ifconfig(config=ENV.CONFIG) wlan.connect(ssid=ENV.SSID, auth=(WLAN.WPA2, ENV.PASS)) while not wlan.isconnected(): machine.idle() print("Wifi has been setup! (" + wlan.ifconfig()[0] + ")") print(wlan.ifconfig())
import pycom (Line 3) from mqtt import MQTTClient from network import WLAN from time import sleep from timestamp import timestamp import machine import socket import urequests import ENV def sub_cb(topic, msg): print("mqtt::",topic,"msg[",msg,"]") #Device setup pycom.heartbeat(False) clock = timestamp() sleep(2) #res = urequests.post('http://' + ENV.MQTT_URL, json={"emotie": 0}) #res.close() #print(res.status_code) #MQTT Setup client = MQTTClient(ENV.DEVICEID, ENV.MQTT_URL, port=1883) client.set_callback(sub_cb) client.connect() #Our main program while True: print("Sending") ##client.publish(topic="piot/storedata/"+ ENV.DEVICEID + "/ping/" + clock.getTimestamp(), msg="pong") sleep(1)
-
Hey, yhea before when i put it in the main.py it took long to connect, and sometimes just wont.
Still seems to be the problem when setting up the connection in main.py, but atleast the client works after no problem...EDIT: Because in the boot.py it connects realquick everytime.
-
Hmm. This is just a quick guess, but it looks to me as if your wlan object goes out of scope when boot.py completes. I suspect that its destructor takes down the network.
I would try moving all the wifi setup code into main.py and run it before instantiating the MQTTClient object.
Also, consider that you may eventually need to handle wifi dropouts or MQTT client disconnects due to underlying TCP connection failures at some point depending on what you ultimately expect this to do and for how long.