Introduction
This example will demonstrate how to report the temperature and humidity data collected by the DHT11 sensor to the MQTT service in the cloud through the MQTT protocol and the NodeMCU based on ESP8266 , and show how the application subscribes to and processes the data. The reason why mqtt protocol is used in this article is that it is lightweight and energy-saving, which is very suitable for the use scenarios of the Internet of things. At present, all major public cloud providers have basically opened IOT hub services based on MQTT protocol. For example, the IOT Core of AWS and the IOT Hub of Azure can easily access these data to these public cloud services through MQTT protocol.
The overall architecture of this example is as follows:
Configuration
Hardware Configuration
After the data is successfully collected through NodeMCU, it needs to be sent to the MQTT cloud service in the cloud. This article uses the MQTT cloud service provided by EMQX. Readers can also choose other MQTT cloud services according to their own circumstances, such as Azure IoT Hub or AWS IoT Core. Each cloud service needs to provide different authentication methods when accessing. Therefore, when connecting to the MQTT service in the cloud via NodeMCU, it is required to set the connection method according to the security requirements of the target cloud service. For the sake of simplicity, this article uses a non-secure connection method. In a formal production environment, a connection with a secure authentication method must be set.
Coding
Follow these steps to edit the code to suit your own Wi-Fi and MQTT settings
Running
Summary
So far, it has completed the simple process of collecting data from NodeMCU, uploading it to the MQTT cloud service provided by EMQ, and processing the data by the back-end program written by Python. However, in actual production applications, higher requirements will be required, for example,
In order to achieve high data security (avoid uploading to the cloud), reduce business processing delays, and reduce data transmission costs, edge computing can be considered in the solution. Azure IoT Edge and AWS's Greengrass provide solutions at the edge. EMQ also provides an open sourced ultra-lightweight edge IoT edge streaming analytics solution . Readers can refer to for more detailed information.
Welcome to our open source project . Please visit the for details.
This example will demonstrate how to report the temperature and humidity data collected by the DHT11 sensor to the MQTT service in the cloud through the MQTT protocol and the NodeMCU based on ESP8266 , and show how the application subscribes to and processes the data. The reason why mqtt protocol is used in this article is that it is lightweight and energy-saving, which is very suitable for the use scenarios of the Internet of things. At present, all major public cloud providers have basically opened IOT hub services based on MQTT protocol. For example, the IOT Core of AWS and the IOT Hub of Azure can easily access these data to these public cloud services through MQTT protocol.
The overall architecture of this example is as follows:

Configuration
Hardware Configuration
- NodeMCU board x 1: NodeMCU is an open source IoT platform. It includes firmware which runs on the ESP8266 Wi-Fi SoC, and hardware which is based on the ESP-12 module. The term "NodeMCU" by default refers to the firmware rather than the development kits . The firmware uses the Lua scripting language.
- DHT11 temperature / humidity sensor x 1: DHT11 digital temperature and humidity sensor is a composite sensor with calibrated digital signal output
- Breadboard x 1
- Several jumper wires
- Please refer to the following screenshot for Connection Graph
- Download and install driver
- Install ESP8266 module
- Install PubSubClient library (by Nick O'Leary)
Sketch -> Include Library -> Manage Libraries... -> Type PubSub in Search field -> Install
After the data is successfully collected through NodeMCU, it needs to be sent to the MQTT cloud service in the cloud. This article uses the MQTT cloud service provided by EMQX. Readers can also choose other MQTT cloud services according to their own circumstances, such as Azure IoT Hub or AWS IoT Core. Each cloud service needs to provide different authentication methods when accessing. Therefore, when connecting to the MQTT service in the cloud via NodeMCU, it is required to set the connection method according to the security requirements of the target cloud service. For the sake of simplicity, this article uses a non-secure connection method. In a formal production environment, a connection with a secure authentication method must be set.
- Click registration address to register
- After registration, click to apply for a free trial of 15-day deployment
- View the broker connection address
Coding
Follow these steps to edit the code to suit your own Wi-Fi and MQTT settings
- Wi-Fi Setting
#define wifi_ssid ""
#define wifi_password ""
- Broker server setting
#define mqtt_server "broker-internet-facing-f1429d8cb54ca4a7.elb.us-east-1.amazonaws.com"
- Arduion configuration
Running
- Code upload
Connect the NodeMCU to the PC via USB and select the 115200 port in the Arduion IDE. Use the upload button to compile the sketch and upload it to the device. - Open Arduino monitor window to view data reporting.
- MQTT client receives messages
Use to test the reported message
MQTT Websocket Toolkit is a recently open sourced MQTT (WebSocket) test tool from EMQ, which supports online access (tools.emqx.io). We can easily verify whether the NodeMCU reports MQTT messages.- Create an MQTT connection
- Subscribe to topics and receive test messages
- Create an MQTT connection
- Use Python MQTT client to view reported messages
-
- Screenshot of Python script running:
- Troubleshooting: In order to perform troubleshooting, connecte the USB adapter to the PC and select the port of USB-TTL adapter in the Arduino IDE. Open the Serial Monitor to view the debug information generated by the serial output
Summary
So far, it has completed the simple process of collecting data from NodeMCU, uploading it to the MQTT cloud service provided by EMQ, and processing the data by the back-end program written by Python. However, in actual production applications, higher requirements will be required, for example,
- More secure connection method
- Real-time processing of IoT data
- Persistence of data
- Larger scale connection
In order to achieve high data security (avoid uploading to the cloud), reduce business processing delays, and reduce data transmission costs, edge computing can be considered in the solution. Azure IoT Edge and AWS's Greengrass provide solutions at the edge. EMQ also provides an open sourced ultra-lightweight edge IoT edge streaming analytics solution . Readers can refer to for more detailed information.
Welcome to our open source project . Please visit the for details.