Raspberry BASIC

Author Topic: ESP32 MicroPython  (Read 6520 times)

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
ESP32 MicroPython
« on: March 14, 2021, 09:35:57 AM »
I was able to get MicroPython loaded on my ESP32-DEVKITC32D board. I'm using Putty on the RPi 3B to connect via /dev/ttyUSB0.


>>> help('modules')
__main__          gc                ubinascii         urandom
_boot             inisetup          ubluetooth        ure
_onewire          machine           ucollections      urequests
_thread           math              ucryptolib        uselect
_uasyncio         micropython       uctypes           usocket
_webrepl          neopixel          uerrno            ussl
apa106            network           uhashlib          ustruct
btree             ntptime           uheapq            usys
builtins          onewire           uio               utime
cmath             uarray            ujson             utimeq
dht               uasyncio/__init__ umqtt/robust      uwebsocket
ds18x20           uasyncio/core     umqtt/simple      uzlib
esp               uasyncio/event    uos               webrepl
esp32             uasyncio/funcs    upip              webrepl_setup
flashbdev         uasyncio/lock     upip_utarfile     websocket_helper
framebuf          uasyncio/stream   upysh
Plus any modules on the filesystem
>>>
>>> machine.freq(240000000)
>>> machine.freq()
240000000
>>>




« Last Edit: March 14, 2021, 08:49:59 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: ESP32 MicroPython
« Reply #1 on: March 15, 2021, 12:45:30 AM »
I was able to get my DS18B20 temperature sensor to work with the ESP32-DEVKITC32D board.

The interpretive method of programming sure beats having to compile an image and flash it to the controller with each experiment.

I'm going to flash my ESP32-PICO with MicroPython to free up my faster board for more demanding projects.


>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import time, ds18x20
=== from machine import Pin
=== import onewire
===
=== ow = onewire.OneWire(Pin(12))
=== ds = ds18x20.DS18X20(ow)
=== i = 1
=== while i < 11:
===   roms = ds.scan()
===   ds.convert_temp()
===   for rom in roms:
===     print("Sensor " + str(rom) + ": " + str(ds.read_temp(rom)))
===   time.sleep_ms(2000)
===   i += 1
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.375
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.3125
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.3125
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.3125
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.3125
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.375
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.375
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.375
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.375
Sensor bytearray(b'(\xff&\xc2\x80\x17\x04\x93'): 22.375
>>> print(rom)
bytearray(b'(\xff&\xc2\x80\x17\x04\x93')
>>> print(roms)
[bytearray(b'(\xff&\xc2\x80\x17\x04\x93')]
>>> print(ow)
<OneWire object at 3ffe5360>
>>> print(ds)
<DS18X20 object at 3ffe5370>
>>>

« Last Edit: March 15, 2021, 10:35:55 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: ESP32 MicroPython
« Reply #2 on: March 15, 2021, 09:28:47 PM »
I got MicroPython running on my ESP32-PICO board. The sensor blinks each time it's scanned.

Picture


pi@RPi3B:~/esp $ /home/pi/esp/esp-idf/components/esptool_py/esptool/esptool.py --port /dev/ttyUSB0 erase_flash
esptool.py v3.1-dev
Serial port /dev/ttyUSB0
Connecting........_
Detecting chip type... ESP32
Chip is ESP32-PICO-D4 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, Embedded Flash, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: d8:a0:1d:5f:c1:54
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 5.1s
Hard resetting via RTS pin...
pi@RPi3B:~/esp $

