ESP-Now support for ESP32 (and ESP8266)

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
mhepp63
Posts: 8
Joined: Wed Apr 28, 2021 2:17 pm

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

Post by mhepp63 » Sat Aug 07, 2021 10:05 am

davef wrote:
Fri Aug 06, 2021 10:22 pm
Look at the top of this page maybe it applies to your situation.
Thanks, but I do not have a problem with receiver - this part works fine and it is able to receive bigger load then with one sender, but problem is on sending side - only first message after (re)boot is sent. Sending of each other is failing.

And

Code: Select all

machine.sleep()
: https://docs.micropython.org/en/latest/ ... hine.sleep, but machine.lightsleep acts the same (sleep is probably alias to lightsleep).

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 Aug 07, 2021 7:43 pm

OK on the docs re machine.
machine.sleep()

Note

This function is deprecated, use lightsleep() instead with no arguments.

machine.lightsleep([time_ms])
machine.deepsleep([time_ms])
Not sure if deprecated means that you can't even call machine.sleep() or you get an error if you do or it just reverts to lightsleep().

What is the objective of that bit of code? It looks like you want to go to sleep for fixed periods of time taking into account how long it takes to send a message.

I'll go and look at your gateway code. I am a beginner when it comes to sleep modes.

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 Aug 09, 2021 2:23 am

mhepp63 wrote:
Fri Aug 06, 2021 10:07 pm
And I found possible issue with lightsleep.
Problem is the same even with w0.active(True) etc... after machine.sleep().
See https://docs.espressif.com/projects/esp ... modes.html.

It is advised to shutdown the wifi peripheral before entering lightsleep (machine.sleep() has been deprecated in favour of machine.lightsleep()) and then you will need to fully re-initialise the wifi peripheral after you emerge from lightsleep().

I just ran a small test... it works (at least for the small test I ran) if you:

Code: Select all

	...
	w0.active(0)
	machine.lightsleep(1000)
	w0.active(1)
	w0.config(channel=chan)
	...

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 Aug 09, 2021 2:28 am

davef wrote:
Sat Aug 07, 2021 7:43 pm
This function is deprecated, use lightsleep() instead with no arguments.

machine.lightsleep([time_ms])
machine.deepsleep([time_ms])
Not sure if deprecated means that you can't even call machine.sleep() or you get an error if you do or it just reverts to lightsleep().
Machine.sleep() is still working (at least on the esp32), but it's clearly advisable to switch to machine.lightsleep().

dhust
Posts: 40
Joined: Sun Jul 11, 2021 10:59 pm

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

Post by dhust » Tue Aug 17, 2021 2:04 pm

My two devices are having issues communicating. It's like the receiver and sender devices aren't in sync. The receiver should be getting a message every 6 or so seconds. Sometimes I'll wait minutes before it starts to receive something. Then the receiver will get a few messages in a row. Then it won't again. Does it matter if one device is an ESP32 and the other is an ESP8266?

Sender:

Code: Select all

import network
from esp import espnow
import machine
import time

print("ESP SETUP")
w0 = network.WLAN(network.STA_IF)
w0.active(True)
e = espnow.ESPNow()
e.init()
peer = b'\x24\xA1\x60\x2E\x0F\x99'
e.add_peer(peer)

print("PINS SETUP")
moisture_sensor = machine.ADC(machine.Pin(35))
moisture_power = machine.Pin(19, machine.Pin.OUT)
moisture_power.on()
time.sleep(2)

moistness_threshold = 2100
print(moisture_sensor.read())
if moisture_sensor.read() < moistness_threshold:
    print("---WET")
    e.send(b'wet')
    
if moisture_sensor.read() >= moistness_threshold:
    print("---DRY")
    e.send(b'dry')

moisture_power.off()

print("DEEP SLEEPY")
machine.deepsleep(6000)
Receiver:

Code: Select all

import network
from esp import espnow
from machine import Pin
import time

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

e = espnow.ESPNow()
e.init()
peer = b'\xA8\x03\x2A\xEA\x6C\xE4'
e.add_peer(peer)

pump = Pin(13, Pin.OUT)
pump.off()
print("Begin while loop")
while True:
    print("In loop")
    host, msg = e.irecv()
    print(msg)
    if msg: # msg == None if timeout in irecv()
        if msg == b'wet':
            pump.off()
        elif msg == b'dry':
            print("Turning pump on")
            pump.on()
            time.sleep(2)
            pump.off()
        else:
            print("Message received but not wet or dry")
    else:
        print("No Message")

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 Aug 17, 2021 8:27 pm

Which device is the transmitter running on? Try running both on ESP8266s first.
What chip revision are your ESP32s?
Maybe try utime.sleep(6) first, which would require a while loop.

dhust
Posts: 40
Joined: Sun Jul 11, 2021 10:59 pm

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

Post by dhust » Tue Aug 17, 2021 10:11 pm

The sender is an ESP32U-01. I won't be able to use an ESP8266 for both but I'll try an ESP32 for both. I looked up utime.sleep() and it doesn't seem any different from time.sleep() other than it's for MicroPython. Maybe I'm missing something there. Also, why does it need a while loop? 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 Aug 17, 2021 10:54 pm

When you use deepsleep() the program on ESP32 is re-run. If you put a utime.sleep at the end of your main program when the time is up the program does not re-run, it just finishes.

lightsleep() is suppose to retain program state and just turn off the WiFi. My guess that would have to be in a while loop. I see glenn20 says that the WiFi needs initialising as well.

It might be helpful to know what revision chips are used in the ESP32U-01, but for that you need to run a command in Linux using the esptool app. Maybe there is a version for Windows or maybe your IDE will tell you.

Yes, utime is Micropython's version of time ... which may not have all the features of Python's time.

dhust
Posts: 40
Joined: Sun Jul 11, 2021 10:59 pm

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

Post by dhust » Wed Aug 18, 2021 1:50 am

davef wrote:
Tue Aug 17, 2021 10:54 pm
It might be helpful to know what revision chips are used in the ESP32U-01, but for that you need to run a command in Linux using the esptool app. Maybe there is a version for Windows or maybe your IDE will tell you.
It's a ESP32-D0WD-V3 (revision 3).

dhust
Posts: 40
Joined: Sun Jul 11, 2021 10:59 pm

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

Post by dhust » Wed Aug 18, 2021 3:19 am

I tried using two ESP32 devices, that are the same, but I have the same issue. It looks like irecv() is timing out at the default 5 minutes, since it's printing msg as 'None'. Every once-in-a-while it will receive a message from the other device. Not very often though. So it can't be that I have an incorrect MAC address. I don't know why it's being so weird. Is it a timing issue or is the code incorrect for one, or both, of the devices? Is there a way that I can test anything in my code to see if there is an issue of some kind?

Post Reply