Last active
November 4, 2024 20:49
-
-
Save anecdata/f46a1d07add5fc60cfbcf42dc7be6528 to your computer and use it in GitHub Desktop.
CircuitPython example for Espressif ESP-NOW protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2023 anecdata | |
# | |
# SPDX-License-Identifier: MIT | |
import time | |
import traceback | |
import supervisor | |
import os | |
import rtc | |
import espnow | |
import espidf | |
import wifi | |
import socketpool | |
import adafruit_ntp | |
from sekrets import * | |
#### ESPNOW Receiver | |
TZ_DEFAULT = -5 | |
SNDR_CH = 0 # channel 1 (unless connected to an AP or acting as an AP) | |
def struct_time_to_iso_time(): | |
st = time.localtime() | |
tz = TZ_DEFAULT | |
return f"{st[0]:04d}-{st[1]:02d}-{st[2]:02d}T{st[3]:02d}:{st[4]:02d}:{st[5]:02d}{tz:+03}:00" | |
def connect(): | |
try: | |
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"), bssid=AP_BSSID) # use AP on channel <> 1 | |
time.sleep(1) # wait for ap_info | |
print(f"{struct_time_to_iso_time()} ipv4={wifi.radio.ipv4_address} channel={wifi.radio.ap_info.channel} rssi={wifi.radio.ap_info.rssi}") | |
except ConnectionError as ex: | |
traceback.print_exception(ex, ex, ex.__traceback__) | |
def ntp_to_rtc(): | |
wifi.radio.enabled = True | |
connect() | |
try: | |
ntp = adafruit_ntp.NTP(pool, tz_offset=TZ_DEFAULT) | |
rtc.RTC().datetime = ntp.datetime | |
print(f"{struct_time_to_iso_time()} RTC time set with NTP time") | |
except Exception as e: | |
traceback.print_exception(e, e, e.__traceback__) | |
wifi.radio.enabled = False # lose the wifi channel | |
time.sleep(3) # wait for serial | |
print(f"{'='*25}") | |
pool = socketpool.SocketPool(wifi.radio) | |
ntp_to_rtc() | |
peers = [espnow.Peer(mac=SNDR_MAC, lmk=SNDR_LMK, encrypted=True, channel=SNDR_CH),] | |
while True: | |
with espnow.ESPNow() as e: | |
e.set_pmk(RCVR_PMK) | |
peers_report = "" | |
for peer in peers: | |
e.peers.append(peer) | |
peers_report += f"mac={peer.mac} lmk={peer.lmk} ch={peer.channel} if={peer.interface} enc={peer.encrypted}\n" | |
print(f"{'-'*25}\n{struct_time_to_iso_time()} Receiving...", end=" ") | |
while True: | |
if e: | |
try: | |
packet = e.read() | |
print(f"{packet}") | |
break | |
except ValueError as ex: # Invalid buffer | |
traceback.print_exception(ex, ex, ex.__traceback__) | |
supervisor.reload() | |
break | |
print(f"send=[{e.send_success} {e.send_failure}] read=[{e.read_success} {e.read_failure}] buf={e.buffer_size} phy={e.phy_rate} peers={peers_report}", end="") |
I've tried all the phy_rate values between 0 and 32, and 16 and up do not work. So, I am unable to select the 31 or 32nd values (lora 250 and lora 500) that I want to enable long range. Where should I file a bug report about this issue?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. Is there a better place for my questions than this thread?