01 May, 2015

Tellstick Duo revisited

For one or another reason I have replaced/upgraded my first Raspberry Pi (RPi) /256MB RAM/ with the latest RPi2 - the advantages are kind of obvious. I used to run the old telldus-core and now it was perfect time to upgrade it and rewrite my programs written in python with bash script using the new features in telldus-core-2.1.2.

I have tried before but failed with the device recognition and lost my patience to find the cause for the problem i.e. missing controller configuration - here is the solution.

There is really nice way to compile everything in a very clean way: instructions written in Swedish (perhaps here as well ). I got tempted to try the GUI suggested there but the repository was 5 years old which was clear NO to me.

Anyway, before I had python program running as a daemon to log selected device events and automate different switches + poor man's security monitoring solution. The only reason I was using python is to catch device and sensors triggered interrupts.


Let's start with sensors. I have three temperature and humidity sensors which data is logged and plotted for convenience. The example on the Telldus blog is simple and easy to alter for this purpose. But there is a problem. I get the data in burst of three packages having essentially identical data. In python, one uses counters to ignore the close in time events from the same device... here I was not sure how to avoid racing conditions etc. Anyway, even before I was logging only the last time of all devices by touching a zero size file with the deviceID. Now this come rather handy. Here is what I have as a bash script to log the sensors data.

#!/bin/bash

# get curent time and date - Epoch
dateE=$(date  '+%s')

# sensor file name and last access
printf -v fname "/var/tellstick/S_%03d.%1d\n" ${SENSORID} ${DATATYPE}
if [ -e $fname ]; then
  fdate=$(stat --format=%Y $fname)
else
  fdate=0
fi

# update the access time
touch $fname

# check if the device was recently used
delta=$(($dateE-${fdate}))
if [[ ${delta} -lt 7 ]]; then
  exit
fi

# get readable date
date=$(date  '+%Y-%m-%d %H:%M:%S')

echo "${date} ${VALUE}" >> ${fname}

So, I have 7 seconds of delay before the script will accept new value from the same sensor.

Here is how the device event script looks like - rather unpolished, I have to admit. Again, I have list of devices that I want to log locally, to the web or send as an SMS. Additionally SMS is sent only as many times as I set in fnotify file. The value I can change from home written web interface, via SMS command or terminal. For SMS gate I use the same solution as before: SMS Gateway. The code is way cleaner and easier to follow than the code I had in python.

#!/bin/bash

deviceIdDO=""
deviceIdLOG=" 10 14 17 18 19 "
deviceIdWEB=" 10 14 "
deviceIdSMS=" 10 17 "
fnotify="/var/tellstick/notify.gsm"

# some web addresses
addressWEB="http://weblogd/log.php?logstr=";
addressSMS="http://192.168.x.x:yyyy/sendsms?password=password&phone=0750000000&text="

# get curent time and date - Epoch
dateE=$(date  '+%s')

# device file name and last access
printf -v fname "/var/tellstick/D_%02d.%1d\n" ${DEVICEID} ${METHOD}
if [ -e $fname ]; then
  fdate=$(stat --format=%Y $fname)
else
  fdate=0
fi

# update the access time
touch $fname

# check if the device was recently used
delta=$(($dateE-${fdate}))
if [[ ${delta} -lt 7 ]]; then
  exit
fi

# get readable date
date=$(date  '+%Y-%m-%d %H:%M:%S')

# log in to a file if in the list
if [[ $deviceIdLOG =~ " ${DEVICEID} " ]]; then
  printf  "D_%02d.%1d    %s\n" ${DEVICEID} ${METHOD} "${date}" >> /var/tellstick/device-event.dat
fi

# log to the web if in the list
if [[ $deviceIdWEB =~ " ${DEVICEID} " ]]; then
  printf  -v text "D_%02d.%1d    %s" ${DEVICEID} ${METHOD}
  wget $addressWEB"${text}"   -O /dev/null -o /dev/null
fi

# Send SMS if in the list
if [[ $deviceIdSMS =~ " ${DEVICEID} " ]]; then
  if [ -e ${fnotify} ]; then
    nSMS=$(cat ${fnotify})
    if [[ $nSMS -gt 0 ]]; then
      printf  -v text "D_%02d.%1d    %s" ${DEVICEID} ${METHOD}
      wget $addressSMS"${text}"   -O /dev/null -o /dev/null
      nSMS=$((nSMS-1))
      echo -n $nSMS > ${fnotify}
    fi
  fi
fi

Notice the spaces around the deviceIDs in the list and when I compare them. This is to avoid "10" matching "100" etc. Probably there is a better way, but this is what came to me first as solution...

Here is a snapshot of some of the data collected recently.

No comments: