Tutorial del Internet de las Cosas con el ESP8266
Juan Antonio Villalpando
Volver al índice del tutorial
____________________________
345.- ESP8266. App Inventor envía mensaje a LCD. LED on/off.
p145wemos_led_LCD.aia
- Escribimos un mensaje en App Inventor y lo enviamos por WiFi para visualizarlo en una pantalla LCD.
- Si escribimos on5, off5, on6, off6 se encenderán/apagarán esos LED.
- En este código no obtenemos respuesta del servidor.
_________________________________
- Diseño.
_________________________________
- Bloques.
_________________________________
- Conexiones.
_________________________________
- Código.
- Vamos a configurarlo con IP estática.
AI2_LED_LCD.ino |
// Juan A. Villalpando.
// KIO4.COM
// Envía mensaje
// Desde App Inventor
#include <ESP8266WiFi.h>
const char* ssid = "Nombre_de_tu_Red_WiFi";
const char* password = "Clave_WiFi";
// Configuración de la IP estática.
IPAddress local_IP(192, 168, 1, 12);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
#define LED5 D5 // LED en terminal 5
#define LED6 D6 // LED en terminal 6
#include <LiquidCrystal_I2C.h>
int columnas = 16;
int filas = 2;
LiquidCrystal_I2C lcd(0x27, columnas, filas);
// LiquidCrystal_I2C lcd(0x3F, columnas, filas);
WiFiServer server(80);
void setup() {
Serial.begin(115200);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
lcd.init();
lcd.backlight();
// Establecimiento de la IP estática.
WiFi.config(local_IP, gateway, subnet);
// Conecta a la red wifi.
Serial.println();
Serial.print("Conectando con ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Conectado con WiFi.");
// Inicio del Servidor web.
server.begin();
Serial.println("Servidor web iniciado.");
// Esta es la IP
Serial.print("Esta es la IP para conectar: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
}
void loop() {
// Consulta si se ha conectado algún cliente.
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.print("Nuevo cliente: ");
Serial.println(client.remoteIP());
// Espera hasta que el cliente envíe datos.
while(!client.available()){ delay(1); }
/////////////////////////////////////////////////////
// Lee la información enviada por el cliente.
String req = client.readStringUntil('\r');
Serial.println(req);
req.replace("+", " "); // Para que los espacios no salgan con +
req.replace(" HTTP/1.1", ""); // Para quitar HTTP/1.1
req.replace("GET /", ""); // Para quitar GET /
lcd.clear(); // Borra pantalla.
lcd.setCursor(0, 0); // Inicio del cursor
lcd.print("Mensaje");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(req);
// Realiza la petición del cliente.
if (req.indexOf("on5") != -1) {digitalWrite(LED5, HIGH);}
if (req.indexOf("off5") != -1){digitalWrite(LED5, LOW);}
if (req.indexOf("on6") != -1) {digitalWrite(LED6, HIGH);}
if (req.indexOf("off6") != -1){digitalWrite(LED6, LOW);}
//////////////////////////////////////////////
// Página WEB. ////////////////////////////
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Comillas importantes.
Serial.print("Cliente desconectado: ");
Serial.println(client.remoteIP());
client.flush();
client.stop();
}
|
_________________________________
- Comentarios.
- La información: String req = client.readStringUntil('\r');
llega así: GET /hola+amigo HTTP/1.1
- Reemplazamos GET / y HTTP/1.1 por espacio vacío.
- Quitamos el signo + porque desde Internet el espacio viene así:
hola+amigo
_________________________________
- Propuestas.
- Añade un CampoDeTexto para que el usuario escriba la IP a la que desea conectarse.
- Añade respuesta.
_______________________________
|