MicroPython on ESP32 with SPIRAM support

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: MicroPython on ESP32 with SPIRAM support

Post by loboris » Wed Oct 25, 2017 2:39 pm

Hint:

For preserving variables during the deepsleep it is recomended to save them in RTC memory.
64 integer variables are available (indexed 0~63) and one string variable with max length of 2047 bytes.
CRC16 is used to guarantee the integrity of the saved values.
The string variable can, for example, contain json string with current working state which has to be restored after deepsleep wake-up.

Code: Select all

>>> import machine                                                                                                                          
>>> rtc=machine.RTC()                                                                                                                       
>>> rtc.read_string()                                                                                                                       
>>> rtc.write_string('This string is preserved after deepsleep wake-up')                                                                    
True                                                                                                                                        
>>> rtc.write(1,123456)                                                                                                                     
True                                                                                                                                        
>>> rtc.write(10,654321)                                                                                                                    
True                                                                                                                                        
>>> machine.deepsleep(10000)                                                                                                                
ESP32: DEEP SLEEP                                                                                                                           
...
...
FreeRTOS running on BOTH CORES, MicroPython task started on App Core.                                                                       
                                                                                                                                            
uPY stack size = 19456 bytes                                                                                                                
uPY  heap size = 81920 bytes                                                                                                                
                                                                                                                                            
D (275) intr_alloc: Connected src 34 to int 3 (cpu 1)                                                                                       
 Reset reason: Deepsleep wake-up                                                                                                            
Wakeup source: RTC wake-up                                                                                                                  
D (285) intr_alloc: Connected src 22 to int 4 (cpu 1)                                                                                       
MicroPython ESP32_LoBo_v2.0.6 - 2017-10-24 on ESP32 board with ESP32                                                                        
Type "help()" for more information.                                                                                                         
>>> import machine                                                                                                                          
>>> rtc=machine.RTC()                                                                                                                       
>>> rtc.read_string()                                                                                                                       
'This string is preserved after deepsleep wake-up'                                                                                          
>>> rtc.read(1)                                                                                                                             
123456                                                                                                                                      
>>> rtc.read(10)                                                                                                                            
654321                                                                                                                                      
>>> 

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: MicroPython on ESP32 with SPIRAM support - MQTT client

Post by ttmetro » Fri Oct 27, 2017 11:40 pm

I'm using the built-in MQTT client. Works great, except when I connect two clients to the same broker (iot.eclipse.org) I get errors

Code: Select all

[Mqtt client]: Read error 9
and the MQTT client keeps disconnecting (and reconnecting).

If I disconnect the other client there are no more read errors.

I am also connecting to the same client with mqtt-spy (a java mqtt client). That does not produce read errors and I can control the "offending" client from mqtt-spy. It's only the other instance running the client built into this firmware that results in these read errors.

Hints what might cause "read error 9" and ways to diagnose are most appreciated. I realize this may be an mqtt problem and have nothing to do with this port. Many thanks!
Bernhard Boser

slzatz
Posts: 92
Joined: Mon Feb 09, 2015 1:09 am

Re: MicroPython on ESP32 with SPIRAM support

Post by slzatz » Sat Oct 28, 2017 12:04 am

Just a guess but if you aren't explicitly setting the client id then both clients will have the same id and only one will be able to connect to the broker. So when you construct the clients, use a different client id for each one:

Code: Select all

mqtt = network.mqtt(name="description", server="server", clientid = "whatever")
I think if you use a different client id for your two clients it should work fine.

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: MicroPython on ESP32 with SPIRAM support

Post by ttmetro » Sat Oct 28, 2017 12:34 am

Thanks!!!!! This was precisely it.

Now everything works fine for while, then I get

Code: Select all

Guru Meditation Error of type LoadProhibited occurred on core 0. Exception was unhandled.
followed by a register dump and "CPU halted."

Tried many times, and sometimes I get an error in gc.c, line 714, function gc_realloc instead, but I always get "CPU halted." after a while.
Bernhard Boser

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MicroPython on ESP32 with SPIRAM support

Post by pythoncoder » Sat Oct 28, 2017 5:23 am

Regarding client ID's these can produce a variety of unexpected effects if not controlled. I use the approach of deriving it from the hardware unique ID as per the official library, see https://github.com/micropython/micropyt ... led.py#L14.
Peter Hinch
Index to my micropython libraries.

