0% found this document useful (0 votes)
10 views3 pages

Real-time Sensor Data with ESP32

This document is an Arduino sketch for an ESP32 microcontroller that sets up a WiFi access point and a web server to display real-time sensor values. It includes a web page with buttons to control a GPIO pin and uses WebSockets to communicate sensor data and commands. The code handles incoming data from a UART interface, processes it, and updates the web page accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Real-time Sensor Data with ESP32

This document is an Arduino sketch for an ESP32 microcontroller that sets up a WiFi access point and a web server to display real-time sensor values. It includes a web page with buttons to control a GPIO pin and uses WebSockets to communicate sensor data and commands. The code handles incoming data from a UART interface, processes it, and updates the web page accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <WiFi.

h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>

#define RX_PIN 16
#define TX_PIN 17
#define BAUD_RATE 115200 // Altere conforme necessário

const char* ssid = "ESP32-AP";


const char* password = "12345678";

HardwareSerial UART(2);

uint8_t rx_buffer[7];
uint16_t sensor1 =0;
uint8_t sensor2 = 0;
uint8_t status = 0;

AsyncWebServer server(80);
AsyncWebSocket ws("/ws");

const char* htmlPage = R"rawliteral(


<!DOCTYPE html>
<html>
<head>
<title>Sensor em tempo real</title>
<style>
body { font-family: sans-serif; text-align: center; margin-top: 50px; }
ul { list-style: none; padding: 0; }
li { margin: 10px 0; font-size: 20px; }
button { margin: 10px; padding: 10px 20px; font-size: 16px; }
</style>
</head>
<body>
<h2>Valores do sensor:</h2>
<ul>
<li>Valor 1: <span id="valor1">--</span></li>
<li>Valor 2: <span id="valor2">--</span></li>
<li>Valor 3: <span id="valor3">--</span></li>
</ul>

<button onclick="ligar()">LIGA</button>
<button onclick="desligar()">DESLIGA</button>

<script>
const gateway = `[Link]
const ws = new WebSocket(gateway);

[Link] = (event) => {


const valores = [Link](","); // Ex: "123,456,789"
[Link]('valor1').innerText = valores[0] || "--";
[Link]('valor2').innerText = valores[1] || "--";
[Link]('valor3').innerText = valores[2] || "--";
};

function ligar() {
[Link]("LIGA");
}
function desligar() {
[Link]("DESLIGA");
}
</script>
</body>
</html>
)rawliteral";

void update_variables(){
while ([Link]()){
if ([Link]() == 0xAA) { // Encontrou início

rx_buffer[0] = 0xAA;
[Link](&rx_buffer[1], 6); // lê os outros 6 bytes

if (rx_buffer[6] == 0x55){

uint8_t checksum = (rx_buffer[2] << 8 | rx_buffer[1]) + rx_buffer[3] +


rx_buffer[4];

if (checksum == rx_buffer[5]) {
// Dados válidos
uint16_t sensor1 = rx_buffer[2] << 8 | rx_buffer[1];
uint8_t sensor2 = rx_buffer[3];
uint8_t status = rx_buffer[4];

[Link]("Sensor 1: ");
[Link](sensor1);
[Link]("Sensor 2: ");
[Link](sensor2);
[Link]("Status: ");
[Link](status);

String mensagem = String(sensor1) + "," + String(sensor2) + "," +


String(status);
[Link](mensagem);

} else {
[Link]("Checksum inválido!");
}
} else {
[Link]("Endbit inválido!");
}
}
}
}

void onWebSocketEvent(AsyncWebSocket * server, AsyncWebSocketClient * client,


AwsEventType type, void * arg, uint8_t *data, size_t len) {
if (type == WS_EVT_DATA) {
AwsFrameInfo * info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode ==
WS_TEXT) {
String msg = "";
for (size_t i = 0; i < len; i++) {
msg += (char)data[i];
}

[Link]("Mensagem recebida: ");


[Link](msg);

if (msg == "LIGA") {
[Link]("GPIO 2: LIGADO");
digitalWrite(2, HIGH); // ✅ Liga GPIO 2
} else if (msg == "DESLIGA") {
[Link]("GPIO 2: DESLIGADO");
digitalWrite(2, LOW); // ✅ Desliga GPIO 2
}
}
}
}

void setup() {
[Link](115200);
[Link](BAUD_RATE, SERIAL_8N1, RX_PIN, TX_PIN);
[Link](5);
[Link](ssid, password);
[Link]("Access Point iniciado");
[Link]([Link]());

pinMode(2, OUTPUT); // ✅ Configura o GPIO 2 como saída


digitalWrite(2, LOW); // Inicialmente desligado

[Link](onWebSocketEvent);
[Link](&ws);

[Link]("/", HTTP_GET, [](AsyncWebServerRequest *request){


request->send_P(200, "text/html", htmlPage);
});

[Link]();
}

void loop() {

update_variables();

You might also like