Diferencia entre revisiones de «WiFi»
De ArduWiki
(→Referencias externas) |
(→Ejemplo) |
||
Línea 15: | Línea 15: | ||
== Ejemplo == | == Ejemplo == | ||
+ | Con este ejemplo logras prender y apagar un LED desde la pagina web alojada en el nodeMCU. | ||
+ | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
+ | #include <ESP8266WiFi.h> | ||
+ | #include <ESP8266WebServer.h> | ||
+ | |||
+ | const char* ssid = "MIRED"; //Nombre SSID | ||
+ | const char* password = "12345678"; //Clave | ||
+ | |||
+ | IPAddress local_ip(192,168,2,100); | ||
+ | IPAddress gateway(192,168,2,1); | ||
+ | IPAddress subnet(255,255,255,0); | ||
+ | |||
+ | ESP8266WebServer server(80); | ||
+ | |||
+ | bool LEDstatus = LOW; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | pinMode(D5, OUTPUT); | ||
+ | |||
+ | WiFi.softAP(ssid, password); | ||
+ | WiFi.softAPConfig(local_ip, gateway, subnet); | ||
+ | delay(100); | ||
+ | |||
+ | server.on("/", handle_OnConnect); | ||
+ | server.on("/ledon", handle_ledon); | ||
+ | server.on("/ledoff", handle_ledoff); | ||
+ | server.onNotFound(handle_NotFound); | ||
+ | |||
+ | server.begin(); | ||
+ | Serial.println("Servido HTTP iniciado"); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | server.handleClient(); | ||
+ | if (LEDstatus){ | ||
+ | digitalWrite(D5, HIGH); | ||
+ | }else{ | ||
+ | digitalWrite(D5, LOW); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void handle_OnConnect() { | ||
+ | LEDstatus = LOW; | ||
+ | server.send(200, "text/html", SendHTML(false)); | ||
+ | } | ||
+ | |||
+ | void handle_ledon() { | ||
+ | LEDstatus = HIGH; | ||
+ | server.send(200, "text/html", SendHTML(true)); | ||
+ | delay(3000); | ||
+ | LEDstatus = LOW; | ||
+ | } | ||
+ | |||
+ | void handle_ledoff() { | ||
+ | LEDstatus = LOW; | ||
+ | server.send(200, "text/html", SendHTML(false)); | ||
+ | |||
+ | } | ||
+ | |||
+ | void handle_NotFound(){ | ||
+ | server.send(404, "text/plain", "Not found"); | ||
+ | } | ||
+ | |||
+ | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revisión del 22:52 27 may 2019
Contenido
Descripción
La libreria WiFi de Arduino...
Placas aplicables
Sintaxis
Métodos
Comentarios
Advertencias
Ejemplo
Con este ejemplo logras prender y apagar un LED desde la pagina web alojada en el nodeMCU.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "MIRED"; //Nombre SSID
const char* password = "12345678"; //Clave
IPAddress local_ip(192,168,2,100);
IPAddress gateway(192,168,2,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
bool LEDstatus = LOW;
void setup() {
Serial.begin(9600);
pinMode(D5, OUTPUT);
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/ledon", handle_ledon);
server.on("/ledoff", handle_ledoff);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("Servido HTTP iniciado");
}
void loop() {
server.handleClient();
if (LEDstatus){
digitalWrite(D5, HIGH);
}else{
digitalWrite(D5, LOW);
}
}
void handle_OnConnect() {
LEDstatus = LOW;
server.send(200, "text/html", SendHTML(false));
}
void handle_ledon() {
LEDstatus = HIGH;
server.send(200, "text/html", SendHTML(true));
delay(3000);
LEDstatus = LOW;
}
void handle_ledoff() {
LEDstatus = LOW;
server.send(200, "text/html", SendHTML(false));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
Vea también
Referencias externas
- All Libraries
- WiFi
- Arduino Inalambrico - Luis Del Valle