ESP-NOW on CircuitPython 9.0.3, tested on ESP32-S3/ESP32-C3.

ESP-NOW is a kind of connectionless Wi-Fi communication protocol that is defined by Espressif. In ESP-NOW, application data is encapsulated in a vendor-specific action frame and then transmitted from one Wi-Fi device to another without connection.

CircuitPython espnow module provides an interface to the ESP-NOW protocol.

It's exercises of CircuitPython espnow to communicate between sender (Waveshare ESP32-S3-Zero/Seeed Studio XIAO ESP32C3) and receiver (Espressif ESP32-S3-DevKitC-1).

In the receiver side, the receiver message are display on ILI9341 SPI LCD. For the setting of using ILI9341 SPI LCD, read 3.2" 320x240 IPS LCD (ILI9341 SPI) with Cap. Touch (FT6336) and Micro SD Slot on ESP32-S3/CircuitPython 9.0.3.


Exercise Code:

cpyS3_ili9341_espnow_receiver.py
, run on Espressif ESP32-S3-DevKitC-1 and display on ILI9341 SPI LCD.

"""
CircuitPython exercise of espnow

Act as ESP-NOW receiver,
display received msg/mac-address on LCD.

Display Module:
3.2 inch IPS SPI Module ILI9341 with cap. touch FT6336U

Test on:
dev. board:     Espressif ESP32-S3-DevKitC-1
CircuitPython:  9.0.3

Libraries needed:
- adafruit_ili9341.mpy
- adafruit_display_text folder

ref: CircuitPython espnow
https://docs.circuitpython.org/en/9.0.x/shared-bindings/espnow/index.html

"""
import os, sys
import board
import busio
import terminalio
import displayio
import fourwire
from adafruit_display_text import label
import adafruit_ili9341

import time

import wifi
import espnow

ILI9341_INVOFF = 0x20   #Display Inversion OFF
ILI9341_INVON  = 0x21   #Display Inversion ON

# Release any resources currently in use for the displays
displayio.release_displays()

mac = ""
for m in wifi.radio.mac_address:
    mac = mac + "\\" + str(hex(m)[1:])

#===init display ======================
#Connection to ILI9341/SD
DISP_CS  = board.IO17
DISP_RES = board.IO16
DISP_DC  = board.IO15
SPI_MOSI = board.IO7
SPI_SCL  = board.IO6
DISP_BL  = board.IO5
SPI_MISO = board.IO4
SD_CS    = board.IO43

spi = busio.SPI(clock=SPI_SCL,
                MOSI=SPI_MOSI,
                MISO=SPI_MISO)

#On CircuitPython 9  FourWire moved from displayio to fourwire
display_bus = fourwire.FourWire(spi, command=DISP_DC, chip_select=DISP_CS, reset=DISP_RES)

#--- Setup display ---
# for 3.2" 320x240 SPI ILI9341 TFT
disp_width = 320
disp_height = 240
display = adafruit_ili9341.ILI9341(display_bus,
                                   width=disp_width,
                                   height=disp_height,
                                   backlight_pin=DISP_BL,
                                   )

display_bus.send(int(ILI9341_INVON), "") #invert color

sysinfo = sys.implementation[0] + " " + os.uname()[3] + "\nrun on " + os.uname()[4]
drv_info = adafruit_ili9341.__name__ + " " + adafruit_ili9341.__version__

print("=============================================")
print("coXXect.blogspot.com")
print("---------------------------------------------")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4],
      "\nMAC address:", mac)
print("=============================================")
print(drv_info)
time.sleep(1)
# Make the display context
bgGroup = displayio.Group()

display.root_group = bgGroup

COL_RED   = 0xFF0000
COL_GREEN = 0x00FF00
COL_BLUE  = 0x0000FF
COL_WHITE = 0xFFFFFF
COL_BLACK = 0x000000

bg_bitmap = displayio.Bitmap(display.width, display.height, 1)  # with one color
bg_palette = displayio.Palette(1)
bg_palette[0] = COL_WHITE
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)

border = 1
color_bitmap = displayio.Bitmap(display.width-border*2, display.height-border*2, 1)  # with one color
color_palette = displayio.Palette(1)
color_palette[0] = COL_BLACK
color_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=border, y=border)
bgGroup.append(color_sprite)

# Draw label
text_group = displayio.Group(scale=2, x=10, y=12)
text = "coXXect.blogspot.com"
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
text_group.append(text_area)  # Subgroup for text scaling
bgGroup.append(text_group)

info_group = displayio.Group(scale=1, x=10, y=200)
info_label = label.Label(terminalio.FONT,
                         text=sysinfo + "\n" + drv_info,
                         color=0xFFFFFF)
info_group.append(info_label)  # Subgroup for text scaling
bgGroup.append(info_group)

msg_group = displayio.Group(scale=2, x=10, y=40)
msg_label = label.Label(terminalio.FONT,
                        text="msg",
                        color=0xFFFF00)
msg_group.append(msg_label)
bgGroup.append(msg_group)

from_group = displayio.Group(scale=2, x=10, y=180)
from_label = label.Label(terminalio.FONT,
                        text="sender mac",
                        color=0xFFFF00)
from_group.append(from_label)
bgGroup.append(from_group)

e = espnow.ESPNow()
packets = []

while True:
    while True:
        if e:
            packet = e.read()
            packets.append(packet)
            if packet.msg == b'end':
                break
            elif packet.msg == b'#RED':
                color_palette[0] = COL_RED
            elif packet.msg == b'#GREEN':
                color_palette[0] = COL_GREEN
            elif packet.msg == b'#BLUE':
                color_palette[0] = COL_BLUE
            elif packet.msg == b'#WHITE':
                color_palette[0] = COL_WHITE
            elif packet.msg == b'#BLACK':
                color_palette[0] = COL_BLACK
            else:
                sender_mac = ""
                for m in packet.mac:
                    sender_mac = sender_mac + f"0x{m:02x}"[2:].upper() + "."
                sender_mac = sender_mac[:-1]  # remove last '.'
                print("from sender:", sender_mac)
                print(packet.msg)
                
                msg_label.text=packet.msg.decode()
                from_label.text=sender_mac

print("~ bye ~")


esp_espnow_sender.py, run on Waveshare ESP32-S3-Zero/Seeed Studio XIAO ESP32C3.
notice: have to modify <receiver MAC> to match your receiver.
"""
CircuitPython exercise of espnow

Act as ESP-Now sender,
get user input in REPL, send to receiver.

Have to register receiver mac-address by call espnow.Peer() function.

ref: CircuitPython espnow
https://docs.circuitpython.org/en/9.0.x/shared-bindings/espnow/index.html
"""
import wifi
import espnow

my_mac = "  "
for m in wifi.radio.mac_address:
    my_mac = my_mac + f"0x{m:02x}"[2:].upper() + "."
my_mac = my_mac[:-1]  # remove last '.'
print("my mac address:", my_mac)

e = espnow.ESPNow()
peer = espnow.Peer(mac=b'<receiver MAC>') # MAC of receiver
e.peers.append(peer)

e.send("- CircuitPython espnow -\n" + my_mac)

e.send(b'end')

while True:
    user_in = input("Enter something? ")
    if user_in is "":
        break
    e.send(user_in)
    e.send(b'end')

More exercise:
CircuitPython ESP-NOW: Remote control on-board RGB LED via wireless, between ESP32-S3.

Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator