News:

We're live! Join us!

Main Menu

ESP-NOW

Started by Jeff_T, Apr 07, 2024, 11:43 AM

Previous topic - Next topic

Jeff_T

Hi, here is a short demo of the esp-now protocol, it can be used with many of the esp32/8266 varieties. It's quick and inexpensive to set up and this particular example is using micro python.



BTW this network is WLAN but does not use an access point or WiFi

Chris Savage

Very cool! I just got home, but I will watch this tomorrow during lunch.

        I'm only responsible for what I say, not what you understand.

Jeff_T

For anyone wanting to try this there are a couple of things that are useful to know.

First is obtaining, and making a note of the MAC address of each device. Here is a snippet that will print the six figure value in the REPL for each device.

import network

sta = network.WLAN(network.STA_IF)
sta.active(True)

wlan_mac = sta.config('mac')

print('\r')

for val in wlan_mac:
    print(hex(val),' ',end='')

Second ESP NOW uses a buffer obect called a bytearray to send and receive messages, so to add the receiver mac address to the peer group of the transmitter we need to write it as a bytearray.

For example when I run the above code on one of my devices I get the following address

0x34 0x85 0x18 0x7B 0x00 0xC8

to code that address into my transmitter code I would replace each of the leading zeros with a back slash then enclose the values in single quotes preceded with a lower case b

peer = b'\x34\x85\x18\x7B\x00\xC8'

There are lots of articles explaining bytearrays here is a link to one of them https://python-reference.readthedocs.io/en/latest/docs/functions/bytearray.html


Chris Savage

#3
I LOVE the way you put the Savage///Chats logo on the screen!  ;) I'll have to wait until tomorrow when I'm home to try the demo code you posted. I also find it interesting that you connect via MAC address, rather than IP address, but I can see some wisdom in that with regards to security.

        I'm only responsible for what I say, not what you understand.

Jeff_T

Hi Chris, the code I used is basically taken from the documentation at this link https://docs.micropython.org/en/latest/library/espnow.html. The example at that link is the first two code snippets, transmitter and receiver.

To get the best from this example I would recommend opening two instances of the Thonny IDE, one for transmitter and one for receiver..

By default you can only have one instance open at a time but if you go to Tools\Options unchecking the first checkbox will allow you to open more than one instance, you will need to restart Thonny for it to apply.

Like I said you can have as many as 20 ESP's sending/receiving in all kinds of ways, I'm going to do some more reading and see how others have managed the connections.

The display was something I added for effect, I'm trying to create a small driver using custom fonts and implementing a Micropython framebuffer module. I'm glad you were ok with me using your logo.

At some point the display code might make another good post.



Chris Savage

#5
I really appreciate you sharing these little tutorials and examples. I used to have so many on the old website. I love sharing this stuff. But I also enjoy learning new things myself. Any chance you could attach the transmitter / receiver code to a post? It would be nice to see a one-to-one of what's in the video.  ;)

As for the site logo...everything on the main site is either MIT license for code, no license or sometimes CC (Creative Commons). But I made it a point not to put a copyright notice on these forums as I hoped others would share and didn't want to put claim on anything.

I truly love to share and try to give as freely as I can. There are no ads on the main site or these forums and no cost to join. Eventually things will pick up on here and I know more people will appreciate the things you share and may be inspired to share their stuff as well.

        I'm only responsible for what I say, not what you understand.

Jeff_T

This is from the docs with a little modification to the transmit code, I programmed the transmitter to transmit user input and it should work fine out of the box, I encourage further modification to experiment with esp nows capabilities.

This link to the official docs https://docs.micropython.org/en/latest/library/espnow.html which details the various methods of esp-now.

I have been looking and learning from different configurations and I would like to put together a simple one to many network and perhaps get some feedback on its faults or good points, I think that would be something fun to do

Transmitter

import network
import espnow
from time import sleep

sta = network.WLAN(network.STA_IF) 
sta.active(True)

e = espnow.ESPNow()
e.active(True)

peer = b'\x00\x00\x00\x00\x00\x00'  # replace this with your receivers MAC
e.add_peer(peer)

msg = " "
e.send(peer, msg,False)
 
while True:
    msg = input("Input --> ")

    rt=e.send(peer, msg,True)

    sleep(0.5)
   
    if msg == 'end':
        break   

Receiver

import network
import espnow

sta = network.WLAN(network.STA_IF)
sta.active(True)

e = espnow.ESPNow()
e.active(True)

