Node Red IoT with Arduino DHT11

 Node Red is a graphical programming environment suitable for building IoT(Internet of Things) application quickly. Here it is illustrated how one can quickly build IoT application with Node Red, Arduino and DHT11 temperature and humidity sensor module. For illustration purpose we will we use DHT11 humidity and temperature sensor module with Arduino to capture surrounding humidity and temperature and send it over the serial port to the PC. The node red is installed on the PC and using node red the sensor data is displayed on a browser. The data are displayed using gauge meters and can be opened on local IP or public IP address. 

Example is shown below.

If you are new to node red and if you are interested in using node red with Arduino you can first read the simple LED state display tutorial Node Red with Arduino Simple Example.

DHT and Arduino

First we need to connect DHT11 module with Arduino. Here Arduino Nano is used but you can use any other similar Arduino such as Arduino Uno or Mega. 

The following shows the circuit diagram for connecting Arduino nano and DHT11 sensor module.

DHT11 Arduino Nano

The DHT VCC and ground pin is connected to 5V pin and ground pin of Arduino Nano respectively. The DHT11 data pin is connected to pin 7 of the Arduino. 

This circuit on actual boardboard is shown below.

The Arduino program code to read DHT11 data and send then serially over the USB is below.

// Read DHT11 sensor and send serially to PC

#include <DHT.h>         // Include Adafruit DHT11 Sensors Library

#define DHTPIN 7          // DHT11 Output Pin connection
#define DHTTYPE DHT11     // DHT Type is DHT11

DHT dht(DHTPIN, DHTTYPE);   // Initialize DHT sensor

void setup () {
  Serial.begin(9600);         // To see data on serial monitor

void loop (){

    float H = dht.readHumidity();     //Read Humidity
    float T = dht.readTemperature();    // Read temperature as Celsius

    // Check if any reads failed and if exit
    if (isnan(H) || isnan(T)){
      Serial.println("Failed to read from DHT sensor!");
    // Combine Humidity and Temperature into single string
    String dhtData = String(H) + "," + String(T);
    delay(2000);   // Wait two seconds between measurements

This is basic code which just reads in the DHT11 sensor data and sends to PC. For details on how it works see the earlier tutorial DHT11 Humidity and Temperature Sensor with Arduino.

You should compile and upload this code and then open the serial monitor in the Arduino IDE and see data that is being transmitted. It should show like 65.00,23.00 with the first being the humidity data followed by a comma and the second the temperature data.

At this point you should take note on which COM port(COM17 here) you are connected to and the baud rate we have used here which is 9600.

Node Red DHT11 application

The next part is creating node red application that reads in the DHT11 sensor data and display on a dashboard. 

You should now run Node Red. Then create a flow diagram as shown.

DHT11 Arduino Red Node Flow

The node named Arduino is a "serial in" node which you can find in the network section of the node library. If you don't see the serial in node in the network library then you have to install it first. You can install it using npm or using the manage palette.

Then from the manage palette go to install tab, type serial and install the node-red-node-serialport module.

 Once installed the serial nodes should be available in the network node section of the library.

Now you have to dragged the serial in node onto the flow workspace. Instead of dragging into the node you can use CTRL with double click on the workspace to add a node(by searching serial in node).

Double click on the serial in node to open its properties window. Then select the COM port your Arduino is connected to which in this case is COM17. Also optionally you can name the node which here is Arduino.

The next node in the application is the function node named DHT11 Data Transform. This function node is available in the function section of the node library.

Drag the function node and place it after the serial in node. Then double click it to open its configuration window. In the window and in the on message tab type in the following code:

m = msg.payload.split(',');
H = {payload:parseFloat(m[0])};
T = {payload:parseFloat(m[1])};

return [H,T];

What the above code does is that it takes as input the message passed into it and then splits the data with comma as the delimiter and puts them into an array called m. Then we use two variables H and T and store the value of humidity and temperature as payload into those variables. the we return the message with value of H and T.

We can give the function a name like DHT11 Data Transform.

 Next click on the Setup tab and choose 2 as output.

You should see two output nodes. 

Next from the dashboard section in the library drag two gauge nodes on the workspace.

If you don't see the dashboard nodes in the library then you might not have installed it. It must be installed because it does not come with the default new node red application. As in case of serial library described above, go to the manage palette, then to the install tab, search for dashboard and install the node-red-dashboard.

Connect the two output nodes fro the function node to these two gauge nodes. 

Node Red Tab and Group

The dashboard nodes requires tab and group names so that they can be manipulated for display.  On the node red configuration palette click on dashboard.

Then click on the tab to create a new tab.

 In the tab configuration window provide the name for the tab like Humidity/Temperature in this example.

From the newly created tab add a new group.

 In the group edit window, type in the name for the group such as DHT11 here. Humidity/Temperature should be selected in the Tab and type in 8 in the Width section.

Click on the gauge node for each humidity and temperature data and select the tab/group as shown below.

The size should be selected as 4x4 for both the gauge node. In the value format section add a % for the humidity gauge and °C for the temperature. 

The node red programming at this point is complete and the next we will deploy it and start the application. 

Click on the deploy button. This is check any nodes for error and inform the user about it.

Next from the dropdown menu click on start to start the node red application.

Then go to localhost:1880/ui to open the dashboard or use the dashboard open link as shown below.

Then you should see the DHT11 humidity and temperature live data in the browser like the one shown below.

 You may have noticed that in this example, the url is not http://localhost:1880 but This is because here custom domain is used to display the DHT11 sensor data which is publicly available and hence illustrates how IoT application can be build with node-red IoT framework. The same display will also be available at  http://localhost:1880. To use custom domain for actual IoT implementation see the tutorial Deploy Arduino Red Node App on Web with Cloudflare Tunnel.

So in this way you can easily create DHT11 sensor or other sensor based IoT application with Node Red and Arduino. See next node red IR Proximity Sensor IoT Application tutorial .

Post a Comment

Previous Post Next Post