At the end of this project you will know how to get data from different sensors you can place around your home and display the gathered data on graphs.
In case you want to use a Raspberry Pi, please note that you will need to use an Arduino as well. No analog pin dependent piece can be used with your Raspberry Pi only. You will need the help of an Arduino board.
Let's begin with the temperature sensor.
In this figure you can see a 3-leg temperature sensor. There is an alternative to this one, the thermistor. The latter has two legs and it should be connected as a voltage divider, which is explained in the electronics chapter.
In this case, the sensor needs to be connected to the GND pin, the VCC and an analog pin. This sensor consists of a thermistor and several other electronic pieces. The leg situated in the middle outputs a different voltage depending on the temperature.
In order to get the values from the the temperature sensor you will just have to use the usual Analog read blocks and read the analog pin. The values that you obtain are in a range of 0 to 1023. However, this doesn't look at all like the values you would expect from a thermometer.
To transform these values into Celsius degrees, you are going to use this formula.
temperature= (value*5/1024-0.500)*100
How does it look in Streams programming language? You need no more than 3 nodes.
The first mandatory one, which is run. The next one will read the value that comes from the analog pin, in this case A0. Now the payload of the message sent will be this very value, between 0 and 1023. Now, you have to convert it to Celsius degrees.
To get from this value to Celsius degrees you have to follow the formula above. The resulting value is stored in the temperature variable.
To write the formula you can use either a visual node or a formula node. Choose the latter and the code inside should look like the one in the next figure. In this JavaScipt function you create an object called new_message and a variable with the value of the message from the payload. Next, you apply the formula from which you obtain the temperature in degrees and make it the message to be sent forward. Your function will return this very message.
As promised, you will now display the temperature on a thermometer using the send signal node.
In dashboard choose the thermometer and assign it a signal with the same name as use the nodes
The send signal node stores the value it receives from the previous block and sends it further to the dashboard, where a graph with the exact same name as the node's signal is searched. Once found, the connection is established. The node will receive the signal the data, store it and send it to the graph via a signal.
As mentioned at the beginning, the same project can be easily built with a thermistor.
Note that there is a difference between the two sensors, consisting of the fact that the temperature sensor previously used has a linear resistance-temperature characteristic, while the other one usually has the output voltage as a logarithmic or exponential function.
The wiring is quite simple, just follow the schematics of a voltage divider.
For the code you will need to know that this time the formulas you use are different to convert the values into Centigrade.
V_m = analogRead(pin) * 5/1023
R_t = 5/V_m * 10000
Ratio = 1/B* ln(R_t/10000)
temperature = 1/(1/298 + Ratio)
B = 4050, is one of the constants that comes with the sensor's specifications.
Vm is the voltage that you get in your analog input while Rt is the resistance of the thermistor. By having this value, you just have to apply the following formulas and you will obtain the temperature in Celsius degrees. You only have to calculate what the formulas above will give and put it in a variable. Send the value of this variable to the thermometer in the Dashboard. The only difference will be in the function that you use to do the conversion. In Visual Programming, the same function would look like this:
You that now have a thermometer can you find something extra to make it more interesting? Try a light sensor.
The photocell works just like a voltage divider, as well (check the Introduction to Electronics chapter for more details). You need to connect it to the Gnd and Vcc and from between the photocell and the resistor to the analog pin of the board. This light sensor has a resistance that varies with the light, the more light there is, the lower the resistance of the photocell. Depending on how you place the sensor you can obtain a pull up or pull down voltage divider.
The new nodes are much simpler. You just add a block to read the analog pin that you use and send it to the chart.