Demo 14: How to use MQTT and Arduino ESP32 to build a simple Smart home system - IoT Sharing

Hot

Tuesday, May 23, 2017

Demo 14: How to use MQTT and Arduino ESP32 to build a simple Smart home system

1. Introduction
- Currently, there are many IoT protocols such as: CoAP, MQTT, AMQP, … In this tutorial, I will introduce MQTT, one of the famous IoT protocols. This protocol is to control and transfer data between devices in an IoT network
Note: for MQTTS please refer Demo 30: How to use Arduino ESP32 MQTTS with MQTTS Mosquitto broker (TLS/SSL).
MQTT is stand for Message Queuing Telemetry Transport. It has some features:
+    Use Publish/Subscribe/Topic mechanism
+    Lightweight protocol
+    Small code footprint
+    Build on top of the TCP/IP protocol
+    Less network bandwidth.
- The principal of MQTT is traditional Client-Server model. In this model, there is one MQTT Server (also called Broker) and many MQTT Clients. The MQTT Clients always keep connection with MQTT Server. The role of MQTT Server (broker) is to filter and forward the messages to subscribed MQTT Clients. The communication between clients is based on Publish/Subscribe/Topic pattern in which:
+ Message: has a topic.
+ Publish: sending the messages to network.
+ Subscribe: listening messages that contain topic that the client is interested in.
+ Broker: coordinating the communication between publishers and subscribers.
- Topic is an utf-8 string and has one - many levels which is separated by splash "/". For example: "floor1/room1/temp": this topic has 3 levels, human readable and easy to understand (we have floor 1 and in room 1 with temperature sensor). You can refer:
http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices
- Beside that, there are other concept that you need to know:
*QoS (Quality of Service): this indicator perform the guaranty of message exchange between sender and receiver. There are 3 levels:
+ QoS 0 - at most once (this level is the fastest, but not reliable)
+ QoS 1 - at least once (this is the default mode)
+ QoS 2 - exactly once (this level is the most reliable, but slowest)
You can refer at: 
*Retained Messages:  broker will keep the sent message so that when there is new subscriber that subscribe the topic that matches the retained message then that message will be sent to that subscriber.
- Most of MQTT libraries define some standard methods such as:
+    Connect(): connect to MQTT server.
+    Disconnect(): disconnect from MQTT server.
+    Subscribe(): subscribe a topic with MQTT server.
+    UnSubscribe(): unsubscribe a topic with MQTT server
+    Publish(): client publish a topic to network.
- For demo, we create a simple smart home network that have 3 client nodes (Smart phone, WiFi MCU with temperature sensor, WiFi MCU with LED/bulb controller) and 1 server node as a broker (PC or Raspberry Pi). In our application, we want to use smart phone to monitor the temperature and control the LED/bulb on or off.  So we design the MQTT model like below:
Figure: MQTT model for simple smart home application
2. Hardware
- To implement the model above, I will collect Node2 and Node3 into one node and this node is our ESP32 with DHT22 sensor and LED (bulb). Finally, we have 2 nodes: SM node and ESP32 node. We re-use the hardware schematic of Demo 13: How to display temperature/humidity using Google Chart/Jquery and control LED through Arduino ESP32 Web Server. In this demo we do not use microSD so please ignore it.
- Connections:
Connect VCC and GND of DHT22 (VCC=3.3V) to VCC (Vcc=3.3V) and GND of ESP32.
[ESP32 IO15 - DHT22 DATA]
[ESP32 IO2 - LED ANODE]
[ESP32 GND - LED CATHODE]
Figure: esp32 + dht22 + LED for MQTT smart home demo
3. Software
3.1 MQTT Client side

