Skip to content

Instantly share code, notes, and snippets.

@gsampallo
Last active October 24, 2020 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsampallo/d625855d8d81a819fabbca79a5375468 to your computer and use it in GitHub Desktop.
Save gsampallo/d625855d8d81a819fabbca79a5375468 to your computer and use it in GitHub Desktop.
DPRE - APP Inventor - Control de Leds
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "NOMBRE_RED";
const char* password = "CLAVE";
ESP8266WebServer servidor(80);
int pinLed1 = 5; //D1
int pinLed2 = 4; //D2
int pinLed3 = 0; //D3
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
Serial.print("Conectando");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Conectado");
Serial.print("Direccion IP: ");
Serial.println(WiFi.localIP());
pinMode(pinLed1,OUTPUT);
pinMode(pinLed2,OUTPUT);
pinMode(pinLed3,OUTPUT);
digitalWrite(pinLed1,LOW);
digitalWrite(pinLed2,LOW);
digitalWrite(pinLed3,LOW);
// http://192.168.xxx.xxx/led1/on
servidor.on("/led1/on",actividadLed1);
// http://192.168.xxx.xxx/led1/off
servidor.on("/led1/off",actividadLed1);
servidor.on("/led2/on",actividadLed2);
servidor.on("/led2/off",actividadLed2);
servidor.on("/led3/on",actividadLed3);
servidor.on("/led3/off",actividadLed3);
// http://192.168.xxx.xxx/otroRequerimientoNoConocido
servidor.onNotFound(handleNotFound);
servidor.begin();
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += servidor.uri();
message += "\nMethod: ";
message += (servidor.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += servidor.args();
message += "\n";
for (uint8_t i = 0; i < servidor.args(); i++) {
message += " " + servidor.argName(i) + ": " + servidor.arg(i) + "\n";
}
servidor.send(404, "text/plain", message);
}
void actividadLed1() {
digitalWrite(pinLed1,!digitalRead(pinLed1));
servidor.send(200,"text/plain","OK");
}
void actividadLed2() {
digitalWrite(pinLed2,!digitalRead(pinLed2));
servidor.send(200,"text/plain","OK");
}
void actividadLed3() {
digitalWrite(pinLed3,!digitalRead(pinLed3));
servidor.send(200,"text/plain","OK");
}
void loop() {
servidor.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment