22 October, 2016

rtl_433: simple daemon and tools

Last time I wrote about simple awk script that prints the latest values collected from rtl_433 application. I was pretty much satisfied with this "quick and dirty" solution until recently, when I needed to use the data from two different projects...
Right. It is time for simple daemon solution that will collect the data in format that makes it easy to be used from different clients. And here is my code:


#!/usr/bin/awk -f
BEGIN{
  FS=","
  cmd="rtl_433 -F csv -q -p 24 2> /dev/null"

  # Get csv column labels and count
  cmd | getline; csvNF= NF;  header=$0;

  while( cmd | getline){
    if (NF==csvNF){

      if ($2=="Proove") sensor=$3+$14
      else sensor=$3$4

      fn= "/dev/shm/rtl_"sensor
      print $0 > fn; close(fn);

      if (($5!="OK") && ($5!="")) {
        fn= "/dev/shm/rtl-BAT"
        print $0 >> fn; close(fn);
      }
    }
  }
}


Disclaimer: The script/daemon is meant to be simple and it is "designed" solely for my purposes i.e. it takes care for my particular situation - several temperature sensors, remote controls, motion sensors etc.
It stores the latest values in files in the shared memory /dev/shm. This way it is easy to access them and it does not wear the SD card. Note that you will lose the values if the computer is rebooted. I am also interested if some of the sensors has complained for low battery - this is stored in separate file. Almost all of the remotes have 3 channels, so to simplify the situation, I have concatenated the sensor ID with the channel, to separate them in different files. This is how the folder looks:
...
-rw-r--r-- 1 pi pi 138 Oct 22 06:48 rtl_411
-rw-r--r-- 1 pi pi 118 Oct 21 10:05 rtl_42449921
-rw-r--r-- 1 pi pi 117 Oct 22 08:12 rtl_42449922
-rw-r--r-- 1 pi pi 118 Oct 22 07:25 rtl_43876366
-rw-r--r-- 1 pi pi 118 Oct 22 09:40 rtl_52337311
-rw-r--r-- 1 pi pi 118 Oct 20 09:38 rtl_52597761
-rw-r--r-- 1 pi pi 118 Oct 21 07:33 rtl_52597762
...

and each file contains the data in csv format:
2016-10-22 10:14:39,WT450 sensor,15,1,OK,4.520,,,,74,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-10-22 08:56:50,Proove,52673473,3,,,,,,,OFF,,,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

At this point, you are free to use whatever program you want to visualize or monitor the data. Here I include my monitoring bash script with 3 variants of pooling over the data. I use the latest, since it suits me best. All variants have advantages and disadvantages, so choose what suits you. The "watch solution" is simple and clean, but does not capture when new sensor is detected. The "sleep" variant is mimicking the first but solves the problem of the first (new sensor in the pool). The problem with both is that they print the collected data every 2s or whatever you select. This is not perfect for use over mobile connection... Instead, the third variant is using inotifywait to detect changes and refresh the screen.


#!/bin/bash

NFILES=0
clear

#watch -c -t rtl_433_parse.awk /dev/shm/rtl_*

#while [ 1 -gt 0 ]
#do
#  echo -ne "\033[0;0H"
#  ntmp=$(ls -1 /dev/shm/rtl_* | wc -l)
#  if [ $ntmp != $NFILES ]; then clear ; NFILES=$ntmp; fi
#  rtl_433_parse.awk /dev/shm/rtl_*
#  sleep 2
#done

while true
do
  echo -ne "\033[0;0H"
  ntmp=$(ls -1 /dev/shm/rtl_* | wc -l)
  if [ $ntmp != $NFILES ]; then clear ; NFILES=$ntmp; fi
  rtl_433_parse.awk /dev/shm/rtl_*
  inotifywait -qq -e close_write /dev/shm
done


echo -ne "\033[0;0H" avoids using the clear command, which makes the terminal flickering.

As you can see, I use awk to parse and visualize my data. The script is an improved version of the old script. I highlight the data that was collected within 2 minutes to easily track activity. I will publish the script once I polish the minor visual problem that I encounter now and then. 

 







No comments: