Sunday 21 June 2015

Control your termostat



Hi everyone!, this post is to explain how do i control the thermostat in my home. As you know the electrical thermostat is one of the appliances that consumes more energy, especially in winter it is a good idea to turn off the thermostat when you are not in home. There are a lot of cheap timers you can buy anywhere, however,  the principal advantage applying the concept of internet of things is  control the appliance everywhere and apply more intelligence.


Imagine, if you have a common timer installed in the thermostat you have to turn off manually on holidays day (if you remember it), or if you want to turn on when you are arriving home you have to wait the time that you previously set up in the timer.
With this solution, applying openhab, you can configure the schedule on google calendar for example, an incredible interface to set up when the thermostat have to turn on/off. Inclusive you can install a temperature sensor in your home and establish a rule on openhab to turn on the thermostat according the current temperature.
Another advantage is to install a sensor and turn on only if you are in home, even if you don't have a motion sensor you can establish a rule of presence in openhab with the ip of your cell phone.

Ok, let's start, the procedure is very simple, you have only follow two steps

  1. the wifi socket
    http://www.ebay.com/itm/1pc-USR-WP1-1-Channel-WIFI-Plug-Intelligent-Remote-Control-Power-Socket-Outlet/291340358989?_trksid=p2047675.c100011.m1850&_trkparms=aid%3D222007%26algo%3DSIC.MBE%26ao%3D1%26asc%3D30084%26meid%3D5d1a93e8033f4582b88a3c17b4d61049%26pid%3D100011%26rk%3D1%26rkt%3D4%26sd%3D291234926809
  2. raspberry pi with openhab connected to the lan

As you know you need to install openhab in your raspberry pi, this was explained in a previous post.
This wall socket was bought in ebay, there are a lot of them with the same functionality, i chose this one because is cheap and it was easy to hack. In my case i needed an EU format, there are not many options in this format with this price.
To hack the tcp message i installed the windows version and sniff the traffic with wireshark, so i made a script to turn on/off and see the state of the thermostat.
I made scripts for each functionality, one for turn off, other for turn on and the last one to show the state of the thermostat.

Turn off script (Thermostat_OFF.bash)


exec 3<>/dev/tcp/192.168.1.150/8899
echo -e  '\x55\xaa\x00\x03\x00\x01\x01\x05' >&3





Turn on script (Thermostat_ON.bash)

exec 3<>/dev/tcp/192.168.1.150/8899
echo -e  '\x55\xaa\x00\x03\x00\x02\x01\x06' >&3


Turn on script (Thermostat_ON.bash)

exec 3<>/dev/tcp/192.168.1.150/8899

STATE_OFF=5
STATE_ON=6
echo -e  '\x55\xaa\x00\x02\x00\x0a\x0c' >&3

for i in {1..8}
do
   read -r -n 1 -d $'\0' var  <&3
done

#echo $var | hexdump

#echo -ne "$var" | od -c
state=$(echo -ne "$var" | od -c)
#echo "the state is $state"
IFS=$'\v' read -a foo <<<"$(echo $state | sed 's/./&\v/g')"
#declare -p foo
state=${foo[10]}
if [ $state -eq $STATE_ON ]; then
    echo "ON"
fi
if [ $state -eq $STATE_OFF ]; then
        echo "OFF"
fi    


as you can see, the wall socket has the ip 192.168.1.150 in my lan, this can vary in your local net. I configured in my router (flashed with dd wrt) a non-dynamic ip for the device, just for run my scripts without changes.
I created bash script because at that time i don' t know how to implement an openhab action, if i do it today i prefer this way, and i don't use the tcp binding because i need to send hex values, i can' t send characters as the tcp binding does. I searched in google groups but i don't find a solution. It would be a very good proposal to the openhab project :), don't you think?.

In the other hand in the openhab server you have to create the item like this

Item declaration

Switch Thermostat "Thermostat"   (Appliance) { exec=">[ON:/bin/bash /home/pi/openhab/scripts/calefon_ON.bash] >[OFF:/bin/bash /home/pi/openhab/scripts/calefon_OFF.bash] <[/bin/bash /home/pi/openhab/scripts/calefon_STATE.bash:30000:REGEX((.*?))]"  }

Sitemap declaration

Switch item=Thermostat icon="heating"

You can see there are the two output script for turn on/off and one input for query the state, it run every half minute. About the item, show the heating icon, if it is in state ON shows heating.png in other case shows heating-off.png. It is a very good advantage of openhab, it is easy to use differents icons according the state of the appliance, the heating icon come with the openhab distribution, but c'mon, it is very easy to use your personal icons. It is recommended to use 32x32 pixel size, specially for the mobiles versions.
As i said before it is a very good idea to use the google calendar binding to configure the schedule routine. The official documentation in the github's wiki is deprecated, you must follow the steps explained in this conversation thread.


No comments:

Post a Comment