Page 13 of 37
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Sun Aug 29, 2021 1:54 am
by davef
On the line:
Code: Select all
e.send(local, msg, sync=True)
TypeError: function doesn't take keyword arguments
so I just changed it to
e.send(local, msg)
This code seems to be working just need to test it as a complete system.
Code: Select all
# ESPNow repeater
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)
mac = w0.config('mac')
print (mac)
e0 = espnow.ESPNow()
print (e0)
retval = e0.init()
if (retval != None):
print ('init() did NOT work')
machine.reset()
local = b'\x08:\xf2\xacj\xe4'
remote = b'\x08:\xf2\xab\xe2\x0c'
retval = e0.add_peer(local)
if (retval != None):
print ('add peer did NOT work')
machine.reset()
while True:
print ('waiting for msg')
for mac, msg in e0:
if mac == remote:
wanted_msg = msg.decode('utf-8')
print (wanted_msg)
e0.send(local, msg, True) # corrected
else:
print ('Recv from {}: "{}"'.format(mac, msg))
Thanks again for the kickstart.
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Sun Aug 29, 2021 6:04 am
by glenn20
davef wrote: ↑Sun Aug 29, 2021 1:54 am
This code seems to be working just need to test it as a complete system.
Excellent - a few things you might want to keep in mind when in production:
- In the for loop, mac, msg will equal None, None if the recv timeout is exceeded.
- the for loop will exit if the espnow object is not initialised - eg. if you forget to call e.init() before entering the loop or if you call e.deinit() during the loop.
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Sun Aug 29, 2021 6:11 am
by davef
Production, I might end up with a 2nd system for a friend. I'll keep your suggestions in mind.
Any comment about the error:
Code: Select all
e.send(local, msg, sync=True)
TypeError: function doesn't take keyword arguments
so I just changed it to
e.send(local, msg)
Thanks
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Sun Aug 29, 2021 7:03 am
by glenn20
davef wrote: ↑Sun Aug 29, 2021 6:11 am
Production, I might end up with a 2nd system for a friend. I'll keep your suggestions in mind.
Any comment about the error:
Code: Select all
e.send(local, msg, sync=True)
TypeError: function doesn't take keyword arguments
so I just changed it to
e.send(local, msg)
Thanks
I would expect that message on an esp8266 (I don't allow keyword args on the esp8266 because I have to squeeze the size of the code so damn hard to make it fit without disabling other micropython functionality).
Correction: Actually, I just checked and the 'sync' argument is also only a positional argument on the ESP32 too. At one point in history it was also a keyword arg - and I got confused!!! Sorry about that.
If you do want to specify the "sync" argument, you can just provide it as a 3rd positional argument, eg:
or
It defaults to
True if you don't specify it. Generally it is best to keep sync=True unless you are trying to achieve high throughput and don't care about possibly missing a few messages - or possibly if you are really trying to save every electron in your battery
.
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Sun Aug 29, 2021 7:22 am
by davef
For all my "production" and testing I only use ESP32. I see I didn't understand how to call True!
I'll worry about the electrons after I gain some understanding of using the various sleep modes on the ESP32 with ESP-Now.
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Mon Aug 30, 2021 12:32 am
by fromvega
Hey friends, I wanted to try using the ESP-NOW library but I'm getting an error when trying to import it. Seems like espnow is not available in the module esp. I did a search in the source code and there's nothing related to espnow. I'm a bit confused because I can see there are docs for it at
https://micropython-glenn20.readthedocs ... spnow.html.
And then I found this
https://github.com/micropython/micropython/pull/6515, so it seems like the library is not yet merged? Do I need to switch to that branch and recompile?
I would be glad if someone could help me getting this setup.
Thank you very much,
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Mon Aug 30, 2021 6:03 am
by glenn20
fromvega wrote: ↑Mon Aug 30, 2021 12:32 am
Hey friends, I wanted to try using the ESP-NOW library but I'm getting an error when trying to import it. Seems like espnow is not available in the module esp. I did a search in the source code and there's nothing related to espnow. I'm a bit confused because I can see there are docs for it at
https://micropython-glenn20.readthedocs ... spnow.html.
And then I found this
https://github.com/micropython/micropython/pull/6515, so it seems like the library is not yet merged? Do I need to switch to that branch and recompile?
I would be glad if someone could help me getting this setup.
Thank you very much,
Yes, you can checkout that branch (at
https://github.com/glenn20/micropython/tree/espnow-g20), follow the build and compile instructions at:
https://github.com/micropython/micropyt ... /README.md.
Or, you can download a pre-compiled image from:
https://github.com/glenn20/micropython-espnow-images.
Good luck.
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Fri Sep 03, 2021 5:04 am
by glenn20
Update: New v1.17 Images available, Add on_recv callback, Updated docs.
I have added support for scheduling a callback whenever an espnow message is received. A simple example:
Code: Select all
def recv_cb(e):
print(e.irecv(0))
e.config(on_recv=recv_cb)
However, you should check
Notes on using on_recv_callbacks before using.
New espnow-enabled
micropython release v1.17 images are available at
https://github.com/glenn20/micropython-espnow-images.
These images have been:
- built from the espnow-g20-v1.17 branch which contains the latest espnow code rebased against micropython release v1.17.
I have also been updating the
docs following various clarifications arising out of this forum (thanks for unearthing the ambiguities etc.) and these are now merged into the main
espnow-g20 branch (
PR#6515).
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Fri Sep 03, 2021 6:33 am
by glenn20
Also, if you like to compile your own images, you may prefer to use the release branches (eg.
espnow-g20-v1.17 or
espnow-g20-v116) instead of
espnow-g20 (which is regularly rebased against the main branch).
Re: ESP-Now support for ESP32 (and ESP8266)
Posted: Sun Sep 19, 2021 8:12 am
by davef
Links to a ESP-Now repeater implementation used in a water tank level alarm system.
viewtopic.php?f=5&t=11145 and
https://github.com/davefes/ESP-Now-repeater