SM node: I will use an Android Smartphone with a MQTT client application (IoT MQTT Dashboard) that is available on Google Play. You can download it here:
https://play.google.com/store/apps/details?id=com.thn.iotmqttdashboard
ESP2 node: I will use a MQTT client library (Pubsubclient). You can download it here:
https://github.com/knolleary/pubsubclient
Then unzip the downloaded file and copy it to Arduino/libraries folder.
- This library supports some standard functions that are mentioned above. To use these function we create an instance PubSubClient client(wifiClient). Because MQTT is built on top of the TCP/IP protocol so the input of this constructor is a TCP WiFiClient object.
3.2 MQTT server side
- I will use a popular MQTT server called Mosquito. You can download and install it here:
- Windows user
http://www.eclipse.org/downloads/download.php?file=/mosquitto/binary/win32/mosquitto-1.4.11-install-win32.exe
After finishing, from command line just run this command to start mosquito server: "mosquitto"
-  Ubuntu user:
From command line type the command below: 
sudo apt-get install mosquitto mosquitto-clients
This will install mosquito as a service. You can check whether the service is start or not by using command line: 
sudo service --status-all 2>&1 | grep mosquitto
-  Other OS, just follow:  https://mosquitto.org/download/
Note: when we install mosquito, it also install 2 client programs called "mosquitt_sub" and "mosquito_pub" that we can use for debugging. 
For example:
- To monitor all topic on network using:
mosquitto_sub -v -h broker_ip -p 1883 -t "#" (change broker_ip to mqtt server ip)
- To publish a topic (publish topic room1/temp with value 30) using:
mosquitto_pub -t 'room1/temp' -m 30
 3.3 Assign roles
We define topics: 
-  Topic1: smarthome/room1/bulb #value : value can take 0 or 1 means on/off the LED (bulb).
- Topic2: smarthome/room1/temperature #value : value can take float number to express temperature.
Example: smarthome/room1/temperature 30 
With these topics SM node can subscribe Topic2 and publish Topic1. ESP32 node can publish Topic2 and subscribe Topic1.
3.4 Steps to run the system
- Start the MQTT server (on Wins invoke it manually, on Linux it is a service so just check the service is started)
- From Terminal run this: mosquitto_sub -v -h broker_ip -p 1883 -t '#' for debugging. You will see all the messages on the network.
- Create an Arduino project and Save as esp32mqtt with code:

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Here ESP32 will keep 2 roles: 
1/ read data from DHT11/DHT22 sensor
2/ control led on-off
So it willpublish temperature topic and scribe topic bulb on/off
*/

#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

/* change it with your ssid-password */
const char* ssid = "dd-wrt";
const char* password = "0000000000";
/* this is the IP of PC/raspberry where you installed MQTT Server 
on Wins use "ipconfig" 
on Linux use "ifconfig" to get its IP address */
const char* mqtt_server = "192.168.1.103";

/* define DHT pins */
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float temperature = 0;

/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);

/*LED GPIO pin*/
const char led = 12;

/* topics */
#define TEMP_TOPIC    "smarthome/room1/temp"
#define LED_TOPIC     "smarthome/room1/led" /* 1=on, 0=off */

long lastMsg = 0;
char msg[20];

void receivedCallback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message received: ");
  Serial.println(topic);

  Serial.print("payload: ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  /* we got '1' -> on */
  if ((char)payload[0] == '1') {
    digitalWrite(led, HIGH); 
  } else {
    /* we got '0' -> on */
    digitalWrite(led, LOW);
  }

}

void mqttconnect() {
  /* Loop until reconnected */
  while (!client.connected()) {
    Serial.print("MQTT connecting ...");
    /* client ID */
    String clientId = "ESP32Client";
    /* connect now */
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      /* subscribe topic with default QoS 0*/
      client.subscribe(LED_TOPIC);
    } else {
      Serial.print("failed, status code =");
      Serial.print(client.state());
      Serial.println("try again in 5 seconds");
      /* Wait 5 seconds before retrying */
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  /* set led as output to control led on-off */
  pinMode(led, OUTPUT);

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  /* configure the MQTT server with IPaddress and port */
  client.setServer(mqtt_server, 1883);
  /* this receivedCallback function will be invoked 
  when client received subscribed topic */
  client.setCallback(receivedCallback);
  /*start DHT sensor */
  dht.begin();
}
void loop() {
  /* if client was disconnected then try to reconnect again */
  if (!client.connected()) {
    mqttconnect();
  }
  /* this function will listen for incomming 
  subscribed topic-process-invoke receivedCallback */
  client.loop();
  /* we measure temperature every 3 secs
  we count until 3 secs reached to avoid blocking program if using delay()*/
  long now = millis();
  if (now - lastMsg > 3000) {
    lastMsg = now;
    /* read DHT11/DHT22 sensor and convert to string */
    temperature = dht.readTemperature();
    if (!isnan(temperature)) {
      snprintf (msg, 20, "%lf", temperature);
      /* publish the message */
      client.publish(TEMP_TOPIC, msg);
    }
  }
}
- From Android smart phone, open IoT MQTT Dashboard) and follow steps below to set up it a MQTT client:
Figure: Configure MQTT Server that it will connect to
Figure: After configuring server, choose it 
Figure: Choose Subscribe tab and create topic temp
 Figure: Choose Publish tab to create a Switch for toogling LED
 Figure: Fill topic led for Switch
