Video 4.3 using DHT 11 Raspberry Pi, part one. In this video, we will learn how to connect the HD 11 and use its data in Node red. I will take it that you know basic electronics, so I won't be explaining much. If you're not familiar with basic electronics, I have given a link in the resources for you to get up and going with electronic projects. I also have given the circuit diagram for this video in the resources. First what is DHT 11 DHT 11 is a basic ultra low cost temperature and humidity sensor.
It has a capacitive humidity sensor and a thermistor to measure humidity and temperature values from the surrounding air. It has a very basic chip that does some analog to digital version two output digital data on the data pin. Good for us, we can connect it directly to our PI. It has two other pins Vcc and ground. You can provide supply voltage from three volts to five volts. To know more about the HD 11 check the resources.
Another question of all the sensors out there by the HD 11 It is a very basic and easy to use sensor and therefore we will start our project using the HD 11. The DHT 11 will be interfaced with raspberry pi and the data from the HD 11 will be accessed by the node red running on Raspberry Pi. I'm using a Raspberry Pi three running on Raspbian stretch. You can use Jesse or stretch let's see how it goes. have been ordered to run on both in my raspberry pi. So I just use the URL to access the note rate editor.
Now we can get values from this sensor using the RPI GPIO nodes from the palette. RPI GPIO Yes, we saw them while scrolling the palette in video 2.4. There are two nodes input and output. These nodes are installed by default when we updated node j, s and Audrey using the bash command. These notes are used to interact with the GPIO pins of the Raspberry Pi using the PI GPIO daemon. Nice right.
There are even nodes to get inputs from peripherals like keyboard and mouse connected to your Raspberry Pi. Now let's test these nodes with an LED and a button. This is the 40 pin GPIO pin diagram. First, let's connect the common ground. Connect your led to GPIO four, that is the seventh pin and your button to GPIO 17 which is the 11th pin like shown in the diagram. Note that I'm using a pulldown circuit.
So whenever I press the button I'll send a value one to the 11th pin. Now let's configure these nodes so that whenever I press the button, the LED should blow. The input node should be configured with the pin the button is connected to that is GPIO 17. You can also set an internal pull up or pulldown resistor to set initial values. You can also let note rate treat the initial state during deployment. This node outputs a message dot payload And the output node should be configured to GPIO for the pin where the LED is connected to.
Here, you can select the mode of output digital, or pw M and also initialize the state of the pin. This node accepts message dot payload. Now join these both and deploy. Now, play time. Okay, let's stop here. Now that we've seen how these work, let's connect DHT 11 and use a debug node to see what its outputs.
Let's connect this we're going to connect the data pane here on GPIO four, or the seventh pin Vcc is five volts and ground to ground. Now let's modify the input node. Delete the output node and add a debug node, deploy and check. Are you getting what I'm getting? Why does it show just one because the DHT 11 output serial data, the output given out by the data pin will be in the order of eight bit of humidity integer data, plus eight bit of humidity decimal data, plus eight bit of temperature integer data, plus eight bit of fractional temperature data plus eight bit parity. To request the DHD 11 module to send these data, the input output pin has to be momentarily made low.
And then Hello, hi. Oh, so how are we going to get the sensor data We'll find a way. First, let's print the sensor data on the terminal by running a Python program. If you're thinking, but I don't know how to write a Python code, forgetting the hedge t 11 sensor data. For that, I have to go through the data sheets and spend a lot of time, then don't worry, we have libraries for the HDx 11. We will first install the add a fruit library for the HD 11 which will take care of all those complexities and display its data.
I have given the list of commands to download the library in the resources. First, let's do an update and install a Python library which acts as a dependency for the one we're going to install to install it if you've not already installed it before. Python dash dev is the package that contains the header files for the Python c API. Then we need to clone the Adafruit library from the GitHub repo using this command, after which you need to install the library for Python two and Python three. That's it. Now let's check out the example program present in the examples folder.
Let's select simple test.py Wait, let me clear the screen. Let us see what's inside simple test.py. We are importing the module here. Then we are setting up the sensor type which is the HD 11 and GPIO pin number. We have to comment the first one because they have given that for BeagleBone boards. This is for Raspberry Pi boards.
Now we've connected our DHD 11 at seventh pin GPIO four, so change 23 to four. Then we're getting the humidity and temperature values from this default function after which we are formatting it in a nice way to present. Now let's save and run this. There you go. Temperature and Humidity data. Good.
But how are we supposed to make this data available in Node read? Well, there is a way it is by using the exec node. It can be found under Advanced category in the palette. The exec node is used to run a system command and output its STD out STD error and return code. By default, the node uses exec which calls the command blocks while waiting for completion and then returns the complete result in one go along with any errors. That is, it replaces the current running process with the new process and returns its output.
To know more about exact function, check out the resources. Remember the command which we use to run the DHD 11 program, we are going to use the same command here you can append the payload to the executed command. You can also add extra input parameters, the output can be got in two modes. One is the exec mode, where the output is shown only after the whole program finishes execution and the other one is spawn mode, where the output is shown during the execution of the program. Check out the flow and the Python program in the resources which shows the difference between these two modes. You can also add a timeout To kill the program automatically after the specified number of seconds.
To know more about the exact node, please see the node help on the sidebar. Now I have one more thing to do. I want to modify the output format a little bit. You'll understand why soon done. Now let's add an inject node with interval of five seconds and a debug node to the STD output of the exec node, because we only need the output data. Now deploy.
Yeah, it's working. I wanted it this way, because now we can take the number alone and use it for further processing. Now I want you to send a tweet if the value of temperature goes above 25 degrees centigrade if the value of humidity goes below 10% you will send a mail. If both happen at the same time, then you will send data to your Android using mq TT node. I know first try out the first two tasks, and then we will come to mq TT. So how did it go?
Did it work? Good if it worked, rewatch the videos from section three. If it doesn't work, you all should look like mine. Note that I have removed the units from the print statement in the Python program to get the numbers separately and use it for processing easily. You can find this flow in the resources. Now let's come to the third task.
Mq TT ring any bells? Maybe not. I'll explain. mq TT stands for message queuing telemetry transport. It is a protocol from this we can understand that it is a kind of messaging protocol like the one that Facebook uses. Facebook uses mq TT.
Yes it does. The publish subscribe form of trend came from that. Actually, mq TT uses publish subscribe message types to communicate. In mq TT. There'll be a broker and lots of clients all in the same network. Any client can publish a message under topic, the Publish message reaches the broker.
The other clients who have subscribed to this particular topic will get the message from the broker. All right, look out for the link in the resources to learn some concepts like retain QoS and different types of messages. So why am I telling you to use mq TT can use HTTP itself right? Check out the resources section to know why. So mq TT is what we're going to use now. There are two mq TT nodes input to subscribe and output to publish.
Now let's try an example. First, we can try sending a message from one workspace to another using mq TT nodes. I'm using an inject node to define the data to which we will attach the Publish node. So let's configure this a new broker configuration. Add a broker name. Enter the server address as your Raspberry Pi address.
If you have an mq TT broker running on your Raspberry Pi, or you can use the public mq TT broker provided by hive mq using the broker address broker dot hive mq dot com. If you want to know how to set up an mq TT broker in your PI, then look for a link in the resources. If you want, you can give some client ID check out for a link in the resources to know why we use client ID. You can add your credentials for extra layer of security. These fields are optional. If you have gone through the resource, then you will know what these are.
You can make this true if you want to retain your message and choose the Q or s appropriately for your application. I'm going to set my topic in inject node to let have it as time and pass the timestamp as message. Now add a subscribe mq TT node. Here. The configured option will be automatically available. Set the topic field and QoS you can choose the The type of output you want, then connect a debug node and deploy.
Let's try super. Now let's step up a notch. Let's try controlling an LED with a button using mq TT. For that, we need to connect the GPIO in node to mq TT out node and mq TT in node to GPIO output node. Pause the video and configure accordingly. Something like this.
Good. Now let's deploy and test it out. Give me a five. Awesome. Now we have to complete that last task. We'll do that along with some other cool stuff in the next lecture.
In this video, we learned about how to connect the HD 11 to node grid, and how we can get its data into node red. In the next video, we'll finish this pending task and see how does dashboard work on your Android