Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    OSError: Network card not available for Wifi (pycom4)

    Scheduled Pinned Locked Moved Issues & Bugs
    3 Posts 2 Posters 61 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ? Offline
      A Former User
      last edited by A Former User

      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
      

      boot.py

      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())
      

      main.py

      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)
      
      1 Reply Last reply Reply Quote 0
      • G Offline
        g0hww
        last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • ? Offline
          A Former User
          last edited by A Former User

          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.

          1 Reply Last reply Reply Quote 0

          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
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom