Bienvenido a un nuevo tutorial de Lunegate, hoy vamos a prender a usar un Arduino equipado con un modulo Wifi. El Wemos D1 Mini v2.3. Este peque帽o controlador, nos ahorrar谩 mucho trabajo y tiempo.
INTRODUCCI脫N
El Wemos D1 mini usa la arquitectura de Arduino, pero cuenta con menos pines de los normal. Aun as铆 es muy barato, vers谩til y cuenta con una gran cantidad de shields, que nos har谩n no tener que volvernos locos haciendo circuitos en protoboard, sino simplemente pinchando uno encima de otro.
Un punto a remarcar, es que os recomiendo compraros la versi贸n 2.3, ya que primeramente compre la v1.0.0 y me ha tra铆do bastantes dolores de cabeza.
Las dimensiones son 13cm x 11cm x 1cm y un peso de 22 g. El precio es de 2,98€ m谩s gastos de envi贸 4,37€.
Para este tutorial, se requerir谩:
MATERIAL NECESARIO
A continuaci贸n os describo el material necesario para realizar el tutorial y donde conseguirlo:
PROGRAMACI脫N
Para poder hacer funcionar nuestro Wemos, debemos cargar en nuestro IDE de Arduino, las librer铆as necesarias para hacerle funcionar.
Para ello, abriremos nuestro IDE, e iremos a Archive->Preferencias y donde hay campo llamado, "Gestor de URLs adicionales para tarjetas", deb茅is pulsar la venta e introducir esta url.
http://arduino.esp8266.com/stable/package_esp8266com_index.json
A continuaci贸n, despu茅s de introducir la url, pulsaremos en Herramientas -> Placa -> Gestor de tarjetas. Cargara un rato las nuevas librer铆as, y cuando haya descargado todas, en el buscador pon茅is, wemos. Busc谩is la que se llame esp8266 by ESP8266 Communty. La seleccion谩is y os aparecer谩n los botones de selecci贸n de versi贸n, puls谩is la 煤ltima (no recomiendo usar las RC), coged las estables. Puls谩is instalar y cuando acabe cerr谩is la vista.
Ahora ya desde Herramientas -> Placa -> Encontrareis en mi caso la 煤ltima que es: Wemos D1 R2 & mini.
Ahora si queremos cargar ejemplos o carlas las librer铆as en nuestro proyecto, deb茅is ir a Programa -> incluir librer铆a -> Gestionar librer铆as.
Adem谩s del controlador, los paquetes que hab茅is instalado traen consigo una bater铆a de ejemplos muy 煤tiles y muy completos. Para ello, ir茅is a Archivo -> Ejemplos -> ESP8266, y elegir茅is Blink.
Puls谩is a cargar y ver茅is que el led mas cercano a la antena wifi lucir谩, yo tengo configurado Wemos a 115200 (ah铆 es configurarlo como prefir谩is, luego el Serial vereis que esta configurado a 74880), y carga el software relativamente r谩pida.
Una vez que vemos que nuestro hardware funciona, vamos a cargar el software de control para configurar nuestra conexi贸n wifi.
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
const char* ssid = "#######"; //Set your wifi network name(ssid)
const char* password = "######"; //Set your router password
int RelayPin = D1; //Relay Shield is controlled by pin D1
WiFiServer server(80);
void setup() {
Serial.begin(74880); //change according to your com port baud rate
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(RelayPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check for an active client
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until client responds
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read client request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/Relay=ON") != -1) {
digitalWrite(LED_BUILTIN, HIGH);
value = HIGH;
}
if (request.indexOf("/Relay=OFF") != -1){
digitalWrite(LED_BUILTIN, LOW);
value = LOW;
}
// Return the client response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Relay pin is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("Click <a href=\"/Relay=ON\">here</a> to turn the Relay ON<br>");
client.println("Click <a href=\"/Relay=OFF\">here</a> to turn the Relay OFF<br>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
const char* ssid = "#######"; //Set your wifi network name(ssid)
const char* password = "######"; //Set your router password
int RelayPin = D1; //Relay Shield is controlled by pin D1
WiFiServer server(80);
void setup() {
Serial.begin(74880); //change according to your com port baud rate
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(RelayPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check for an active client
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until client responds
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read client request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/Relay=ON") != -1) {
digitalWrite(LED_BUILTIN, HIGH);
value = HIGH;
}
if (request.indexOf("/Relay=OFF") != -1){
digitalWrite(LED_BUILTIN, LOW);
value = LOW;
}
// Return the client response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Relay pin is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("Click <a href=\"/Relay=ON\">here</a> to turn the Relay ON<br>");
client.println("Click <a href=\"/Relay=OFF\">here</a> to turn the Relay OFF<br>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}
Bien, vamos a explicar que debemos hacer antes de lanzarlo, tenemos que configurar 2 par谩metros, el ssid y el password. El ssid, es el nombre de nuestra red wifi de la casa, si no la hebeis modificado podr茅is encontrarla debajo del router. Y el password pues exactamente lo mismo.
Una vez cargado abrir茅is el monitor site, y ver茅is que carga los Serial.print, que est谩n en la aplicaci贸n. Ah铆 si es todo correcto los datos que has introducido, te mostrar谩 que la conexi贸n ha sido correcta, devolvi茅ndote una direcci贸n IP. C贸pia dicha direcci贸n y p茅gala en tu explorador web. En dicha web, tendr谩s 2 opciones, encender y apagar, puls谩ndolas podr谩s ver como se enciende y se apaga el led de vuestro Wemos.
Bien, con esto acaba nuestro tutorial de iniciaci贸n a Wemos, en este caso la comunicaci贸n es mono-direccional entre Web y Wemos. en tutoriales pr贸ximos, explicaremos como se realiza la comunicaci贸n bi-direccional entre web y hardware.
Como siempre, dejo la fuente de donde he obtenido la informaci贸n: Fuente.
PUNTUACI脫N
Calidad Componentes
4,5
Montaje
4,5
Precio
4,0
Caracter铆sticas
4,0
Puntuaci贸n Global
4,5
Arduino
Driver
Tutoriales
Wemos


























