Weather Monitoring with SDR and Cacti

Weather Monitoring with SDR and Cacti

This post will cover how to monitor a Acurite temperature and humidity sensor with RTL_433 and a SDR dongle. Values from the sensor will be collected using a shell script and the results will be graphed in Cacti. Let’s begin.

The Acurite sensor used is model number 06002M and it is the indoor outdoor sensor. It can be found on Amazon and currently lists for $13 each, http://a.co/d/fPybnET. It is powered by 2 AA batteries. The manufacturer suggests using Lithium type AA batteries for locations that go below freezing. The sensor offers 3 band selections, A through C which run on the 433 Mhz RF band. Once power is applied, it begins to transmit readings every 30 seconds and a led on the sensor flashes to indicate operation. The manufacturer recommends keeping the sensor out of direct sunlight or rain. The distance limit to receiving stations designed for use with the sensor is 100 meters. More details can be found on the manufactures website, https://www.acurite.com.

RTL_433 and SDR hardware were covered in a previous post, refer to that for details on setup and operation.

The shell script used in this example will be gathering sensor readings from sensors located in 2 different areas. Here is the code. Be sure to set permissions on the shell script so it can be executed.

sudo nano /home/local/Tasks/AcuriteSensors.sh
#!/bin/sh
# /home/local/Tasks/AcuriteSensors.sh
# Version 1.2 (12102018)
# Purpose - Run SDR scan of Acurite Temp / Humidity Sensors and log readings
# Author - Patrick Gilfeather - CloudACM

# Delay the start of the script for 2 minutes so system has time to boot
sleep 120

# Run the process in a loop endlessly
while :
do

# Set variables and define work files
AcuriteSensor="/home/local/Tasks/AcuriteSensor.tmp"
AcuriteSensorTmp1="/home/local/Tasks/AcuriteSensorTmp1.tmp"
AcuriteSensorTmp2="/home/local/Tasks/AcuriteSensorTmp2.tmp"
AcuriteSensorTmpA="/home/local/Tasks/AcuriteSensorTmpA.tmp"
AcuriteSensorTmpB="/home/local/Tasks/AcuriteSensorTmpB.tmp"
AcuriteSensorTmpC="/home/local/Tasks/AcuriteSensorTmpC.tmp"

# Resulting Logs with final readings - Cacti References these
AcuriteSensorBathRoomTemp="/home/local/Tasks/BathRoomTempResults.log"
AcuriteSensorBathRoomHumidity="/home/local/Tasks/BathRoomHumidityResults.log"
AcuriteSensorBillTemp="/home/local/Tasks/BillTempResults.log"
AcuriteSensorBillHumidity="/home/local/Tasks/BillHumidityResults.log"
AcuriteSensorDuckTemp="/home/local/Tasks/DuckTempResults.log"
AcuriteSensorDuckHumidity="/home/local/Tasks/DuckHumidityResults.log"


# Use the full process path, use "whereis rtl_433" to find it if not known
# Frequency is defined, it can be found using SDRSharp or GQRX, -f 433942200
# This command will only detect Acurite sensor protocols, -R 40 switch
# It is also set to run for 65 seconds which should be enough to get readings, -T 65 switch
/usr/local/bin/rtl_433 -f 433942200 -q -R 40 -T 65 > $AcuriteSensor

# Format output so each reading is a single string of text
sed "s/\t/ /" $AcuriteSensor > $AcuriteSensorTmp1
tr "\n" " " < $AcuriteSensorTmp1 > $AcuriteSensorTmp2
sed -i 's/2018/\n2018/g' $AcuriteSensorTmp2

# The following lines uniquely identify each sensor
# Use RTL_433 to inventory sensors to establish the correct identifier
grep "0x1b38" $AcuriteSensorTmp2 > $AcuriteSensorTmpA
grep "0x009f" $AcuriteSensorTmp2 > $AcuriteSensorTmpB
grep "0x39fb" $AcuriteSensorTmp2 > $AcuriteSensorTmpC

# Bathroom sensor readings, Identifier 0x1b38
# The temperturea value is in the 14 block while humidity is in the 17 block of the text string
AcuriteSensorTmp5=$(tail -1 $AcuriteSensorTmpA)
AcuriteSensorTmp6=$(echo "$AcuriteSensorTmp5" | awk '{print $14}')
AcuriteSensorTmp7=$(echo "$AcuriteSensorTmp5" | awk '{print $17}')
echo $AcuriteSensorTmp6 > "$AcuriteSensorBathRoomTemp" 
echo $AcuriteSensorTmp7 > "$AcuriteSensorBathRoomHumidity" 

