ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
glenn20
Posts: 22
Joined: Mon Jun 04, 2018 10:09 am

ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Wed Oct 21, 2020 8:08 am

Hi All,

For those who may be interested, I have a new PR to progress more robust ESP-Now support on the ESP32 (and ESP8266).

You can see the PR at: https://github.com/micropython/micropython/pull/6515.
Docs at https://github.com/glenn20/micropython/ ... espnow.rst.

Testers are welcome, and user requirements (if not covered by the current PR).

Ring buffers in esp32/ringbuffer.[ch] for robust IO.
Allocate the ESPNow singleton dynamically (protect bufs from gc).
[/list]
New methods:
- recv() to read incoming messages (instead of callbacks).
- irecv() for allocation-free read of incoming messages.
- Stream IO support: read(), write() and poll through ioctl(). This
can be used to support asyncio use of espnow.
- Also added read1() and readinto1().
- Add support for iteration on ESPNow object for alloc-free read.
- config(): set tx and rx buf sizes and read timeout
- stats(): Returns transfer stats: (tx_packets, tx_packet_responses, rx_packets, lost_rx_packets).
- add_peer(mac[,lmk[,channel[,ifidx[,encrypt]]]]): Add peer info
- get_peer(mac): Return peer info: (mac,lmk,channel,ifidx,encrypt)
- mod_peer(mac, ...) to change peer info parameters.
- get_peers(): to return all peer info tuples.

dtrivinoj
Posts: 1
Joined: Mon Dec 07, 2020 6:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by dtrivinoj » Mon Dec 07, 2020 6:21 am

Hello Glenn
i sent the following message in github, hopping someone can help
If you have the time, please point me in the right direction to get this work for me

Thanks in advance for your help

Daniel
Hi
i am starting using ESP8266 and MicroPython is my preferred language. The question is, can anyone help me guiding me on how to load esp now in the module, please.
I am using ESP8266 12E
Thonny is my IDE
and the load is esp8266-20200911-v1.13.bin

The issue is, this error
from esp import espnow
Traceback (most recent call last):
File "", line 1, in
ImportError: can't import name espnow

Thanks in advance for your help

Daniel Trivino

davef
Posts: 134
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Thu Dec 10, 2020 1:03 am

This project look interesting as I want to be able to reliably load-shed if my main power inverter fails, or else the standby inverter would be damaged.

One of the many tutorials I read seemed to suggest that when the ESPs are configured as peer to peer that you couldn't then switch to WiFi operation. Correct?

I would run them as peer to peer and then for alarms and sending daily logs want to revert to WiFi so that I can connect to a Hotspot. Doable?

Thanks,
Dave

davef
Posts: 134
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Thu Dec 10, 2020 4:30 am

More searching and I think this sorts my requirements:
https://randomnerdtutorials.com/esp32-e ... eb-server/

davef
Posts: 134
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Sat Dec 12, 2020 8:23 am

Daniel,

I think you need to go to https://github.com/glenn20/micropython/ ... w-g20-8266, click on the green code button and either clone or download the .zip file. I think you have to then build this.

At https://www.eevblog.com/forum/beginners ... it-python/there is a zip file attached to the last post which has a bin file in it. I will try flashing that to a ESP8266 and see what happens.

Good luck,
Dave

glenn20
Posts: 22
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Wed Dec 16, 2020 5:37 am

Apologies for the lack of response - I have not been getting email notifications (fixed now - I hope).

@dtrivinoj - As helpfully explained by @davef, until my PR is accepted into the micropython main branch you have to re-compile and build micropython from my branch at https://github.com/glenn20/micropython/ ... w-g20-8266.

Follow the instructions at https://github.com/micropython/micropyt ... orts/esp32 to create a build environment, compile and deploy a new esp8266 image with ESPNOW support.

Good luck,
Glenn.

glenn20
Posts: 22
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Wed Dec 16, 2020 6:02 am

davef wrote:
Sat Dec 12, 2020 8:23 am
At https://www.eevblog.com/forum/beginners ... it-python/there is a zip file attached to the last post which has a bin file in it. I will try flashing that to a ESP8266 and see what happens.
That code uses an earlier version of the espnow support (which is much more susceptible to buffer overruns and dropped packets and uses a different API).

