ESP32-S3/CircuitPython act as socket client, connect to HC-25 WiFi Module.
It's my CircuitPython 8.1.0 Beta 0 exercise run on Espressif ESP32-S3-DevKitC-1 , to act as WiFi station/socket client connect to socket server on HC-25 WiFi Module.
cpyESP32S3_client.py
cpyESP32S3_client_st7789.py
cpyESP32S3_client.py
import os
import sys
import time
import wifi
import socketpool
import ipaddress
ssid = "myHC-25"
password = "password"
serverAddr = "192.168.4.1"
serverPort = 8080
print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
'run on ' + os.uname()[4]
print(info)
print("=================================================")
print("Connect to network")
# In my trial, most time wifi.radio.connect() raise:
# ConnectionError: No network with that ssid
# So I implement re-try here.
connErrIcon = True
while True:
time.sleep(0.5)
try:
wifi.radio.connect("myHC-25","password")
break
except ConnectionError:
if connErrIcon:
print(end="*\r")
else:
print(end="-\r")
connErrIcon = not connErrIcon
print("Network connected")
myIP = wifi.radio.ipv4_address
print("My IP:", myIP)
pool = socketpool.SocketPool(wifi.radio)
"""
ai = pool.getaddrinfo(serverAddr, serverPort)
print(ai)
print("Server Addr:Port = ", ipaddress.ip_address(ai[0][4][0]),
":", ai[0][4][1])
"""
print("Connect to socket")
s = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
s.settimeout(None)
s.connect((serverAddr, serverPort))
print("socket connected")
rxBuf = bytearray(512)
s.send(b"\nHello from ESP32-C3 at " + str(myIP))
try:
while True:
rxSize = s.recv_into(rxBuf)
print(rxBuf[:rxSize])
except:
print("Somethong wrong")
s.close()
print("socket closed")
#I'm not sure is it need/how to disconnect from wifi
wifi.radio.enabled=False
cpyESP32S3_client_st7789.py
"""
Client side run on ESP32-S3, connect to myHC-25 socket server
display on 1.54" IPS 240x240 (RGB)
"""
import os
import sys
import time
import wifi
import socketpool
import ipaddress
import board
import busio
import terminalio
import displayio
from adafruit_display_text import label
import adafruit_st7789
# Release any resources currently in use for the displays
displayio.release_displays()
ssid = "myHC-25"
password = "password"
serverAddr = "192.168.4.1"
serverPort = 8080
#===init display ======================
disp_blk = board.IO4
disp_cs = board.IO5
disp_dc = board.IO6
disp_res = board.IO7
disp_mosi = board.IO15
disp_clk = board.IO16
disp_spi = busio.SPI(clock=disp_clk,
MOSI=disp_mosi)
display_bus = displayio.FourWire(
disp_spi,
command=disp_dc,
chip_select=disp_cs,
reset=disp_res
)
#--- Setup display ---
# for 1.54" IPS 240x240 (RGB)
display = adafruit_st7789.ST7789(display_bus,
width=240,
height=240,
rowstart=80,
rotation=180,
backlight_pin=disp_blk)
#=======================================
info = os.uname()[4] + "\n" + \
sys.implementation[0] + " " + os.uname()[3] + "\n" + \
adafruit_st7789.__name__ + " " + adafruit_st7789.__version__ + "\n" + \
str(display.width) + "x" + str(display.height)
print("=======================================")
print(info)
print("=======================================")
time.sleep(2)
print("Connect to network")
# In my trial, most time wifi.radio.connect() raise:
# ConnectionError: No network with that ssid
# So I implement re-try here.
connErrIcon = True
while True:
time.sleep(0.5)
try:
wifi.radio.connect("myHC-25","password")
break
except ConnectionError:
if connErrIcon:
print(end="*\r")
else:
print(end="-\r")
connErrIcon = not connErrIcon
print("Network connected")
myIP = wifi.radio.ipv4_address
print("My IP:", myIP)
pool = socketpool.SocketPool(wifi.radio)
"""
ai = pool.getaddrinfo(serverAddr, serverPort)
print(ai)
print("Server Addr:Port = ", ipaddress.ip_address(ai[0][4][0]),
":", ai[0][4][1])
"""
print("Connect to socket")
s = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
s.settimeout(None)
s.connect((serverAddr, serverPort))
print("socket connected")
# Make the display context
bgGroup = displayio.Group()
display.show(bgGroup)
bg_bitmap = displayio.Bitmap(display.width, display.height, 1) # with one color
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette, x=0, y=0)
bgGroup.append(bg_sprite)
colorSet = ((0xFF0000, "RED"),
(0x00FF00, "GREEN"),
(0x0000FF, "BLUE"),
(0xFFFFFF, "WHITE"),
(0x000000, "BLACK"))
color_bitmap = displayio.Bitmap(display.width-2, display.height-2, 1) # with one color
color_palette = displayio.Palette(1)
color_palette[0] = 0x000080 # BLUE
color_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=1, y=1)
bgGroup.append(color_sprite)
# Draw Title label
title_group = displayio.Group(scale=2, x=5, y=15)
title = "ESP32-S3 Client"
title_area = label.Label(terminalio.FONT, text=title, color=0xFFFFFF)
title_group.append(title_area) # Subgroup for text scaling
bgGroup.append(title_group)
# Draw Msg label
msg_group = displayio.Group(scale=2, x=5, y=50)
msg_area = label.Label(terminalio.FONT, text="", color=0xFFFFFF)
msg_group.append(msg_area) # Subgroup for text scaling
bgGroup.append(msg_group)
rxBuf = bytearray(512)
s.send(b"\nHello from ESP32-C3 at " + str(myIP))
try:
while True:
rxSize = s.recv_into(rxBuf)
msg_in = rxBuf[:rxSize]
str_msg = str(msg_in, 'utf-8')
print(str_msg)
msg_area.text=str_msg
except:
print("Somethong wrong")
s.close()
print("socket closed")
#I'm not sure is it need/how to disconnect from wifi
wifi.radio.enabled=False
To setup library and connection for ST7789, refer to post ESP32-S3/CircuitPython 8 display on ST7789 SPI TFT.
Comments
Post a Comment