Automation With ESP8266 Using Relays

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects. New tutorials on Tuesdays and Fridays.

Today, we are going to program the ESP8266 with the Arduino IDE. Let's set up a server to turn on and off the device. In this case, we are using a circuit with two relays.

In this circuit, we will use the ESP8266 with Wi-Fi. We will also use the GPIO connectors, and we'll specifically show ESP01.

Step 1: Assembly

The circuit we assembled is very simple and results in a very compact assembly. We use a polystyrene plate in the area behind to keep everything in place. This plate is also used to assist the fitting inside the power box, and for avoiding exposure to the assembly, as this can be used to control various devices in a residence, such as air conditioning, lamps, among others.

Then, we use a switched power supply, which I turn from 110 or 220 volts to 5 volts. We also have a 3v3 voltage regulator, the AM1117. We took two GPIOs and plugged in the relay board input. It is important to remember that with ESP8266, we have to take some precautions, like grounding the pins.

Step 2: ESP8266 in the Arduino IDE

It is important to remember that when you are programming ESP8266, you need to load the libraries of this device into the Arduino. For this, you should be with the IDE in version 1.6.4. Now go to the preferences and "Additional Board Manager URLs" and add the url: http://arduino.esp8266.com/stable/package_esp8266com_index.json

Then, go to Tools>Boards>Boards Manager...

In the search, enter esp8266 and install the package "esp8266 by ESP8266 Community".

Now, you can select your ESP8266 from the list of cards

*** In today's installation, the ESP866 will be a server. You will therefore pick up your smartphone, and it will connect in the IP of the device, meaning that you can access and it will serve a page for you.

In the video, you can see a demonstration of how this works.

Step 3: Source Code

The first step is to include the lib that allows us to control the ESP8266 WiFi. After that, we will create a variable that will save a reference to our server that will run on port 80. The reason we chose port 80 is that this is the default port for the http protocol, and we will use a browser to connect to the module.

//Includes the lib for Wifi
#include <ESP8266WiFi.h>
//Creates a server on port 80 (this is the default port for http requests)
WiFiServer server(80);

Step 4: Setup

In the setup, we will initialize the Serial only so that we have a log using the serial monitor.

We will use GPIO0 and GPIO2 as OUTPUT and initialize the initial state with LOW.

void setup()
{
  //Initializes the Serial just for logging
  Serial.begin(115200);
//Sets GPIO0 and GPIO2 as output, so we can change their value
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
//Puts the GPIO0 and GPIO2 in LOW output
  digitalWrite(0, LOW);
  digitalWrite(2, LOW);

We now call WiFi.begin ("ssid", "password") to connect the ESP8266 to the router. In the example, we have the ssid "TestESP" and the password "87654321", but you must replace this with the network that you will use.

Serial.print("Connecting");
//Connects to your WiFi network. In this example the SSID is TestESP and the password is 87654321
WiFi.begin("TestESP", "87654321");

We will check every 100 milliseconds to see if the ESP8266 has connected to the network (it returns WL_CONNECTED status when it is connected).

When you leave the "while", it means
that you have connected.
//While our ESP is trying to connect
while (WiFi.status() != WL_CONNECTED)
  {
    //Waits for 100 milliseconds
    delay(100);
    Serial.print(".");
  }

//Here it's already connected, so we'll just show a feedback on Serial Monitor
Serial.println("");
  Serial.println("Connected");

This is where we put the network settings. The IP, gateway, and mask settings must be changed according to your network.

//Settings for static ip<br>  IPAddress ip(192, 168, 2, 8);
  IPAddress gateway(192, 168, 2, 1);
  IPAddress subnet(255, 255, 255, 0);
  Serial.print("Static IP is: ");
  Serial.println(ip);
//Sends the settings to the WiFi router
  WiFi.config(ip, gateway, subnet);

Now, we can initialize the server and view on the serial monitor if the IP that is linked to the ESP8266 is the same one that we configured. This is the end of setup.

//Starts the server we created on port 80<br>  server.begin();
//Shows the IP for the server
Serial.print("Server is on: ");
  Serial.println(WiFi.localIP());
}

Step 5: Loop

In the main loop of the program, we check if any clients are trying to connect, and if so, we wait until they return their request.

void loop()
{
  //Checks if there is any client trying to connect
  WiFiClient client = server.available();
  if (!client)
  {
    //If there isn't, we just return
    return;
  }
Serial.println("New Client Connected!");

We store the request in the variable "req" to later know what action the client wants to be executed.

//Reads the request<br>  String req = client.readStringUntil('\r');
  Serial.print("Request: ");
  Serial.println(req);

Finally, we close the connection with the client. This completes the loop and the code.

//Closes the connection
client.stop();
  Serial.println("Client disconnected!");
}

Testing

To test, just open the browser and enter the ip that will appear on the serial monitor. Click on the actions and see if the corresponding GPIO is changing.

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    Discussions