0% found this document useful (0 votes)
3 views41 pages

Programas 2

The document contains code for various Arduino projects using the ESP32-S3, including a web server that reads temperature and humidity from a DHT11 sensor, controls an RGB LED, and displays graphics on an LED matrix. It includes setup and loop functions for handling WiFi connections, sensor readings, and web server requests. Additionally, there are multiple examples of LED matrix animations and RGB color controls through a web interface.

Uploaded by

rodogamer3020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views41 pages

Programas 2

The document contains code for various Arduino projects using the ESP32-S3, including a web server that reads temperature and humidity from a DHT11 sensor, controls an RGB LED, and displays graphics on an LED matrix. It includes setup and loop functions for handling WiFi connections, sensor readings, and web server requests. Additionally, there are multiple examples of LED matrix animations and RGB color controls through a web interface.

Uploaded by

rodogamer3020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ESP32S3 DHT WEBSERVER

#include <WiFi.h>
#include <WebServer.h>
#include "DHT.h"

// ==========================
// CONFIGURACIÓN WIFI
// ==========================
const char* ssid = "FBARRA";
const char* password = "GFAgfa123";

// ==========================
// CONFIGURACIÓN DHT11
// ==========================
#define DHTPIN 4 // Pin DATA del DHT11
#define DHTTYPE DHT11 // Tipo de sensor

DHT dht(DHTPIN, DHTTYPE);

// ==========================
// SERVIDOR WEB
// ==========================
WebServer server(80);

// Variables globales
float temperatura = 0;
float humedad = 0;

// ==========================
// FUNCIÓN PARA LEER SENSOR
// ==========================
void leerDHT11() {
humedad = [Link]();
temperatura = [Link]();

if (isnan(humedad) || isnan(temperatura)) {
[Link]("Error al leer el sensor DHT11");
humedad = 0;
temperatura = 0;
} else {
[Link]("Temperatura: ");
[Link](temperatura);
[Link](" °C");

[Link]("Humedad: ");
[Link](humedad);
[Link](" %");
}
}

// ==========================
// PÁGINA WEB
// ==========================
String paginaWeb() {
String html = "";

html += "<!DOCTYPE html>";


html += "<html>";
html += "<head>";
html += "<meta charset='UTF-8'>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<meta http-equiv='refresh' content='5'>";
html += "<title>ESP32-S3 DHT11 Web Server</title>";

html += "<style>";
html += "body{font-family:Arial;text-
align:center;background:#f2f2f2;margin:0;padding:20px;}";
html += "h1{color:#222;}";
html += ".card{background:white;padding:25px;border-radius:15px;max-
width:450px;margin:auto;box-shadow:0 0 15px #ccc;}";
html += ".dato{font-size:28px;margin:20px;padding:15px;border-
radius:10px;background:#e8f0fe;}";
html += ".temp{color:#d32f2f;}";
html += ".hum{color:#1976d2;}";
html += ".footer{font-size:14px;color:#666;margin-top:20px;}";
html += "</style>";

html += "</head>";
html += "<body>";

html += "<div class='card'>";


html += "<h1>ESP32-S3 Web Server</h1>";
html += "<h2>Sensor DHT11</h2>";

html += "<div class='dato temp'>";


html += "Temperatura<br>";
html += String(temperatura);
html += " &deg;C";
html += "</div>";

html += "<div class='dato hum'>";


html += "Humedad<br>";
html += String(humedad);
html += " %";
html += "</div>";

html += "<div class='footer'>";


html += "La página se actualiza automáticamente cada 5 segundos";
html += "</div>";
html += "</div>";

html += "</body>";
html += "</html>";

return html;
}

// ==========================
// MANEJADOR PRINCIPAL
// ==========================
void handleRoot() {
leerDHT11();
[Link](200, "text/html", paginaWeb());
}

// ==========================
// ENDPOINT JSON OPCIONAL
// ==========================
void handleDatos() {
leerDHT11();

String json = "{";


json += "\"temperatura\":";
json += String(temperatura);
json += ",";
json += "\"humedad\":";
json += String(humedad);
json += "}";

[Link](200, "application/json", json);


}
// ==========================
// SETUP
// ==========================
void setup() {
[Link](115200);

[Link]();

[Link]();
[Link]("Iniciando ESP32-S3 con sensor DHT11...");

[Link](ssid, password);

[Link]("Conectando a WiFi");

while ([Link]() != WL_CONNECTED) {


delay(500);
[Link](".");
}

[Link]();
[Link]("WiFi conectado");
[Link]("Dirección IP: ");
[Link]([Link]());

[Link]("/", handleRoot);
[Link]("/datos", handleDatos);

[Link]();

[Link]("Servidor web iniciado");


}
// ==========================
// LOOP
// ==========================
void loop() {
[Link]();
}
PRACTICA AR4 MATRIX1
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;

