Friday

Real-Time Data With IoT & MQTT

 

unplush

For real time data analysis we need real time data to be collected. I would like to share with you this small project for capturing real time temperature and humidity data from sensors and showing that in a local dashboard.

You can connect to the real time data analysis dashboard as well. If you are interested in how to can read my other article here.

We will collect data by temperature and humidity sensor DHT11. Which is very small and cheap. However, work perfectly.

We will be using a microcontroller Arduino with ESP8266 wifi on board. You can use Uno+WiFi R3 AtMega328p+NodeMSU ESP8266 8mb Memory USB-TTL CH340G Compatible for Arduino UNO

Also, we need a local or free MQTT broker. And node-red. Node-RED is a flow-based development tool for visual programming. It makes it easy to wire together different hardware devices, APIs, and online services to create applications. Node-RED is a great tool for developers who want to create IoT applications quickly and easily.

Here are some of the things you can do with Node-RED:

* Control hardware devices, such as lights, sensors, and motors.

* Connect to APIs, such as weather data, stock prices, or social media feeds.

* Send and receive data from online services, such as Google Cloud Platform or Amazon Web Services.

* Create dashboards and visualizations to display data.

* Automate tasks and workflows.

Node-RED is a powerful tool that can be used to create a wide variety of applications. If you are interested in IoT development, Node-RED is a great place to start.

Here are some of the benefits of using Node-RED:

  • Easy to learn and use. Node-RED is a visual programming tool, which means that you don’t need to know how to code to use it. You can simply drag and drop nodes together to create your application.
  • Flexible. Node-RED is very flexible and can be used to create a wide variety of applications.
  • Extensible. There is a large community of Node-RED users who have created a wide range of nodes that can be used in your applications.

Free and open source. Node-RED is free and open source software, which means that it is available to everyone.

MQTT stands for Message Queuing Telemetry Transport. It is a lightweight messaging protocol that is commonly used for Internet of Things (IoT) applications. MQTT is a publish/subscribe protocol, which means that devices can publish messages to topics, and other devices can subscribe to topics to receive messages.

To run MQTT locally, you will need to install an MQTT broker. An MQTT broker is a software application that manages the communication between devices that are publishing and subscribing to topics. There are many different MQTT brokers available, both open source and commercial. Some popular MQTT brokers include:

* Mosquitto

* Eclipse Paho

* HiveMQ

* ActiveMQ

Once you have installed an MQTT broker, you can start publishing and subscribing to topics. To publish a message, you need to specify the topic name and the message payload. The message payload is the actual data that you want to send. To subscribe to a topic, you simply specify the topic name.

Here is an example of how to publish a message to a topic using the Mosquitto broker:

mosquitto_pub -h localhost -t mytopic -m "Hello world!"

This command will publish the message “Hello world!” to the topic “mytopic” on the MQTT broker running on the localhost.

Here is an example of how to subscribe to a topic using the Mosquitto broker:

mosquitto_sub -h localhost -t mytopic

This command will subscribe to the topic “mytopic” on the MQTT broker running on the localhost. When a message is published to the topic “mytopic”, the mosquitto_sub command will print the message payload to the console.

MQTT is a simple and lightweight messaging protocol that is well-suited for IoT applications. It is easy to install and use, and it can be used to connect a wide variety of devices.

Hope you already installed the Arduino IDE in your system. Add the following libraries from the IDE.

  • ESP8266WiFi
  • PubSubClient
  • DHT

Connect to your microcontroller and port in IDE. Create a new sketch with this code do not forget to update your wifi username, password etc.

Upload the code to your microcontroller.

Run the mqtt server.

Run node-red to create two pub of temperature and humidity. And two subscribers for node-red dashboard. Below is the node-red flow chart for this application.

Temperature node:

{
"id": "8af3f91e138dfc7f",
"type": "mqtt in",
"z": "7c0214a9d7501627",
"name": "temp mqtt",
"topic": "sensor/temperature",
"qos": "0",
"datatype": "auto-detect",
"broker": "89cb5cc4b9d9ccc2",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 160,
"y": 120,
"wires": [
[
"91b12e2e350dc914",
"fe8591a0be99386b"
]
]
},
{
"id": "89cb5cc4b9d9ccc2",
"type": "mqtt-broker",
"name": "remote broker",
"broker": "broker.emqx.io",
"port": "1883",
"clientid": "dhiraj",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "test",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]

Humidity node:

{
"id": "62cd8fffc101abf7",
"type": "mqtt in",
"z": "7c0214a9d7501627",
"name": "humidity mqtt",
"topic": "sensor/humidity",
"qos": "0",
"datatype": "auto-detect",
"broker": "89cb5cc4b9d9ccc2",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 170,
"y": 260,
"wires": [
[
"91b12e2e350dc914",
"bb13832944ebe60a"
]
]
},
{
"id": "89cb5cc4b9d9ccc2",
"type": "mqtt-broker",
"name": "remote broker",
"broker": "broker.emqx.io",
"port": "1883",
"clientid": "dhiraj",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "test",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]

Temperature gauge node:

{
"id": "fe8591a0be99386b",
"type": "ui_gauge",
"z": "7c0214a9d7501627",
"name": "",
"group": "30b7c6cd1061cb67",
"order": 0,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "Temperature ",
"label": "Celsius ",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#00b500",
"#e60039",
"#e93407"
],
"seg1": "",
"seg2": "",
"diff": false,
"className": "",
"x": 650,
"y": 300,
"wires": []
},
{
"id": "30b7c6cd1061cb67",
"type": "ui_group",
"name": "Group 1",
"tab": "6cc408fc9b1895d7",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "6cc408fc9b1895d7",
"type": "ui_tab",
"name": "Tab 1",
"icon": "dashboard",
"order": 1,
"disabled": false,
"hidden": false
}
]

Humidity gauge node:

{
"id": "bb13832944ebe60a",
"type": "ui_gauge",
"z": "7c0214a9d7501627",
"name": "",
"group": "d8f94d45e6e6f994",
"order": 0,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "Humidity",
"label": "%RH",
"format": "{{value}}",
"min": 0,
"max": "100",
"colors": [
"#0ef10e",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"diff": false,
"className": "",
"x": 640,
"y": 380,
"wires": []
},
{
"id": "d8f94d45e6e6f994",
"type": "ui_group",
"name": "Group 2",
"tab": "6cc408fc9b1895d7",
"order": 2,
"disp": true,
"width": 6
},
{
"id": "6cc408fc9b1895d7",
"type": "ui_tab",
"name": "Tab 1",
"icon": "dashboard",
"order": 1,
"disabled": false,
"hidden": false
}
]

Dashboard

I am a Software Architect | AI, Data Science, IoT, Cloud ⌨️ 👨🏽 💻

I love the learn and share knowledge. Thank you.

No comments:

Incremental Data Loading from Databases for ETL

  pexel Let first discuss what is incremental loading into the data warehouse by ETL from different data sources including databases. Increm...