blog.fridns.se

Automate Your Lights With Tellstick and Nexa in Fedora 20

| Comments

In this post I’m going to describe the way I have configured my setup to automate my bedroom-lights. In this particular setup I use the following components,

The goal of the setup is to automate the lights so that they can be turned on in the morning, I imagine that if I wake up to a light instead of a buzzer I will be in a better mood and it will be easier for me to get up. The other goal is to be able to control the lights from my phone.

So the first thing is obviously to attach the tellstick to your computer, after it’s been attached look for something like this in your dmesg,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[1199357.989893] usb 1-1.1: new full-speed USB device number 12 using ehci-pci
[1199358.081310] usb 1-1.1: New USB device found, idVendor=1781, idProduct=0c30
[1199358.081313] usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[1199358.081314] usb 1-1.1: Product: TellStick
[1199358.081316] usb 1-1.1: Manufacturer: Telldus
[1199358.081317] usb 1-1.1: SerialNumber: A7029QUB
[1199358.128916] usbcore: registered new interface driver ftdi_sio
[1199358.128926] usbserial: USB Serial support registered for FTDI USB Serial Device
[1199358.128991] ftdi_sio 1-1.1:1.0: FTDI USB Serial Device converter detected
[1199358.129009] usb 1-1.1: Detected FT232RL
[1199358.129010] usb 1-1.1: Number of endpoints 2
[1199358.129012] usb 1-1.1: Endpoint 1 MaxPacketSize 64
[1199358.129013] usb 1-1.1: Endpoint 2 MaxPacketSize 64
[1199358.129014] usb 1-1.1: Setting MaxPacketSize 64
[1199358.129427] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUSB0

That’s basically the kernel telling you that it has found the device you just inserted and attached it to /dev/ttyUSB0. If you don’t see this in your log, then you have issues that’s beyond the scope of this article, sorry ;)

Assuming that the device actually was found, we continue with downloading, building and installing the telldus-core,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Make sure you have necessary tools/libs, 
sudo yum groupinstall "Development Tools" "C Development Tools and Libraries"
sudo yum install libconfuse-devel libftdi-devel cmake

# Download telldus-core, 
cd /tmp/
wget http://download.telldus.se/TellStick/Software/telldus-core/telldus-core-2.1.1.tar.gz
tar xvfp telldus-core-2.1.1.tar.gz
cd telldus-core-2.1.1/

# Patch Socket.h since it needs to include unistd.h,
sed -i '/#include <string>/a \#include <unistd.h>' common/Socket.h

# Build it and install the telldus-core, 
cmake
make
sudo make install

If you get any errors above while build the telldus-core, you should be able to find the culprit by looking at the error-message, hopefully it will make some sense (or not, you never know, however I’ve done these exact steps on a clean install of Fedora 20 so it would surprise me if it failed for you if you’re in the same environment).

Since telldus puts its beloved libtelldus-core.so.2 under /usr/local/lib (even though it’s 64bit in my case), you have some options

  • Make ldconfig pick up libs under /usr/local/lib by creating a usr-local.conf under /etc/ld.so.conf.d/ with the path /usr/local/lib in it (or even better, move the libs to /usr/local/lib64 and include that path instead, since it’s a 64bit system).
  • Make a symbolic in /usr/lib64 that points to /usr/local/lib/libtelldus-core.so.2.1.1

Personally I took the easy way out by creating a symlink,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Create a link, 
sudo ln -s /usr/lib64/libtelldus-core.so.2 /usr/local/lib/libtelldus-core.so.2.1.1

# Whichever method you prefer, make sure the lib is available, you can check it with 
ldd /usr/local/bin/tdtool

> linux-vdso.so.1 =>  (0x00007fffb63fe000)
> libtelldus-core.so.2 => /lib64/libtelldus-core.so.2 (0x00007fd9482c2000)
> libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fd947fba000)
> libm.so.6 => /lib64/libm.so.6 (0x00007fd947cb2000)
> etc..

# If the line that says "libtelldus-core.so.2" does say "Not found", you are doing something
# wrong. Try again (you need to run sudo ldconfig, if you're choosing the first method).

Go on with configuring the telldus-core so we can connect it to our Nexa-WMR-252,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Edit the config-file with your favorite editor, 
sudo gvim /etc/tellstick.conf

# My config looks like this, but of course this could differ depending on your setup, 
# --- Start --- 
user = "nobody"
group = "plugdev"
ignoreControllerConfirmation = "false"
deviceNode = "/dev/ttyUSB0"
device {
  id = 1
    name = "Dimmer bedroom"
    protocol = "arctech"
    model = "selflearning-dimmer"
    parameters {
      house = "123456" # This is the code that the nexa-reciever will learn, 
      unit = "1"
    }
}
# --- End --- 

Now, it’s time to get the things going,

1
2
# First step is to start the telldus-core-daemon, 
sudo telldusd

Next step is to learn the nexa our “code”, press the little button under the red-diode to put the Nexa-WMR-252 in “self-learning-mode”, then type the command below,

Notes,

  • The lights must be off when doing this.
  • The “self-learning-mode” lasts for 12 seconds, that means you have to continue with the “learning-step” (tdtool –learn 1) before the 12 seconds runs out. The red diode will blink as long as the nexa is in the “self-learning-mode”. Once it receives the code it will dim on and off the lights.
1
2
3
4
5
6
7
8
9
10
11
12
# First, make sure that you have your devices configured properly, 
tdtool -l
> Number of devices: 1
> 1    Dimmer bedroom  OFF