Figure: After finishing, here is the GUI for Publish tab, one Switch
 Figure: You can see smart phone received temp topic 
4. Result 

16 comments:

Anonymous said...

I appreciate your tutorial, yet there are some mistakes that should be clarified, the most obvious being your statement:
"Topic: the message that is transferred on network"
A topic by far is not a message itself but rather a category for a message.
The client is never sending topics but sending messages that have a / a classified for a specific topic.
You should possibly consider a topic string as part of a message object from a sending client perspective.

iotsharing dotcom said...

Thank you very much, I updated it. I just want to express it in a general way so that users are easy to understand. :)

iftikhar jatoi said...

good effort...!very useful

Ubi Bot said...

IOT Based products changed the world. I would like to share this informative article with https://www.ubibot.io/ team.

sheela rajesh said...

Such a great information are there in your post and this will guide more people.
JAVA Training in Chennai
JAVA Training in Velachery
Software testing training in chennai
Android Training in Chennai
Selenium Training in Chennai
Hadoop Training in Chennai
JAVA Training in Chennai
Java Training in Tnagar

Sivanandhana Girish said...

Thanks to the admin for sharing this blog as I am able to get an idea of more updates in a clear way.
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Mumbai
Best IELTS Coaching in Mumbai
IELTS Center in Mumbai
IELTS Classes in Chennai
Best IELTS Coaching Centre in Chennai
IELTS Centre in Chennai
IELTS Training Centre in Chennai
IELTS Course in Chennai

lekha mathan said...

Excellent post gained so much of information, Keep posting like this.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
Aviation Courses in Chennai
Air hostess training in Chennai
Airline Courses in Chennai
airport ground staff training courses in Chennai

sandeep saxena said...

Wow,great information. I am sure the info on your blog will help others,Thanks.
C C++ Training in Chennai
C Training in Chennai
C++ Training in Chennai
C C++ Training in Velachery
core java training in chennai
javascript training in chennai
SAS Training in Chennai
QTP Training in Chennai

Aruna Ram said...


Really this is very superb! I like the post and the explanation is very great. Keeping your great work.
Primavera Training in Chennai
Primavera Course in Chennai
Corporate Training in Chennai
Embedded System Course Chennai
Oracle Training in Chennai
Tableau Training in Chennai
Spark Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai

priyanka usha said...

A good blog for the people who really needs information about this.
Good work keep it up.
TOEFL Coaching in Chennai
TOEFL Classes in Chennai
German Language Classes in Chennai
IELTS Training in Chennai
Japanese Language Course in Chennai
spanish language course in chennai
TOEFL Coaching in Porur
TOEFL Coaching in Adyar
German classes in anna nagar
spoken english class anna nagar

Sadhana Rathore said...

Your blog is interesting to read, thanks for sharing this and keep update your blog regularly.
PHP Training in Chennai
PHP Course in Chennai
PHP classes in Chennai
Angular 6 Training in Chennai
Web Designing course in Chennai
Salesforce Training in Chennai
PHP Training in Porur
PHP Training in Tambaram
PHP Training in Adyar

Zuan said...

Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
mobile application development training online
mobile app development course
mobile application development training
mobile app development course online
mobile application development course
online mobile application development
learn mobile application development

Chris Hemsworth said...

The article is so informative. This is more helpful for our
Learn best software testing online certification course class in chennai with placement
Best selenium testing online course training in chennai
Best online software testing training course institute in chennai with placement
Thanks for sharing.

adhi said...

thanks for your information really good and very nice web design company in velachery

daizy mathew said...

Thanks for sharing this useful information.
python training in chennai

daizy mathew said...

Thanks for sharing this useful information
php training in chennai

Thường mất vài phút để quảng cáo xuất hiện trên trang nhưng thỉnh thoảng, việc này có thể mất đến 1 giờ. Hãy xem hướng dẫn triển khai mã của chúng tôi để biết thêm chi tiết. Ðã xong