while True:
    host, msg = e.recv()
    if msg:           
        print(host, msg)
        if msg == b'end':
            break

@Chris, I like being able to share this stuff, some of it has been around a long time but the Micropython side is perhaps not so well known, I'm just learning Micropython and it can do pretty amazing things.

Chris Savage

I did download the current version of Thonny (4.1.4). I will install it on my development machine this week / weekend.

Since I have the two ESP32 modules I can, of course, recreate that demo. I just need to see what displays I have. Not sure what display you used there. I think I need to reread some messages.

        I'm only responsible for what I say, not what you understand.

Jeff_T

#8
Hi Chris, initially the above demo can be run and the output viewed in the Thonny IDE, just two ESP's absolutely no other hardware needed. Not only the Nano but any ESP32 you have available.

Micropython has a feature that it uses in an IDE called REPL, READ-EVAL-PRINT LOOP , it's a test/debug feature that allows the user to type input, modify running code and view the results in the output pane.

Initially installing the Micropython firmware might be a little tricky but when you have done it once it's a breeze. The instructions for the Nano ESP32 are here https://docs.arduino.cc/micropython/basics/board-installation/

Once you have that you just connect to their respective com port with Thonny and you can modify the code and see different results. Below are the links you should have for the Nano.

The pin out is confusing because they give two different numbers for each pin, one for Micropython and one for Arduino. Just remember that for Micropython you use the GPIO numbers alternately called ESP32 numbers.

https://docs.arduino.cc/tutorials/nano-esp32/cheat-sheet/

chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://docs.arduino.cc/resources/pinouts/ABX00083-full-pinout.pdf

https://thonny.org/

Chris Savage

This weekend will see me at least doing the demo you posted so I can get some starting experience.  8)

        I'm only responsible for what I say, not what you understand.

Jeff_T

#10
I think once you get into it you will enjoy it Chris, Micropython is not for everyone, c/c++ is definitely faster but MP is a capable language with lots of resources.

With regard to ESP NOW I have been trying out a one to many system, when you get up and running perhaps we can talk and you can give me your opinion on how best to set things up, I would like to tap into your experience on designing a good communications protocol.

P.S. Check out the little write up on tft displays Chris, that's something else I think you might find interesting.

Chris Savage

Will do. I am eager to get something started now that parts are incoming.

        I'm only responsible for what I say, not what you understand.

Chris Savage

As an update, I created a stub for where I will start posting my work. I didn't get as far as I planned with getting everything up today. I got a lot of content up, but by the time I got to the ESP32 stuff, it was (is) well after midnight. More to come.

Arduino Nano ESP32 Experiments Thread

        I'm only responsible for what I say, not what you understand.

Jeff_T

I know the feeling Chris, the weather here is getting nice and it's time for outdoor work. I spent yesterday pressure washing the back patio and the day before mowing and cleaning up the yard.

I have plans for ESP Now and have made a tentative start that I will share soon. I have an ESP Wroom which I have named Base and for now I have two Nano Esp32 which act as slaves and I have started to call remote IO, rio_1 and rio_2.

The way it works so far is that the Base station has a list of known Mac address's, every remote IO is provided with a hard coded entry of the Base's Mac which it transmits in a heartbeat kind of way.

So Base is up and running and I power up one of the remote IO, Base checks this remote IO against it's list of known Macs and if there is a match it will add it to the peer group and return ack then look for the next device.

Base is programmed to do this as many as two times at which point it says the network is satisfied and it begins the tx/rx control loop. I say two times but if you wanted a larger network the number can be made larger.

This configuration allows for remote IO to be pre-programmed as spares or have a slightly different group of functions or even allow for network expansion and the remote IO will simply be plug and play device's.

The initialization is looking good I'm looking at a way now to detect and react to any hardware failures.

The experiments thread is a good idea, will it be open to c/c++ and micropython?

Chris Savage

Quote from: Jeff_T on Apr 15, 2024, 10:12 AMThe experiments thread is a good idea, will it be open to c/c++ and micropython?

I just realized that I never answered this question. The thread on here is open season. The project on Savage///Circuits will specifically cover MicroPython / ESP-NOW demos that you have posted here, as well as any changes I make while experimenting. The idea is to see if anything leads to a project around the ESP32.

You have the option to discuss any Savage///Circuits thread here (they're cross-linked) or you can comment on Savage///Circuts (requires login).

        I'm only responsible for what I say, not what you understand.

SMF spam blocked by CleanTalk