slzatz
Posts: 92
Joined: Mon Feb 09, 2015 1:09 am

Re: MicroPython on ESP32 with SPIRAM support

Post by slzatz » Sat Oct 28, 2017 2:20 pm

I am also seeing that same unhandled exception. A script that includes an mqtt subscription runs for a while correctly and then exits as follows:

Code: Select all

Guru Meditation Error of type LoadProhibited occurred on core  0. Exception was unhandled.
Register dump:
PC      : 0x4012db63  PS      : 0x00060430  A0      : 0x8012db4f  A1      : 0x3ffece30  
A2      : 0x3ffece30  A3      : 0x3ffece30  A4      : 0x3ffc31fc  A5      : 0x00000003  
A6      : 0x3ffc33d8  A7      : 0x3ffed100  A8      : 0x8012db63  A9      : 0x3ffece10  
A10     : 0x00000000  A11     : 0x3ffc32d8  A12     : 0x00000057  A13     : 0x3ffc32e0  
A14     : 0x0000000a  A15     : 0x3ffed0a0  SAR     : 0x00000000  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x0000000c  LBEG    : 0x4011257c  LEND    : 0x401125da  LCOUNT  : 0x00000000  

Backtrace: 0x4012db63:0x3ffece30 0x4012db4c:0x3ffece60 0x4012db4c:0x3ffece90 0x4012db4c:0x3ffecec0 0x4012db4c:0x3ffecef0 0x4012db4c:0x3ffecf20 0x4012db4c:0x3ffecf50 0x4012db4c:0x3ffecf80 0x4012db4c:0x3ffecfb0 0x4012db89:0x3ffecfe0 0x40112b89:0x3ffed000 0x40112471:0x3ffed030 0x40120d8f:0x3ffed050 0x401216bd:0x3ffed070 0x401324e9:0x3ffed090 0x401679e3:0x3ffed0c0 0x40167bcd:0x3ffed120 0x40167e18:0x3ffed140

CPU halted.
Perhaps @loboris has a thought about what's going on.

Update: This problem resolved with @loboris suggestion to put a gc.collect() at the end of the data received call back [datacb()]
Last edited by slzatz on Thu Nov 02, 2017 11:45 am, edited 1 time in total.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: MicroPython on ESP32 with SPIRAM support

Post by loboris » Sat Oct 28, 2017 6:21 pm

slzatz wrote:
Sat Oct 28, 2017 2:20 pm
I am also seeing that same unhandled exception. ..
I'll investigate the issue.
Could you , please, execute the code with esp-idf monitor and post the complete log, it could help the exact location of the error...

slzatz
Posts: 92
Joined: Mon Feb 09, 2015 1:09 am

Re: MicroPython on ESP32 with SPIRAM support

Post by slzatz » Sun Oct 29, 2017 1:29 am

Could you , please, execute the code with esp-idf monitor and post the complete log, it could help the exact location of the error...
Emailed it to you.

ttmetro
Posts: 104
Joined: Mon Jul 31, 2017 12:44 am

Re: MicroPython on ESP32 with SPIRAM support

Post by ttmetro » Tue Oct 31, 2017 9:41 pm

I tried ./BUILD.sh monitor but get an error

Code: Select all

  File "/Applications/anaconda3/lib/python3.6/site-packages/serial/tools/miniterm.py", line 63, in write_bytes
    self.byte_output.write(byte_string)
TypeError: a bytes-like object is required, not 'int'
make: *** [monitor] Error 1
'make monitor' FAILED!
Presumably it's since I am using Python 3.6 and the monitor expects perhaps V2 - I constant source of frustration (some programs need V2, others V3 - my single biggest gripe of Python).

Please let me know if there is another way I can collect debug information.

Bernhard
Bernhard Boser

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: MicroPython on ESP32 with SPIRAM support

Post by loboris » Tue Oct 31, 2017 11:02 pm

ttmetro wrote:
Tue Oct 31, 2017 9:41 pm
... Presumably it's since I am using Python 3.6 and the monitor expects perhaps V2 ...
Can't you install Python 2.7 ?
On which OS are you building ?

If your default python (try python --version) is 3.x you can change it via menuconfig:
→ SDK tool configuration → Python 2 interpreter
CONFIG_PYTHON:

The executable name/path that is used to run python. On some systems Python 2.x
may need to be invoked as python2.

Post Reply