Dec50132 Internet Based Controller Pw6

You are on page 1of 13

Department of Electrical Engineering

DEC 50132 – Internet Based Controller

PROGRAMME :
PRACTICAL WORK NO : 6
TITLE : SIMPLE IOT APPLICATION FOR MONITORING
SENSOR READING WITH NODE-RED DASHBOARD
DATE :
LECTURER NAME :

PRACTICAL SKILL ASSESSMENT LAB REPORT


ATTAINMENT ATTAINMENT
[CLO2, PLO5, P4] ASSESSMENT
PBL- Able to draw the node S1 
flow and deploy the node-red S2 
and broker without supervision S3  Results 
S4  
Discussion
PBL- Able to establish S1 
connection to the broker from S2  Conclusion 
MQTTbox/ IOT MQTT panel S3 
S4 
S1 
PBL- Able to code using S2 
Arduino IDE (weightage x 2) S3  Score (30)
S4 
S1
S2 Percentage
Score (30)
S3 (30%)
S4
S1 S1
S2 Total CA S2
Percentage (70%)
S3 Marks (100%) S3
S4 S4
BIL GROUP MEMBERS REGISTRATION NO.
S1
S2

JKE PSP
PRACTICAL SKILL RUBRIC

Description Mark Scale Attainment

Student can complete all tasks assigned WITHOUT errors 5 x2 10

Student can complete all tasks assigned with A FEW errors 4 x2 8

Student can complete all tasks assigned with MORE errors 3 x2 6

Student can complete partial tasks assigned WITHOUT errors 2 x2 4

Student can complete partial tasks assigned with A FEW errors 1 x2 2

Student shows no response/task not attempted 0 x2 0

DATA ACQUISITION AND COMMUNICATION RUBRIC

Excellent Very Good Good Fair Unsatisfactory


Report Component
(PLO2,LD2)
5 4 3 2 1

Results Professional looking Accurate Accurate Incomplete result, Data are not
and accurate representation representations of the major mistakes. shown OR are
• Results in the form representation of the of the data in data in written form, inaccurate.
of data, data in tables and/or tables and/or but no graphs or
calculation, graphs. Graphs and graphs. tables are presented.
waveform, graph tables are labelled and
etc. titled.

Analysis/ Discussion All point of discussion Most points of Some points of Some points of Very few points
on the results obtained discussion on discussion on results discussion on of discussion, not
• Ability to present, covered and results obtained covered and results obtained properly
interpret and elaborated. obtained elaborated. covered and but elaborated.
analyse result. covered and not properly
elaborated. elaborated.

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

JKE PSP
Practical Work 6

Title: SIMPLE IOT APPLICATION FOR MONITORING SENSOR READING WITH NODE-RED
DASHBOARD

THEORY

Configuring the MQTT Publish and Subscribe Nodes in Node-Red

Node-Red provides both an MQTT subscribe (input) and publish (output) node.

The configuration for these nodes are almost Identical as the main part of the configuration
concerns the actual client connection.

Because of this it is useful to think of the publish and subscribe nodes as consisting of two
components as shown in the schematic below:

Before we look a the actual configuration we will look at MQTT client connections in general.

Connecting to an MQTT Broker or Server.

To connect to an MQTT broker or server you need to know the Broker IP address or name, and the
port that it is using (default 1883).

JKE PSP
In addition a client needs to provide a client name to identify itself. Each client connection requires a
unique client name.

A single client connection can be used to both publish and subscribe.

An MQTT broker can enforce encryption and username/password authentication in which case the
connecting client must comply

Procedure:

PART A – DIGITAL INPUT OUTPUT

1. Refer practical work 3 for installation. Run Node-js application.

2. Open cmd-prompt and type node-red

3. On your browser type : localhost:1880

4. Join the node as shown below

JKE PSP
debug node
MQTT in node

Gauge node

Graph node

5. Edit MQTT in node as shown below:

JKE PSP
6. Edit debug node as below:

JKE PSP
7. Edit Gauge node as below:

8. Edit chart node:

9. Then, DEPLOY the node.

JKE PSP
10. Launch the node-red dashboard, and the interface will be shown below:

A. ESP32 and Node-red connection

1. Open your Arduino IDE.


2. Download PubSubClient file at https://www.arduinolibraries.info/libraries/pub-sub-client
3. Extract the *.zip file.
4. Paste the folder to C:\Program Files (x86)\Arduino\libraries\PubSubClient
5. Write the given code:

/* Project ESP32, DHT, MQTT and Deepsleep */


#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h" // Library for DHT sensors

#define wifi_ssid "________" //wifi ssid


#define wifi_password "________l" //wifi password

#define mqtt_server "broker.hivemq.com" // server name or IP


#define mqtt_user "" // username
#define mqtt_password "" // password

#define temperature_topic "____________" //Topic temperature


