Trying usocket on WROOM module

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Trying usocket on WROOM module

Post by liudr » Wed Jul 25, 2018 9:24 pm

I am trying to get a bit of networking using usocket on WROOM but maybe there wasn't enough memory. I have the standard firmware:

Code: Select all

rst:0x1 (POWERON_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:4596
load:0x40078000,len:0
load:0x40078000,len:12768
entry 0x4007ad68
I (387) cpu_start: Pro cpu up.
I (387) cpu_start: Single core mode
I (388) heap_init: Initializing. RAM available for dynamic allocation:
I (391) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (397) heap_init: At 3FFC57D0 len 0001A830 (106 KiB): DRAM
I (403) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (410) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (416) heap_init: At 40091B28 len 0000E4D8 (57 KiB): IRAM
I (422) cpu_start: Pro cpu start user code
I (216) cpu_start: Starting scheduler on PRO CPU.
OSError: [Errno 2] ENOENT
MicroPython v1.9.4-403-g81e320aec on 2018-07-21; ESP32 module with ESP32
Type "help()" for more information.
>>> import network
>>> sta=network.WLAN(network.STA_IF)
I (75895) wifi: wifi driver task: 3ffcb834, prio:23, stack:4096, core=0
I (75895) wifi: wifi firmware version: ac8d7b4
I (75895) wifi: config NVS flash: enabled
I (75895) wifi: config nano formating: disabled
I (75905) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (75915) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (75925) wifi: Init dynamic tx buffer num: 32
I (75925) wifi: Init data frame dynamic rx buffer num: 64
I (75935) wifi: Init management frame dynamic rx buffer num: 64
I (75935) wifi: Init static rx buffer size: 1600
I (75945) wifi: Init static rx buffer num: 10
I (75945) wifi: Init dynamic rx buffer num: 0
>>> sta.active(True)
I (86585) phy: phy_version: 3910, c0c45a3, May 21 2018, 18:07:06, 0, 0
I (86585) wifi: mode : sta (30:ae:a4:0d:b0:20)
I (86585) wifi: STA_START
True
>>> sta.connect("xxxx","xxxx")
>>> I (121245) wifi: n:1 0, o:1 0, ap:255 255, sta:1 0, prof:1
I (121815) wifi: state: init -> auth (b0)
I (121815) wifi: state: auth -> assoc (0)
I (121815) wifi: state: assoc -> run (10)
I (121845) wifi: connected with xxxx, channel 1
I (121845) wifi: pm start, type: 1

I (121845) network: CONNECTED
I (123605) event: sta ip: 192.168.1.24, mask: 255.255.255.0, gw: 192.168.1.1
I (123605) network: GOT_IP

>>> import usocket
I (257785) modsocket: Initializing
>>> sockaddr=usocket.getaddrinfo('www.micropython.org',80)
>>> sock=usocket.socket(usocket.AF_INET,usocket.SOCK_STREAM)
>>>
>>> sock.connect(sockaddr)
CORRUPT HEAP: multi_heap.c:165 detected at 0x3ffe641c
abort() was called at PC 0x4008ec9b on core 0

Backtrace: 0x4008f49b:0x3ffb6e30 0x4008f4c7:0x3ffb6e50 0x4008ec9b:0x3ffb6e70 0x4008ef40:0x3ffb6e90 0x4008f09d:0x3ffb6eb0 0x40083812:0x3ffb6ed0 0x400812bd:0x3ffb6ef0 0x4000bec7:0x3ffb6f10 0x4010829f:0x3ffb6f30 0x400f220a:0x3ffb6f50 0x400df9b6:0x3ffb6f80 0x400dbd61:0x3ffb6fa0 0x400dbdc9:0x3ffb6fc0 0x400e9015:0x3ffb6fe0 0x400dfad8:0x3ffb7080 0x400dbd61:0x3ffb70f0 0x400dbd8e:0x3ffb7110 0x400f8db6:0x3ffb7130 0x400f8ff4:0x3ffb71d0 0x400efde4:0x3ffb7210

Rebooting...
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:4596
load:0x40078000,len:0
load:0x40078000,len:12768
entry 0x4007ad68
I (387) cpu_start: Pro cpu up.
I (387) cpu_start: Single core mode
I (388) heap_init: Initializing. RAM available for dynamic allocation:
I (391) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (397) heap_init: At 3FFC57D0 len 0001A830 (106 KiB): DRAM
I (403) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (410) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (416) heap_init: At 40091B28 len 0000E4D8 (57 KiB): IRAM
I (422) cpu_start: Pro cpu start user code
I (216) cpu_start: Starting scheduler on PRO CPU.
OSError: [Errno 2] ENOENT
MicroPython v1.9.4-403-g81e320aec on 2018-07-21; ESP32 module with ESP32
Type "help()" for more information.
>>>
>>>
Can someone explain to me regarding the different memory heaps and their usages? Should I custom-build a firmware and remove some libraries?
Last edited by liudr on Fri Jul 27, 2018 4:25 pm, edited 1 time in total.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Trying usocket on WROOM module

Post by liudr » Fri Jul 27, 2018 4:24 pm

I started tracking memory usage and thought memory shouldn't be a problem. There is 100K left before I attempted to connect socket but the module still restarts with this error:

Code: Select all

>>> gc.mem_free()
103168
>>> sock.connect(sockaddr)
Guru Meditation Error: Core  0 panic'ed (LoadProhibited)
. Exception was unhandled.
Core 0 register dump:
PC      : 0x4010ac48  PS      : 0x00060e30  A0      : 0x8010b9aa  A1      : 0x3ffb6ef0
A2      : 0x00000039  A3      : 0x00040000  A4      : 0x00040000  A5      : 0x3ffbffc0
A6      : 0x00000001  A7      : 0x00000000  A8      : 0x8010ac40  A9      : 0x3ffb6ed0
A10     : 0x3ffbffc0  A11     : 0x3ffd04ac  A12     : 0x3ffd06c4  A13     : 0x00000000
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00040001  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000

Backtrace: 0x4010ac48:0x3ffb6ef0 0x4010b9a7:0x3ffb6f30 0x400f21fc:0x3ffb6f50 0x400df9b6:0x3ffb6f80 0x400dbd61:0x3ffb6fa0 0x400dbdc9:0x3ffb6fc0 0x400e9015:0x3ffb6fe0 0x400dfad8:0x3ffb7080 0x400dbd61:0x3ffb70f0 0x400dbd8e:0x3ffb7110 0x400f8db6:0x3ffb7130 0x400f8ff4:0x3ffb71d0 0x400efde4:0x3ffb7210

Rebooting...
Some Guru Meditation Error? WTH?

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: Trying usocket on WROOM module

Post by Capstan » Fri Jul 27, 2018 9:48 pm

It has been working for me (though adding SSL/TLS does suck up considerable memory).

Code: Select all

import usocket as _socket

def connect(secure, host, port):
	sock = _socket.socket()
	addr = _socket.getaddrinfo(host, port)
	sock.connect(addr[0][-1])
	if secure is True:
		import ussl as ssl
        	sock = ssl.wrap_socket(sock)

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Trying usocket on WROOM module

Post by liudr » Sat Jul 28, 2018 4:58 am

Thanks for responding. I used sock.connect(sockaddr) instead of sockaddr[0][-1]. I thought that was the right syntax. The ESP32 port is essentially missing official documentation. All I found was wipy port documentation that told me that.

Code: Select all

>>> sockaddr=usocket.getaddrinfo('www.micropython.org',80)
>>> sock=usocket.socket(usocket.AF_INET,usocket.SOCK_STREAM)
>>>
>>> sock.connect(sockaddr)
https://docs.micropython.org/en/latest/ ... ocket.html

Maybe the doc is missing something like addr=sockaddr[0][-1]. With your syntax I was able to connect. Now I need to figure out how to send an http request with the socket. Any quick reference?

Capstan
Posts: 117
Joined: Sun Jan 29, 2017 4:03 pm
Location: Texas, USA

Re: Trying usocket on WROOM module

Post by Capstan » Sat Jul 28, 2018 3:13 pm

liudr wrote:
Sat Jul 28, 2018 4:58 am
Now I need to figure out how to send an http request with the socket. Any quick reference?
Have you tried the MicroWebCli? It appears to open and manage its own socket so you don't have to bother with that. All you have to do is connect to WiFi. Decodes responses so you can handle them conveniently in Python.

https://github.com/jczic/MicroWebCli

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Trying usocket on WROOM module

Post by SpotlightKid » Sat Jul 28, 2018 3:23 pm

Have a look at the source of the urequests module from the micopython-lib here:

https://github.com/micropython/micropyt ... sts.py#L53

Or just use it, instead of re-inventing the wheel :)

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Trying usocket on WROOM module

Post by liudr » Sun Jul 29, 2018 12:26 am

Great! Thanks to both! I'll read the urequests first. My connect request is simple, just an HTTP GET request. Server response is super simple. If I can get that done using urequests as a starting point, I'll be good.

Post Reply