Over a million developers have joined DZone.

Collect and Visualize Data Using NodeMCU and Thingsboard

DZone's Guide to

Collect and Visualize Data Using NodeMCU and Thingsboard

Leverage open source frameworks to do the hard work for you. In this case, you can use NodeMCU and Thingsboard to quickly prototype your IoT work.

· IoT Zone ·
Free Resource

Digi-Key Electronics’ Internet of Things (IoT) Resource Center Inspires the Future: Read More

In this tutorial, we are going to collect and visualize temperature and humidity values produced by DHT11 sensor. We will use a NodeMCU device that will upload data over MQTT to the Thingsboard open-source IoT platform. The platform itself is licensed under Apache 2.0 and is free for use in commercial projects and solutions.

Data Flow

The DHT11 sensor is connected to NodeMCU. NodeMCU pushes data to the Thingsboard server via the MQTT protocol. Data is visualized using a built-in, customizable dashboard. The application that is running on NodeMCU is written using the Lua scripting language, which is quite simple and easy to understand.

Hardware Components

  • NodeMCU v3: includes both firmware, which runs on the ESP8266 Wi-Fi SoC, and hardware, which is based on the ESP-12 module.

Image title

Image titleWiring Schema

Image title

NodeMCU Pin

DHT11 Pin

NodeMCU 3.3V



DHT-11 GND (-)

NodeMCU D5

DHT-11 Data (S)

Software Components

You will need to have a Thingsboard server up and running. Use this guide to install Thingsboard using Docker.

Programming the NodeMCU Device

We need to download and build firmware with a Lua interpreter for NodeMCU. This process is described in the official documentation, and there are multiple ways to do it.

You can use a cloud build service for this purpose, however, we will use this Docker image.

Firmware Download

Use the following commands to clone the official GitHub repository for NodeMCU firmware.

$ mkdir -p ~/samples/nodemcu
$ cd ~/samples/nodemcu
$ git clone https://github.com/nodemcu/nodemcu-firmware.git

We have the ability to customize firmware by changing two files:

  • ~/samples/nodemcu/nodemcu-firmware/app/include/user_config.h: This is where we have the ability to change the default baud rate

  • ~/samples/nodemcu/nodemcu-firmware/app/include/user_modules.h: This contains a list of what kind of modules are included by default.

In our case, all necessary modules are included by default. However, please check that the following modules are uncommented:


Building Firmware using Docker

The easiest way to build NodeMCU firmware is by using a prepared Docker container for that task.

Please visit the Docker installation page and install Docker on your machine.

After installation, you need to download a Docker image from Docker Hub with the command:

$ sudo docker pull marcelstoer/nodemcu-build 

Eventually, you'll build the firmware using the command:

$ sudo docker run --rm -ti -v ~/samples/nodemcu/nodemcu-firmware:/opt/nodemcu-firmware marcelstoer/nodemcu-build

As a result, the binary firmware will be located in the ~/samples/nodemcu/nodemcu-firmware/bin folder.

Application Source Code

Our application consists of three .lua files.


wifi_mode = 1
wifi_ssid = "YOUR_WIFI_SSID_HERE"
mqtt_port = 1883
access_token = "DHT11_DEMO_TOKEN"

You need to modify this file in order to set up your Wi-Fi network parameters and the address of the Thingsboard server.
- wifi_ssid: name of the wifi network.
- wifi_pass: password to the network.
- mqtt_ip: host where your thingsboard installation.
- mqtt_port: 1883 is the default value.
- access_token: DHT11_DEMO_TOKEN is the default value that corresponds to pre-provisioned .


This file sends temperature and humidity data every 10 seconds to the Thingsboard server via MQTT.

-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("esp8266", 120, access_token, "password", 1)

print("Connecting to MQTT broker...")
m:connect(mqtt_ip, mqtt_port, 0, 1, function(client) print("Connected to MQTT!") end,
    function(client, reason) print("Could not connect, failed reason: " .. reason) end)

m:on("offline", function(client) print("MQTT offline") end)

pin = 5

print("Collecting Temperature and Humidity...")
tmr.alarm(2, 10000, tmr.ALARM_AUTO, function()
    status, temp, humi, temp_dec, humi_dec = dht.read(pin)
    if status == dht.OK then
        -- Integer firmware using this example
        print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n",
        m:publish("v1/devices/me/telemetry", string.format("[{\"temperature\":%d}, {\"humidity\":%d}]", math.floor(temp), math.floor(humi)), 0, 0, function(client) print("Data sent") end)
    elseif status == dht.ERROR_CHECKSUM then
        print("DHT Checksum error.")
    elseif status == dht.ERROR_TIMEOUT then
        print("DHT timed out.")


This is the main initialization script that is loaded by default. It's basically an entry point for our application.

function startup()
    if file.open("init.lua") == nil then
        print("init.lua deleted")

-- setup wifi
function setup_wifi(mode, ssid, pass)
    wifi.sta.config(ssid, pass, 1)


if file.exists("config.lua") then
    print("Loading configuration from config.lua")

setup_wifi((wifi_mode or wifi.AP), (wifi_ssid or "node_esp8266"), (wifi_pass or "_esp8266_"))

print("connecting to wifi...")
tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
    if wifi.sta.getip() == nil then
        print("IP is unavaiable, Waiting...")
        print("Configuration is done, IP is " .. wifi.sta.getip())
        print("Waiting 10 seconds before startup...")
        tmr.alarm(0, 10000, 0, startup)

Flashing the Firmware

Before flashing the firmware, we need to figure out which serial interface is used to communicate with NodeMCU using the following command:

$ 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 the firmware for NodeMCU, please download and install the following utilities:

Upload NodeMCU firmware using the command:

$ 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:

$ 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


Sometimes, you'll observe frequent blinking of the blue LED after a firmware upload. This is probably related to missing initialization data. Use thefollowing command to fix this:

$ 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

And sometimes you are not able to upload Lua files. Try to reset your device and execute the command again within the first 10 seconds after reset. If that doesn't work, try to delete init.lua code from NodeMCU.

$ sudo ./luatool.py --port /dev/ttyUSB0 -b 115200 --delete init.lua

Data Visualization

In order to simplify this guide, we have included our "Temperature and Humidity Demo Dashboard" in the demo data that is available in each Thingsboard installation. 

Of course, you can modify this dashboard: tune it, add or delete widgets, etc. You can access this dashboard by logging in as a tenant administrator:

  • login: tenant@thingsboard.org

  • password: tenant

Once logged in, open Dashboards->Temperature & Humidity Demo Dashboard. You should observe the demo dashboard with live data from your device. A screenshot of the dashboard is attached below. 

Image title


Now you've seen NodeMCU and Thingsboard demonstrate how open-source frameworks may be used together in order to quickly prototype IoT projects and solutions. This tutorial covers data collection over MQTT and real-time visualization use cases. However, Thingsboard supports other network protocols (CoAP and HTTP) and has other useful features. I am going to cover them in my next articles.

Useful Links

Digi-Key’s IoT Component Selector is your one-stop-shop for the IoT

iot app development ,iot ,nodemcu ,mqtt ,thingsboard

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}