4.- Load a Sketch. Turn On/Off the Arduino LED13. Web page. App Inventor.
/***********
Juan A. Villalpando
KIO4.COM
25/11/22
Version del módulo 9.2.4
Velocidad 9600
Carga el programa. Ve al Serial Monitor.
Escribe en un navegador 192.168.1.5
Pulsa los botones para encender o apagar el LED13 del Arduino.
***********/
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(3,2);
// El TX del módulo al terminal 3 del Arduino.
// El RX del módulo al terminal 2 del Arduino.
void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
esp8266.begin(9600); // Importante la velocidad del módulo.
sendData("AT+RST\r\n",2000,DEBUG); // Borra la configuración que tenía el módulo
sendData("AT+CWJAP=\"Nombre_de_tu_WiFi\",\"Clave_de_tu_WiFi\"\r\n", 2000, DEBUG);
delay(5000); // Espera un poco que conecte con el Router.
sendData("AT+CWMODE=3\r\n",1000,DEBUG); // Modo de cliente y servidor.
sendData("AT+CIFSR\r\n",1000,DEBUG); // En el Serial Monitor aparece la IP de cliente y servidor.
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // Multiples conexiones.
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // El Puerto web es el 80
}
void loop(){
if(esp8266.available()) // Consulta si el módulo está enviando algún mensaje
{
if(esp8266.find("+IPD,"))
{
delay(500);
int connectionId = esp8266.read()-48;
// Aquí las construcción de la PAGINA WEB.
String webpage = "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n\r\n";
webpage += "<h1>KIO4.COM</h1>";
webpage += "<form method=\"get\" action=\"/enci\">";
webpage += "<button type=\"submit\">ON - ENCIENDE</button></form>";
webpage += "<form method=\"get\" action=\"/apag\">";
webpage += "<button type=\"submit\">OFF - APAGA</button></form>\r\n\r\n";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,500,DEBUG);
sendData(webpage,500,DEBUG);
// Lee el pin 13
int pin13 = digitalRead(13);
// Retorno de la lectura.
String retorno = "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n\r\n";
if (pin13 == 1) {retorno += "<br> ON - Encendido";}
if (pin13 == 0) {retorno += "<br> OFF - Apagado";}
// ResponseCode App Inventor
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += retorno.length();
cipSend +="\r\n";
sendData(cipSend,500,DEBUG);
sendData(retorno,500,DEBUG);
// Cierra la conexión
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId;
closeCommand+="\r\n";
sendData(closeCommand,500,DEBUG);
}
}
}
// Función para Enviar datos al Servidor.
String sendData(String command, const int timeout, boolean debug){
String response = "";
esp8266.print(command); // Envía la información de command al servidor
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// A response van los datos que regresan al servidor.
char c = esp8266.read(); // Va leyendo caracter a caracter.
response+=c;
// Consulta si en la información que regresa al servidor
// viene "GET /enci" o "GET /apag"
// Encenderá o apagará el LED13 del Arduino
if(response.indexOf("GET /enci") >0){
// Serial.print("enciende");
digitalWrite(13,HIGH);
}
if(response.indexOf("GET /apag") >0){
//Serial.print("apaga");
digitalWrite(13,LOW);
}
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
p57_esp8266_01.aia (2.0 KB)



- Comments.
-
We can turn on/off the LED13 of the Arduino through an app with the Web component or through a web page.
-
In order for the Web component's GotText event to recognize the responseContent, Sketch needs to send an HTTP header.
-
The Sketch shown is didactic, note that the HTTP header is sent twice, also note that the app receives the source code of the web page, you can modify and improve it.
-
In this example, the ESP8266-01 works as a client of a local WiFi network.
-
Note that the Sketch has several delays of 500 ms, so we must wait about 6 seconds to make the changes.