Control of light with esp8266 and arduino

The objectives of this practical one are to handle a light, soon to extend it to other loads, from a WIFI module esp8266 and arduino

For it we are going step by step to begin from the control of the port series of arduino, labels AT of the module, the programming and the putting in good condition of the module as Web server, activate-to deactivate loads from Internet

Before home, we must consider the objectives of this practice, that are not other that to introduce us in the Internet of the things, (IOT), where the tendency is to have machines and components more and more connected to Internet. It is a work of development, therefore, IT IS NOT a CLOSED PRACTICE and we will maintain it as a starting point so that, between all, we develop but this exciting field of the technologies of networks.

As the approach is to our cycle of average degree of the is Mare nostrum, we will be explaining some other things that, without being own of the curriculum of the module, are necessary to implement all the system. Hands to the work

1 Port series of arduino.

In the first image, we have represented an old connector series RS232, that contains both terminal of data Rx and Tx. In our arduino also we have those two points shown as terminal 0 and 1 (to see plate).

First that we are going to do it is to install a program in arduino so that, according to the pressed key, LED 13 ignites.

The instructions that are used for the port are:

 - begin (speed)    The port is opened and establishes the speed to the value marked between parenthesis. It varies between 9600bps to 115000bps. For the One, better 9600.

- available ()    We verify the state of the buffer to see if there are data.

- read ()    It reads a single character (byte) of the buffer.

- write ()    It writes so many characters as you want in the buffer.

- print ()    It prints the data to the port series as text ASCII.

- println ()    It prints the data to the port series as print as text ASCII with carriage return

- flush ()    Empty the buffer of entrance of data.

- end ()    For the communication series. When finishing, pins 0 (RX) and 1 (TX) can be used by arduino as digital entrances or exits.

int reading;
int LED = 13;
void setup () \ {
Serial.begin (9600);
pinMode (LED, OUTPUT);
}
void loop () \ {
//Comprobamos if it exists data available and we read them
if (Serial.available () >0) \ {

//leemos the sent option
lectura=Serial.read ();
if (lectura=='1 €²) \ {
digitalWrite (LED, LOW);
Serial.println (dull LED€);
delay (500);
}
if (lectura=='2 €²) \ {
digitalWrite (LED, HIGH);
Serial.println (Ignited LED€);
delay (500);
}}}

 2 Conectar the module ESP 8266 to a network wifi
What we see above is a wonder with a price smaller to 2 ‚¬. It is made up of a microcontroller and 8 connections. We as always have the feeding, the entrance of data (Rx and Tx), two exits GPIO and other pins of configuration.
It is very important, to observe that this chip works to 3.3 volts, therefore, we must use an external feeding to be able to provide the suitable current
 Deserts here, to incorporate a circuit with fritzing that the arduino incorporates, a power supply of 3.3 and the connections Rx and Tx to terminals 2 and 3.
3 Important. Not to do anything until reading all this point. Soon to make the tasks that are indicated.
We must use a specific bookstore for control of this unit and these
 bookstore lowers in this one connection.
This file has name ITEADLIB Arduino WeeESP8266, and is quite complete.
It is necessary to have much taken care of, because once settles, it does not appear in the general folder of libriries, but in a listing a little more down, than indicates as contributed libraries
 In order to locate the bookstore, we must go to the route where normally they settle this type of bookstore (it would be necessary to see the configuration of the program). Usually it is:
 Library - > Documents - > arduino - > libraries
When we accede to her, we must eliminate a commentary to activate a function, as simple as to clear //in the line
#define ESP8266_USE_SOFTWARE_SERIAL
I leave the modified file you, in case you have problem modifying the general bookstore esp8266.h.
I have used a publisher wonder, Lopetedit, that opens until the archives that come from the space€.
Hay a great programmer in Saragossa, that has helped me to realise this practice (Luis Flames), but when lowering its program, we found that it did not work. That first program of verification we have it in his it paginates and he is the one that we are going to discuss.  We find that, when loading the program, if we put SSID, we will see that the program takes it as a reserved variable, which, brings about errors. For that reason, to call to ssid, we called it with another name.
We are already going to load the program. For it, we unloaded the file this file of arduino and to explain the different activities
Explicamos step by step the blocks that conform the program
 5.1 first it is to load some bookstores (3), to form the network wifi of our house and to put the webpage where we are going to have the Dom³tico control of the system. For the page, I have registered myself in hostinget, that offers a very complete pack, but to the gratuitous being, we cannot either demand demasiado.#include <doxygen.h>
