基于WebServer的网络串口转发
本帖最后由 vany5921 于 2020-7-9 16:28 编辑通过ESP32内建的web页面发送消息,经过webserver接收后转发至串口。所有ASCII字符均为可见字符,如需转义,请自行通过截取字符串进行处理,页面显示带有滚动换行。以下代码在原有基础上更新滚屏换行显示
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#include <M5Stack.h>
AsyncWebServer server(80);
const char* ssid = "SSID"; // Your WiFi SSID
const char* password = "PASSWORD"; // Your WiFi Password
#define TEXT_HEIGHT 16
#define TOP_FIXED_AREA 14
#define BOT_FIXED_AREA 0
#define YMAX 240
uint16_t yStart = 0;
uint16_t yArea = YMAX-TOP_FIXED_AREA-BOT_FIXED_AREA;
uint16_t yDraw = 0;
uint16_t xPos = 0;
void scrollAddress(uint16_t vsp) {
M5.Lcd.writecommand(ILI9341_VSCRSADD);
M5.Lcd.writedata(vsp>>8);
M5.Lcd.writedata(vsp);
}
void setupScrollArea(uint16_t tfa, uint16_t bfa) {
M5.Lcd.writecommand(ILI9341_VSCRDEF);
M5.Lcd.writedata(tfa >> 8);
M5.Lcd.writedata(tfa);
M5.Lcd.writedata((YMAX-tfa-bfa)>>8);
M5.Lcd.writedata(YMAX-tfa-bfa);
M5.Lcd.writedata(bfa >> 8);
M5.Lcd.writedata(bfa);
}
int scroll_line() {
int yTemp = yStart;
M5.Lcd.fillRect(0,yStart,320,TEXT_HEIGHT, TFT_BLACK);
yStart+=TEXT_HEIGHT;
if (yStart >= YMAX) yStart = 0;
scrollAddress(yStart);
returnyTemp;
}
void recvMsg(uint8_t *data, size_t len){
WebSerial.println("Received Data...");
String d = "";
for(int i=0; i < len; i++){
d += char(data);
}
WebSerial.println(d);
xPos = 0;
yDraw = scroll_line();
for(int i=0;i<d.length();i++){
char ch = d.charAt(i);
if(xPos > 311){
xPos = 0;
yDraw = scroll_line();
}
xPos += M5.Lcd.drawChar(ch, xPos, yDraw, 2);
}
}
void setup() {
M5.begin(true, false, true);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
setupScrollArea(0, 0);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// WebSerial is accessible at "<IP Address>/webserial" in browser
WebSerial.begin(&server);
WebSerial.msgCallback(recvMsg);
server.begin();
}
void loop() {
}
相关库文件地址 https://github.com/ayushsharma82/WebSerial
界面挺好看
页:
[1]