About NodeMCU
NodeMCU is a very popular Micro controller with wifi access. It is based on ESP8266 – a cheap but powerful chip and fully support Arduino IDE. If you familiar with Arduino IDE, you can program NodeMCU in no time.
Objective
In this project, we will use NodeMCU to get environment temperature/humidity from DHT11 sensor, then send the data to other internet device through MQTT broker. MQTT is a Machine to Machine IOT protocol. This protocol allows one device(sender) to publish data to a MQTT broker with specified Topic, another device(receiver) which subscribes the same topic can easily get message from sending device
Parts and Devices
NodeMCU board x 1 pc
DHT11 temperature/humidity sensor x 1 pc
Breadboard x 1 pc
jumper wires
Connection Graph
NodeMCU | DHT11 sensor |
3.3v | VCC |
D3 | DATA |
GND | Ground |
Programming Prerequisite:
Before running the project, we need set up the Arduino IDE as per following procedures:
Library Installation:
In Arduino IDE, we need install MQTT client library to communicate with MQTT broker and DHT library to access DHT11 sensor, please download these two library from following two links:
Unzip above two files, move the unzipped folder to Arduino IDE library folder (you can also import these two zip files to Arduino library from IDE).
Download sample code from following link
https://osoyoo.com/wp-content/uploads/samplecode/dht11-esp8266.txt , copy the code into Arduino IDE. Before running the code, please do following changes to fit your wifi and MQTT setting:
1)Line 19 and 20:
const char* ssid = “your_hotspot_ssid”;
const char* password = “your_hotspot_password”;
You need change these 2 lines to match your wifi SSID and password
2)Line 21
const char* mqtt_server = “broker.emqx.io”;
You can use your own MQTT broker URL or IP address to set above mqtt_server value. You can also use some famous free MQTT server to test the project such as “broker.emqx.io”, “iot.eclipse.org” etc
If you want to install your own MQTT broker in Ubuntu Linux, please read this article https://osoyoo.com/2016/09/07/how-to-install-mosquitto-mqtt-server-on-linux
3)if your MQTT server require username and password authentication, you need change line 86
Running the code
After you running the code, please open the serial terminal window in upright corner of Arduino IDE.
If wifi hotspot name and password setting is ok and MQTT broker is connected, you will see following result:
You can see NodeMCU will publish realtime temperature and humidity to MQTT broker every 6 seconds,you can use a MQTT client software to subscribe the topic “Osoyoo” and see the published data from NodeMCU.
If you use MQTT client software to send a MQTT message “0” or “1” with topic “OsoyooCommand”, NodeMCU will do some action as per your command. In our code, you will see message “0” will make NodeMCU print Humidity and Message “1” will make NodeMCU print temperature.
Result as following:
In above testing procedure, I used Linux mosquitto MQTT client, you can read this article https://osoyoo.com/2016/09/26/2610 to learn mosquitto-clients software installation on Ubuntu. You can also google other MQTT client software for windows,iOS or Android.
DownLoad Url osoyoo.com