ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
Zoland
Posts: 20
Joined: Wed Jan 23, 2019 2:12 pm
Location: Russia, Altai

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

Post by Zoland » Wed Nov 10, 2021 5:10 am

1: In DOCs:
espnow — Support for the ESP-NOW protocol¶
This module provides an interface to the ESP-Now protocol provided by Espressif on ESP32 and ESP8266 devices. Some calls are only available on the ESP32 due to code size restrictions on the ESP8266 and differences in the Espressif API.

Link to ESP-Now protocol not worked, may be we can use:
https://www.espressif.com/sites/default ... de_en.pdf

2: I suggest adding section Features to the documentation as it is in Espressiff DOCs:
1.2. Features
ESP-NOW supports the following features:
• Encrypted and unencrypted unicast communication.
• Mixed encrypted and unencrypted peer devices.
• Up to 250-byte payload can be carried.
• The sending callback function that can be set to inform the application layer of transmission success or failure.
ESP-NOW technology also has the following limitations:
• Broadcast is not supported.
• Limited encrypted peers. 10 encrypted peers at the most are supported in Station mode; 6 at the most in SoftAP or SoftAP + Station mode. Multiple unencrypted peers are supported, however, their total number should be less than 20, including encrypted peers.
• Payload is limited to 250 bytes.

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

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

Post by glenn20 » Wed Nov 10, 2021 2:22 pm

@zoland Thanks for the suggestion. I've added the link to the Espressif PDF, fixed the link to the API docs and changed the intro text as below (not online yet). Let me know if you have any thoughts or suggested edits.
This module provides an interface to the ESP-NOW protocol provided by Espressif on ESP32 and ESP8266 devices (API docs).

ESP-NOW is a connectionless wireless communication protocol supporting:
  • Direct communication between up to 20 registered peers:
    • Without the need for a wireless access point (AP),
  • Encrypted and unencrypted communication (up to 6 encrypted peers),
  • Message sizes up to 250 bytes,
  • Can operate alongside Wifi operation (network.WLAN) on ESP32 and ESP8266 devices.
It is especially useful for small IoT networks, latency sensitive or power sensitive applications (such as battery operated devices) and for long-range communication between devices (hundreds of metres).

User avatar
Zoland
Posts: 20
Joined: Wed Jan 23, 2019 2:12 pm
Location: Russia, Altai

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

Post by Zoland » Wed Nov 10, 2021 5:56 pm

glenn20 wrote:
Wed Nov 10, 2021 2:22 pm
@zoland Thanks for the suggestion. I've added the link to the Espressif PDF, fixed the link to the API docs and changed the intro text as below (not online yet). Let me know if you have any thoughts or suggested edits.
Thanks for your job! New DOCs looks cool and informative
Last edited by Zoland on Thu Nov 11, 2021 3:50 am, edited 1 time in total.

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

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

Post by glenn20 » Wed Nov 10, 2021 9:12 pm

New images for ESP32, ESP8266 and ESP32S2 are uploaded to https://github.com/glenn20/micropython-espnow-images.

