You are on page 1of 14

DEPARTMENT OF ELECTRICAL ENGINEERING

DEC50132 - INTERNET BASED CONTROLLER

PROGRAMME : DTK
PRACTICAL WORK : 5
TITLE : DHT11 MONITORING VIA MQTT
LECTURER NAME : MOHD. ZAKI DOI
DATE :

# GROUP MEMBERS REGISTRATION NO.


S1 NUR IMAN BATRISYIA BINTI MOHAMAD KHAIRY 16DTK21F2007
S2 SADARAHTUL NAJWA BINTI SHAMSOL 16DTK21F2003
S3 SYUHAIBA AMNI BINTI SAIFUL ANUAR 16DTK21F2005
S4 SWETHA A/P CHANDREW 16DTK21F2997

SCORE
PRACTICAL SKILL (CLO2, PLO5, P4) ATTAINMENT LAB REPORT ATTAINMENT
S1  Results 
1) Able to connect the circuit S2  Discussion 
independently. S3  Conclusion 
S4 
S1 
2) Able to identify relevant pin and S2 
suitable identifier/ variable
independently. S3 
S4 
S1 
S2 
3) Able to code independently.
S3 
S4 

ASSESSMENT SCORE WEIGHTAGE


Practical Skill (PS) @ 70% Lab Report (LR) @ 30% Total (100%)
S1 S1
S2 S2
PS = Score / 15 * 70 LR = Score / 15 * 30
S3 S3
S4 S4

PRACTICAL SKILL RUBRIC

1/9
Score Description
5 Student can complete all tasks assigned WITHOUT errors
4 Student can complete all tasks assigned with A FEW errors
3 Student can complete all tasks assigned with MORE errors
2 Student can complete partial tasks assigned WITHOUT errors
1 Student can complete partial tasks assigned with A FEW errors

LAB REPORT RUBRIC

Excellent Very Good Good Fair Unsatisfactory


Report Component
5 4 3 2 1
Professional
looking and
Accurate
Results accurate
Accurate representations
 Results in the representation
representation of the data in Incomplete Data are not
form of data, of the data in
of the data in written form, result, major shown OR are
calculation, tables and/or
tables and/or but no graphs mistakes. inaccurate.
waveform, graphs. Graphs
graphs. or tables are
graph etc. and tables are
presented.
labelled and
titled.
Analysis / All point of Some points of
Most points of Some points of
Discussion discussion on discussion on Very few points
discussion on discussion on
 Ability to the results results obtained of discussion,
results obtained results obtained
present, obtained covered and but not properly
covered and covered and
interpret and covered and not properly elaborated.
elaborated. elaborated.
analyse result. elaborated. elaborated.

Conclusion
includes
Conclusion The closing
whether the
 Provide answers The closing The closing paragraph do
findings
to objectives paragraph paragraph not attempt to
supported the No conclusion
stated summarizes and attempts to summarize the
hypothesis, was included in
earlier. draws a summarize but experiment OR
possible sources the report.
 Ability to learn sufficient draws a weak shows little
of error, and
something from conclusion. conclusion. effort and
what was
the experiment. reflection.
learned from
the experiment.

DHT11 MONITORING VIA MQTT

2/9
Objectives
Upon completion of this practical work, students should be able to:
1. Write code to established internet connection (MQTT Client).
2. Write code to publish and subscribe MQTT.
3. Apply writing simple IoT application for monitoring sensor reading.
Equipment
1. Computer with Node-RED installed.
2. Android phone with IoT MQTT Panel app.
3. Internet connection (access point dedicated to the laboratory).
Procedure
Part A - ESP32 and DHT11 Circuit.

1. Connect the circuit below:

Circuit

Part B - Install Required Libraries (AsyncMQTT_Generic and AsyncTCP).

3/9
1. Open Arduino IDE and click on Library Manager.
2. Search for “asyncmqtt” and install AsyncMQTT_Generic.

3. Check if AsyncTCP is already installed when Step 2 is completed. If not, go to Step 3.


4. Search for “asynctcp” and install AsyncTCP (this step can be skipped if it is already completed in Step 2).

Part C - Node-RED Dashboard Setup.

1. Start Node-RED.

2. Insert Node-RED nodes to make a flow below:

4/9
Node-RED

Part D - Monitoring DHT11 Readings via Node-RED.

1. Open Arduino IDE and type in these codes.

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

//replace “X1” and “X2” with your network credentials


#define WIFI_SSID "X1"
#define WIFI_PASSWORD "X2"

