Introduction: First Steps to Domotics (ESP8266 Whit Arduino and AT Commands English/Español)

This project is an easy start to make domotic whit Arduino. The project consist in turn on and turn off 2 led across an IP adress connect in to any WIFI network and you can control the led with any device connect to the same WIFI network.

Este proyecto es un facil comienzo para hacer domotica con Arduino. Consiste en prender y apagar 2 leds a traves de una direccion IP conectada en culquier red WIFI. Puedes controlar los leds con cualquier dispositivo conectado a la misma red WIFI.

Video: https://youtu.be/9iDqiS62YWg

Step 1: Material

The material you need is:
- Arduino Uno.

- ESP8266 module (WIFI module).

- 3 LED´s

- 3 resistence of 220 or 330 Ohms

- Wired Male/Male

- Wired Male/Female

El material que se necesita es:

- Arduino Uno

- Modulo ESP8266 (Modulo WIFI)

- 3 LED´s

- 3 resistencias de 220 o 330 Ohms

- Cable Macho/Macho

- Cable Macho/Hembra

Step 2: How to Connect/Como Conectar

First you need to connect the ESP8266 module, you need to connect
the CVV and CH-PD to high (3.3V), and the GND to ground, and like I say, this project work with Serial Communication, so then you need to connect RX and TX with the arduino, I use 2 and 3 port. I leave you a image of the ESP8266 Datasheet.

Next you need to connect the 3 LED's, I use the 11, 12 and 13 port. I belive that the rest of the connection of the LED are easy for you...

Primero tienes que conectar el module ESP8266, debes de conectar CVV y CH-PD a el voltaje, en este caso 5V y GND a tierra, y como es por comunicacion serial, deberas de conectar RX y TX al arduino, yo use el puerto 2 y 3. Arriba dejare una imagen del ESP8266 Datasheet. Despues debes de conectar os 3 LED's, yo use los puertos 11, 12 y 13. Creo que el resto de la conexion de los LED's es facil para ustedes.

Step 3: How It Works/Como Funciona

The code works whit some AT command. The code first print that commands for the ESP8266, and ensures that the device connect properly. Then you get an IP adress and use the specific string for turn on and turn off the LED's.

First on the code you need to put your network and password to connect the ESP8266 module to the internet, then, the code ensures that all works properly, if doesn't connect after a minute the reset the Arduino, or try with the AT commands across the Serial monitor of Arduino.

The fist time you use the code, you need see the IP adress in the Serial monitor of arduino, and sometime you will get some trash, if the IP adress you get doesn´t work, send "AT+CIFSR" in the Serial monitor, then, while your Modem don´t reset, you will hace the same IP adress, if the modem reset, the adress will change. If you want to imprive the device, you can add and LCD screen and print the IP adress, i will do it later...

When the device is connect, the pin 11 will write HIGH, and you can go to the browser and write the IP adress, you will see some information in the Serial monitor arrive, that mean that works.

To turn on/off the LED, I use a reserved words, in this case are "pin1on" and "pin1of", you can change the string, but if you change the string, you need to check the number or digits that you use and change the code in the indexOf(" "), the "+2" is where the substring start, and the "+8" is where ends, just change these numbers and make sure that the numbers of the substring match with your string.

Sometimes when you send the string from the browser, you will get some trash, just send it untill works, dont worry, sometime will works at the first try, sometimes will works after 10 tries.

I leave you at the end of this step a webpage where you can check all the AT commands.

El codigo funciona con comandos AT. Lo primero que hace el codigo es imprimir comandos directamente para el ESP8266, y se asegura que el sistema se conecta correctamente, luego obtendras la direccion IP y podras usar un String reservado para prender y apagar el LED.

Primero en el codigo necesitas poner la Red y la clave para conectar el modulo ESP8266 a internet, luego, el codigo se asegura que funcione adecuadamente, si no se conecta despues de 1 minuto resetea el codigo, o puede intentar con los comandos AT en el monitor serial de Arduino.

La primera vez tendras que ver la direccion IP a traves del monitor serial, y en ocaciones tendras basura, si la direccion IP proporcionada no funciona, enve "AT+CIFSR" en el monitor serial, luego, mientras el modem no se resete, mantendras la misma direccion IP, si el modem se resetea, la direccion IP podria cambiar. Si deseas mejorar el sistema, puedes agregar una pantalla LCD e imprimir la direccion IP, personalmente me encargare de eso luego.