# Pet lizard home sensor readings, Identifier 0x009f
# The temperturea value is in the 14 block while humidity is in the 17 block of the text string
AcuriteSensorTmp5=$(tail -1 $AcuriteSensorTmpB)
AcuriteSensorTmp6=$(echo "$AcuriteSensorTmp5" | awk '{print $14}')
AcuriteSensorTmp7=$(echo "$AcuriteSensorTmp5" | awk '{print $17}')
echo $AcuriteSensorTmp6 > "$AcuriteSensorBillTemp" 
echo $AcuriteSensorTmp7 > "$AcuriteSensorBillHumidity" 

# Duck Coop sensor readings, Identifier 0x39fb
# The temperturea value is in the 14 block while humidity is in the 17 block of the text string
AcuriteSensorTmp5=$(tail -1 $AcuriteSensorTmpC)
AcuriteSensorTmp6=$(echo "$AcuriteSensorTmp5" | awk '{print $14}')
AcuriteSensorTmp7=$(echo "$AcuriteSensorTmp5" | awk '{print $17}')
echo $AcuriteSensorTmp6 > "$AcuriteSensorDuckTemp" 
echo $AcuriteSensorTmp7 > "$AcuriteSensorDuckHumidity"

# Clean up work files
rm /home/local/Tasks/*.tmp

# Pause the process for a bit and start again, attempt to run every 5 minutes.
sleep 230
done

exit

The shell script is set to run from a Raspberry Pi at startup. This was done using Webmin > System > Scheduled Cron Jobs. The task is set to run as the standard user and the command defined is “/home/local/Tasks/AcuriteSensors.sh”. It uses the Simple schedule setting “When system boots”.

The script comments should be enough to go on how it works. Now for setting up Cacti to read the results and graph them. For that I created a perl script that does the leg work. Enter in this command at the terminal window.

sudo nano /usr/share/cacti/site/scripts/Duck_Temp.pl

Here is the code for that perl script.

#!/usr/bin/perl

use strict;
use warnings;

open my $DuckFileT, '<', "/home/local/Tasks/DuckTempResults.log"; 
my $DuckFirstLineT = <$DuckFileT>; 
print $DuckFirstLineT;
close $DuckFileT;

Once all the text is entered into the file, write it, save it, then close it. Now lets change the permissions to the file so it is level 0755. That’s it, now we’re ready to get inside Cacti and get this value polled and graphed.

Login to Cacti and go to the Console tab and select Data Input Methods under the Collection Methods section, then select Add. Next enter in the name of the Data Input Method, “Localhost – Duck Temperature”. Choose Input Type as “Script/Command”. Enter in the Input String “perl /usr/share/cacti/site/scripts/Duck_Temp.pl” then click Create.

Now Enter in the Output Fields section by clicking the Add link. In the Field [Output] entry put “ducktemp”, next enter in the Friendly Name “Duck Celsius” followed by the Create button.

From there we continue on to the Templates section and choose Data Templates followed by clicking the Add link. Enter in the Data Templates Name “Localhost Duck Temperature” and the Data Source Name “|host_description| – Duck Temperature”. Next choose from the pulldown menu for the Data Input Method “Localhost – Duck Temperature”. In the Data Source Item enter in “ducktemp” for the Internal Data Source Name. I then set my Maximum Value at “50” followed by clicking the Create button. When I did that the Output Field auto populated with “ducktemp – Duck Celsius”.

Next I went back to the Templates section and choose Graph Templates followed by clicking the Add link. Here I entered in the Template Name “Duck Temperature” and the Graph Template Name “|host_description| – Duck Temperature”. Then I set the Vertical Label to “Celsius” and clicked the Create button.

Now I added the Graph Template Item by clicking the Add link. From the Data Source pulldown I selected “Localhost Duck Temperature – (ducktemp)”. Then I set the color from the pulldown “FF00FF”. Then I set the Graph Item Type to “AREA”. Afterward, I set the Text Format to “Duck Celsius” and clicked Create.

From there I then went to the Management section and selected Devices. In the list, I selected “Localhost”. In the Associated Graph Template portion, I selected “Duck Temperature” from the pulldown and clicked the Add button. Then up above, I clicked the “Create Graphs for this Host”. In the next window, I check to box next to “Create: Duck Temperature”, followed by the Create button.

Next I went to the Management section and choose Graph Management. I clicked on the entry “Localhost – Duck Temperature” and verified the entries were correct. I did the same verification under the Templates section for Graph Templates.

It’ll take time for “stuff” to populate the graph, but you should see a graph listed when you go to the Graphs tab. Cacti’s inner workings is beyond this subject matter, so I’ll leave it at that.

Comments are closed.