原汇总帖【得捷电子Follow me第4期】任务汇总提交 - DigiKey得捷技术专区 - 电子工程世界-论坛 (eeworld.com.cn)中介绍了使用Seeed的GROVE - OLED DISPLAY0.96" 显示屏和M5的ATOMS3U制作tcp协议传输的网络显示屏,当时考虑到 1.Seeed的GROVE - OLED DISPLAY0.96"为GROVE接口恰巧能与M5的ATOMS3U对接省去接线的麻烦;2.刚好趁着任务中学习了TCP网络传输。想着就利用起来。原帖中介绍的有限现在摘出来详细介绍下:
1.接线
因为都是GROVE接口直接对插即可非常方便。
2.ATOMS3U端代码:
需要调用U8g2lib来驱动lcd。
#include <M5AtomS3.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <U8g2lib.h>
// Set the name and password of the wifi to be connected.
// 配置所连接wifi的名称和密码
const char *ssid = "OpenWrt";
const char *password = "01010101";
const uint16_t servPort = 80;
WiFiServer server(servPort);
const char *servIp = "192.168.1.200";
WiFiClient client;
const uint16_t port = 23;
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/G1, /* data=*/G2, /* reset=*/U8X8_PIN_NONE); // All Boards without Reset of the Display
void lcdprint(String str)
{
const u16_t strL = 22;
u8g2.firstPage();
do
{
if (str.length() > strL)
{
u8g2.setFont(u8g2_font_ncenB08_tr);
const int row = str.length() / strL;
for (int i = 0; i < row+1; i++)
{
u8g2.drawStr(0, 10 * (i + 1), str.substring(i * strL, (i + 1) * strL).c_str());
}
}
else
{
u8g2.drawStr(0, 12, str.c_str());
}
} while (u8g2.nextPage());
}
void setup()
{
Serial.begin(115200);
u8g2.begin();
int sum = 0;
M5.begin(); // Init M5AtomS3. 初始化M5AtomS3
WiFi.config(IPAddress(192, 168, 1, 200), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0), IPAddress(192, 168, 1, 1));
WiFi.begin(ssid, password);
String msgStr;
msgStr = "Waiting connect to WiFi: " + String(ssid);
printf(msgStr.c_str());
lcdprint(msgStr);
while (WiFi.status() != WL_CONNECTED)
{
printf(".");
lcdprint("...");
delay(1000);
sum += 1;
if (sum == 8)
printf("Conncet failed!\n");
lcdprint("Conncet failed!");
}
char buff[100];
sprintf(buff, "WiFi connected\nIP address: \n %s\n", WiFi.localIP().toString());
lcdprint(buff);
printf(buff); // The serial port outputs the IP address
// of the M5AtomS3. 串口输出M5AtomS3的IP地址
delay(500);
sum = 0;
server.begin();
sprintf(buff, "Server started @%s:%d\n", WiFi.localIP().toString(), servPort);
lcdprint(buff);
}
void loop()
{
WiFiClient client = server.available(); // 尝试建立客户对象
char buff[100];
if (client)
{
sprintf(buff, "%s connected", client.localIP().toString());
lcdprint(buff);
String readBuff; // 读取信息暂存
while (client.connected())
{
if (client.available()) // 如果有可读数据
{
String buff = client.readStringUntil('\r');
lcdprint(buff);
}
}
}
client.stop(); // 结束当前连接:
sprintf(buff, "remote lcd @ %s:%d has no client connected!", servIp,servPort);
lcdprint(buff);
delay(1000);
}
3.测试效果:
1)wifi连接好后即可打开tcp服务端进入待配对状态:
2)在w5500-evb-pico端建立一个tcp客户端用于向tcp显示服务端发送数据:
以任务二UDP传输为例,当w5500-evb-pico接受到udp数据就会转而发送至tcp显示服务端显示,代码如下:
from w5500 import w5500
import time
import board
import busio as io
import digitalio
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
print("Task-2 TCP UDP")
led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT
#初始化网卡
eth=w5500()
socket.set_interface(eth)
#创建远程LCD TCP socket
lcd_client = socket.socket()
lcd_server_ip='192.168.1.200'
lcd_server_port = 80
lcd_client.connect((lcd_server_ip,lcd_server_port))
print("remote lcd connected !")
def rmLcdPrint(s:str):
lcd_client.send(s+"\n")
#创建UDP socket
server_udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # Allocate socket for the server
server_port_udp = 5000
server_udp.bind((eth.pretty_ip(eth.ip_address),server_port_udp)) # Bind to IP and Port
print("Udp listening")
def taskUdp():
data, addr = server_udp.recvfrom(50)
if data:
data=data.decode('utf-8')
echoStr=f"get udp data:{data} @ {addr[0]}:{addr[1]}\n"
print(echoStr)
rmLcdPrint(echoStr)
return echoStr
else:
return None
while True:
taskUdp()
3)效果:
tcp显示屏服务端客户端建立连接后:
向w5500-evb-pico发送udp数据。
w5500-evb-pico接受数据发送atomsU显示:
本帖最后由 eew_UPQWC7 于 2024-3-23 21:53 编辑