pi@RPi3B:~/esp $ /home/pi/esp/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-idf4-20210202-v1.14.bin
esptool.py v3.1-dev
Serial port /dev/ttyUSB0
Connecting....
Chip is ESP32-PICO-D4 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, Embedded Flash, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: d8:a0:1d:5f:c1:54
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Compressed 1484624 bytes to 951640...
Wrote 1484624 bytes (951640 compressed) at 0x00001000 in 24.2 seconds (effective 491.4 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
pi@RPi3B:~/esp $

« Last Edit: March 15, 2021, 10:15:15 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: ESP32 MicroPython
« Reply #3 on: March 16, 2021, 08:33:03 PM »
This is a ESP32-PICO MicroPython pystone_lowmem benchmark example. This also shows how to use upip to install additional libraries.

Code: Python
  1. >>>
  2. MPY: soft reboot
  3. MicroPython v1.14 on 2021-02-02; ESP32 module with ESP32
  4. Type "help()" for more information.
  5. >>> import network
  6. >>> sta_if = network.WLAN(network.STA_IF)
  7. >>> sta_if.active()
  8. False
  9. >>> sta_if.active(True)
  10. True
  11. >>>
  12. >>> sta_if.scan()
  13. [(b'jrs', b'\xd8\x07\xb6\xf8\xb1\x97', 10, -31, 4, False), (b'BillingsHouse', b'D\x1c\x12\xd8\x99\x96', 1, -64, 3, False), (b'Nika', b'\xec\xaa\xa02\xb0h', 6, -79, 3, False), (b'chicken shit', b'\xd4\xab\x82\xc7\x8d\x92', 1, -84, 3, False), (b'AOL', b'\xbc\x9bh\xa6\x0f\x18', 11, -84, 3, False), (b'2018NEW3beginsYIP!', b'\xd4\xab\x82\xc3\x15\xea', 11, -84, 3, False), (b'My Bell', b'\x80>H\xb7o\xc5', 11, -86, 4, False), (b'Anahouse', b'\x84\x00-\x03\xc1\x98', 11, -89, 3, False), (b'Guapo', b'\xac\xdbHB\xa9\x86', 11, -89, 3, False), (b'IslandHouse', b'P\xd4\xf7\x02b\x89', 2, -90, 3, False), (b'password', b'<z\x8a9\xce`', 1, -91, 3, False), (b'BK WiFi', b'\\\xe3\x0e\xbc\x13\xb5', 6, -91, 3, False), (b'UmbrellaCorp', b'\x84\xd8\x1bfu\xce', 1, -95, 3, False)]
  14. >>> sta_if.connect('jrs', 'MY_PASSWORD')
  15. >>> sta_if.isconnected()
  16. True
  17. >>> import upip
  18. >>> upip.install("micropython-pystone_lowmem")
  19. Installing to: /lib/
  20. Warning: micropython.org SSL certificate is not validated
  21. Installing micropython-pystone_lowmem 3.4.2.post4 from https://micropython.org/pi/pystone_lowmem/pystone_lowmem-3.4.2.post4.tar.gz
  22. >>> import pystone_lowmem
  23. >>>
  24. >>> pystone_lowmem.main()
  25. Pystone(1.2) time for 500 passes = 536ms
  26. This machine benchmarks at 932 pystones/second
  27. >>>
  28.  

This is the same benchmark but using my ESP32-DEVKITC32D board and increasing the processor frequency rate.

Code: Python
  1. >>>
  2. MPY: soft reboot
  3. MicroPython v1.14 on 2021-02-02; ESP32 module with ESP32
  4. Type "help()" for more information.
  5. >>> import network
  6. >>> sta_if = network.WLAN(network.STA_IF)
  7. >>> sta_if.active()
  8. False
  9. >>> sta_if.active(True)
  10. True
  11. >>> sta_if.connect('jrs', 'MY_PASSWORD')
  12. >>> sta_if.isconnected()
  13. True
  14. >>> import upip
  15. >>> upip.install("micropython-pystone_lowmem")
  16. Installing to: /lib/
  17. Warning: micropython.org SSL certificate is not validated
  18. Installing micropython-pystone_lowmem 3.4.2.post4 from https://micropython.org/pi/pystone_lowmem/pystone_lowmem-3.4.2.post4.tar.gz
  19. >>> import pystone_lowmem
  20. >>> pystone_lowmem.main()
  21. Pystone(1.2) time for 500 passes = 537ms
  22. This machine benchmarks at 931 pystones/second
  23. >>> import machine
  24. >>> machine.freq(240000000)
  25. >>> pystone_lowmem.main()
  26. Pystone(1.2) time for 500 passes = 432ms
  27. This machine benchmarks at 1157 pystones/second
  28. >>>
  29.  

I retried the ESP32-PICO (not related to RPi Pico) and increased the frequency to 240000000 it ran faster than my ESP32-DEVKITC32D board.  :o


>>> pystone_lowmem.main()
Pystone(1.2) time for 500 passes = 428ms
This machine benchmarks at 1168 pystones/second
>>>


I need to do a side-by-side comparison to see what makes them different.

I found a post that shows the RPI Pico pystone_lowmem results.


>>> exec(open('Pi_Pico_Pystone_lowmem.py').read())
Pystone(1.2) time for 500 passes = 460ms
This machine benchmarks at 1086 pystones/second

>>>

« Last Edit: March 16, 2021, 11:25:09 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: ESP32 MicroPython
« Reply #4 on: March 17, 2021, 12:11:11 AM »
ESP32-PICO-D4

Detailed Spec.

ESP32 contains two low-power Xtensa 32-bit LX6 microprocessors. (240 MHz) The internal memory includes:

* 448 KB of ROM for booting and core functions.
* 520 KB of on-chip SRAM for data and instructions.
* 8 KB of SRAM in RTC, which is called RTC FAST Memory and can be used for data storage; it is accessedby the main CPU during RTC Boot from the Deep-sleep mode.
* 8 KB of SRAM in RTC, which is called RTC SLOW Memory and can be accessed by the co-processorduring the Deep-sleep mode.
* 1 Kbit of eFuse: 256 bits are used for the system (MAC address and chip configuration) and the remaining768 bits are reserved for customer applications, including flash-encryption and chip-ID
* 4 MB Flash
* Wi-Fi and Bluetooth
$10


ESP32-DEVKITC32D

Detailed Spec.

ESP32-D0WD contains a dual-core Xtensa 32-bit LX6 MCU microprocessor. (240 MHz) The internal memory includes:

* 448 KB of ROM for booting and core functions.
* 520 KB of on-chip SRAM for data and instructions.
* 8 KB of SRAM in RTC, which is called RTC FAST Memory and can be used for data storage; it is accessedby the main CPU during RTC Boot from the Deep-sleep mode.
* 8 KB of SRAM in RTC, which is called RTC SLOW Memory and can be accessed by the co-processorduring the Deep-sleep mode.
* 1 Kbit of eFuse: 256 bits are used for the system (MAC address and chip configuration) and the remaining768 bits are reserved for customer applications, including flash-encryption and chip-ID.
* 4 MB Flash
* Wi-Fi and Bluetooth
$10


Raspberry Pi Pico

* Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133 MHz
* 264KB on-chip SRAM
* 2MB on-board QSPI Flash
$4
« Last Edit: March 17, 2021, 01:57:14 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: ESP32 MicroPython
« Reply #5 on: March 18, 2021, 05:31:02 AM »
I have spent hours trying to get my DHT11 and DHT22 working on Micropython using the DHT library. The measure() call returns a ETIMEOUT with every attempt.

Has anyone gotten this to work?

You don't need to join the forum to respond. Send an e-mail to support@raspberrybasic.org with suggestions would be appreciated.
« Last Edit: March 18, 2021, 05:45:55 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: ESP32 MicroPython
« Reply #6 on: March 18, 2021, 07:26:12 PM »
I tried to install the ESP-IDF tools on my RPi 4B running Ubuntu 64 bit.

This sucks!


jrs@RPi-Dev:~/esp/esp-idf$ ./install.sh
Detecting the Python interpreter
Checking "python" ...
/home/jrs/esp/esp-idf/tools/detect_python.sh: line 16: python: command not found
Checking "python3" ...
Python 3.8.6
"python3" has been detected
Installing ESP-IDF tools
Installing tools: xtensa-esp32-elf, xtensa-esp32s2-elf, xtensa-esp32s3-elf, riscv32-esp-elf, esp32ulp-elf, esp32s2ulp-elf, openocd-esp32
ERROR: tool xtensa-esp32-elf does not have versions compatible with platform linux-arm64
jrs@RPi-Dev:~/esp/esp-idf$



On a positive note I was able to install the ESP32 development tools on my 4 GB / 2 core @ 2.13 ghz AMD old laptop. It's running Ubuntu 20.04 64 bit OS. Runs a lot faster than my RPi 3B.

« Last Edit: March 19, 2021, 05:17:06 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 234
    • View Profile
    • ScriptBasic
Re: ESP32 MicroPython
« Reply #7 on: March 21, 2021, 12:49:05 AM »
I was able to get my NeoPixel stick working under MicroPython on my ESP32-PICO-D4 controller.

The picture doesn't do the color presentation any justice. Looks great live.

I can see this being used to create outdoor billboards. A string of NeoPixels to decorate a Christmas tree is another idea.

Code: Python
  1. import machine, neopixel
  2. n = 8
  3. p = 5
  4. np = neopixel.NeoPixel(machine.Pin(p), n)
  5. np[0] = (255, 0, 0)
  6. np[1] = (0, 255, 0)
  7. np[2] = (0, 0, 255)
  8. np[3] = (128, 0, 0)
  9. np[4] = (0, 128, 0)
  10. np[5] = (0, 0, 128)
  11. np[6] = (125, 204, 223)
  12. np[7] = (255, 0, 153)
  13. np.write()
  14.  

This example I converted from CircuitPython displays a rainbow effect.

NeoPixel Rainbow video clip. I hide the stick to cut down on the glare.  8)

