ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
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 » Tue Dec 22, 2020 8:56 am

Loaded your latest image on the ESP32 and put your scripts in that you responded with to John.

I get an WiFi mode error on:

Code: Select all

w.config(mac=b'\xaa\xaa\xaa\xaa\xaa\xaa')

which I removed by saying:

Code: Select all

w.active(True)
first.

However, I sometimes get "Guru Meditation Error" and then it won't get past:

Code: Select all

Testing if ubinascii.unhexlify exists ...
So, erase_flash and write_flash and try again.

On further investigation it looks like the problem is more fundamental ... in rshell if I place my espnow_example.py file in /pyboard (ESP32) and try to edit it then it crashes and I have to erase_flash and write_flash again.

I have spent quite a bit of time working with the ESP32 but this is the first time I have hit this particular problem.

Dave
Last edited by davef on Wed Dec 23, 2020 3:06 am, edited 1 time in total.

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 » Tue Dec 22, 2020 10:46 am

And for a ESP-01S:

Code: Select all

dave@dave-AOD255E:~$ sudo esptool.py --chip esp8266 --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 micropython-1.13-espnow-g20-8266.bin
I need to re-boot the board after loading, which I am sure I haven't had to do before. No problem, however using rshell I can modify boot.py and add a small script but if I try to edit that small file things lock-up.

How much room is left for my scripts?

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 » Wed Dec 23, 2020 3:00 am

