Using TweetControl, ThingHTTP and Talkback to control Domoticz/Arduino

Using TweetControl, ThingHTTP and Talkback to control Domoticz/Arduino

posted in: Tinkering | 0

I am slowly building my Home Automation System. One of the things I wanted to implement is the ability to turn on my Sauna remotely.

I tried to find the easiest way to switch sauna on and off, without much development. The idea also was to make it ‘fairly’ secure and at the same time it should be possible to switch sauna on/off almost on any device.

How it works?

First of all, I use TweetControl application from ThingSpeak. TweetControl listens on Twitter for all the Tweets that use specific hashtags. In my case, I’ve set up #turnsaunaon and #turnsaunaoff hashtags for TweetControl to listen on. The hashtags need to be used together with #thingspeak hashtag and the tweet author has to be me : @canmanski. When TweetControl detects a specific tweet it fires an action – ThingHTTP.

ThingHTTP is a universal application from ThingSpeak, that can be used to send/get/execute commands using APIs. The APIs can be either provided by ThingSpeak or any other APIs available on Internet (e.g. provided by apps like ITTT, Slack). In this particular case ThingHTTP is used to add a command to ThingSpeak’s TalkBack application.

TalkBack is a very simple queue system. You can add a command to the queue (using API) and then execute commands in order. The idea is that the home automation system periodically checks the TalkBack queue for commands. If it finds anything, it fetches the command and executes it (like switches the sauna on or off). Once the command is executed it is automatically removed from the TalkBack queue. This ensures that a specific command is executed only once.

My Home Automation System, based on Domoticz, runs a script that checks TalkBack for new commands. When the script fetches a command (SAUNAON or SAUNAOFF) it toggles the sauna switch and the sauna is ON.

Here’s a demonstration video of the system in action:

 

Setting up TalkBack

I’ve added a TalkBack app from the ThingSpeak menu and then added a “Sauna” queue.

talkback1The “Sauna” queue requires almost no set up. All I did is I gave it a name and linked it with one of my ThingSpeak channels.

talkback-sauna

You need to note the API Key of the TalkBack (TalkBack_API_Key) as well as the TalkBack_ID. It will be required to add commands to the queue as well as to get the next command to execute from the queue.

 

Setting up ThingHTTP

This step is probably the most complicated. I’ve added ThingHTTP app from ThingsSpeak Apps menu.

thinghttp1There are two possible actions – SaunaOn and SaunaOff.

SaunaOn is the first ‘recipe’. It calls ThingSpeak TalkBack API and adds a ‘SAUNAON’ command to the queue. Here are parameters:

  • URL: https://api.thingspeak.com/talkbacks/<TalkBack_ID>/commands.json – in my case TalkBack_ID is 11175
  • Method: POST
  • Body: api_key=<TalkBack_API_Key>&command_string=<any_command_you_want> – in my case command_string = SAUNAON

 

thinghttp-saunaon

Similarly I’ve created a second ThingHTTP for switching the sauna off. The parameters are the same as above, except the command in command_string is ‘SAUNAOFF’, meaning I want to switch the sauna off.

thinghttp-saunaoff

 

Setting up TweetControl

I’ve added a TweetControl app to my ThingSpeak account from the Apps menu.

tweetcontrol1

In TweetControl I have defined 2 hashtags that TweetControl listens for #turnsaunaon and #turnsaunaoff.

#turnsaunaon calls SaunaOn ThingHTTP action:

tweetcontrol-turnsaunaon#turnsaunaoff call SaunaOff ThingHTTP action:

 

tweetcontrol-turnsaunaoff

In both cases I’ve set up my own Twitter name and have disabled anonymous TweetControl as I do not want anyone to control sauna, except me.

 

Setting up Domoticz

In Domoticz, I have set up an event that runs periodically and checks the TalkBack queue for new commands. Once a command is found, it is used to update the SaunaMaster switch.

It’s a simple Lua script that uses execute method of the TalkBack API:

https://api.thingspeak.com/talkbacks/<TalkBack_ID>/commands/execute.json?api_key=<TalkBack_API_Key>

This returns the command that needs to be executed (in my case ‘SAUNAON’ or ‘SAUNAOFF’) or nothing if there are no commands to execute. After each call, the command is removed from the queue, which guarantees that the same command will not be executed more than once.

-- Lua script that reads a command from TalkBack app by ThingSpeak
-- if command is SAUNAON then the SaunaMaster switch will be set to on
-- if command is SAUNAOFF then the SaunaMaster switch will be set to off 
-- Bart Czarnecki, canmanski.com, 2016

commandArray = {}

-- temporary file where to save the response
tempfilename = '/var/tmp/sauna.tmp'

-- url to the thingspeak API. Using the call to return the last value in a given channel/field 
url='https://api.thingspeak.com/talkbacks/<TalkBack_ID>/commands/execute.json?api_key=<TalkBalk_API_Key>'

-- reading the contents from the URL and saving to disk 
read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
-- opening the temporary file 
file = io.open(tempfilename, "r")
 
-- parsing the temp file. Although expecting only one line with single value, longer files can be handled
while true do
    --read line 
    line = file:read("*line")
    --if line is empty then exit the while loop
    if not line then break end
    
    -- simple test to see if the line contains the expected command. IF done properly, the JSON string should be parsed and then checked wat the command is.
    -- I took the easy way out so just check if the command is somewhere within the string 
    
    if (string.find(line, 'SAUNAON') ~= nil) then
        -- received the SAUNAON command. If the SaunaMaster is OFF it will be toggled. If ON do nothing as the sauna should be on anyway...
        if (otherdevices['SaunaMaster'] == 'Off') then
            -- debug information
            print("ThingSpeak Turned Sauna On!")
            -- telling the Sauna to switch ON 
            commandArray['SaunaMaster']='On'
        end 
            
    elseif (string.find(line, 'SAUNAOFF') ~= nil) then
        -- checking if sauna is ON.
        if (otherdevices['SaunaMaster'] == 'On') then
            -- switching it OFF 
            print("ThingSpeak Turned Sauna Off!")
            commandArray['SaunaMaster']='Off'
        end
    elseif (line == '{}') then
        --do nothing - the command received is empty
    else
        --something unexpected returned. Just print to log.
        print(line)
    end
    
end

-- closing the file
file:close()

return commandArray

The lua script runs every minute, to check the command queue.

thingspeak-lua-script

Does it actually makes sense?

Well… not really. It’s nice to know that you can spam the whole world, telling everyone that you are going to use the sauna within next couple of hours. But it’s not the way I’d like to actually run my home automation, i.e. publicly.

It will come usefull the next time I’m going snowboarding though. Posting glorious selfie with blue bird sky and knee deep powder, telling #thingspeak to #turnsaunaon for me…

Leave a Reply