Month: January 2015

Turn.py – Using Python to Control the Telldus

There are at least two python libraries for  communicating with the Telldus service. One of them is ‘tellcore-py’ which is an active github project (link). Installing the package by using pip (“python install python”):

 > pip install tellcore-py

The tellcore library is wrapper of the telldus C/C++ library which communicates directly with the Telldus service (the telldusd – daemon). This service was configured in last blog post and it uses the libusb library to send messages/commands via usb to the Tellstick DUO – see picture below.

                              +               
                 +---------+  |               
 tellcore.py +-> |telldusd/|  |  Tellstick DUO
                 |libusb   +---->             
                 +---------+  |               
                              +               
                                  

However in this case only interesting in a narrow use of the library i.e. turning on and off the switches. Later on if buying sensors it will be interesting collecting data through the use of this library.

Here is a small utility ‘turn.py’ written in python using the library, doing what I need – not more not less:

#!/usr/sbin/python
import sys
 import tellcore.telldus
 
 core = tellcore.telldus.TelldusCore()
 
 def turn(state, device):
     devices = [d for d in core.devices() if d.name == device or device == 'all']
 
     for d in devices:
        m = getattr(d, "turn_" + state)
        m()
   
 if __name__ == "__main__":
     if len(sys.argv) == 3:
        turn(sys.argv[1], sys.argv[2])
     else:
        print "turn "
 

Trying it out by first setting the executable flag and then running it directly by turning on/off switches

 > chmod u+x turn.py
 > turn on wallswitch3
 > turn off all

Configure the Telldus Service – Part 2

In part 1 we assured that the service was following best practice in linux be creating a separate ‘telldus’ user and letting the device handle also be owned by that ‘telldus’ user.
Now it is time to configure the light switches for the Telldus service. The light switches are defined in several JSON data structures, one for each light switch. The format is defined by Telldus on the following page.

For example here is the definition from /etc/tellstick.config of two switches from Conrad:

device {
  id = 1
  name = "wallswitch1"  
  protocol = "risingsun"
  model = "selflearning:conrad"
  parameters {
    house = "31942"
    unit = "1"
  }
}
device {
  id = 2
  name = "wallswitch2"  
  protocol = "risingsun"
  model = "selflearning:conrad"
  parameters {  
    house = "32170"
    unit = "15"    
  }
}
The id is a simple identifier increased for each switch. The name is a symbolic name of the switch which is posssible to use in bash commands or when using one of the API to the telldus service. Protocol/model is often easy to find out either by looking at page or googling. The parameters are dependant on the protocol/model but in the case of the risingsun protocol these identifiers are learnt by the switch during the selflearning sequence.

Once the telldus.config file is finished – start or restart the telldus service:

/etc/init.d/telldus restart
[ ok ] Restarting the Telldus TellStick daemon: telldusd.

The command ‘tdtool’ is a command utility communicating with the telldus service. Here are some examples.
List the switches defined above:

> tdtool --list
Number of devices: 2
1 wallswitch1 OFF
2 wallswitch2 OFF
Learn a selflearning-switch the parameters:
> tdtool --learn wallswitchxx
Turning on a switch:
> tdtool --on wallswitch1
Turning off a switch:
> tdtool --off wallswitch1
 

Configure the Telldus Service – Part 1

First stop the service and then create a separate user ‘telldus’ with no login or home directory. The group ‘telldus’ which is connected to the ‘telldus’ user is also created.

/etc/init.d/telldusd stop
adduser --no-create-home --disabled-login --gecos "" telldus

Create the config file of the service daemon: ‘/etc/tellstick.conf’ and add the following two rows:

user = "telldus"
group = "telldus"

Now the service daemon will start up and change process owner to ‘telldus’.

Let’s focus on the tellstick device (in my case the tellstick duo). Connect it to the USB host port of the Raspberry Pi. Verify that it is detected and note the vendor id and product id:

> lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 1781:0c31 Multiple Vendors Telldus TellStick Duo

The tellstick is detected which is shown at the last row, and vendor id: 1781/product id: 0c31.

