07 March, 2016

rtl_433: simple text monitoring panel

There is this really nice rtl_433 "application using librtlsdr to decode the temperature from a wireless temperature sensor (433.92MHz)"  https://github.com/merbanan/rtl_433 which does exactly what is says. 

I just thought that I can write a simple script that will keep showing only the latest decoded data.
As one can see in the example output, further below, some of the sensors sends the data 3 times and if you have couple of them the data starts to get a bit difficult to read. With a small awk script (tailored for my needs) I can get this simple output that contains only the interesting data and few colors to prettify it:

===================================================================================
|        Time         |               Name             |  ID  |  Temp |   %  | Bat
===================================================================================
| 2016-03-07 10:26:19 | Gym - WT450                    |   61 |   5.4 |  84% | OK
| 2016-03-07 10:26:13 | Outdoor - Nexus                |   92 |   1.5 |  90% | OK
| 2016-03-07 10:25:51 | Indoor  - WT450                |  131 |  20.1 |  27% | OK
| 2016-03-07 10:25:45 | Outdoor - WT450                |  151 |   2.0 |  94% | OK
| 2016-03-07 10:25:37 | Ventilation - Nexus            | 1281 |  17.8 |   0% | OK
===================================================================================


The sensor data from the WT450 sensors is collected and presented by separate Telstick Duo setup. So, I use the script mainly to check that all 5 sensors are alive. All sensors are sending signals about every minute, so within a minute or two I get the "health" status of all sensor and if something new is around.

Here is how the .csv formatted output looks like:

...                            
2016-03-07 10:28:44,WT450 sensor,15,1,OK,2.050,,,,94,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,     
2016-03-07 10:28:44,WT450 sensor,15,1,OK,2.070,,,,94,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,     
2016-03-07 10:28:45,WT450 sensor,15,1,OK,2.080,,,,94,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,     
2016-03-07 10:28:51,WT450 sensor,13,1,OK,20.080,,,,27,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-03-07 10:28:51,WT450 sensor,13,1,OK,20.100,,,,27,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-03-07 10:28:51,WT450 sensor,13,1,OK,20.130,,,,27,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-03-07 10:29:19,WT450 sensor,6,1,OK,5.330,,,,84,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-03-07 10:29:19,WT450 sensor,6,1,OK,5.350,,,,84,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-03-07 10:29:19,WT450 sensor,6,1,OK,5.360,,,,84,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-03-07 10:29:25,Nexus Temperature,128,1,OK,17.800,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2016-03-07 10:29:34,Nexus Temperature/Humidity,9,2,OK,1.600,,,,90,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
...



Here is how the json formatted output looks like:

...
{"time" : "2016-03-07 10:31:44", "model" : "WT450 sensor", "id" : 15, "channel" : 1, "battery" : "OK", "temperature_C" : 2.050, "humidity" : 94}
{"time" : "2016-03-07 10:31:45", "model" : "WT450 sensor", "id" : 15, "channel" : 1, "battery" : "OK", "temperature_C" : 2.070, "humidity" : 94}
{"time" : "2016-03-07 10:31:45", "model" : "WT450 sensor", "id" : 15, "channel" : 1, "battery" : "OK", "temperature_C" : 2.080, "humidity" : 94}
{"time" : "2016-03-07 10:31:48", "model" : "Nexus Temperature/Humidity", "id" : 9, "battery" : "OK", "channel" : 2, "temperature_C" : 1.600, "humidity" : 90}
{"time" : "2016-03-07 10:31:51", "model" : "WT450 sensor", "id" : 13, "channel" : 1, "battery" : "OK", "temperature_C" : 20.080, "humidity" : 27}
{"time" : "2016-03-07 10:31:51", "model" : "WT450 sensor", "id" : 13, "channel" : 1, "battery" : "OK", "temperature_C" : 20.100, "humidity" : 27}
{"time" : "2016-03-07 10:31:51", "model" : "WT450 sensor", "id" : 13, "channel" : 1, "battery" : "OK", "temperature_C" : 20.130, "humidity" : 27}
{"time" : "2016-03-07 10:32:16", "model" : "Nexus Temperature", "id" : 128, "battery" : "OK", "channel" : 1, "temperature_C" : 17.800}
{"time" : "2016-03-07 10:32:19", "model" : "WT450 sensor", "id" : 6, "channel" : 1, "battery" : "OK", "temperature_C" : 5.330, "humidity" : 84}
{"time" : "2016-03-07 10:32:19", "model" : "WT450 sensor", "id" : 6, "channel" : 1, "battery" : "OK", "temperature_C" : 5.350, "humidity" : 84}
{"time" : "2016-03-07 10:32:20", "model" : "WT450 sensor", "id" : 6, "channel" : 1, "battery" : "OK", "temperature_C" : 5.360, "humidity" : 84}
{"time" : "2016-03-07 10:32:44", "model" : "WT450 sensor", "id" : 15, "channel" : 1, "battery" : "OK", "temperature_C" : 2.050, "humidity" : 94}
...



Even if you do not know anything about awk, it looks rather straight forward what you might want to change to make it work for you.


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

  Cgreen=  "\033[1;32m"
  Cyellow= "\033[1;33m"
  Cred=    "\033[1;31m"
  CBred=   "\033[0;41m"
  Clblue=  "\033[1;36m"
  Cblue=   "\033[1;34m"
  Cnorm=   "\033[0m"

  cmd | getline; header=$0;
  printdata()

  while( cmd | getline){
    if (NF==60){
      sensor=$3$4
      id  [sensor]= sensor
      time[sensor]= $1
      name[sensor]= $2
      bat [sensor]= $5
      temp[sensor]= $6
      humidity[sensor]= $10

      if (sensor==  61) name[sensor]= "Gym - WT450"
      if (sensor==  92) name[sensor]= "Outdoor - Nexus"
      if (sensor== 131) name[sensor]= "Indoor  - WT450"
      if (sensor== 151) name[sensor]= "Outdoor - WT450"
      if (sensor==1281) name[sensor]= "Ventilation - Nexus"

      if (bat[sensor]=="OK") {bat[sensor]=Cgreen bat[sensor] Cnorm} else {bat[sensor]=Cred bat[sensor] Cnorm}

      printdata()
    }
  }
}

function printdata(){
  system("clear")
  print "==================================================================================="
  print "|        Time         |               Name             |  ID  |  Temp |   %  | Bat"
  print "==================================================================================="
  for (i in id){
    printf("| %19s | %-30s | %4s | %5.1f | %3i% | %s\n",time[i],name[i],id[i], temp[i], humidity[i], bat[i])
  }
  print "==================================================================================="
}


P.S. I tried to optimize a bit the script to make it/me look smarter but it only made unnecessary complicated. After some experimenting, I am back to this version of the script since it is the cleanest and simplest.

No comments: