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);
      }
    }
  }
}