Control your plugs with bluetooth and bash scripts

August 17, 2016

Here in France, most of time electricity is cheaper on night. Everyday you have to think about power consumption for heater, dishwasher or washing machine.

Repeative tasks, so checking solution to automate them.

Solutions

Mechanical

A simple solution exists :

It is very cheap (€5) but not a geek solution :)
Also you can not program complex things with external datas.

With batteries in

For €12 you can purchase such things :

OMG ! Come back to 90's ! Need some batteries in, User Experience is a disaster... Do not think about it.

Wifi but expensive

DLink provides a new way to do what I want with a Wifi solution :

For €49 at Amazon. To expensive for me : I need at least 5 of them for my flat.

Bluetooth solution

With not using Bluetooth, Arduino and relay ? For around €30 I can do this kind of stuff.

During my research I found this :

A bluetooth smartplug for €29 !
https://www.amazon.fr/AwoX-SmartPLUG-Connectique-Bluetooth-Smartphone/dp/B010JWOYIC

An Android application is given to control it :

Smartplug gives electrical consumption in realtime !

Bluetooth USB key

For my desktop computer I purchased this bluetooth key :

(https://www.amazon.fr/gp/product/B009ZIILLI)

It's not the cheapest but it runs perfectly on Linux.

Now I have my hardware. Let's go to write some software.


Reverse engineering : bluetooth commands

First, I installed the Awox Application (https://play.google.com/store/apps/details?id=com.awox.smart.control&hl=fr) to play with my new device. In Android Developer options, you can activate an HCI Bluetooth log :

Retreive file "btsnoophciold.log" from your phone, open it with Wiresark, then take some time to analyse

Many times I saw that handler 0x002b is written with a (random ?) value.
Notification handle is 0x002e.

Let's try :

$ hciconfig hci0 up
$ hciconfig hci0 leady

$ hcitool -i hci0 lescan
LE Scan ...  
98:7B:F3:34:8E:60 SMP-B16-FR

$ gatttool -i hci0 -b 98:7B:F3:34:8E:60 -m 48 --interactive
[98:7B:F3:34:8E:60][LE]> connect
Attempting to connect to 98:7B:F3:34:8E:60  
Connection successful  
[98:7B:F3:34:8E:60][LE]> char-write-req 0x002b 0f06030001000005ffff
Characteristic value was written successfully  
Notification handle = 0x002e value: 0f 04 03 00 00 04 ff ff  
[98:7B:F3:34:8E:60][LE]> char-write-req 0x002b 0f06030000000004ffff
Characteristic value was written successfully  
Notification handle = 0x002e value: 0f 04 03 00 00 04 ff ff  

Wow ! It works !!!

Script it all

non interactive mode is possible :

#power on
gatttool -i hci0 -b 98:7B:F3:34:8E:60 --char-write -a 0x002b -n 0f06030001000005ffff

#power off
gatttool -i hci0 -b 98:7B:F3:34:8E:60 --char-write -a 0x002b -n 0f06030000000004ffff  

There is a lack with this method. Running gatttool by this way includes 3 actions :

By the way, reconnect everytime is not a problem. However, a non associated device has a public visibility and waiting for a connection. Imagine that your neighbors can take control...

hcitool can open a bluetooth LE connection :

$ hcitool lecc 98:7B:F3:34:8E:60
Connection handle 64  

This connection locks the device. Great !

$ gatttool -i hci0 -b 98:7B:F3:34:8E:60 --char-write -a 0x002b -n 0f06030000000004ffff
connect: Device or resource busy (16)  

I did not find a clean solution (hcitool is not full compliant with Bluetooth Low Energy), where is mine :

# open a connection
$ hcitool lecc 98:7B:F3:34:8E:60
Connection handle 64

# disconnect to unlock
$ hcitool ledc 64

# wait a little
sleep 2

# send command
$ gatttool -i hci0 -b 98:7B:F3:34:8E:60 --char-write -a 0x002b -n 0f06030000000004ffff

# wait a little
sleep 2

# re-open connection to lock device
$ hcitool lecc 98:7B:F3:34:8E:60
Connection handle 64