Tutorial del Internet de las Cosas con el ESP8266
Juan Antonio Villalpando
Volver al índice del tutorial
____________________________
346.- ESP8266. App Inventor mueve un Servo y muestra grados en LCD.
p346wemos_Servo.aia
- Escribimos un número en App Inventor y el Servo se mueve a esos grados. Además debe aparecer en la LCD los grados.
_________________________________
- Diseño.
_________________________________
- Bloques.
_________________________________
- Conexiones.
_________________________________
- Código.
- Observa el Monitor Serie para ver la IP de conexión.
AI2_LED_LCD.ino |
// Juan A. Villalpando.
// KIO4.COM
// Mueve Servo. Pantalla LCD.
// Desde App Inventor
#include <ESP8266WiFi.h>
// Nombre y contraseña de tu red WiFi.
const char* ssid = "Nombre_de_tu_Red_WiFi";
const char* password = "Clave_WiFi";
// Pantalla LCD
#include <LiquidCrystal_I2C.h>
int columnas = 16;
int filas = 2;
LiquidCrystal_I2C lcd(0x27, columnas, filas);
// LiquidCrystal_I2C lcd(0x3F, columnas, filas);
// Servo.
#include <Servo.h>
Servo myservo;
#define servoPin D3 // El servo en el D3
int grado = 90;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
// Conecta a la red wifi.
Serial.println();
Serial.print("Conectando con ");
Serial.println(ssid);
WiFi.begin(ssid, password);
myservo.attach(servoPin);
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);
// GET / Hola+amigo HTTP/1.1
req.replace("+", " "); // Para que los espacios no salgan con +
req.replace("%20", " "); // Para que los espacios no salgan con %20
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("Grados:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(req);
// Mueve el servo.
myservo.write(req.toInt()); // Convierte String a Entero.
//////////////////////////////////////////////
// Respuesta, devolución de grados. /////////
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Importante.
client.println(req);
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
- Los espacios pueden venir con %20, así que debemos quitar ese %20
hola%20amigo
_________________________________
- Propuestas.
-
_______________________________
|