Regarding documentation I found this:
https://github.com/glenn20/micropython/ ... espnow.rst
which explains everything. Appears I was looking at the commit changes :(

And thank you for the examples.

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 23, 2020 4:26 am

Update:
- your scripts seem to run better so ignore previous code-related comments.

However, I am stuck with this error:

Code: Select all

MicroPython v1.13-109-g40d183c6f on 2020-12-16; ESP32 module with ESP32
Type "help()" for more information.
>>> import sender
Send buffer: alloc=149 size=141 head=  0, tail=  0, used=  0, free=140, start=0x3ffe5908
Recv buffer: alloc=525 size=517 head=  0, tail=  0, used=  0, free=516, start=0x3ffe56f8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sender.py", line 13, in <module>
OSError: (-12393, 'ESP_ERR_ESPNOW_NOT_FOUND')
line 13 is:

Code: Select all

e.send("Starting...") #  send to all peers
I have one receiver running, in case that is required for the sender code to run properly.

Looking at:
https://docs.espressif.com/projects/esp ... p_now.html
is says that the peer can not be found. After the correct MAC addresses are added this error disappeared.

Dave
Last edited by davef on Sun Dec 27, 2020 9:45 pm, edited 2 times in total.

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 23, 2020 9:34 am

Something in espnow.rst that I do not understand:
import network

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

.. method:: ESPNow.send(mac, msg, [sync=True]) (ESP32 only)
ESPNow.send(msg) (ESP32 only)
Is the last line in the quote suppose to be: (ESP8266)?

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 24, 2020 4:45 am

State of progress.

For the examples given:
receiver
- missing : after if statement<br/>

sender

Code: Select all

import network
from esp import espnow

#  A WLAN interface must be active to send()/recv()
w0 = network.WLAN(network.STA_IF) #  or network.AP_IF
w0.active(True)

e = espnow.ESPNow()
e.init()
peer = b'\xbb\xbb\xbb\xbb\xbb\xbb' #  MAC address of peer's wifi interface
e.add_peer(peer)

e.send(peer, "Starting...", True)

for i in range(100):
    e.send(peer, str(i)*20, True)
e.send(peer, b'end', True)
lines 13 and 17 seemed to work better with (peer, str, True)

For the ESP8266

receiver

Code: Select all

import network
from esp import espnow
import utime


#  A WLAN interface must be active to send()/recv()
w0 = network.WLAN(network.STA_IF)
w0.active(True)

e = espnow.ESPNow()
e.init(100)
peer = b'\xaa\xaa\xaa\xaa\xaa\xaa' #  MAC address of peer's wifi interface
e.add_peer(peer)

utime.sleep(5)
print('got this far')

#print(e.irecv()) # does this work for ESP8266?, see Iteration over ESPNow

#  for ESP32
#for msg in e:
#    print(msg)
#    if (msg[1] == b'end'):
#        break
seemed to work better with a value in init() Also, need to work out how to get the msgs.

sender

Code: Select all

import network
from esp import espnow

#  A WLAN interface must be active to send()/recv()
w0 = network.WLAN(network.STA_IF) #  or network.AP_IF
w0.active(True)

e = espnow.ESPNow()
e.init()
peer = b'\xbb\xbb\xbb\xbb\xbb\xbb' #  MAC address of peer's wifi interface

e.send(peer, "Starting...", True)
for i in range(100):
    e.send(peer, str(i)*20, True)
e.send(peer, b'end', True)

print('finished')
An example of how to grab the messages using a ESP8266 would be appreciated.

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

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

Post by glenn20 » Thu Dec 24, 2020 11:13 pm

Hi Dave,

Glad you have persisted.
davef wrote:
Wed Dec 23, 2020 4:26 am
However, I am stuck with this error:

Code: Select all

MicroPython v1.13-109-g40d183c6f on 2020-12-16; ESP32 module with ESP32
Type "help()" for more information.
>>> import sender
Send buffer: alloc=149 size=141 head=  0, tail=  0, used=  0, free=140, start=0x3ffe5908
Recv buffer: alloc=525 size=517 head=  0, tail=  0, used=  0, free=516, start=0x3ffe56f8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sender.py", line 13, in <module>
OSError: (-12393, 'ESP_ERR_ESPNOW_NOT_FOUND')
line 13 is:

Code: Select all

e.send("Starting...") #  send to all peers
That error message ESP_ERR_ESPNOW_NOT_FOUND is returned by the underlying Espressif ESPNOW software stack. I can't recall this specific error, but I am guessing it may be that there is no peer registered yet (add_peer()). In case I have introduced a bug or ambiguity somewhere, it would be safer to provide all 3 arguments to send(mac, msg, sync).

Good luck.

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

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

Post by glenn20 » Thu Dec 24, 2020 11:40 pm

davef wrote:
Thu Dec 24, 2020 4:45 am
For the examples given:
receiver
- missing : after if statement<br/>
Thanks for picking that up - I'll fix docs in next commit.
lines 13 and 17 seemed to work better with (peer, str, True)
I'll look into that. On the ESP32 it is *supposed* to work with just 'msg' as argument, (not on ESP8266 - due to need to compress the code size) but I may have broken something here - I'll investigate.

Code: Select all

e = espnow.ESPNow()
e.init(100)
seemed to work better with a value in init() Also, need to work out how to get the msgs
It *should* not be necessary to provide an argument to init(), but on the ESP8266 you can provide the read buffer size as an argument - as there is NO config() call on the 8266 (because the code size requirements are too tight) (see my docs). 100 bytes seems small for the buffer size.
An example of how to grab the messages using a ESP8266 would be appreciated.
As inidcated in the docs, the only method for receiving messages on the ESP8266 is irecv(). I recommend leaving the default buffer size: ie. **e.init()** and then **e.irecv()** should work fine.

(I am away from my esp32s for a few days, so can't absolutely verify everything definitively - but all this was working for my tests a few days ago).

If using the esp8266 check my docs to confirm which calls are available on the 8266.

Cheers,
Glenn.

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

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

Post by glenn20 » Thu Dec 24, 2020 11:43 pm

davef wrote:
Tue Dec 22, 2020 6:34 am
Xubuntu said I needed to install sphinxsearch, but sphinx_build is nowhere to be found.

I'll do more reading later. For now I'll just edit the file.

Thanks,
Dave
According to the micropython docs at: https://github.com/glenn20/micropython/ ... w-g20/docs:

Code: Select all

pip install sphinx
pip install sphinx-rtd-theme

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 » Fri Dec 25, 2020 4:39 am

Hi glenn20,

Thank you for the feedback. Anytime you see the word "seemed" it means that things did not seem to work 100% of the time. When looking at traceback messages sometimes it "seemed" that a programming error later in the program would throw up an error at a earlier line number.
I got into a habit of commenting-out every line and introducing them one at a time, until an error would pop-up.

For the ESP8266:
Today, I do not have to put a value in init() in rx8266.py so remove that issue.

Code: Select all

for msg in e:
of course throws a 'ESPNow' object is not iterable as you can only use this on the ESP32

I do not know other ways of getting the "callee-owned" tuple of bytearrays into a form that I can use. Off to check with Mr. Google.

Dave

Post Reply