Error 118, doconnect() not connecting



  • I'm working on debugging this script. It's been a slow process since I can't run serial in VScode.

    It's collecting moisture readings and then sending them to a Adafruit IO feed through a POST. I found my way around th 113 errors from urequests, but now I'm getting OSerror 118. I guess this indicates the wifi isn't connected.

    I'm using wifiCfg's doConnect function in a while loop in hopes that it will keep trying until it works. This works on an initial run, but it looks like it fails after a lightsleep.

    Does anyone have any idea about how I can get the wifi to connect more reliably? What am I missing?

    while(True):
      gc.collect()
      while not wifiCfg.doConnect('guest', '12345678', lcdShow=True): pass
    
      lcd.clear()
      lcd.setTextColor(0xaaaaaa, lcd.BLACK)
      lcd.font(lcd.FONT_DejaVu18)
      axp.setLcdBrightness(50)
      
      test.display(int(get_moisture()/10))
      wait(2)
    
      moisture = get_moisture() #get moisture
      try:
        response = urequests.request(
          method='POST',
          url='http://io.adafruit.com/api/v2/********/feeds/moisture/data',
          json={'value':moisture},
          headers={'Content-Type':'application/json','X-AIO-Key':'**************************'}
        )
        if response.status_code == 200:
          led.duty(99)
          lcd.clear()
          lcd.print("sent", lcd.CENTER, 5)
          lcd.image(0, 50, 'img/success.jpg')
          wait(1)
          axp.setLDO2Volt(0)
        else:
          lcd.print("request \r\n error", lcd.CENTER, 0, 0xFFFFFF)
          lcd.image(0, 50, 'img/error.jpg')
          led.duty(50)
          wait(10)
      except Exception as e:
        lcd.print("OS error", lcd.CENTER, 0, 0xFFFFFF)
        lcd.image(0, 50, 'img/error.jpg')
        print(e)
      
      gc.collect()
      lightsleep(60*1000)
    


  • For anyone interested, I think I found the solution to my problem. The problem wasn't with doConnect.

    First, the 118 error was caused by lightsleep... I think. Since lightsleep allows RAM to persist, when it would wake up it would still think wlan_sta was active, even though it wasn't. Adding wifiCfg.wlan_sta.active(False) at the end turned the network off before going to sleep. That way each time it would connect as it should.

    Next, I kept getting error 23 after a few posts. This was caused by not closing the socket that was opened by the request and stored in response. The memory was filling up with socket objects, and running out of room. Calling request.close() removed the socket and freed up the memory. Now it's working just fine.