The service daemon needs a device handle in /dev to be able to communicate with the device.

Newer kernels contains the udev subsystem handling plugindevices, making it possible to write a rule like: each time this device is plugged in, then create this type of device handle.

The tellstick device uses the FTDI HW component to make it easy to communicate using serial usb. The FTDI device driver is included with the kernel as a loadable device driver. A loadable device driver is possible to load dynamically by running the command ‘modprobe’.

Next step is to write a udev rule which matches the device (vendor id/product id) and runs the modprobe command as well as writing to /sys/bus/usb-serial/drivers/ftdi_sio/new_id to make the device handle appear.

Start by creating the file ’99-ftdi.rules’ in ‘/etc/udev/rules.d’:

ACTION==”add”, ATTRS{idVendor}==”1781″, ATTRS{idProduct}==”0c31″, RUN+=”/sbin/modprobe ftdi_sio” OWNER=”telldus” GROUP=”telldus” RUN+=”/bin/sh -c ‘echo 1781 0c31 > /sys/bus/usb-serial/drivers/ftdi_sio/new_id'”

This rule loads the FTDI driver and creates the device handle ‘/dev/ttyusb0’ owned by telldus.
Restart udev subsystem (making it reread all the the rules):

> udevadm control --reload

Verify that ‘/dev/ttyUSB0’ appears when inserting the tellstick device.

Finally we update the config file ‘/etc/tellstick.conf’ with the following line pointing out the device handle:

deviceNode = "/dev/ttyUSB0"

Compiling/Installing the Telldus Service on Raspberry Pi

Time to take a big breath!

Basicly two alternatives either compiling on the pi or cross-compiling on my linux-box.

Since the Telldus-server is a rather small server and knowing that setting up a cross-compiling environment often takes more time than expected, so the choise is easy!

Starting off by installing the development environment on the pi by doing the following:

apt-get install g++
apt-get install cmake

The ‘g++’ is the gnu C++ compiler including all other necessary libraries, ‘cmake’ is a cross-platform make.

Found an excellent description on how to compile telldus on raspberry pi – Link
Basicly repeating it:

nano /etc/apt/sources.list.d/telldus.list
Add the following to file above "deb-src http://download.telldus.com/debian/ stable main"
wget http://download.telldus.se/debian/telldus-public.key
apt-key add telldus-public.key
apt-get update
apt-get install build-essential
apt-get build-dep telldus-core
apt-get install cmake libconfuse-dev libftdi-dev help2man
mkdir -p ~/src/telldus-core

cd ~/src/telldus-core
apt-get –compile source telldus-core
dpkg --install *.deb

Yes! The last install statement is returning “[ ok ] Starting the Telldus TellStick daemon : telldusd.” which is a receipt that the server has started. There is a start/stop script in /etc/init.d/telldusd and this script is run during startup. It is also possible to use this script to restart the server when configuration has been changed.



Getting Ready for Python

Really liking python more and more!

The windows support is also good – recommending to take a look at ironpython as well as python tools for visual studio (someone called it the best secret feature of visual studio).

Starting installing on the raspberry pi:

apt-get install python
apt-get install python-setuptools

apt-get install python-pip

The last two debian packages are important. They are needed to be able to add different python modules.

Time to Configure the Raspberry Pi!

Time to configure the raspberry pi!

Choosing hexxeh raspbian since it seems small and only have a 4 GB SD-card around.

Starting off by downloading the image from http://www.raspbian.org/HexxehImages

Using Win32DiskImager to flash it to the SD-card.

The raspberry pi boots up and the LEDs flashes => seems promising.

Using ssh root@ to login into the raspberry pi. The password is ‘hexxeh’

Following the steps on the download page to configure the hexxeh image:

apt-get update
apt-get install ntp fake-hwclock
dpkg-reconfigure tzdata
apt-get install locales
dpkg-reconfigure locales
rpi-update

rm /etc/ssh/ssh_host_* && dpkg-reconfigure openssh-server
Finally rebooting and logging into the raspberry pi again using ssh.
Now we have achieved a good base image and will continue to add application and development packages.
  •