byte cara[8][12] = {
{0,0,1,1,0,0,0,0,1,1,0,0},
{0,1,0,0,1,0,0,1,0,0,1,0},
{1,0,0,0,0,1,1,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,1,0,0,0,0,1,0,0,1},
{0,1,0,0,1,1,1,1,0,0,1,0},
{0,0,1,1,0,0,0,0,1,1,0,0}
};

void setup() {
[Link]();
[Link](cara, 8, 12);
}

void loop() {
}
PRACTICA AR4 MATRIX2
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;

byte corazon[8][12] = {
{0,0,0,1,1,0,0,1,1,0,0,0},
{0,0,1,1,1,1,1,1,1,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,0,1,1,1,1,1,1,1,1,0,0},
{0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,0,0,1,1,0,0,0,0,0}
};

void setup() {
[Link]();
[Link](corazon, 8, 12);
}

void loop() {
}
PRACTICA AR4 MATRIX3
#include "Arduino_LED_Matrix.h"

ArduinoLEDMatrix matrix;

byte corazon[8][12] = {
{0,0,0,1,1,0,0,1,1,0,0,0},
{0,0,1,1,1,1,1,1,1,1,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,0,1,1,1,1,1,1,1,1,0,0},
{0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,0,0,1,1,0,0,0,0,0}
};

byte apagado[8][12] = {
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}
};

void setup() {
[Link]();
}

void loop() {
[Link](corazon, 8, 12);
delay(500);

[Link](apagado, 8, 12);
delay(500);
}
PRACTICA AR4 MATRIX4
#include <Arduino_LED_Matrix.h>

ArduinoLEDMatrix matrix;

uint8_t pantalla[8][12];

void limpiarPantalla() {
for (int fila = 0; fila < 8; fila++) {
for (int columna = 0; columna < 12; columna++) {
pantalla[fila][columna] = 0;
}
}
}

void dibujarColumna(int columnaActiva) {


limpiarPantalla();

for (int fila = 0; fila < 8; fila++) {


pantalla[fila][columnaActiva] = 1;
}

[Link](pantalla, 8, 12);
}

void setup() {
[Link]();
}

void loop() {
for (int columna = 0; columna < 12; columna++) {
dibujarColumna(columna);
delay(150);
}
}
RGB ESP32S3
#include <Adafruit_NeoPixel.h>

// LED RGB integrado del ESP32-S3-DevKitC


#define RGB_PIN 48
#define NUM_LEDS 1
Adafruit_NeoPixel rgb(NUM_LEDS, RGB_PIN, NEO_GRB + NEO_KHZ800);

void setColor(uint8_t r, uint8_t g, uint8_t b) {


[Link](0, [Link](r, g, b));
[Link]();
}

void setup() {
[Link](115200);

[Link]();
[Link](50); // Brillo de 0 a 255
[Link](); // Apaga el LED al iniciar

[Link]("Control LED RGB ESP32-S3-DevKitC");


[Link]("Comandos:");
[Link]("R = Rojo");
[Link]("G = Verde");
[Link]("B = Azul");
[Link]("W = Blanco");
[Link]("Y = Amarillo");
[Link]("C = Cian");
[Link]("M = Magenta");
[Link]("O = Apagar");
}

void loop() {
if ([Link]()) {
char comando = [Link]();

switch (comando) {
case 'R':
case 'r':
setColor(255, 0, 0);
[Link]("Color: Rojo");
break;

case 'G':
case 'g':
setColor(0, 255, 0);
[Link]("Color: Verde");
break;

case 'B':
case 'b':
setColor(0, 0, 255);
[Link]("Color: Azul");
break;

case 'W':
case 'w':
setColor(255, 255, 255);
[Link]("Color: Blanco");
break;

case 'Y':
case 'y':
setColor(255, 255, 0);
[Link]("Color: Amarillo");
break;

case 'C':
case 'c':
setColor(0, 255, 255);
[Link]("Color: Cian");
break;

case 'M':
case 'm':
setColor(255, 0, 255);
[Link]("Color: Magenta");
break;

case 'O':
case 'o':
setColor(0, 0, 0);
[Link]("LED apagado");
break;
}
}
}
RGB WEBSERVER
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_NeoPixel.h>

// ==========================
// CONFIGURACIÓN WIFI
// ==========================
const char* ssid = "FBARRA";
const char* password = "GFAgfa123";

// ==========================
// CONFIGURACIÓN LED RGB
// ==========================
#define RGB_PIN 48 // LED RGB integrado ESP32-S3-DevKitC
#define NUM_LEDS 1
Adafruit_NeoPixel rgb(NUM_LEDS, RGB_PIN, NEO_GRB + NEO_KHZ800);

