Python Reference
ESP NOW
ESP NOW is a wireless protocol specifically for ESP devices to create a mesh network.
import machine
import time
import stc
import espnow
# Button
button = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_DOWN)
# Setup ESPNow
e = espnow.ESPNow()
e.active(True)
peer = b'\xff\xff\xff\xff\xff\xff' # Broadcast address
e.add_peer(peer) # Add as peers any devices you wish to transmit to
# Let's go
points = Points()
while True:
sender, msg = e.irecv(200)
if msg:
print(f"Message received from {sender}: {msg}")
if button.value():
print("Broadcasting hello")
e.send(peer, 'Hello'.encode('utf-8'))
time.sleep(0.2)
Note: To convert a MAC address string like "AB:12:34:CD:56:78" into the format required by the espnow.ESPNow.add_peer() method, which requires a bytes object, you can follow these steps:
mac = "AB:12:34:CD:56:78"
peer = bytes(int(part, 16) for part in mac.split(':'))
print(peer) # Output: b'\xab\x12\x34\xcd\x56\x78'