ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
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 » Fri Jan 08, 2021 6:56 pm

Following on from your example if I then try to bring up the network interface:

Code: Select all

>>> sta_if = network.WLAN(network.STA_IF)
>>> sta_if.active(True)
True
>>> sta_if.ifconfig(('192.168.10.99', '255.255.255.0', '192.168.10.1', '8.8.8.8'))
>>> sta_if.isconnected()
False
>>> 
I will now check the official image.

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 » Fri Jan 08, 2021 8:16 pm

I do not see a call to sta_if.connect(). Without that, the module will not connect,

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 » Fri Jan 08, 2021 8:29 pm

Ah, let me focus a bit harder!

Sorry, my previous post was incorrect ... it did not reflect that I was actually doing a re-connect in the code I was testing. I have gone through the process as Peter suggested, line by line, and the re-connection works.

Also, my program now works at home-base, maybe because it is a different wireless router. I will re-check at the remote location.

Thank you for the support guys.

Dave

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 » Sat Jan 16, 2021 7:28 pm

As I get the odd init() and add_peer() errors I thought I would
put in some error-trapping, but it looks like the error test is not
even being run.

For example:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "local.py", line 21, in <module>
AttributeError: 'function' object has no attribute 'init'

Code: Select all

retval = e.init() # line 21
if (retval != None):
    print('init() did NOT work')
    machine.reset()

Any suggestions?

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 Jan 19, 2021 10:31 pm

It appears that 'e' is not an ESPNow object. The error message suggests it is a function object. This suggests there may be a bug in how you initialise 'e'.

Try just print(e) as a diagnostic.

For example, this would happen if you do:

Code: Select all

e = espnow.ESPNow
instead of

Code: Select all

e = espnow.ESPNow()
(Just returned from 10 days out of phone range - at the beach ;).

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 » Tue Jan 19, 2021 11:10 pm

Always:

Code: Select all

e = espnow.ESPNow()
I will try the print(e) statement and see if I can get it to fail. I always focussed on the line that the error occurred, looks like I need to step-backward to get a clearer picture.

Hope the shock of going back to work or whatever is not too big :)

Thanks.

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 » Tue Jan 19, 2021 11:39 pm

So, did a print(e) , after:

Code: Select all

e = espnow.ESPNow()
and when it failed I saw:

Code: Select all

>>> import local
b'$\n\xc4Yd\x88'
2021-1-
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "local.py", line 19, in <module>
AttributeError: 'str' object has no attribute 'init'
Looks like it is returning the date ??

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 Jan 25, 2021 1:25 am

davef wrote:
Tue Jan 19, 2021 11:39 pm
So, did a print(e) , after:

Code: Select all

e = espnow.ESPNow()
and when it failed I saw:

Code: Select all

>>> import local
b'$\n\xc4Yd\x88'
2021-1-
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "local.py", line 19, in <module>
AttributeError: 'str' object has no attribute 'init'
Looks like it is returning the date ??
Without seeing your code, it is difficult to help. For me:

Code: Select all

from esp import espnow
e = espnow.ESPNow()
print(e)
prints (on the ESP32):

Code: Select all

ESPNow(rxbuf=516, timeout=300000)
and (on the esp8266):

Code: Select all

<ESPNow>
The point is that 'e' is an ESPNow object which has 'init' as a bound method. It appears that in your case 'e' does not reference an ESPNow object.

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 » Mon Jan 25, 2021 3:14 am

Thanks.

I get the same when it runs properly.

It is the only place that <e> is used:

Code: Select all

import network
from esp import espnow
import utime
import machine
from machine import Pin
# import ubinascii

pin2 = Pin(2, Pin.OUT) #  Discharge pump clamp pin
pin2.off() #  enable discharge pump on re-boot

#  A WLAN interface must be active to send()/recv()
w0 = network.WLAN(network.STA_IF)
w0.active(True)
#mac = w0.config('mac')
#print(mac)
#mac = ubinascii.hexlify(w0.config('mac'),':').decode()
#print(mac)

e = espnow.ESPNow()

retval = e.init()
if (retval != None):
    print('init() did NOT work')
    machine.reset()

#peer = b'\x18\xfe4\xde\xea\xf3' #  MAC address of ESP-01S local
peer = b'$\n\xc4Yd\x88' # MAC address of ESP32 local

retval= e.add_peer(peer)
if (retval != None):
    print('add_peer did NOT work')
    machine.reset()

print('waiting for command')

while True:
    msg = e.irecv()

    utime.sleep(1)
    
#    if (msg != None):
    if msg:
        command = msg[1].decode('utf-8')
        print(command)

        if (command == 'pump off'):
            print('You have turned the pump off')
            pin2.on() #  this disables the discharge pump
            print('send ACK')

            retval = e.send(peer, "pump is now off", True)
            if (retval !=  True):
                print('send did NOT work')
                machine.reset()

            utime.sleep(5)

            retval = e.deinit()
            if (retval != None):
                print('deinit() did NOT work')
                machine.reset() #  just to be tidy

            w0.active(False) # take-down WiFi

            machine.reset()

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

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

Post by glenn20 » Thu Jan 28, 2021 12:52 pm

I have posted new precompiled images for the esp32 and esp8266 at https://github.com/glenn20/micropython-espnow-images.

Images are available for:
- the latest espnow-g20 branch which has been re-based against the latest micropython master branch, and
- the latest espnow-g20-v113 branch which has been rebased against the v1.13 micropython release.

In principle, the images compiled against v1.13 (with espnow patches) may be more stable overall.

These are provided as a convenience for users until such time as the ESPNow PR is merged into an official micropython release.

Post Reply