Odd behavior with urequests: object with buffer protocol required

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
heikki.hietala
Posts: 13
Joined: Thu Oct 11, 2018 4:52 pm

Odd behavior with urequests: object with buffer protocol required

Post by heikki.hietala » Sun Feb 17, 2019 8:55 am

Hi all,

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:

Code: Select all

try:
  from time import sleep_ms, ticks_ms
  import network
  import socket
  import urequests
  import machine
  import json
    
  SSID="my_ssid"
  PASSWORD="my_passwd"
  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
    url = "my_receiving_php"
    headers = {'content-type': 'application/json'}
    data = {'message': 'WLAN connected'}
    jsonObj = json.dumps(data)
    resp = urequests.post(url, data=jsonObj, headers=headers)
    sleep_ms(1000)  #hold on for 1 second
    return True
 
  def sendmessage(myMessage):
    url = "my_receiving_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:
      try:
        sendmessage("Still alive")
        print("Still alive!")
        hitcount = hitcount + 1
        sleep_ms(60000)
      except Exception as e:
        f = open('buglog.txt','a')
        f.write(e)
        f.close() 
        
  main()
    
except Exception as e:
  print("CRASH!\n")
  print (e)
  f = open('buglog.txt','a')
  f.write(str(e)+"\n")
  f.close()
  import machine
  machine.reset()    
Then I get this:

Code: Select all

>>> Running e:\Dropbox\ZEsp32\workSpace\stillalive\stillalive.py

>>>
>>> I (3288) phy: phy_version: 4007, 9c6b43b, Jan 11 2019, 16:45:07, 0, 0
Still alive!
Still alive!
Still alive!
Still alive!
Still alive!
Still alive!
Still alive!
Still alive!
Still alive!
CRASH!

object with buffer protocol required
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5060
load:0x40078000,len:8788
ho 0 tail 12 room 4
load:0x40080400,len:6772
entry 0x40081610
I (437) cpu_start: Pro cpu up.
I (437) cpu_start: Application information:
I (437) cpu_start: Compile time:     12:32:34
I (439) cpu_start: Compile date:     Feb 14 2019
I (444) cpu_start: ESP-IDF:          v3.3-beta1-268-g5c88c5996
I (451) cpu_start: Single core mode
I (455) heap_init: Initializing. RAM available for dynamic allocation:
I (462) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (468) heap_init: At 3FFB92B0 len 00026D50 (155 KiB): DRAM
I (474) heap_init: At 3FFE0440 len 0001FBC0 (126 KiB): D/IRAM
I (481) heap_init: At 40078000 len 00008000 (32 KiB): IRAM
I (487) heap_init: At 40092834 len 0000D7CC (53 KiB): IRAM
I (493) cpu_start: Pro cpu start user code
I (64) cpu_start: Starting scheduler on PRO CPU.
The error without the try structure looks like this:

Code: Select all

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

Post Reply