#include <ESP8266WebServer.h>
#include <WebSocketsServer.h> // Include the WebSocket library
const char* ssid = "ESP_Serial_Server"; // Replace with your desired AP name
const char* password = "your_password"; // Replace with your desired AP password
IPAddress local_IP(192, 168, 4, 1); // Replace with your desired permanent IP
IPAddress gateway(192, 168, 4, 1); // Replace with your gateway IP (if needed)
IPAddress subnet(255, 255, 255, 0); // Replace with your subnet mask (if needed)
ESP8266WebServer webServer(80);
String serialData = ""; // String to store data from serial monitor
String webPage = "";
WebSocketsServer webSocket = webServer; // Integrate WebSocket with web server
void setup() {
Serial.begin(115200);
delay(10);
// Configure ESP8266 as access point with permanent IP
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(ssid, password);
Serial.println("Connected to AP");
Serial.print("IP Address: ");
Serial.println(local_ip);
// Build the webpage with a placeholder for serial data
webPage = "<!DOCTYPE html><html><head><title>Serial Data</title></head><body>";
webPage += "<h1>Serial Data</h1>";
webPage += "<p id='data'>Waiting for data...</p>";
webPage += "<script>";
webPage += "var ws = new WebSocket('ws://' + window.location.hostname + ':' + window.location.port + '/ws');";
webPage += "ws.onmessage = function (event) { document.getElementById('data').innerHTML = event.data; };";
webPage += "</script>";
webPage += "</body></html>";
// Configure web server and WebSocket
webServer.on("/", HTTP_GET, []() {
webServer.send(200, "text/html", webPage);
});
webSocket.on("/ws", [](WiFiServerClient *client) {
String msg = "";
while (client->available()) {
char c = client->read();
msg += c;
}
// Handle incoming messages from client (if needed)
});
webServer.begin();
}
void loop() {
webServer.handleClient();
webSocket.loop(); // Process WebSocket events
if (Serial.available()) {
serialData = Serial.readStringUntil('\n');
webSocket.broadcastTXT(serialData); // Broadcast data to all connected clients
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4