// ==========================
// SERVIDOR WEB
// ==========================
WebServer server(80);

// Variables de color
int rojo = 0;
int verde = 0;
int azul = 0;

void setColor(int r, int g, int b) {


rojo = r;
verde = g;
azul = b;

[Link](0, [Link](r, g, b));


[Link]();
}

String paginaWeb() {
String html = "";

html += "<!DOCTYPE html>";


html += "<html>";
html += "<head>";
html += "<meta charset='UTF-8'>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<title>Control LED RGB ESP32-S3</title>";

html += "<style>";
html += "body{font-family:Arial;text-
align:center;background:#f2f2f2;margin:0;padding:20px;}";
html += "h1{color:#222;}";
html += ".card{background:white;padding:25px;border-radius:15px;max-
width:420px;margin:auto;box-shadow:0 0 15px #ccc;}";
html += "button{width:130px;height:45px;margin:8px;border:none;border-radius:8px;font-
size:16px;color:white;cursor:pointer;}";
html += ".rojo{background:red;}";
html += ".verde{background:green;}";
html += ".azul{background:blue;}";
html += ".blanco{background:#777;}";
html += ".amarillo{background:#d4a000;}";
html += ".cian{background:#009999;}";
html += ".magenta{background:#b000b0;}";
html += ".off{background:#222;}";
html += "input{width:80%;}";
html += ".valor{font-weight:bold;}";
html += "</style>";

html += "</head>";
html += "<body>";

html += "<div class='card'>";


html += "<h1>ESP32-S3 Web Server</h1>";
html += "<h2>Control de LED RGB</h2>";

html += "<p>Color actual: ";


html += "R=" + String(rojo) + " ";
html += "G=" + String(verde) + " ";
html += "B=" + String(azul);
html += "</p>";

html += "<a href='/rojo'><button class='rojo'>Rojo</button></a>";


html += "<a href='/verde'><button class='verde'>Verde</button></a><br>";
html += "<a href='/azul'><button class='azul'>Azul</button></a>";
html += "<a href='/blanco'><button class='blanco'>Blanco</button></a><br>";

html += "<a href='/amarillo'><button class='amarillo'>Amarillo</button></a>";


html += "<a href='/cian'><button class='cian'>Cian</button></a><br>";

html += "<a href='/magenta'><button class='magenta'>Magenta</button></a>";


html += "<a href='/off'><button class='off'>Apagar</button></a>";

html += "<hr>";

html += "<h3>Control personalizado RGB</h3>";

html += "<form action='/custom' method='GET'>";


html += "<p>Rojo: <span class='valor' id='rv'>0</span></p>";
html += "<input type='range' name='r' min='0' max='255' value='0'
oninput='[Link]=[Link]'>";

html += "<p>Verde: <span class='valor' id='gv'>0</span></p>";


html += "<input type='range' name='g' min='0' max='255' value='0'
oninput='[Link]=[Link]'>";

html += "<p>Azul: <span class='valor' id='bv'>0</span></p>";


html += "<input type='range' name='b' min='0' max='255' value='0'
oninput='[Link]=[Link]'>";

html += "<br><br>";
html += "<button type='submit' style='background:#444;'>Aplicar Color</button>";
html += "</form>";

html += "</div>";

html += "</body>";
html += "</html>";

return html;
}

void handleRoot() {
[Link](200, "text/html", paginaWeb());
}

void handleRojo() {
setColor(255, 0, 0);
[Link](200, "text/html", paginaWeb());
}

void handleVerde() {
setColor(0, 255, 0);
[Link](200, "text/html", paginaWeb());
}

void handleAzul() {
setColor(0, 0, 255);
[Link](200, "text/html", paginaWeb());
}

void handleBlanco() {
setColor(255, 255, 255);
[Link](200, "text/html", paginaWeb());
}

void handleAmarillo() {
setColor(255, 255, 0);
[Link](200, "text/html", paginaWeb());
}
void handleCian() {
setColor(0, 255, 255);
[Link](200, "text/html", paginaWeb());
}

void handleMagenta() {
setColor(255, 0, 255);
[Link](200, "text/html", paginaWeb());
}

void handleOff() {
setColor(0, 0, 0);
[Link](200, "text/html", paginaWeb());
}

void handleCustom() {
if ([Link]("r") && [Link]("g") && [Link]("b")) {
int r = [Link]("r").toInt();
int g = [Link]("g").toInt();
int b = [Link]("b").toInt();

r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);

setColor(r, g, b);
}

[Link](200, "text/html", paginaWeb());


}