Cuando el dispositivo se conecto, se prendera el led del pin 11, entonces podras ir a tu buscador y escribir la direccion IP, veras que el monitor serial recibe informacion, eso significa que funciona.

Para prender y apagar los LEDS, yo uso palabras reservadas, en este paso son "pin1on" y "pin1of", puedes cambiar el string, pero si lo cambias, debes de revisar el numero de letras que usas y cambiar el codigo en indexOf(" "), el "+2" es donde el substring empieza, y el "+8" es donde termina, solo cambia estos numeros y asegurate que el numero de letras del subtring concuerden con tu string.

En ocaciones cuando envies el string desde tu buscador, recibiras basura, no te apures, solo envialo hasta que funcione, en algunas ocaciones funcionara a la primera, en otras funcionara tras el decimo intento.

A continuacion dejare una liga para un documento que muestra os comandos AT disponibles.

https://dl.dropboxusercontent.com/u/307560/ESP8266...

Step 4: Code/Codigo

#include
#define SSID "Totalplay-B054" //WIFI #define PASS "EE92B054" //CONTRASEÑA SoftwareSerial esp8266(3, 2); // RX | TX String Palabra=""; //String String Lectura=""; //Substring int a=0, b=0, c=0, d=0, e=0, g=0;

void setup(){ pinMode(13,OUTPUT); //LED 1 pinMode(12,OUTPUT); //LED 2 pinMode(11,OUTPUT); //LED 3 Serial.begin(9600); esp8266.begin(115200); esp8266.println("AT+RST"); //Reset the ESP8266 Module - Resetea el modulo ESP8266 delay(5000); } void loop() { if(a==0){ esp8266.println("AT+CWMODE=1"); // Turn the module as a receptor - Pone el modulo como receptor if(esp8266.find("OK")){ a=1; Serial.println("DO1"); } else{ Serial.println("NO1"); } } if(b==0 && a==1){ String cmd="AT+CWJAP=\""; // Connect to the Network - Conecta a la red cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; Serial.println(cmd); esp8266.println(cmd); Serial.flush(); delay(5000); if(esp8266.find("OK")){ b=1; Serial.println("DO2"); } else{ Serial.println("NO2"); } } if(c==0 && b==1){ esp8266.println("AT+CIPMUX=1"); //Start multiple connections - Inicia multiples conexiones if(esp8266.find("OK")){ c=1; Serial.println("DO3"); } else{ Serial.println("NO3"); } } if(d==0 && c==1){ delay(1000); esp8266.println("AT+CIPSERVER=1,80"); //Create a server - Crea el servidor if(esp8266.find("OK")){ d=1; Serial.println("DO4"); } else{ Serial.println("NO4"); } } if(d==1 && e==0){ esp8266.println("AT+CIFSR"); //Get the IP adress - Obtiene la direccion IP if (Serial.available()){ char c = Serial.read(); esp8266.print(c); } if (esp8266.available()){ char F = esp8266.read() ; Serial.print(F); } e=1; digitalWrite(11,HIGH); } while (esp8266.available()){ //Get the complete String - Obtiene el String completo char c=esp8266.read(); Palabra = Palabra+c; g=1; Serial.flush(); } if (g==1){ Serial.print(Palabra); Lectura = Palabra.substring(Palabra.indexOf(" ")+2,Palabra.indexOf(" ")+8); //Get the subtring - obtiene el substring Serial.println(); Serial.println(Lectura); //Turn on/off the LED's - Prende y apaga los LED's if(Lectura=="pin1on"){ digitalWrite(13,HIGH); Lectura=""; } if(Lectura=="pin1of"){ digitalWrite(13,LOW); Lectura=""; } if(Lectura=="pin2on"){ digitalWrite(12,HIGH); Lectura=""; } if(Lectura=="pin2of"){ digitalWrite(12,LOW); Lectura=""; } Palabra=""; g=0; } if (Serial.available()){ // Make the serial communication visible whit the Serial monitor - Hace la comunicacion serial visible en el monitor serial char c = Serial.read(); esp8266.print(c); } if (esp8266.available()){ char F = esp8266.read() ; Serial.print(F); } }

Step 5: Thanks/Gracias

If you had some ask, please let me know, and thaks for watch this project and GOOD LUCK!

Si tienes alguna pregunta, porfavor házmelo saber, gracias por ver el proyecto y BUENA SUERTE!

Arduino All The Things! Contest

Participated in the
Arduino All The Things! Contest