#define humidity_topic "_______________" //Topic humidity
#define debug_topic "_____________ " //Topic for debugging

/* definitions for deepsleep */


#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 100 /* Time ESP32 will go to sleep for 1.5 minute (in 100 seconds) */
#define TIME_TO_SLEEP_ERROR 3600 /* Time to sleep in case of error (1 hour) */

bool debug = true; //Display log message if True

#define DHTPIN 14 // DHT Pin

JKE PSP
// Uncomment depending on your sensor type:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)

// Create objects
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
Serial.begin(115200);
setup_wifi(); //Connect to Wifi network

client.setServer(mqtt_server, 1883); // Configure MQTT connection, change port if needed.

if (!client.connected()) {
reconnect();
}
dht.begin();

// Read temperature in Celcius


float t = dht.readTemperature();
// Read humidity
float h = dht.readHumidity();

// Nothing to send. Warn on MQTT debug_topic and then go to sleep for longer period.
if ( isnan(t) || isnan(h)) {
Serial.println("[ERROR] Please check the DHT sensor !");
client.publish(debug_topic, "[ERROR] Please check the DHT sensor !", true); // Publish humidity on topic/humidity

esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //go to sleep


Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
Serial.println("Going to sleep now because of ERROR");
esp_deep_sleep_start();
return;
}

if ( debug ) {
Serial.print("Temperature : ");
Serial.print(t);
Serial.print(" | Humidity : ");
Serial.println(h);
}
// Publish values to MQTT topics
client.publish(temperature_topic, String(t).c_str(), true); // Publish temperature on topic/temperature
if ( debug ) {
Serial.println("Temperature sent to MQTT.");
}
delay(100); //some delay is needed for the mqtt server to accept the message
client.publish(humidity_topic, String(h).c_str(), true); // Publish humidity on topic/humidity
if ( debug ) {
Serial.println("Humidity sent to MQTT.");
}

esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //go to sleep


Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
Serial.println("Going to sleep as normal now.");
esp_deep_sleep_start();
}

//Setup connection to wifi

JKE PSP
void setup_wifi() {
delay(20);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);

WiFi.begin(wifi_ssid, wifi_password);

while (WiFi.status() != WL_CONNECTED) {


delay(100);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi is OK ");
Serial.print("=> ESP32 new IP address is: ");
Serial.print(WiFi.localIP());
Serial.println("");
}

//Reconnect to wifi if connection is lost


void reconnect() {

while (!client.connected()) {
Serial.print("Connecting to MQTT broker ...");
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
Serial.println("OK");
} else {
Serial.print("[Error] Not connected: ");
Serial.print(client.state());
Serial.println("Wait 5 seconds before retry.");
delay(5000);
}
}
}

void loop() {
}

6. Connect DHT to your ESP32 Board.

7. “Upload” in your ESP32 Board.

8. You will get the serial monitor as shown below:

9. Check your Node-red Dashboard, the interface will be shown as below:

JKE PSP
Problem Based Learning

Based on all the steps above and the previous Practical Work 5, use your understanding to get the
output in your node-red dashboard and Android or IOS IOT MQTT Panel to control lamp and fan in
your house. The interface in Node-red dashboard and IOS IOT MQTT Panel is shown below:

JKE PSP
Result:

Observations and Problem based Learning code:

Discussion:

JKE PSP
Write your discussion on observed result.

Conclusion:

Write your conclusion on this practical work.

References

https://iotdesignpro.com/projects/interface-arduino-with-node-red-to-send-sensor-data-on-
webpage
https://randomnerdtutorials.com/esp8266-and-node-red-with-mqtt/ DHT11 and MQTT
http://pdacontrolen.com/esp8266-public-mqtt-broker-hivemq-node-red/ (PW5 example)
http://www.steves-internet-guide.com/node-red-admin-basics/
http://www.steves-internet-guide.com/configuring-the-mqtt-publish-node/
http://noderedguide.com/node-red-lecture-4-a-tour-of-the-core-nodes/
https://github.com/hobbyquaker/awesome-mqtt (list of MQTT)

https://randomnerdtutorials.com/esp8266-and-node-red-with-mqtt/

https://iotbyhvm.ooo/how-to-connect-esp32-to-mqtt-broker-using-cloudmqtt/

https://dustcloud.atlassian.net/wiki/spaces/ALLDOC/pages/110305930/HiveMQ+integration

https://dustcloud.atlassian.net/wiki/spaces/ALLDOC/pages/110305930/HiveMQ+integration#HiveM
Qintegration!-PublishTemperaturetoHiveMQ

https://thingsboard.io/docs/samples/esp8266/temperature/ (good example)

http://www.internetoflego.com/weather-station-dht11-mqtt-node-red-google-chart-oh-my/ (dht11)

JKE PSP

You might also like