ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
PLUB
Posts: 10
Joined: Fri Jan 08, 2021 1:06 pm

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

Post by PLUB » Sat Jun 18, 2022 11:20 am

Hi,
I have 2 ESP8266 communicate with ESP-NOW. I need to generate a random number on one side and send it to the other ESP.
When I write:
import random
nb=random.randint(0.9)
I have the error message: AttributeError: 'module' object has no attribute 'randint'
Where is my mistake?
Thanks.

PLUB
Posts: 10
Joined: Fri Jan 08, 2021 1:06 pm

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

Post by PLUB » Sat Jun 18, 2022 12:01 pm

I tested :
import urandom
nb=urandom.randint(0,9)
And I have the same error message

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

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

Post by Roberthh » Sat Jun 18, 2022 12:11 pm

uos.randint() is not supported. You can use uos.urandom(n), which return n random bytes. Or you use the module urandom with urandom.getrandbits(n), n <= 32m, which returns a random number of n bits size. urandom.getrandbots() returns a pseudo random number, whereas uos.urandom() returns a true random number.
You can use both methods to create a randint() function, if needed.

PLUB
Posts: 10
Joined: Fri Jan 08, 2021 1:06 pm

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

Post by PLUB » Sun Jun 19, 2022 11:03 am

Thanks for your help

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

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

Post by glenn20 » Mon Jun 27, 2022 11:18 am

An update on the status of the ESPNow module:

I haven't released any new espnow images for the 1.19 micropython release as I have been focussed on implementing requested changes to the module API before merging into micropython master branch. You can see the requested list of changes and my responses and progress at: https://github.com/micropython/micropyt ... 1128522735.

I wanted to provide a heads-up to users of the current espnow module that these API changes are in the pipeline. I plan to provide some backward compatibility support in a new espnowio module (will possibly be renamed) that I hope will be accepted into the micropython main branch. If it is not accepted I will distribute it separately on github. You can see the espnowio.py module at: espnowio.py

You can see the documentation for the new API at readthedocs

Usage would be:

Code: Select all

# from esp import espnow
import espnowio as espnow

e = espnow.ESPNow()
...
If you have any questions, comments or criticisms (constructive - I hope ;)) feel free to respond here.

PS. I plan to provide updated images soon based on the previous API (I don't want to release mages based on the new API which is still in flux - additional changes may be requested prior to merging).

PPS. Oh - and I thought I would mention that it has been challenging to implement a clean API which maximises compatibility between the ESP32 and ESP8266 interfaces while meeting the very tight code size restrictions of the ESP8266.

mekanixms
Posts: 28
Joined: Fri Oct 18, 2019 3:46 pm

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

Post by mekanixms » Thu Jul 07, 2022 7:29 am

Thanks much for your work!

Looking forward for the module to be accepted into the main branch - would be a great step forward, and expected one, for esp-now on micropython community

I compiled your latest 1.18 branch for generic ESP32 and included some of my scripts to be frozen.
For some reason I cannot connect to repl ... I must have done something wrong
Tried also without any of my scripts, same issue

I am compiling using the standard instructions, nothing special

Any suggestions?

mekanixms
Posts: 28
Joined: Fri Oct 18, 2019 3:46 pm

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

Post by mekanixms » Thu Jul 07, 2022 8:04 am

One more thing, switching to your yesterday's branch, 1.19.1 I see I can connect to repl however, the prompt say "MicroPython v1.13-1968-g537248958 on 2022-07-07".
Why 1.13? What am I doing wrong?

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

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

Post by glenn20 » Fri Jul 08, 2022 12:57 am

New espnow-enabed micropython images for release v1.19.1:
glenn20 wrote:
Mon Jun 27, 2022 11:18 am
PS. I plan to provide updated images soon based on the previous API (I don't want to release mages based on the new API which is still in flux - additional changes may be requested prior to merging).
I have uploaded new espnow-enabled micropython images to: https://github.com/glenn20/micropython-espnow-images.

Slight change of plan from my intentions stated above. These new images are based on the latest version of my espnow-g20 branch. In the end, the impact on the user API is not so great. As I flagged, the C module code-base has been simplified and streamlined and this is now wrapped in a python module on the esp32 and esp8266 devices. This also makes it easier to support more compatibility between the esp32 and esp8266 devices.

The top-level espnow module is now imported with:

Code: Select all

import espnow
instead of:

Code: Select all

# from esp import espnow
Usage would be:

Code: Select all

# from esp import espnow
import espnow

e = espnow.ESPNow()
...
I have tested images on esp32: GENERIC, GENERIC_S2, UM_FEATHERS2, UM_FEATHERS3, UM_TINYS2 and esp8266: GENERIC, GENERIC_1M. Other images are untested as I don't have compatible devices.

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

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

Post by glenn20 » Fri Jul 08, 2022 1:15 am

mekanixms wrote:
Thu Jul 07, 2022 7:29 am
I compiled your latest 1.18 branch for generic ESP32 and included some of my scripts to be frozen.
For some reason I cannot connect to repl ... I must have done something wrong
Tried also without any of my scripts, same issue

I am compiling using the standard instructions, nothing special

Any suggestions?
I have encountered similar issues in the past myself. Sometimes it is due to a bug I have introduced or some incompatibility with the IDF version I am using (most common).

A good starting point is to compile a vanilla micropython image without the espnow patches applied and check success with that. Also, check the version of the IDF you are using. It is notable that the micropython CI scripts always use v4.0.2 for ESP32 flavours (and v4.4 for s2/s3/2/c3 variants). See the ci_esp32_*() functions in tools/ci.sh for a reproducible procedure to setup a build environment. (However, I *believe* the released versions use IDF v4.2, instead of v4.0.2).

Good luck...

User avatar
glenn20
Posts: 132
Joined: Mon Jun 04, 2018 10:09 am

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

Post by glenn20 » Fri Jul 08, 2022 1:35 am

mekanixms wrote:
Thu Jul 07, 2022 8:04 am
One more thing, switching to your yesterday's branch, 1.19.1 I see I can connect to repl however, the prompt say "MicroPython v1.13-1968-g537248958 on 2022-07-07".
Why 1.13? What am I doing wrong?
That is odd. The image should be tagged with the output of the "git describe" command. You could try running that command in your clone of the espnow-g20-v1.19.1 branch. It should identify the HEAD with respect to the most recent git tag.

Also, try:

Code: Select all

import sys; sys.version
That should confirm if your image is really a v1.19.1 image.

Also - check you have flashed the correct image into the device and you are talking to the right device (rookie mistakes - but I've made them before :-).

BTW. I have released compiled images based on v1.19.1. You could try them as well.

Post Reply