These are built from my espnow-g20 branch (with a few extra fixes).
- latest optimisations of the buffer code
- ESP8266 support for setting the default read timeout and on_recv callback function as positional args of init() (see docs).
- Significant overhaul of the docs (see https://micropython-glenn20.readthedocs ... spnow.html).

This matches the code in PR6515, which I have just updated as well.

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

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

Post by glenn20 » Sat Nov 13, 2021 12:57 am

OK - yet another round of images uploaded. I am now building for all the newly available ESP32 S2/S3/C3 targets in micropython main branch (except GENERIC_D2WD which fails due to image size limitations).

These are all untested except for GENERIC, GENERIC_S2, UM_FEATHERS2 and UM_TINYS2 (those are the boards I have). Note that I can not vouch for the stability of any of the untested targets.

Let me know if there are any issues with these boards. NOTE - these are NOT based on a micropython release, so there may be non-espnow related instabilities. Non-espnow images for many of these targets are also available in the micropython daily builds.

Latest ESP32 images at: micropython-espnow-images.

User avatar
Zoland
Posts: 20
Joined: Wed Jan 23, 2019 2:12 pm
Location: Russia, Altai

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

Post by Zoland » Sat Dec 04, 2021 6:16 pm

SMesh is a tiny mesh network which I created for my Smart Manor projects. SMesh is implemented on the ESP32 microcontroller, based on the Expressif ESP-Now protocol using the terminology used like in MQTT - The Publisher publishes news, the contents of which the Subscriber can receive over the network by specifying the name of the news topic. Knowledge of the MAC-addresses of network nodes is not required.

The news that is published by the Publisher can be implicit (broadcast) or explicit, for which the Subscriber will need to additionally specify:
• quality level of delivery - QoS, which allows the Publisher to determine the delivery protocol of publications
• expirie time for the Subscriber of news. If no updates have been received during this period, it is possible that the Publisher is not online. With an explicit request, the Subscriber is notified of this in order to make a decision - to wait until the connection is restored or to continue working.

The quality level of delivery of an explicit subscription - QoS determines how carefully the Publisher will try to deliver news to Subscribers. If there are several Subscribers, then the Publisher provides delivery with the highest level declared by one of the Subscribers. Higher QoS levels are more reliable, but involve more latency. Acceptable levels:
• 0 : news delivery without Subscriber confirmation.
• 1 : news delivery with confirmation of delivery by the Subscriber. When publishing, the Publisher will be notified that the news has been delivered safely or not. This is useful if the Publisher needs to control the Subscriber's presence on the network.
• 2 : unlike the quality of delivery QoS=1, the content of the news and the serial number remain unchanged until the subscriber's confirmation of receipt is received. This is useful in case of publishing news about emergency situations to fix the moment of the incident.

Foolproof protection is practically absent in order to minimize the code under the assumption that the jambs associated with the operation will be identified independently at the network setup stage.
The only significant limitation other than those imposed by the ESP-Now protocol is the uniqueness of the names of news topics published by the Publisher.

Due to the fact that time synchronization is not implemented in the network, the date of the event can be determined only by the difference in the serial numbers of publications.


There is only four functions with class SMesh:

Code: Select all

def user_on_receive_func ( node_name, topic, order, data ): # user callback
       ...

SMesh ( node_name [, user_on_receive_func] ) # configuration

SMesh.post ( topic, data ) # post news by Publisher

SMesh.set ( topic [,QoS=0] [,expirie=0]) # set explicit subscribe with parameters

SMesh.request ( topic ) # request explicit subscribe
Source on github : https://github.com/zoland/SMesh
That’s all )
Last edited by Zoland on Wed Dec 08, 2021 8:19 pm, edited 2 times in total.

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

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

Post by davef » Sun Dec 05, 2021 3:10 am

Anybody using lightsleep() on a ESP32 and the latest ESP-Now image?

Occasionally the unit locks-up and doesn't recover with the ESP watchdog timer. The code is here:
https://github.com/davefes/ESP-Now-repe ... /remote.py with all the watchdog stuff enabled, in case that might supply a clue.

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

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

Post by glenn20 » Sun Dec 05, 2021 6:39 am

Support for Wifi Signal Strength (RSSI)

I have added support for tracking the Wifi Signal Strength (RSSI) to the micropython ESPNow module (available in the espnow-feature-rssi branch). This is a particularly useful feature for managing networks of peer devices, including:
  • Tracking device proximity
  • Identifying peer devices with strong signal paths among a network
  • Supporting mesh capabilities in a network of devices
This feature has been possible using the secret sauce to be found in the new Espressif espnow higher-level code: espnow.c:espnow_recv_cb() at https://github.com/espressif/esp-now/ to extract the RSSI from the wifi headers.

After some deliberation, I have chosen initially to present this to the user via ESPNow.peers() which returns a reference to a dict of peers and rssi values. The rssi values are updated every time a message is received. This is documented at: Wifi Signal Strength (RSSI).

I figure this is the most convenient method for users to monitor current RSSI values of a network of devices.

The new peer device table has some subtle advantages for the recv()/irecv() interface as well (see the docs). For most use cases recv()/irecv() usage should be unchanged.

I am open to any suggestions, questions or criticisms of this approach. I will make new pre-compiled images availablle after there is time for users to comment or object to the proposed interface.

User avatar
Zoland
Posts: 20
Joined: Wed Jan 23, 2019 2:12 pm
Location: Russia, Altai

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

Post by Zoland » Mon Dec 06, 2021 8:11 am

glenn20 wrote:
Sun Dec 05, 2021 6:39 am
Support for Wifi Signal Strength (RSSI)
After some deliberation, I have chosen initially to present this to the user via ESPNow.peers() which returns a reference to a dict of peers and rssi values. The rssi values are updated every time a message is received. This is documented at: Wifi Signal Strength (RSSI).
I think it's cool for building more deep mesh networks with more complex algorithms. Even more - if we can hide MAC-address and replace them by symbolic names of the network nodes (as I made in my project) - it can make coding easily.

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

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

Post by glenn20 » Tue Dec 07, 2021 12:15 am

Zoland wrote:
Mon Dec 06, 2021 8:11 am
I think it's cool for building more deep mesh networks with more complex algorithms. Even more - if we can hide MAC-address and replace them by symbolic names of the network nodes (as I made in my project) - it can make coding easily.
Agreed. In fact, the Espressif code which uncovered the secret to extracting the rssi from the wifi headers (https://github.com/espressif/esp-now/) uses the rssi to support their espnow mesh protocol (to set thresholds to associate/disassociate with peers in the mesh).

@zoland: do you have links to your Smesh code? I couldn't find it in your github public repos.

Post Reply