void setup() {
[Link](115200);

[Link]();
[Link](80);
[Link]();

setColor(0, 0, 0);

[Link]();
[Link]("Conectando a WiFi...");
[Link](ssid, password);

while ([Link]() != WL_CONNECTED) {


delay(500);
[Link](".");
}

[Link]();
[Link]("WiFi conectado");
[Link]("Direccion IP: ");
[Link]([Link]());

[Link]("/", handleRoot);
[Link]("/rojo", handleRojo);
[Link]("/verde", handleVerde);
[Link]("/azul", handleAzul);
[Link]("/blanco", handleBlanco);
[Link]("/amarillo", handleAmarillo);
[Link]("/cian", handleCian);
[Link]("/magenta", handleMagenta);
[Link]("/off", handleOff);
[Link]("/custom", handleCustom);
[Link]();

[Link]("Servidor web iniciado");


}

void loop() {
[Link]();
}
RGB WEBSERVER
#include <WiFiS3.h>

// ==========================
// CONFIGURACIÓN WIFI
// ==========================
char ssid[] = "DPTO_ELECTRONICA_24G";
char pass[] = "Electronica_2024";

int status = WL_IDLE_STATUS;

// ==========================
// PINES DEL LED RGB
// ==========================
#define PIN_ROJO 9
#define PIN_VERDE 10
#define PIN_AZUL 11

// Si tu LED RGB es de cátodo común, deja esto en false.


// Si tu LED RGB es de ánodo común, cambia a true.
bool ANODO_COMUN = false;

// ==========================
// SERVIDOR WEB
// ==========================
WiFiServer server(80);

// Variables para guardar color actual


int rojo = 0;
int verde = 0;
int azul = 0;

// ==========================
// FUNCIÓN PARA CONTROLAR COLOR
// ==========================
void setColor(int r, int g, int b) {
rojo = r;
verde = g;
azul = b;

if (ANODO_COMUN == false) {
// LED RGB de cátodo común
analogWrite(PIN_ROJO, r);
analogWrite(PIN_VERDE, g);
analogWrite(PIN_AZUL, b);
} else {
// LED RGB de ánodo común
analogWrite(PIN_ROJO, 255 - r);
analogWrite(PIN_VERDE, 255 - g);
analogWrite(PIN_AZUL, 255 - b);
}
}

