|     Inicio    |   |         |  |   FOROS      |  |      |      
   Elastix - VoIP B4A (Basic4Android) App inventor 2 PHP - MySQL
  Estación meteorológica B4J (Basic4Java) ADB Shell - Android Arduino
  AutoIt (Programación) Visual Basic Script (VBS) FireBase (BD autoactualizable) NodeMCU como Arduino
  Teleco - Emisora de A.M. Visual Basic Cosas de Windows Webs interesantes
Translate:
Búsqueda en este sitio:


.

Tutorial del Internet de las Cosas con el ESP8266
Juan Antonio Villalpando

Volver al índice del tutorial

____________________________

325.- ESP8266. App Inventor enciende/apaga LED.

 

p325esp8266_led.aia

- Al pulsar los Botones de la aplicación apagamos y encendemos dos LED de la tarjeta.

_________________________________
- Diseño.

_________________________________
- Bloques.

_________________________________
- Código.

- Vamos a configurarlo con IP estática.

AI2_LED.ino

// Juan A. Villalpando.
// KIO4.COM
// Pone los LED5 y LED6 en alto o bajo.

#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

WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  
// 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.println(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);
  
  // 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(""); //  Importante.

	Serial.print("Cliente desconectado: ");
    Serial.println(client.remoteIP());
    client.flush();
    client.stop();
}

___________________________________________________

- Comentarios.

- Mediante estas órdenes el código comprueba si el texto recibido contiene on5, off5, on6 o off6


	  req.indexOf("on5") != -1)
      req.indexOf("off5") != -1)
      req.indexOf("on6") != -1)
      req.indexOf("off6") != -1) 

___________________________________________________
___________________________________________________
___________________________________________________

- Obtención de respuesta.

p325esp8266_led_B.aia

- En este caso después de pulsar y encender/apagar el LED, el código envía una respuesta.

- Añade una Etiqueta2 al Diseño de App Inventor

_________________________________
- Bloques.

_________________________________
- Código.

- Vamos a configurarlo con IP estática.

AI2_LED_respuesta.ino

// Juan A. Villalpando.
// KIO4.COM
// Pone los LED12 y LED14 en alto o bajo.

#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
String respuesta= "";

WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  
// Establecimiento de la IP estática.
   WiFi.config(local_IP, gateway, subnet);
  
// Conecta a la red wifi.
  Serial.println();
  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());
  Serial.println("/");
}
 
void loop() {
  // Consulta si se ha conectado algún cliente.
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
   Serial.println("Nuevo cliente.");
   
  // 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);
  
  // Realiza la petición del cliente.
       if (req.indexOf("on5") != -1) {digitalWrite(LED5, HIGH); respuesta="LED 5 encendido";}
       if (req.indexOf("off5") != -1){digitalWrite(LED5, LOW); respuesta="LED 5 apagado";}
       if (req.indexOf("on6") != -1) {digitalWrite(LED6, HIGH); respuesta="LED 6 encendido";}
       if (req.indexOf("off6") != -1){digitalWrite(LED6, LOW); respuesta="LED 6 apagado";}

  //////////////////////////////////////////////
  // Página WEB. ////////////////////////////
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  Comillas importantes.
  client.println(respuesta); //  Devuelve respuesta.

  client.flush();
  client.stop();
  Serial.println("Cliente desconectado.");
}

_________________________________
- Propuesta.

- Añade un CampoDeTexto para que el usuario escriba la IP a la que desea conectarse.

_______________________________

- Mi correo:
juana1991@yahoo.com
- KIO4.COM - Política de cookies. Textos e imágenes propiedad del autor:
© Juan A. Villalpando
No se permite la copia de información ni imágenes.
Usamos cookies propias y de terceros que entre otras cosas recogen datos sobre sus hábitos de navegación y realizan análisis de uso de nuestro sitio.
Si continúa navegando consideramos que acepta su uso. Acepto    Más información