/*
WebSerialLite Demo
------
This example code works for both ESP8266 & ESP32 Microcontrollers
WebSerial is accessible at your ESP's <IPAddress>/webserial URL.
Author: HomeboyC
*/
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "style_css.h"
//#include "WebSerialWebPage.h"
#include "WebSerialLite.h"
AsyncWebServer server(80);
uint8_t webSerialClients = 0;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/* Message callback of WebSerial */
void recvMsg(uint8_t *data, size_t len){
WebSerial.println("Received Data...");
String d = "";
for(int i=0; i < len; i++){
d += char(data[i]);
}
WebSerial.println(d);
}
void wslConn(AsyncWebSocketClient *){
webSerialClients++;
Serial.printf("+ %i\n",webSerialClients);
}
void wslDisconn(AsyncWebSocketClient *){
webSerialClients--;
Serial.printf("- %i\n",webSerialClients);
}
void setup() {
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", style_css);
response->addHeader("Cache-Control","max-age=600");
request->send(response);
});
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
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);
/* Attach Message Callback */
WebSerial.onConnect(wslConn);
WebSerial.onDisconnect(wslDisconn);
WebSerial.onMessage(recvMsg);
server.begin();
}
void loop() {
delay(2000);
// we suggest you to use `print + "\n"` instead of `println`
// because the println will send "\n" separately, which means
// it will cost a sending buffer just for storage "\n". (there
// only 8 buffer space in ESPAsyncWebServer by default)
WebSerial.print(F("IP address: "));
// if not use toString the ip will be sent in 7 part,
// which means it will cost 7 sending buffer.
WebSerial.println(WiFi.localIP().toString());
WebSerial.printf("Millis=%lu\n", millis());
WebSerial.printf("Free heap=[%u]\n", ESP.getFreeHeap());
Serial.printf("Free heap=[%u]\n", ESP.getFreeHeap());
}