// ==========================
// PÁGINA WEB
// ==========================
void enviarPagina(WiFiClient client) {
[Link]("HTTP/1.1 200 OK");
[Link]("Content-Type: text/html");
[Link]("Connection: close");
[Link]();

[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<meta charset='UTF-8'>");
[Link]("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
[Link]("<title>Arduino UNO R4 WiFi - LED RGB</title>");

[Link]("<style>");
[Link]("body{font-family:Arial;text-
align:center;background:#f2f2f2;margin:0;padding:20px;}");
[Link](".card{background:white;max-width:450px;margin:auto;padding:25px;border-
radius:15px;box-shadow:0 0 15px #ccc;}");
[Link]("button{width:130px;height:45px;margin:8px;border:none;border-
radius:8px;font-size:16px;color:white;cursor:pointer;}");
[Link](".rojo{background:red;}");
[Link](".verde{background:green;}");
[Link](".azul{background:blue;}");
[Link](".blanco{background:#777;}");
[Link](".amarillo{background:#d4a000;}");
[Link](".cian{background:#009999;}");
[Link](".magenta{background:#b000b0;}");
[Link](".off{background:#222;}");
[Link]("input{width:80%;}");
[Link]("</style>");

[Link]("</head>");
[Link]("<body>");
[Link]("<div class='card'>");

[Link]("<h1>Arduino UNO R4 WiFi</h1>");


[Link]("<h2>Control Web de LED RGB</h2>");

[Link]("<p>Color actual: R=");


[Link](rojo);
[Link](" G=");
[Link](verde);
[Link](" B=");
[Link](azul);
[Link]("</p>");

[Link]("<a href='/rojo'><button class='rojo'>Rojo</button></a>");


[Link]("<a href='/verde'><button class='verde'>Verde</button></a><br>");

[Link]("<a href='/azul'><button class='azul'>Azul</button></a>");


[Link]("<a href='/blanco'><button class='blanco'>Blanco</button></a><br>");

[Link]("<a href='/amarillo'><button class='amarillo'>Amarillo</button></a>");


[Link]("<a href='/cian'><button class='cian'>Cian</button></a><br>");

[Link]("<a href='/magenta'><button class='magenta'>Magenta</button></a>");


[Link]("<a href='/off'><button class='off'>Apagar</button></a>");

[Link]("<hr>");
[Link]("<h3>Control personalizado RGB</h3>");

[Link]("<form action='/custom' method='GET'>");

[Link]("<p>Rojo: <span id='rv'>0</span></p>");


[Link]("<input type='range' name='r' min='0' max='255' value='0'
oninput='[Link]=[Link]'>");

[Link]("<p>Verde: <span id='gv'>0</span></p>");


[Link]("<input type='range' name='g' min='0' max='255' value='0'
oninput='[Link]=[Link]'>");

[Link]("<p>Azul: <span id='bv'>0</span></p>");


[Link]("<input type='range' name='b' min='0' max='255' value='0'
oninput='[Link]=[Link]'>");

[Link]("<br><br>");
[Link]("<button type='submit' style='background:#444;'>Aplicar Color</button>");

[Link]("</form>");

[Link]("</div>");
[Link]("</body>");
[Link]("</html>");
}

// ==========================
// EXTRAER VALORES RGB DEL URL
// ==========================
int obtenerValor(String request, String parametro) {
int inicio = [Link](parametro + "=");

if (inicio == -1) {
return 0;
}

inicio += [Link]() + 1;

int fin = [Link]("&", inicio);

if (fin == -1) {
fin = [Link](" ", inicio);
}

String valor = [Link](inicio, fin);


return constrain([Link](), 0, 255);
}

// ==========================
// SETUP
// ==========================
void setup() {
[Link](115200);

pinMode(PIN_ROJO, OUTPUT);
pinMode(PIN_VERDE, OUTPUT);
pinMode(PIN_AZUL, OUTPUT);

setColor(0, 0, 0);

[Link]("Iniciando Arduino UNO R4 WiFi...");


[Link]("Conectando a WiFi...");

if ([Link]() == WL_NO_MODULE) {
[Link]("Error: No se detecta el módulo WiFi.");
while (true);
}

while (status != WL_CONNECTED) {


[Link]("Conectando a: ");
[Link](ssid);

status = [Link](ssid, pass);


delay(5000);
}
[Link]();

[Link]("WiFi conectado.");
[Link]("Dirección IP: ");
[Link]([Link]());

[Link]("Servidor web iniciado.");


}

// ==========================
// LOOP PRINCIPAL
// ==========================
void loop() {
WiFiClient client = [Link]();

if (client) {
[Link]("Nuevo cliente conectado");

String request = "";


unsigned long timeout = millis();

while ([Link]() && millis() - timeout < 2000) {


if ([Link]()) {
char c = [Link]();
request += c;

if (c == '\n') {
break;
}
}
}
[Link](request);

if ([Link]("GET /rojo") >= 0) {


setColor(255, 0, 0);
}
else if ([Link]("GET /verde") >= 0) {
setColor(0, 255, 0);
}
else if ([Link]("GET /azul") >= 0) {
setColor(0, 0, 255);
}
else if ([Link]("GET /blanco") >= 0) {
setColor(255, 255, 255);
}
else if ([Link]("GET /amarillo") >= 0) {
setColor(255, 255, 0);
}
else if ([Link]("GET /cian") >= 0) {
setColor(0, 255, 255);
}
else if ([Link]("GET /magenta") >= 0) {
setColor(255, 0, 255);
}
else if ([Link]("GET /off") >= 0) {
setColor(0, 0, 0);
}
else if ([Link]("GET /custom") >= 0) {
int r = obtenerValor(request, "r");
int g = obtenerValor(request, "g");
int b = obtenerValor(request, "b");

setColor(r, g, b);
}
enviarPagina(client);

delay(1);
[Link]();

[Link]("Cliente desconectado");
}
}

WEBSERVER SENSORES
#include <WiFiS3.h>
#include "DHT.h"

// =====================================================
// CONFIGURACIÓN WIFI
// =====================================================
char ssid[] = "DPTO_ELECTRONICA_24G";
char pass[] = "Electronica_2024";

int status = WL_IDLE_STATUS;

// =====================================================
// CONFIGURACIÓN DE PINES
// =====================================================
#define DHTPIN 4
#define DHTTYPE DHT11

#define PIN_POT A0
#define PIN_LUZ A1
#define PIN_LM35 A2

DHT dht(DHTPIN, DHTTYPE);


// =====================================================
// SERVIDOR WEB
// =====================================================
WiFiServer server(80);

// =====================================================
// VARIABLES DE SENSORES
// =====================================================
float tempDHT = 0.0;
float humDHT = 0.0;
float tempLM35 = 0.0;

int rawPot = 0;
int rawLuz = 0;
int rawLM35 = 0;

float voltPot = 0.0;


float voltLuz = 0.0;
float voltLM35 = 0.0;

float porcentajePot = 0.0;


float porcentajeLuz = 0.0;

bool dhtOK = false;

// =====================================================
// CONFIGURACIÓN ADC
// =====================================================
// Por defecto, analogRead() trabaja de 0 a 1023.
// Como el UNO R4 WiFi trabaja con lógica de 5V en sus pines,
// usamos 5.0 V como referencia para el cálculo de voltaje.
const float VREF = 5.0;
const float ADC_MAX = 1023.0;

// =====================================================
// TIEMPOS DE LECTURA
// =====================================================
unsigned long tiempoAnteriorLectura = 0;
const unsigned long intervaloLectura = 2000; // DHT11 recomienda lectura cada 2 s

// =====================================================
// FUNCIÓN PARA LEER SENSORES
// =====================================================
void leerSensores() {
unsigned long tiempoActual = millis();

if (tiempoActual - tiempoAnteriorLectura >= intervaloLectura) {


tiempoAnteriorLectura = tiempoActual;

// Lectura DHT11
float h = [Link]();
float t = [Link]();

if (isnan(h) || isnan(t)) {
[Link]("Error al leer DHT11");
dhtOK = false;
} else {
humDHT = h;
tempDHT = t;
dhtOK = true;
}

// Lectura potenciómetro
rawPot = analogRead(PIN_POT);
voltPot = (rawPot * VREF) / ADC_MAX;
porcentajePot = (rawPot * 100.0) / ADC_MAX;

// Lectura sensor de luz


rawLuz = analogRead(PIN_LUZ);
voltLuz = (rawLuz * VREF) / ADC_MAX;
porcentajeLuz = (rawLuz * 100.0) / ADC_MAX;

// Lectura LM35
rawLM35 = analogRead(PIN_LM35);
voltLM35 = (rawLM35 * 3) / ADC_MAX;

// LM35 entrega aproximadamente 10 mV por cada °C


// Temperatura = voltaje / 0.01
tempLM35 = voltLM35 * 100.0;

// Monitor Serial
[Link]("================================");
[Link]("DHT11 Temperatura: ");
[Link](tempDHT);
[Link](" °C");

[Link]("DHT11 Humedad: ");


[Link](humDHT);
[Link](" %");

[Link]("Potenciometro ADC: ");


[Link](rawPot);
[Link](" | Voltaje: ");
[Link](voltPot);
[Link](" V");

[Link]("Luz ADC: ");


[Link](rawLuz);
[Link](" | Voltaje: ");
[Link](voltLuz);
[Link](" V");

[Link]("LM35 ADC: ");


[Link](rawLM35);
[Link](" | Voltaje: ");
[Link](voltLM35);
[Link](" V | Temp: ");
[Link](tempLM35);
[Link](" °C");
}
}

// =====================================================
// PÁGINA WEB PRINCIPAL
// =====================================================
void enviarPaginaWeb(WiFiClient client) {
leerSensores();

[Link]("HTTP/1.1 200 OK");


[Link]("Content-Type: text/html; charset=UTF-8");
[Link]("Connection: close");
[Link]();

[Link]("<!DOCTYPE html>");
[Link]("<html lang='es'>");
[Link]("<head>");
[Link]("<meta charset='UTF-8'>");
[Link]("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
[Link]("<meta http-equiv='refresh' content='5'>");
[Link]("<title>Estacion IoT Arduino UNO R4 WiFi</title>");
[Link]("<style>");
[Link]("body{margin:0;font-family:Arial,Helvetica,sans-serif;background:linear-
gradient(135deg,#dbeafe,#f8fafc);color:#0f172a;text-align:center;}");
[Link](".contenedor{max-width:1000px;margin:auto;padding:25px;}");
[Link](".panel{background:white;border-radius:24px;padding:25px;box-shadow:0 8px
25px rgba(0,0,0,0.16);}");
[Link]("h1{margin-bottom:5px;color:#1e3a8a;}");
[Link]("h2{margin-top:0;color:#334155;font-weight:normal;}");
[Link](".grid{display:grid;grid-template-columns:repeat(auto-
fit,minmax(220px,1fr));gap:18px;margin-top:25px;}");
[Link](".card{background:#f8fafc;border-radius:18px;padding:18px;box-shadow:inset 0
0 0 1px #e2e8f0;}");
[Link](".titulo{font-size:18px;font-weight:bold;color:#334155;}");
[Link](".valor{font-size:36px;font-weight:bold;margin:10px 0;}");
[Link](".sub{font-size:14px;color:#64748b;}");
[Link](".temp{color:#dc2626;}");
[Link](".hum{color:#2563eb;}");
[Link](".pot{color:#9333ea;}");
[Link](".luz{color:#ca8a04;}");
[Link](".lm{color:#ea580c;}");
[Link](".ok{background:#dcfce7;color:#166534;padding:12px;border-
radius:12px;margin-top:20px;font-weight:bold;}");
[Link](".error{background:#fee2e2;color:#991b1b;padding:12px;border-
radius:12px;margin-top:20px;font-weight:bold;}");
[Link](".barra{height:16px;background:#e5e7eb;border-
radius:20px;overflow:hidden;margin-top:12px;}");
[Link](".relleno{height:100%;background:#2563eb;border-radius:20px;}");
[Link](".footer{font-size:14px;color:#64748b;margin-top:20px;}");
[Link](".figura{max-width:420px;margin:20px auto;}");
[Link]("</style>");

[Link]("</head>");
[Link]("<body>");
[Link]("<div class='contenedor'>");
[Link]("<div class='panel'>");
[Link]("<h1>Arduino UNO R4 WiFi</h1>");
[Link]("<h2>Web Server - Estacion IoT con Sensores Analogicos y DHT11</h2>");

// FIGURA SVG
[Link]("<div class='figura'>");
[Link]("<svg viewBox='0 0 460 270' width='100%'
xmlns='[Link]
[Link]("<rect x='10' y='10' width='440' height='250' rx='28' fill='#e0f2fe'
stroke='#38bdf8' stroke-width='3'/>");

// Sol
[Link]("<circle cx='95' cy='75' r='35' fill='#facc15' stroke='#eab308' stroke-
width='4'/>");
[Link]("<line x1='95' y1='25' x2='95' y2='5' stroke='#facc15' stroke-width='6' stroke-
linecap='round'/>");
[Link]("<line x1='95' y1='125' x2='95' y2='145' stroke='#facc15' stroke-width='6' stroke-
linecap='round'/>");
[Link]("<line x1='45' y1='75' x2='25' y2='75' stroke='#facc15' stroke-width='6' stroke-
linecap='round'/>");
[Link]("<line x1='145' y1='75' x2='165' y2='75' stroke='#facc15' stroke-width='6' stroke-
linecap='round'/>");

// Nube
[Link]("<ellipse cx='315' cy='82' rx='55' ry='32' fill='white'/>");
[Link]("<ellipse cx='275' cy='88' rx='38' ry='28' fill='white'/>");
[Link]("<ellipse cx='350' cy='92' rx='42' ry='30' fill='white'/>");
[Link]("<rect x='260' y='82' width='120' height='42' rx='22' fill='white'/>");

// Gotas
[Link]("<path d='M290 145 C280 160,280 172,290 178 C300 172,300 160,290 145Z'
fill='#38bdf8'/>");
[Link]("<path d='M335 145 C325 160,325 172,335 178 C345 172,345 160,335 145Z'
fill='#38bdf8'/>");

// Placa Arduino
[Link]("<rect x='160' y='165' width='145' height='70' rx='12' fill='#334155'/>");
[Link]("<rect x='178' y='182' width='110' height='28' rx='6' fill='#e5e7eb'/>");
[Link]("<circle cx='195' cy='197' r='6' fill='#22c55e'/>");
[Link]("<circle cx='225' cy='197' r='6' fill='#facc15'/>");
[Link]("<circle cx='255' cy='197' r='6' fill='#ef4444'/>");

// Antena WiFi
[Link]("<line x1='232' y1='165' x2='232' y2='120' stroke='#334155' stroke-width='5'
stroke-linecap='round'/>");
[Link]("<path d='M212 130 Q232 110 252 130' fill='none' stroke='#2563eb' stroke-
width='4' stroke-linecap='round'/>");
[Link]("<path d='M197 118 Q232 85 267 118' fill='none' stroke='#2563eb' stroke-
width='4' stroke-linecap='round'/>");

[Link]("<text x='230' y='252' text-anchor='middle' font-size='16' fill='#0f172a'>Estacion


IoT: DHT11 + LM35 + Luz + Potenciometro</text>");
[Link]("</svg>");
[Link]("</div>");

// TARJETAS DE DATOS
[Link]("<div class='grid'>");

[Link]("<div class='card'>");
[Link]("<div class='titulo'>Temperatura DHT11</div>");
[Link]("<div class='valor temp'>");
[Link](tempDHT, 1);
[Link](" &deg;C</div>");
[Link]("<div class='sub'>Sensor digital conectado en D4</div>");
[Link]("</div>");

[Link]("<div class='card'>");
[Link]("<div class='titulo'>Humedad DHT11</div>");
[Link]("<div class='valor hum'>");
[Link](humDHT, 1);
[Link](" %</div>");
[Link]("<div class='sub'>Humedad relativa ambiental</div>");
[Link]("</div>");

[Link]("<div class='card'>");
[Link]("<div class='titulo'>Potenciometro</div>");
[Link]("<div class='valor pot'>");
[Link](porcentajePot, 1);
[Link](" %</div>");
[Link]("<div class='sub'>ADC: ");
[Link](rawPot);
[Link](" | Voltaje: ");
[Link](voltPot, 2);
[Link](" V</div>");
[Link]("<div class='barra'><div class='relleno' style='width:");
[Link](porcentajePot, 1);
[Link]("%;background:#9333ea;'></div></div>");
[Link]("</div>");

[Link]("<div class='card'>");
[Link]("<div class='titulo'>Sensor de Luz</div>");
[Link]("<div class='valor luz'>");
[Link](porcentajeLuz, 1);
[Link](" %</div>");
[Link]("<div class='sub'>ADC: ");
[Link](rawLuz);
[Link](" | Voltaje: ");
[Link](voltLuz, 2);
[Link](" V</div>");
[Link]("<div class='barra'><div class='relleno' style='width:");
[Link](porcentajeLuz, 1);
[Link]("%;background:#ca8a04;'></div></div>");
[Link]("</div>");
[Link]("<div class='card'>");
[Link]("<div class='titulo'>Temperatura LM35</div>");
[Link]("<div class='valor lm'>");
[Link](tempLM35, 1);
[Link](" &deg;C</div>");
[Link]("<div class='sub'>ADC: ");
[Link](rawLM35);
[Link](" | Voltaje: ");
[Link](voltLM35, 3);
[Link](" V</div>");
[Link]("</div>");

[Link]("</div>");

if (dhtOK) {
[Link]("<div class='ok'>Sistema funcionando correctamente</div>");
} else {
[Link]("<div class='error'>Error al leer el sensor DHT11. Revisar conexion en
D4.</div>");
}

[Link]("<p><b>IP del servidor:</b> ");


[Link]([Link]());
[Link]("</p>");

[Link]("<div class='footer'>");
[Link]("La pagina se actualiza automaticamente cada 5 segundos.<br>");
[Link]("Endpoint JSON disponible en: <b>/datos</b>");
[Link]("</div>");

[Link]("</div>");
[Link]("</div>");
[Link]("</body>");
[Link]("</html>");
}

// =====================================================
// RESPUESTA JSON PARA NODE-RED, GRAFANA O APPS
// =====================================================
void enviarDatosJSON(WiFiClient client) {
leerSensores();

[Link]("HTTP/1.1 200 OK");


[Link]("Content-Type: application/json");
[Link]("Connection: close");
[Link]();

[Link]("{");

[Link]("\"temp_dht\":");
[Link](tempDHT, 1);
[Link](",");

[Link]("\"hum_dht\":");
[Link](humDHT, 1);
[Link](",");

[Link]("\"pot_adc\":");
[Link](rawPot);
[Link](",");

[Link]("\"pot_voltaje\":");
[Link](voltPot, 2);
[Link](",");
[Link]("\"pot_porcentaje\":");
[Link](porcentajePot, 1);
[Link](",");

[Link]("\"luz_adc\":");
[Link](rawLuz);
[Link](",");

[Link]("\"luz_voltaje\":");
[Link](voltLuz, 2);
[Link](",");

[Link]("\"luz_porcentaje\":");
[Link](porcentajeLuz, 1);
[Link](",");

[Link]("\"lm35_adc\":");
[Link](rawLM35);
[Link](",");

[Link]("\"lm35_voltaje\":");
[Link](voltLM35, 3);
[Link](",");

[Link]("\"temp_lm35\":");
[Link](tempLM35, 1);
[Link](",");

[Link]("\"dht_ok\":");
[Link](dhtOK ? "true" : "false");

[Link]("}");
}
// =====================================================
// SETUP
// =====================================================
void setup() {
[Link](115200);
delay(1000);

[Link]();

pinMode(PIN_POT, INPUT);
pinMode(PIN_LUZ, INPUT);
pinMode(PIN_LM35, INPUT);

[Link]("======================================");
[Link]("Arduino UNO R4 WiFi - Web Server IoT");
[Link]("DHT11 + Potenciometro + Luz + LM35");
[Link]("======================================");

if ([Link]() == WL_NO_MODULE) {
[Link]("Error: No se detecta el modulo WiFi.");
while (true);
}

while (status != WL_CONNECTED) {


[Link]("Conectando a WiFi: ");
[Link](ssid);

status = [Link](ssid, pass);


delay(5000);
}

[Link]();
[Link]("WiFi conectado correctamente.");
[Link]("Direccion IP: ");
[Link]([Link]());
[Link]("Servidor web iniciado.");
}

// =====================================================
// LOOP PRINCIPAL
// =====================================================
void loop() {
leerSensores();

WiFiClient client = [Link]();

if (client) {
[Link]("Nuevo cliente conectado");

String request = "";


unsigned long tiempoInicio = millis();

while ([Link]() && millis() - tiempoInicio < 2000) {


if ([Link]()) {
char c = [Link]();
request += c;

if (c == '\n') {
break;
}
}
}

[Link](request);
if ([Link]("GET /datos") >= 0) {
enviarDatosJSON(client);
} else {
enviarPaginaWeb(client);
}

delay(1);
[Link]();

[Link]("Cliente desconectado");
}
}

You might also like