# If no devices are listed above, go back and make sure that telldusd is started 
# correctly, you can get some debug from the daemon by appending the --nodaemon-flag
# to it. 

# Use the tdtool to learn the nexadevice the code, 
tdtool --learn 1
> Learning device: 1 Dimmer bedroom - Success

Awesomeness, now we can do all sorts of fun stuff,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Turn the dimmers on, 
tdtool --on 1
> Turning on device 1, Dimmer bedroom - Success

# Turn the dimmers off, 
tdtool --off 1
> Turning on device 1, Dimmer bedroom - Success

# Set a specific dim-level, 
tdtool --dimlevel 25 --dim 1

# Why not go all the way, 
for i in $(seq 10 10 250)
do
  tdtool --dimlevel $i --dim 1
done
for i in $(seq 250 -10 10)
do
  tdtool --dimlevel $i --dim 1
done

To control my lights through my phone I use the tellprox, found here. This is a local server that you can use if you don’t want to use the “tellstick-live-cloud”-thingie, which fits me perfect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Make sure you have git installed, 
sudo yum install git

# Clone the tellprox-repo, 
cd ~/bin/
git clone https://github.com/p3tecracknell/tellprox

# Edit the configuration for our needs,
gvim config.ini

# My config looks like this, 
# --- Start --- 
host = localhost
port = 8081
debug = False
editable = False
client_name = TellProx
client_id = 1
pretty_print = False
timezone = Europe/London
latitude = 51.4500007629
longitude = -2.58330011368
tzoffset = 3600
webroot = ""
username =
password =
apikey = ""
[sensors]
# --- End --- 

# Save the config above and start the tellprox, 
python -m tellprox

> Bottle v0.11.6 server starting up (using CherryPyServer())...
> Listening on http://localhost:8081/
> Hit Ctrl-C to quit.

# Note, config.ini should be in the directory from where you are starting tellprox, 

Open up a web browser and point it to http://localhost:8081 and you should see the “Dimmer bedroom”-device there.

Lastly I want to be able to control this from my phone, and since I don’t have a “local network” at home, I need to be able to reach it from the “internet”. Since there is no basic authentication implemented yet in tellprox (or at least I can’t seem to get it to work, I’ll use an apache instance with basic auth infront of tellprox).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Install apache, 
sudo yum install httpd

# Enable it, 
sudo systemctl enable httpd

# Configure it, 
sudo gvim /etc/httpd/conf.d/tellprox.conf

# My config looks like this, since we are sending passwords you should be using 
# ssl (there are tons of guides on how to generate your own self-signed-certificate), 

# --- Start ---
Listen xxxx.xx.xx:8082
<VirtualHost *:8082>
  SSLEngine on
  SSLProxyEngine On
  SSLCertificateFile  /path-your-certificate
  SSLCertificateKeyFile /path-to-your-key
  SSLProxyCheckPeerName off
  SSLProxyCheckPeerCN off

  <Location />
    AuthType Basic
    AuthName "You're not controlling my bedroom-lights!"
    AuthUserFile /etc/httpd/conf.d/passwords
    Require user xxx
  </Location>

  # Proxy to tellprox, 
  ProxyPass / http://localhost:8081/
  ProxyPassReverse / http://localhost:8081/

  # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
  LogLevel info
  ErrorLog /var/log/httpd/tellprox-error.log
  CustomLog /var/log/httpd/tellprox-access.log combined
</VirtualHost>
# --- End --- 

# Create a password for myuser, 
sudo htpasswd -c /etc/httpd/conf.d/passwords myuser

# To allow us to use port 8082 we need 
# Since the port 8082 isn't a pre-configured port for a webserver, we need to manually
# add with semanage, otherwise selinux will prevent us from listening on that particular 
# port,  
sudo semanage port -a -t http_port_t -p tcp 8082

# Start the webserver, 
sudo systemctl start httpd

Lastly, we make sure that this will keep working after a reboot, and we also set up a small crontab-job for making the lights turn on in the morning,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Since I'm quite lazy, I choose the rc.local-way of starting things at boot, 
# but yes, I know, there are "better" (define better) ways of doing this. 
# But again, this is simple and doesn't require much, 

# Enable rc.local at boot, 
sudo systemctl enable rc-local.service

# Create a /etc/rc.d/rc.local, 
sudo gvim /etc/rc.d/rc.local

# --- Start ---
#!/bin/bash 

# Start the telldus-core-daemon, 
/usr/local/sbin/telldusd

# Start our tellprox, maybe not the most beutiful you have seen, bit it works for me, for now, 
sudo -u myuser bash -c "cd /home/myuser/bin/tellprox && python -m tellprox" &
# --- End --- 

# Then make a small script, 
gvim ~/bin/turnonbedroomlights.sh

# --- Start ---
#!/bin/bash 

for i in $(seq 10 10 250)
do
  tdtool --dimlevel $i --dim 1
done
# --- End --- 

# Make it executable, 
chmod +x ~/bin/turnonbedroomlights.sh

# Add a crontab-entry making the lights go on every morning at 6:30, 
crontab -e

# Every weekday morning, 
30 06 * * 1-5 /home/username/bin/turnonbedroomlights.sh

And there you have it, enjoy !

  • Automatically turn on my lights in the morning.
  • Control them from my phone

Comments, suggestions and questions are welcome!

Comments