Skill Level: Any Skill Level


1 Raspberry Pi v1.0 B ou newer - 1 sensor DHT 11 - Protoboard e jumpers


Protoboard e jumpers




  1. Introdution

    The purpose of this editorial is to build an Internet of Things application that performs the temperature and humidity sensing using DHT11 sensor on a Raspberry Pi sending data to the cloud of the IBM Bluemix. The internet platform of IBM things is based on the Node-RED tool for this tutorial is using the Node-RED installed either on the Raspberry Pi as the Bluemix.

  2. Construction, layout and sensor coding

    The wiring diagram is quite simple, the DHT11 works at 5V voltage 3.3va, therefore can be directly connected to GPIO of the Raspberry Pi. The DHT11 has four terminals (VCC, Sign, not connected, GND), then the VCC pin is connected to the pin of 3.3V GPIO, the signal pin is connected to GPIO 04 and GND GND of the sensor to the GPIO of the Raspberry Pi as illustrated in the following diagram.

    The next step was the installation of the Raspberry Pi Node-RED tool by following the tutorial steps: http://nodered.org/docs/hardware/raspberrypi.html. As the first version of Raspberry Pi was used, the tutorial indicated for this release was followed:

    Download debian package:

     wget http://node-arm.herokuapp.com/node_archive_armhf.deb

    Install package:

     sudo dpkg -i node_archive_armhf.deb

    Install dependencies:

     sudo apt-get install build-essential python-dev python-rpi.gpio

    Once the installation is complete, the users default directory pi run the tool through the node-red command:

    pi@raspberrypi ~ $ node-red

    The following log is displayed on screen:

    Welcome to Node-RED

    31 Dec 17:07:06 [info] Node-RED version: v0.12.4
    31 Dec 17:07:06 [info] Node.js version: v0.12.6
    31 Dec 17:07:06 [info] Loading palette nodes
    31 Dec 17:07:30 [info] Settings file : /home/pi/.node-red/settings.js
    31 Dec 17:07:30 [info] User directory : /home/pi/.node-red
    31 Dec 17:07:30 [info] Flows file : /home/pi/.node-red/flows_raspberrypi.json
    31 Dec 17:07:30 [info] Server now running at
    31 Dec 17:07:31 [info] Starting flows
    31 Dec 17:07:31 [info] Started flows

    Accessing through a browser raspberry IP port 1880 will be presented in the interface below confirming that the installation was successful, after confirming, press Ctrl + C in the bash terminal to terminate execution of the tool.

    Despite the Node-RED have a set of blocks that reads the GPIO Raspberry Pi, the next step we will install a library written in JavaScript for Node.js language that lets you directly access data from DHT11 sensor. The Node-DHT-Sensor bibioteca can be found at link: https://www.npmjs.com/package/node-dht-sensor, but first we need to install a dependency that is the library in C language fit called BCM2835 (http://www.airspayce.com/mikem/bcm2835/):

    Download package:

     wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.48.tar.gz


     tar zxvf bcm2835-1.48.tar.gz

    Compile process:

    cd bcm2835-1.48
    sudo make check
    sudo make install

    After completing the installation of the BCM2835 library, we can install the Node-DHT-Sensor library:

     sudo npm install node-dht-sensor

    IMPORTANT: I suggest running this last step twice, the first with the SUDO command and another without SUDO. So the library will be installed both in the context of user libraries pi of node-red which is in /home/pi/.node-red/node_modules/node-dht-sensor folder and in the context of the node root user libraries -red which is in the /usr/local/lib/node_modules/node-red/node_modules.

    The Node-RED does not allow it to be used the required command (library-name) within its function blocks for security reasons. For the Node-DHT-Sensor library is available you must change the settins.js File Node-RED, noting that there are two files (one in the context root and the other on the users context pi) that are respectively in the directories:


    In both files look for the function declaration function Global Context and insert the line:


    Now we can start the sensor code for that run the node-red tool with SUDO policy, this is necessary so that there is the error of lack of permission library BCM2835 access to GPIO.

    sudo node-red

  3. Coding

    Drag a block to inject the sheet1, this pack aims to make the trigger job that starts the algorithm executed by the sensor. For timing the trigger double click on the inject block and set the property to generate repeat the event every ten seconds, confirm the OK button.

    The second step is to include a calling function block that allows pure javascript code within your application. Note that after adding a connection block was carried out between the two blocks, clicking with the cursor in the terminal inject block and connect it to the function block terminal. This causes a block trigger each other or pass information in a message format (msg) to the connected block. After connecting the blocks double click on the function block and enter the source code as follows:

    var sensorLib = context.global.sensorLib;
    sensorLib.initialize(11, 4);
    var readout = sensorLib.read();
    msg.payload = {temp:readout.temperature.toFixed(2), hum:readout.humidity.toFixed(2)};
    return msg;

    The last step is to include a debug block, you will temporarily display the data collected from the sensor. So drag the reed debug button, connect the function block to block debug this way the message generated in the function will be passed as parameter to the block. No configuration is required in debug block. Finally and perform the test trigger the deploy button is on the top tool bar on success of the deployment, see the contents of the debug tab will display the temperature and humidity data collected from the sensor.

  4. Application construction in Bluemix

    Now we can start building the application in the cloud that will be responsible for receiving application data, for this we will use IBM Bluemix (https://console.ng.bluemix.net/) on the main screen to access the catalog option. Select the Node-RED Starter option

    The next application creation wizard screen enter the name that will be considered for the creation of the same address, and confirm the CREATE button.

    Wait for the Bluemix create the application and at the top of the next screen of the wizard will display the URL to access your instance of Node-RED in the cloud, just click the URL as the following image. When the Interface Node-RED in BlueMix welcome screen loads click the go button to your Node-RED flow editor.

    To start a server coding will use the websocket protocol because of its simplicity, although it should be clear that a real solution of IoT must use more robust protocols such as MQTT. So drag the websocket block for sheet1 and double-click on the block, set its type to Listen on the path field, select the icon editing, and editing window enter the path /ws/temp and select the option that will be sent/received payload property of the message and confirm the update button/confirm.

    To finish the server coding will use the same debug block that was used in the sensor, just drag the reed block and connect to websocket. Also important to note that in a real application, the data received from the sensors must be processed and stored in a database. The Node-REDs Bluemix own custom blocks that allow the storage of data in NoSQL databases. After coding trigger the deploy button and change the focus to the tab debug, note that messages are not reaching the server because we need to modify the sensor NODE code to send to the server.

    The last step is to return to the Node-RED flow sensor editor in Raspberry Pi and include a new websocket output block, connect the block to function in parallel to debug block. Double-click to edit websocket block on the screen select the type connect to and enter the complete URL of your application on the screen Edit Websocket. Once completed, click the deploy button. Return to the Node-RED in IBM Bluemix and note that the messages begin to arrive at the server.

    Node-RED sensor flow code

     [{"id":"d000c98d.2fff38","type":"websocket-client","z":"f3abdcef.0c542","path":"ws://univilledht11sensor.mybluemix.net/ws/temp","wholemsg":"false"},{"id":"80d99abe.7f2668","type":"inject","z":"f3abdcef.0c542","name":"","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":166,"y":143,"wires":[["f647d48f.09b828"]]},{"id":"f647d48f.09b828","type":"function","z":"f3abdcef.0c542","name":"sensorread","func":"var sensorLib = context.global.sensorLib;n nsensorLib.initialize(11, 4);nvar readout = sensorLib.read();nmsg.topic="temp";nmsg.payload = {temp:readout.temperature.toFixed(2),n hum:readout.humidity.toFixed(2)};nconsole.log(msg);nreturn msg;","outputs":1,"noerr":0,"x":376,"y":214,"wires":[["bb2124b9.44ded8","e290a1d4.1d6f6"]]},{"id":"bb2124b9.44ded8","type":"debug","z":"f3abdcef.0c542","name":"","active":false,"console":"false","complete":"true","x":613,"y":256,"wires":[]},{"id":"e290a1d4.1d6f6","type":"websocket out","z":"f3abdcef.0c542","name":"","server":"","client":"d000c98d.2fff38","x":739,"y":126,"wires":[]}]

    Node-RED Bluemix Server code

     [{"id":"61c3751b.9e3c8c","type":"websocket-listener","z":"a777d62e.588828","path":"/ws/temp","wholemsg":"false"},{"id":"8ad9eea6.75261","type":"websocket in","z":"a777d62e.588828","name":"","server":"61c3751b.9e3c8c","client":"","x":256,"y":138,"wires":[["4478ab1c.bb8754"]]},{"id":"4478ab1c.bb8754","type":"debug","z":"a777d62e.588828","name":"","active":true,"console":"false","complete":"false","x":509,"y":230,"wires":[]}]

9 comments on"IoT temperature and moisture sensor integrated to Node-RED and Bluemix IBM"

  1. inject payload is mentioned as Blank in the input section, can some one please assist in clarifying which property to take

  2. Mr. Sgera thanks for your comment. When you asked about payload injection, you are coding server side or sensor side?

  3. In text doc Instead of – sensorLib:require(node-dht-sensor), – it should be – sensorLib:require(‘node-dht-sensor’), – like in sample pic. Thx for guide.

  4. Hi , I have installed everything as per your steps. But after injecting to the payload I got the following error.

    [error] [function:sentemp] TypeError: Cannot read property ‘initialize’ of undefined

    Kindly advise on this.

    • pantafanta June 08, 2017

      Hi Preshy,
      I got the same error, it can be solved by sensorLib:require(“node-dht-sensor”) [with “”] in settings.js files.

  5. Hi ,

    I am still getting same error “Cannot read property ‘initialize’ of undefined”. I tried with ” as well as “” in settings.js files .

  6. Hi

    When I change the pin in the json code and the DHT11 sensor is still connected to the same pin on RPi, I receive same message: {“temp”:”0″,”humidity”:”0″} .
    Any help?

  7. I don’t get messages , at the last step, in Node-RED-Bluemix app from Node-RED-RPi. Debug tab is empty!
    Because of websockets or ?

  8. It is a nice recipe.
    One little remark regarding the title. I think it should be “humidity sensor” and not “moisture sensor”.

Join The Discussion