I have put up a compiled micropython image (v1.13 with espnow support) for the ESP8266 at https://drive.google.com/file/d/1za7GcP ... tgs9RgsAU4.

Good luck,
Glenn.

glenn20
Posts: 22
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Wed Dec 16, 2020 6:08 am

davef wrote:
Thu Dec 10, 2020 1:03 am
This project look interesting as I want to be able to reliably load-shed if my main power inverter fails, or else the standby inverter would be damaged.

One of the many tutorials I read seemed to suggest that when the ESPs are configured as peer to peer that you couldn't then switch to WiFi operation. Correct?

I would run them as peer to peer and then for alarms and sending daily logs want to revert to WiFi so that I can connect to a Hotspot. Doable?

Thanks,
Dave
You can send and recieve ESPNOW packets on the ESP32 interspersed with wifi traffic. There are also two wireless interfaces on the ESP32. so you can use one interface for ESPNow traffic and the other for wifi if you prefer (that is how I am using my ESP32s now.). For battery use, I use ESPNow on one interface for ESPNow traffic and wifi on the other interface for mqtt (only powered as needed).

davef
Posts: 134
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-Now support for ESP32 (and ESP8266)

Post by davef » Wed Dec 16, 2020 6:19 am

Hi Glenn,

Thanks for your work on this implementation. As soon as some ESP-07S arrive I will try out firmware-combined.bin

I hadn't seen that link. Would it not require quite a few changes for a different compiler? I followed some instructions that I found for building esp-open-sdk on this site, mainly to set up the toolchain, and then modified it to build your fork.
esp-open-sdk

sudo apt-get install make unrar-free autoconf automake libtool gcc g++ gperf \
flex bison texinfo gawk ncurses-dev libexpat-dev python-dev python python-serial \
sed git unzip bash help2man wget bzip2 libtool-bin
git clone --recursive https://github.com/pfalcon/esp-open-sdk.git
cd esp-open-sdk
make
export PATH=/home/dave/esp-open-sdk/xtensa-lx106-elf/bin/:$PATH
cd ..
sudo apt-get install build-essential libreadline-dev libffi-dev git pkg-config gcc-arm-none-eabi libnewlib-arm-none-eabi
git clone --recurse-submodules https://github.com/micropython/micropython.git
cd micropython/mpy-cross
make
cd ..
cd ports/unix
make
export PATH="$HOME/micropython/ports/unix:$PATH" (didn’t do this)
cd ..
cd esp8266/
make

micropython-espnow-g20-8266

git clone --recursive https://github.com/glenn20/micropython.git
cd micropython/mpy-cross
make
cd ..
cd ports/unix
make
export PATH="$HOME/micropython/ports/unix:$PATH" (didn’t do this)
cd ..
cd esp8266/
make board=GENERIC_1M
firmware-combined.bin
I need to check that I have selected the correct board variant. If there are errors above please let me know. I assume making it for unix is just to test that the build system works correctly.

Cheers,
Dave

glenn20
Posts: 22
Joined: Mon Jun 04, 2018 10:09 am

Re: ESP-Now support for ESP32 (and ESP8266)

Post by glenn20 » Wed Dec 16, 2020 6:24 am

glenn20 wrote:
Wed Dec 16, 2020 6:02 am
davef wrote:
Sat Dec 12, 2020 8:23 am
At https://www.eevblog.com/forum/beginners ... it-python/there is a zip file attached to the last post which has a bin file in it. I will try flashing that to a ESP8266 and see what happens.
That code uses an earlier version of the espnow support (which is much more susceptible to buffer overruns and dropped packets and uses a different API).

I have put up a compiled micropython image (v1.13 with espnow support) for the ESP8266 at https://drive.google.com/file/d/1za7GcP ... tgs9RgsAU4.

Good luck,
Glenn.
...and a precompiled image for the ESP32 at https://drive.google.com/file/d/1IIStoi ... UgOrHRe7VF

Post Reply