#include <ESP8266.h>#include <SoftwareSerial.h>const char* SSIDE = Celi€;
const char* PASSWORD = password of the network Celi€;
const char* HOST_NAME = www.marenostrum.esy.es€; /Webpage
const int HOST_PORT = 80; //connection port. For Web he is the 80
SoftSerial SoftwareSerial (2, 3); //I put ports 2 and 3 for RX, TX
ESP8266 wifi (softSerial); //Communication for the module esp8266
 5.2 Second block of setup
void setup (void)
\ {Serial.begin (9600); //We put the speed from the port to 9600 bauds
pinMode (13, OUTPUT); //our pin 13 to exit
/* Now we will see if we contact with the module ESP 8266. If we are successful, we printed the text to station + softap ok and if we are not successful, is to the text to station + softap err. We see that \ n appears \ r and this is to indicate that is finished and skips of line *
if (wifi.setOprToStationSoftAP ()) \ {
Serial.print (to station + softap ok \ r \ n€);
}
else \ {
Serial.print (to station + softap err \ r \ n€);
}
/* a little step but. Now we are going to verify if we managed to connect with the network wifi that we used. In my case, the call as Celi. As before, if we are successful, is the text we have connected with the network WIFI, next to the IP data. On the contrary, we showed another text, for example: Uff, something happens. We do not have connection 

A little but down we have another verification of wifi.disableMUX. This, according to the documentation of the module, given in http://docs.iteadstudio.com/ITEADLIB_Arduino_WeeESP8266/class_e_s_p8266.html does not indicate that

Disable IP MUX (single connection mode).

In single connection mode, only one TCP or UDP communication dog sees builded.

*
if (wifi.joinAP (SSIDE, PASSWORD)) \ {
Serial.print (we have connected with the network WIFI \ r \ n€);
Serial.print (IP: €);
Serial.println (wifi.getLocalIP () .c_str ());
}
else \ {
Serial.print (Uff, something happens. We do not have connection \ r \ n€);
}
if (wifi.disableMUX ()) \ {//if we have only communication, is OK
Serial.print (single ok \ r \ n€);
}
else \ {
Serial.print (single err \ r \ n€); //is not obtained ¼nica TCP (this in multi TCP)
}
Serial.print (setup end \ r \ n€); //aim of setup
}
 5,3 Loop Bow

void loop (void)
\ {
uint8_t buffer [800] = \ {0}; //we establish the buffer of initial datas to 0

if (wifi.createTCP (HOST_NAME, HOST_PORT)) \ {//if we have TCP connection to the Web and in port 80
Serial.print (TCP Connection ok \ r \ n€); //prints TCP Connection ok
}
else \ {//on the contrary
Serial.print (we showed to error in the connection tcp \ r \ n€); //we showed to error in the connection tcp
}

/* Now is created a variable, call request, that later we are going to enter the module to obtain the reading of the page. The variation that we have added is after the GET/that it indicates to us that we are not going to read the main page, but another one of which we will speak, that is called resultado.php. In this page we are going to keep all the information that we are going to need *

to char *request = GET /resultado.php HTTP/1.1 \ r \ nHost: marenostrum.esy.es \ r \ nConnection: close \ r \ n \ r \ n€;

 

wifi.send ((const uint8_t*) request, strlen (request)); //we sent the request to the page

uint32_t wifi.recv len = (buffer, sizeof (buffer), 10000); /we took the length from the received buffer
if (they len > 0) //if there is reading, (buffer with data)
\ {
Serial.print (received Data of the Web: \ r \ n€); //We showed received Data of the Web

/* Although is not necessary, we are going to explain that he is that of uint32_t, or uint8_t*. This it comes to say how the data are due to try that are received.

  1. The U comes from type unsigned is, that is to say, not defined
  2. the following thing (int) indicates to us that it is a whole value
  3. I number that it accompanies it, 8, 16, 32, etc, makes reference to the used bits
  4. Finally, _t says to us that the size is standard for all the platforms

*

for (uint32_t i = 0; is < len; i++)
\ {
to char c = (to char) buffer [i];

/* to read a concrete part of the information that arrives to us, we have added gusanillo as entrance joker, so that if we got to find that value, we began to create an Array yet the text that is between gusalillo of entrance and exit *

if (c == €˜~€™)
\ {
for (uint32_t j = i + 1; j < len; j++)

/*

here, the departure value of accountant j is the following one to the value detected in gusalillo, that is to say, that if in the previous one of i, we see that gusanillo has position 45, the value of j, will be 46 and from that value, will be taken data *
\ {
c = (to char) buffer [j];
if (c == €˜1€™) //If we detected that there is a 1, we issue order to ignite LED 13
\ {digitalWrite (13, HIGH);}
if (c == €˜0€™) //If he appears the 0, we extinguished LED 13
\ {digitalWrite (13, LOW);}
//else \ {digitalWrite (13, LOW);}
if (c == €˜~€™) break; //if gusanillo is detected, we closed the Array in we printed c
Serial.print (c);
}
break;
}

}

Serial.print (\ r \ n€); //we printed car jump
}

//while (1) delay (1000); //this we have cleared it
delay (1000); //we hoped a second to take the following reading. - In case the thing has changed
}

 Finally, in our test we see that there are times that much work costs to him to connect with the network. It seems that it has to do with the order of entrance of the modules. He is better to ignite the arduino later and unit esp8266. If it is made on the contrary, it gives problems us.
Webpages
The following step is to make our webpage, that will be formed by three archives
  1. index.php. This page contains the action forms. In our case we began with igniting to extinguish of LED. We can see in http://marenostrum.esy.es/
  2.  led.php This page takes the variables sent by index.php to make a recording of data in the data base
  3. db.php. In this file we have the configuration of the data base (data and keys)
  4. resultado.php. This page, whenever we called it, makes a request SQL to the data base, to show the variables that arduino needs to ignite or to extinguish the LED

All of them, we can lower in the CONNECTION WEB to them 

Base of data
As we shelp before, first it is to be created an account in the company to hostinger. Once we have the keys and page URL generated in the discharge process, we go to the administration panel and created the data base mysql. This base is going to have 4 important values, that they are:
  • Where it is. In this case, it comes as Host Mysql, and it says to us that the base is lodged in mysql.hostinger.es. Other times usually it puts localhost
  • How the data base is called.
  • How the user of the data base is called
  • The password of that user

With those 4 data, we cannot connect to the data base and, those data must be kept in the file db.php

We already have the created base, but we do not have the table. For this reason, we must go within the tools of the Control Panel of hostinger, to the basic section of data and phpmyadmin
We enter to create our table, of name data€
Once inside, we punctured in eyelash SQL and we add this code
  • CREATE TABLE to YOU IF NOT EXISTS €˜data €˜(
    €˜you go €˜int (11) NOT NULL,
    €˜LED €˜tinyint (4) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  • INSERT INTO €˜data €˜(€˜you go €˜, €˜LED €˜) VALUEs
    (1, 0);

8 File to record the data (led.php)

If we opened it, we par excellence have the code used in the world Web, the PHP. we happen to comment some cochairs.

Does the promere thing, open with the labels <? php, soon tenth that initiates a session session_start (); €, which is not obligatory, but necessary in our case it happens to create session variables, soon we included the file db.php with the connection data and later a series of line to know if we have been successful in the connection.

In the line if, we see if from the contact form, we received variable $_POST [€˜to ignite€™], that will have the value of igniting if somebody in the form has punctured in that button (soon we will see it).

If yes, the data in the data base is recorded resultado.php€ using laa line

1 $sql = UPDATE data SET led=1 WHERE you go = 1 ;

2 mysqli_query ($link, $sql);

1 is created the text variable $sql with the value that is, saying that the table is updated data putting (SET) registry LED to 1, where (WHERE) you go is 1

2 line says us that we connect to the data base and overturn that text $sql

Furthermore, [LED€ appears $_SESSION] =1. This does not say is created a variable, call $_SESSION [LED€], that is always going to be 1, independent of the fact that it paginates of our project we are. That S-value will be picked up soon by the page of the form to show it in screen.

The same we can say for the rest of the program. The last line of header sends us to the main page, spent two seconds

<? php
session_start ();
include (db.php€);

$link = mysqli_connect ($db_host, to $db_user, $db_pass, $db_name);
//mysqli_set_charset ($link, €˜utf8 €²);
mysqli_set_charset ($link, €˜iso-8859-1 €²);
/* to verify the connection *
if (mysqli_connect_errno ()) \ {
printf (insolvent Connection: %s \ n€, mysqli_connect_error ());
exit ();
}
if ($_POST [€˜to ignite€™] ==€ to ignite€)
\ {
$sql = UPDATE data SET led=1 WHERE you go = 1 ;

mysqli_query ($link, $sql);

I throw We ignite the LED€;
$_SESSION [LED€] =1;
}

if ($_POST [€˜to extinguish€™] ==€ to extinguish€)
\ {
$sql = UPDATE data SET led=0 WHERE you go = 1 ;

mysqli_query ($link, $sql);
I throw We extinguished the LED€;
$_SESSION [LED€] =0;
}

to header (refresh: 2; url= http://marenostrum.esy.es/€);
>

 

But info enhttps://www.luisllamas.es/arduino-wifi-esp8266-esp01/