- Introduction
- Prerequisites
- List of hardware and pinouts
- Wiring schema
- Programming the NodeMCU device
- Data visualization
- See also
- Your feedback
- Next steps
Introduction
IoT Hub is an open-source server-side platform that allows you to monitor and control IoT devices. It is free for both personal and commercial usage and you can deploy it anywhere. If this is your first experience with the platform we recommend to review what-is-iothub page and getting-started guide.
This sample application performs collection of temperature and humidity values produced by DHT11 sensor. Collected data is pushed to IoT Hub for storage and visualization. The purpose of this application is to demonstrate IoT Hub data collection API and visualization capabilities.
The DHT11 sensor is connected to NodeMCU. NodeMCU push data to IoT Hub server via MQTT protocol. Data is visualized using built-in customizable dashboard. The application that is running on NodeMCU is written using Lua scripting language which is quite simple and easy to understand.
Once you complete this sample/tutorial, you will see your sensor data on the following dashboard.
Prerequisites
You will need to have IoT Hub server up and running. The easiest way is to use Live Demo server.
The alternative option is to install IoT Hub using Installation Guide. Windows users should follow this guide. Linux users that have docker installed should execute the following commands:
1
2
3
4
5
mkdir -p ~/.mytb-data && sudo chown -R 799:799 ~/.mytb-data
mkdir -p ~/.mytb-logs && sudo chown -R 799:799 ~/.mytb-logs
docker run -it -p 9090:9090 -p 7070:7070 -p 1883:1883 -p 5683-5688:5683-5688/udp -v ~/.mytb-data:/data \
-v ~/.mytb-logs:/var/log/thingsboard --name mytb --restart always thingsboard/tb-postgres
These commands install IoT Hub and load demo data and accounts.
IoT Hub UI will be available using the URL: http://localhost:8080. You may use username tenant@thingsboard.org and password tenant. More info about demo accounts is available here.
List of hardware and pinouts
- NodeMCU V3 - You can find list of additional documentation in NodeMCU overview
- Keyes DHT-11 - DHT11 sensor with built-in resistor.
- 3 female-to-female jumper wires
Wiring schema
NodeMCU Pin | DHT-11 Pin |
---|---|
NodeMCU 3.3V | DHT-11 VCC |
NodeMCU GND | DHT-11 GND (-) |
NodeMCU D5 | DHT-11 Data (S) |
Programming the NodeMCU device
We need to download and build firmware with Lua interpreter for NodeMCU. This process is described in official documentation and there are multiple ways to do this. You can use cloud build service for this purpose, however, we will use Docker Image.
Firmware download
Use the following commands to clone the official GitHub repository for NodeMCU firmware.
1
2
3
$ mkdir -p ~/samples/nodemcu
$ cd ~/samples/nodemcu
$ git clone https://github.com/nodemcu/nodemcu-firmware.git
There is ability to customize firmware by changing two files:
- ~/samples/nodemcu/nodemcu-firmware/app/include/user_config.h - There is an ability to change default baud rate in.
Please find and update line below to specify custom baud rate.
1
2
3
...
#define BIT_RATE_DEFAULT BIT_RATE_115200
...
- ~/samples/nodemcu/nodemcu-firmware/app/include/user_modules.h - Contains list of what kind of modules included by default.
In our case, all necessary modules included by default. However, please check that these modules are uncommented.
1
2
3
4
5
...
define LUA_USE_MODULES_DHT
...
define LUA_USE_MODULES_MQTT
...
Building firmware using Docker
The easiest way to build nodemcu firmware is by using prepared docker container for that task.
Please visit docker installation page and install docker on your machine.
After installation you need to download docker image from docker hub by the command:
1
$ sudo docker pull marcelstoer/nodemcu-build
Eventually build the firmware by next command:
1
$ sudo docker run --rm -ti -v ~/samples/nodemcu/nodemcu-firmware:/opt/nodemcu-firmware marcelstoer/nodemcu-build
As the result binary firmware located in the ~/samples/nodemcu/nodemcu-firmware/bin folder.
Application source code
Our application consists of three .lua files:
- config.lua - configuration file, where we define a custom configuration.
You need to modify this file in order to setup your wifi network parameters and address of IoT Hub server.
- your wifi network SSID - name of the wifi network.
- your wifi network password - password to the network.
- thingsboard server IP - host of your thingsboard installation. Use “iothub.magenta.at” if you are using live demo server.
- thingsboard mqtt port - 1883 is the default value.
- thingsboard access token - DHT11_DEMO_TOKEN is the default value that corresponds to pre-provisioned demo account.
If you are using live demo server - get the access token for pre-provisioned “DHT11 Demo Device”.
- dht11.lua - sending temperature and humidity every 10 seconds to thingsboard server via MQTT protocol.
- init.lua - initalization file that contains config.lua:
|
|
|
Flashing the firmware
Before flashing firmware, we need to figure out which serial interface using to communicate with NodeMCU.
1
2
3
4
$ dmesg
...
[845270.901509] usb 3-3: ch341-uart converter now attached to ttyUSB0
...
In our case /dev/ttyUSB0 is used for communication.
In order to flash firmware for NodeMCU, please download and install following utilities
Upload nodemcu firmware using command:
1
$ sudo ./esptool.py -b 115200 write_flash --flash_mode dio --flash_size 32m 0x0 ~~/samples/nodemcu/nodemcu-firmware/bin/nodemcu_integer_master_*.bin --verify
Upload application files using following commands:
1
2
3
$ sudo ./luatool.py --port /dev/ttyUSB0 -b 115200 --src config.lua --dest config.lua -v
$ sudo ./luatool.py --port /dev/ttyUSB0 -b 115200 --src dht11.lua --dest dht11.lua -v
$ sudo ./luatool.py --port /dev/ttyUSB0 -b 115200 --src init.lua --dest init.lua -v
Troubleshooting
Sometimes you can observe frequent blinking of the blue led after firmware upload. This is probably related to missing initialization data. Use following command to fix this:
1
$ sudo ./esptool.py -b 115200 write_flash --flash_mode dio --flash_size 32m 0x3fc000 ~/samples/nodemcu/nodemcu-firmware/bin/esp_init_data_default.bin --verify
Sometimes you are not able to upload lua files. Try to reset the device and execute a command again within the first 10 seconds after reset. If no success, try to delete init.lua code from NodeMCU:
1
$ sudo ./luatool.py --port /dev/ttyUSB0 -b 115200 --delete init.lua
Data visualization
In order to simplify this guide, we have included “Temperature & Humidity Demo Dashboard” to the demo data that is available in each IoT Hub installation. You still can modify this dashboard: tune, add, delete widgets, etc. You can access this dashboard by logging in as a tenant administrator. Use
- login: tenant@thingsboard.org
- password: tenant
in case of local IoT Hub installation.
Once logged in, open Dashboards->Temperature & Humidity Demo Dashboard page. You should observe demo dashboard with live data from your device (similar to dashboard image in the introduction).
See also
Browse other samples or explore guides related to main IoT Hub features:
- Device attributes - how to use device attributes.
- Telemetry data collection - how to collect telemetry data.
- Using RPC capabilities - how to send commands to/from devices.
- Rule Engine - how to use rule engine to analyze data from devices.
- Data Visualization - how to visualize collected data.
Your feedback
Don’t hesitate to star IoT Hub on github to help us spread the word. If you have any questions about this sample - post it on the issues.
Next steps
- Getting started guides - These guides provide quick overview of main IoT Hub features. Designed to be completed in 15-30 minutes.
-
Connect your device - Learn how to connect devices based on your connectivity technology or solution.
-
Data visualization - These guides contain instructions how to configure complex IoT Hub dashboards.
-
Data processing & actions - Learn how to use IoT Hub Rule Engine.
-
IoT Data analytics - Learn how to use rule engine to perform basic analytics tasks.
-
Advanced features - Learn about advanced IoT Hub features.
-
Contribution and Development - Learn about contribution and development in IoT Hub.