Code: Python
  1. # MicroPython demo - NeoPixel
  2. import time
  3.  
  4. import machine
  5. import neopixel
  6.  
  7. pixel_pin = machine.Pin(5)
  8. num_pixels = 8
  9.  
  10. pixels = neopixel.NeoPixel(pixel_pin, num_pixels)
  11.  
  12.  
  13. def wheel(pos):
  14.     # Input a value 0 to 255 to get a color value.
  15.     # The colours are a transition r - g - b - back to r.
  16.     if pos < 0 or pos > 255:
  17.         return (0, 0, 0)
  18.     if pos < 85:
  19.         return (255 - pos * 3, pos * 3, 0)
  20.     if pos < 170:
  21.         pos -= 85
  22.         return (0, 255 - pos * 3, pos * 3)
  23.     pos -= 170
  24.     return (pos * 3, 0, 255 - pos * 3)
  25.  
  26.  
  27. def rainbow_cycle(wait):
  28.     for j in range(255):
  29.         for i in range(num_pixels):
  30.             rc_index = (i * 256 // num_pixels) + j
  31.             pixels[i] = wheel(rc_index & 255)
  32.         pixels.write()
  33.         time.sleep(wait)
  34.  
  35.  
  36. while True:
  37.     rainbow_cycle(0)  # Increase the number to slow down the rainbow
  38.  


« Last Edit: March 21, 2021, 04:39:54 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator