0

I am trying to wear out a battery to see how long it powers my ESP32. To do this, I wrote a simple program that writes "Still alive" to a website once a minute. However, the program crashes every time it has written the string successfully to the web file. I can't figure out why it works nine times, then fails. Here is the code:

from time import sleep_ms, ticks_ms
import network
import socket
import urequests
import machine
import json

SSID="my_ssid"
PASSWORD="my_password"
port=100
wlan=None
s=None

def connectWifi(ssid,passwd): #function to connect to the Web
  global wlan #declare a WLAN object
  wlan=network.WLAN(network.STA_IF)                     #create a wlan object
  wlan.active(True)                                     #Activate the network interface
  wlan.disconnect()                                     #Disconnect the last connected WiFi
  wlan.connect(ssid,passwd)                             #connect wifi
  while(wlan.ifconfig()[0]=='0.0.0.0'): #wait for connection
    sleep_ms(1)
  sleep_ms(1000) #hold on for 1 second
  sendmessage("Connected to WLAN")
  sleep_ms(1000)  #hold on for 1 second
  return True

def sendmessage(myMessage):
  url = "http://www.sabulo.com/stillalive.php"
  headers = {'content-type': 'application/json'}
  data = {'message': myMessage}
  jsonObj = json.dumps(data)
  resp = urequests.post(url, data=jsonObj, headers=headers)
  return True

def main():
  connectWifi(SSID,PASSWORD)
  hitcount = 0
  while True:
      sendmessage("Still  alive " + str(hitcount))
      print("Still alive " + str(hitcount))
      hitcount = hitcount + 1
      sleep_ms(1000)

main()

Then I get this:

>>>
>>> I (5906) phy: phy_version: 4007, 9c6b43b, Jan 11 2019, 16:45:07, 0, 0
Still alive 0
Still alive 1
Still alive 2
Still alive 3
Still alive 4
Still alive 5
Still alive 6
Still alive 7
Still alive 8
╝Traceback (most recent call last):
  File "<stdin>", line 45, in <module>
  File "<stdin>", line 40, in main
  File "<stdin>", line 33, in sendmessage
  File "urequests.py", line 111, in post
  File "urequests.py", line 56, in request
OSError: 23
╝>
MicroPython v1.10-98-g4daee3170 on 2019-02-14; ESP32 module with ESP32
Type "help()" for more information.

I am using the same web upload code piece in various apps and it never gives me this error.

Any help gratefully acknowledged :)

  • From the way you have handled the exception it doesn't look as if we can see which line of the program actually raised the error - can you run it without the error handling and show us the traceback? – nekomatic Feb 18 '19 at 15:36
  • Good point. I'll do that as soon as I get access to the device again. – Heikki Hietala Feb 19 '19 at 18:06
  • I changed the code by removing the try structures. It still fails with the output shown above. – Heikki Hietala Feb 21 '19 at 08:36
  • Searching the MicroPython forum, I find [this](https://forum.micropython.org/viewtopic.php?f=2&t=2856&p=16899) and [this](https://forum.micropython.org/viewtopic.php?f=18&t=4733&p=27338) - I'm not familiar enough with urequests to tell what's wrong in your case but is it possible you're not closing or releasing a resource somewhere? If that doesn't help I'd try asking on the forum yourself as that's where all the MicroPython experts should be. – nekomatic Feb 21 '19 at 11:15
  • ...according to [this](https://github.com/micropython/micropython-lib/blob/master/errno/errno.py), 23 is a `File table overflow` – nekomatic Feb 21 '19 at 11:19
  • OK, I see you [already did](https://forum.micropython.org/viewtopic.php?f=2&t=5964) try the forum. I suggest adding that traceback there as well. – nekomatic Feb 21 '19 at 13:44
  • Thanks for the help. I am a little surprised that this operation works fine in two other applications I have built, but when it is repeated 8 times it fails. – Heikki Hietala Feb 21 '19 at 15:21
  • On the other forum, someone said that sockets are not being closed. I must try to do a resp.close() and see if I can get it to work. – Heikki Hietala Feb 21 '19 at 15:25
  • No, doing a resp.close() didn't work. – Heikki Hietala Feb 21 '19 at 15:50

0 Answers0