//MQTT broker
#define MQTT_HOST "broker.hivemq.com"
#define MQTT_PORT 1883

5/9
//MQTT Topics
#define MQTT_PUB_TEMP_DHT "lab5/esp32/dht/temperature"
#define MQTT_PUB_HUM_DHT "lab5/esp32/dht/humidity"

#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

float temperature_DHT, humidity_DHT; //variables for DHT

AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;

unsigned long previousMillis = 0;


const long interval = 10000;

void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}

void WiFiEvent(WiFiEvent_t event) {


Serial.printf("[WiFi-event] event: %dn", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0);
xTimerStart(wifiReconnectTimer, 0);
break;
}
}

void onMqttConnect(bool sessionPresent) {


Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {


Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}

void onMqttPublish(uint16_t packetId) {


Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);

6/9
}

void setup() {
Serial.begin(115200);
Serial.println();

dht.begin();
delay(1000);

mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE,


(void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE,
(void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

WiFi.onEvent(WiFiEvent);

mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectToWifi();
}

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

//Read from DHT


humidity_DHT = dht.readHumidity();
temperature_DHT = dht.readTemperature();

if (isnan(temperature_DHT) || isnan(humidity_DHT)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

// Publish an MQTT message on topic lab5/esp32/dht/temperature


uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP_DHT, 1, true,
String(temperature_DHT).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i",
MQTT_PUB_TEMP_DHT, packetIdPub1);
Serial.printf("Message: %.2f \n", temperature_DHT);

// Publish an MQTT message on topic lab5/esp32/dht/humidity


uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM_DHT, 1, true,
String(humidity_DHT).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ",
MQTT_PUB_HUM_DHT, packetIdPub2);
Serial.printf("Message: %.2f \n", humidity_DHT);
}
}

2. Verify the codes and upload it to your ESP32.


3. Deploy your Node-RED flow.
4. Observe and record the results.

7/9
Part E - Monitoring Temperature Readings via IoT MQTT Panel app.

1. Go the Google Play Store to download and install IoT MQTT Panel app.
2. Launch the app. Tap on “Setup a Connection” set the new connection for first time use.

 Name the connection using any name you prefer.


 Type broker.hivemq.com in Broker Web / IP address.

8/9
 Port number and network protocol should already be set as 1883 and TCP.

3. Tap on “+” on the right side of “Add Dashboard” to add a new MQT Panel dashboard.
4. Name the dashboard and tap “Save”. Tap “Create” to finish this step. Record your result.

5. Tap on your newly created connection to open it. Tap “Add Panel” and select “Gauge”. Set the following as:
 “Panel Name” as DHT11 - Temperature and “Topic” as lab5/esp32/dht/temperature.
 “Payload min” = 0 and “Payload max” = 100.
 “Unit” as Celsius.

6. Tap “Create”. Record your result.


7. Observe and record the results.

NOTE: You can also use any other app for this procedure, depending on the operating system of your smartphones.
The equivalent settings are applicable.

9/9
10/9
PART F - Problem Based Learning

Build an interface to monitor DHT11 output using IoT MQTT Panel app on your smartphone to resemble the display
of your Node-RED dashboard. You are free to choose any type of dashboard components if they are reasonable.

Gauge Setting for Humidity and Temperature

Line Graph

11/9
Text Input

Text Log

12/9
Result

Result

13/9
Already shown above.

Discussion
Write your discussion on observed result.

The most practical and effective way to gather and send temperature and humidity data from the
DHT11 sensor is to use MQTTBox for DHT11 monitoring. The multipurpose MQTT client
MQTTBox facilitates smooth connection between the MQTT broker and the sensor. By setting
up the DHT11 sensor to broadcast data to a particular topic allows MQTTBox to receive that
data.
subject and obtain the real-time sensor measurements. This configuration makes monitoring
simple and viewing of the DHT11 data, given that MQTTBox offers a number of display and
studying the MQTT messages that were received. MQTTBox allows users to easily track patterns
in temperature and humidity, and use the information into their chosen Internet of Things
applications, systems.

Conclusion
Write your conclusion on this practical work.

As a conclusion, we now know how to install the DHT sensor and other sensor libraries in the
Arduino IDE so that they may be used in code and operate. Here, we've implemented the
knowledge we learned in the previous two experiments utilizing the MQTTbox application. We
now know that we can publish and subscribe using MQTTbox. In order to link the esp32 and the
MQTT panel to the internet, we have additionally utilized the information from lab 2.

14/9

You might also like