A Simple DIY Smart Home IOT Project

In the previous article we connected Arduino UNO with ESP8266-01 and tried various AT commands to create an Access Point, making a connection to a Wifi Network, listening to the TCP port and passing message back and forth with AT commands. That was all basic tasting water, in the post we will step up the game and we will read sensor data from Arduino and send it to a central server and then will display that sensor data on the mobile client.

For the purpose of demonstration, we will use DHT11 temperature and humidity sensor and will send temperature and humidity data to the central server. For the server and mobile client, we will use Blynk which is has a server which collects sensor data from various devices and there is also Blynk mobile client which you can create a dashboard with drag and drop interface and play around with visualization of the sensor data.

#Components required:

  1. Arduino Mega
  2. DHT11 sensor
  3. ESP8266-01
  4. bread board
  5. Couple of jumper wires

#Flow of the data

Once we get the big picture of the system it will be easy to understand all the moving parts. Arduino will read data from the DHT11 sensor and send temperature and humidity data to Blynk server. We need to create an application in a Blynk mobile app to get an Auth Token which will be used to authenticate the connected device(Arduino). Arduino will send data to the central server at regular interval (say every 3 sec), this is the push model where the sensor pushes the data to the server, we could also use the pull model where the server will ask the device for the data on demand, for example, we only want to read the sensor data when the mobile client wants to read the data not interested otherwise. We will use push model in this article. Blynk also has Arduino library which will send data to the server in an efficient manner which I will describe shortly.

#Setting up central server

You cloud use Blynk hosted cloud server or you cloud setup your own, I create my own server is not really that much of a headache and plus you don’t compromise your privacy by sending your data to cloud. Just follow the step below.

  1. Download the server-.jar in a folder

  2. Paste the below configuration in server.properties file, place the config file in the same folder in which you downloaded the server-.jar

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    https.port=9443
    http.port=8080
    data.folder=.tmp/blynk
    log.level=trace
    enable.raw.data.store=true
    admin.rootPath=/admin
    allowed.administrator.ips=0.0.0.0/0
    admin.email=admin@blynk.cc
    admin.pass=admin
    hardware.default.port=8442
    hardware.ssl.port=8441
    server.ssl.cert=./server_embedded.crt
    server.ssl.key=./server_embedded.pem
    server.ssl.key.pass=pupkin123
    enable.raw.data.store=true
  3. Run the server with command shown in below

    1
    java -jar server-0.25.3.jar -dataFolder .path -serverConfig server.properties

#Setting up the mobile client and getting the Auth Token

If you are using your own server then you have to configure the Blynk mobile client to point to your server, or if you are using Blynk’s cloud server the simply sign up and create an app, auth token will be sent to you email address.

Follow below steps to create a mobile app, steps are valid irrespective of, you host your own server or using the Blynk cloud.

  1. Create the application, choose device type as Arduino Mega and Connection type as WIFI

    Create New Project

    Choose Device type and Connection Type

  2. Get the Auth token by login into your local server by visiting this address https://127.0.0.1:9443/admin, or you could copy if from project setting of your mobile client and in case you are using Blynk cloud you must have got your auth token from in your email account.

#Connecting Arduino to central server

Below is the pin dialog of the Arduino setup follow it carefully and double check it, I often make mistake in this part of the project.

Upload this sketch to Arduino Mega we are using hardware serial1 for the communications with ESP8266-01, so be careful of the TX/RX pins of ESP8266 connecting to Arduino

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// this is to prints debug logs on serial console
#define BLYNK_PRINT Serial
// import the dht sensor library
#include <dht.h>
dht DHT;

// data pin of the DHT11 as per the pin diagram
#define DHT11_PIN 31

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "<AUTH TOKEN>";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "<WIFI_WHATEVER>";
char pass[] = "<MY_PASSWORD>";

#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);
BlynkTimer timer;

void myTimerEvent()
{ //read the temperature and humidity from the sensor
int chk = DHT.read11(DHT11_PIN);
// send temperature data on Virtual pin 3
Blynk.virtualWrite(3, DHT.temperature);
// send humidity data on Virtual pin 4
Blynk.virtualWrite(4, DHT.humidity);
}

void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(1000);
// I am host my own that local copy of blynk server
Blynk.begin(auth, wifi, ssid, pass, "<IP address of blynk server>", 8442);
// send data every 5 seconds
timer.setInterval(5000L, myTimerEvent);
}

void loop()
{
Blynk.run();
timer.run();
// avoid delay() function!
}

You must have noticed the ‘myTimerEvent’ function which reads sensor data and send to the virtual pins, one must be thinking I could have done it in ‘loop’ function but doing that would flood the server, and the server would start dropping the data which is bad. ‘loop’ function run forever and we cannot predict at what interval data is sent to the server so Blynk provides a nice way of doing it by registering a timer function which is called at configured interval.

If you are using your own Blynk setup then, your Arduino should be connecting to the same WiFi network on which your Blynk server resides.

If you have made the connections properly and uploaded the code, next open up the serial console and set the baud rate to 9600 from the drop down in the bottom corner of the console and then you should be able to see the debug logs as shown below.

1
2
3
4
5
6
7
8
9
10
[999]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.4.8 on Arduino Mega

[1585] Connecting to ARPANET
[2606] ESP is not responding
[10081] Ready (ping: 20ms).

#Setup Mobile client dashboard

Creating dashboard is very simple, you just have to drag and drop the widgets and configure which from which pin the widgets will show the data. In our case temperature data is sent on Blynk Virtual pin 3 and humidity data is sent on virtual pin 4.

Virtual pin is the concept created by Blynk in their software ecosystem. It’s nothing complicated we just binding a particular data to a virtual pin in our case temperature to pin 3 and humidity to pin 4.

Follow the below steps to create the dashboard:

  1. Open Widgets menu from the plus icon from the top left of the screen

  2. Add Widgets from the menu and make sure you properly configure them as shown below
    Temprature Gauge Widget

    Humidity Gauge Widget

    History Graph Widget : this Widget will show history of both temprature and humidity both in one graph time axis can be scaled.

    Button Widget : Arduino Mega has a on board led which can be controlled from digital PIN 13

  3. Final dashboard: once you have configured all the widgets properly then click the play button on the top right corner of the app and see the dashboard in action. You can also see the device status on the top which shows if the device is online or when it was last online.

#Conclusion

We successfully connected sensor with the Arduino and managed to send sensor data to the central server via ESP8266 and show it on mobile client with very few lines of code. You can also extend this project to add more sensor and add more widgets in mobile client, Blynk’s mobile client has very pretty and easy to use interface with which you can also create rules to trigger some actions like send tweets/email or send high/low signal on other Arduino pins when certain conditions are meet.

Share