Daniel Alome
8 min readDec 10, 2018

IoT with Arduino

“IoT is transforming the everyday physical objects that surround us into an ecosystem of information that will enrich our lives. …

According to wikipedia, IoT (Internet of things ) is the network of devices, vehicles, and home appliances that contain electronics, software, actuators, and connectivity which allows these things to connect, interact and exchange data.

With these technology, you can do a varieties of cool stuffs at the tip of your fingers ! Can you imagine how cool it would be if you can just pick up your smart phone and send a tweet to your “pot” at home to start preparing your Favourite meal while you are on the way home ! Cool right ? yeah i know that’s pretty cool !

courtesy : Alex Knight @ Unsplash

IoT is a very amazing topic, so guys we are going to start learning about it and how we can build real life IoT devices. So today, will’ll learn how to create a simple Home Automation system Using the Arduino development board. First, if you’re not familiar with the arduino board, you can click here to get started.

For this simple project, will’ll need the following supplies :

  1. Arduino UNO R3 1X
  2. ESP8266 wifi Module 1X
  3. FTDI programmer 1X
  4. A USB capble ( male to male peripheral type{ the ones mostly used in printers} )
  5. A computer.

if you stay in Lagos, you can get all the supplies here

Software Needed :

  1. Ardunio IDE — you can get that here.

Step 1.

Preparing the IDE

After installing the IDE, you need to set up the ESP8266 “add-on ” for the IDE to be able to communicate with your Wifi module. To do that in the IDE, navigate to File → preferences, then Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into the “Additional Board Manager URLs” field as shown in the figure below. Then, click the “OK” button.

4. Go to Tools > Board > Boards Manager

5. Scroll down, select the ESP8266 board menu and install “esp8266 platform”, as shown in the figure below.

6. Go to Tools > Board and choose your ESP8266 board. Then, re-open your Arduino IDE.

Code

The arduino board can be programmed with many languages , like javascript(J5), python, C++ ETC, the arduino team also created a language based on C++ called the Arduino Sketch which basically looks like C++. So for the purpose of this leson, will’be working with C++, if you’re not familiar with C++, here is a place to get started https://www.tutorialspoint.com/cplusplus/

First we need to initialize our ESP8266 by including the library

//including the wifi module library to enable the board communicate with it
#include <ESP8266WiFi.h>

Next we need to create two variables which will hold our WIFI credentials like this ( dont forget to replace your SSID and password with your WIFI network credentials !):

// Replace with your network credentials, for more info check this out  : https://www.esp8266.com/viewtopic.php?f=33&t=6603
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output5State = "off";
String output4State = "off";

// Assign output variables to GPIO pins
const int output5 = 5;
const int output4 = 4;

Next we initiate the setup():

void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output5, OUTPUT);
pinMode(output4, OUTPUT);
// Set outputs to LOW
digitalWrite(output5, LOW);
digitalWrite(output4, LOW);

// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}

Next, we handle the server (U.I and client responses):

void loop(){
WiFiClient client = server.available(); // Listen for incoming clients

if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();

// turns the GPIOs on and off
if (header.indexOf("GET /5/on") >= 0) {
Serial.println("GPIO 5 on");
output5State = "on";
digitalWrite(output5, HIGH);
} else if (header.indexOf("GET /5/off") >= 0) {
Serial.println("GPIO 5 off");
output5State = "off";
digitalWrite(output5, LOW);
} else if (header.indexOf("GET /4/on") >= 0) {
Serial.println("GPIO 4 on");
output4State = "on";
digitalWrite(output4, HIGH);
} else if (header.indexOf("GET /4/off") >= 0) {
Serial.println("GPIO 4 off");
output4State = "off";
digitalWrite(output4, LOW);
}

// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");

// Web Page Heading
client.println("<body><h1>ESP8266 Web Server</h1>");

// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>GPIO 5 - State " + output5State + "</p>");
// If the output5State is off, it displays the ON button
if (output5State=="off") {
client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
}

// Display current state, and ON/OFF buttons for GPIO 4
client.println("<p>GPIO 4 - State " + output4State + "</p>");
// If the output4State is off, it displays the ON button
if (output4State=="off") {
client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");

// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}

Thats it ! for the complete code click here: https://we.tl/t-cTfrhKUgPn

Uploading the Sketch

Plug your board to your computer. Make sure you have the right board and COM port selected.

Then, click the “Upload Button” in the Arduino IDE and wait a few seconds until you see the message “Done uploading.” in the bottom left corner.

Uploading Sketch to the ESP8266

Uploading code to the ESP8266 requires establishing a serial communication between your ESP8266 and a FTDI Programmer as shown in the schematic diagram below.

The following shows the connections you need to make between the ESP8266 and the FTDI programmer.

ESP8266 — — — — — — — — FTDI

RX — — — -TX

TX — — — RX

CH_PD — — — -3.3v

GPIO 0 — — — GND

VCC — — — -3.3v

GND — — — — GND

If you have a brand new FTDI Programmer and you need to install your FTDI drivers on Windows PC, visit this website for the official drivers. Alternatively, you can contact
the seller that sold you the FTDI Programmer.

Then, you just need to connect the FTDI programmer to your computer, and upload the code to the ESP8266.

Step 5: Testing the Webserver

Don’t forget to check if you have the right board and COM port selected, otherwise you’ll get an error when trying to upload. Open the Serial Monitor at a baud rate of 115200.

Finding the ESP IP Address

Press the ESP8266 RESET button, and it will output the ESP IP address on the Serial Monitor similar to this :

Welldone ! you’re almost done !. Now copy the ip address because you will need it to access the server from your browser !

Final Step :

If everything went well you should see something similar to this when connected to the ip address of Your ESP8266:

Congratulations !!!, you have just set up your very on Arduino Web Server !

Now testing if your Automation really works

Connect to LED ton your ESP8266 pin GPIO 4 and GPIO 5 like this schematic

Go to your browser, load the IP address of your ESP8266 and hit the ON button and Watch the action ! If an LED turns on , then your home automation works !!!

Wrapping Up

with these cool gadget, you can set up a simple browser controlled appliances like your T.V, fans and lights !

If you have any question or suggestions please feel free to drop a message on facecebook @Daniel Alome .

THANKS FOR READING