It’s been two years since my Tasker automation profiles are complimented by a Raspberry Pi board. I understand that spending $30-40 on a new and shiny Raspberry Pi 3 may not be within your reach, but we have Raspberry Pi Zero and ZeroW out and this would be the best $5-10 ever spent! Trust me! I’m going to show you three ways you can connect Tasker and Raspberry Pi together.
Sending data to Tasker and Raspberry Pi
There is a reason why companies want to use a hub or a central server to handle the requests and process the data. It saves the battery power and CPU use. Plus if you change your phone, or want to add an additional device, the whole process is much simpler to perform.
AutoRemote
I will assume, that you know how AutoRemote plugin works already. If not please have a look here. You’ll need curl installed on your Raspberry Pi (new Raspbian Jessie versions should have it already)
sudo apt-get install curl
Open AutoRemote app and get the personal URL. Open in in the web browser – you will see the URL change once the website is loaded. Copy the key that starts after the:
https://autoremotejoaomgcd.appspot.com/?key=
you will need this to complete the URL later.
Check your external IP or set up a DNS if you want to have an access to the device from outside of your local network. Google My IP to get the IP address or use https://www.noip.com/ to set up a free domain. Make a note of it.
Edit the following URL accordingly:
curl "https://autoremotejoaomgcd.appspot.com/registerpc?key=YOUR_KEY&name=NAME_TO_APPEAR_ON_YOUR_PHONE&id=ANY_UNIQUE_ID&type=linux&publicip=YOUR_PUBLIC_IP_OR_HOST_NAME&localip=$(sudo ifconfig eth0 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}')" YOUR_KEY - The key obtained in the point 2 NAME_TO_APPEAR_ON_YOUR_PHONE - Give it a name ie RPI3 ANY_UNIQUE_ID - make up an ID ie 314 YOUR_PUBLIC_IP_OR_HOST_NAME - the IP or DNS obtained in the point 3
Modify the URL and open up a terminal on the Raspberry Pi you wish to register. It will register the device running the AutoRemote. Paste the URL and hit enter.
Open up a file /etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config
and scroll down to the end. You will need to add the following line
KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1
Then Ctrl-X to close and Y to save the changes.
Your device will be visible in the AutoRemote app, you should be prompted to enter SSH username and password. If this won’t happen automatically when clicking on the device in AutoRemote hold down the device to bring up the options. Enter the username and password (default: pi, raspberry)Click a message icon and send a test command:
date
After a shoer moment you should see a toast with the current date. If you have disabled the toasts previously, you can access the log to check if the message has been received. Each response issued back to the AutoRemote will have the following format:
linux=:=message
To send messages back to the tasker you can use either:
curl “https://autoremotejoaomgcd.appspot.com/sendmessage?key=YOUR_KEY&message=TYPE_MESSAGE_HERE” OR import urlib.request urllib.request.urlopen(https://autoremotejoaomgcd.appspot.com/sendmessage?key=YOUR_KEY&message=TYPE_MESSAGE_HERE).read()
NodeRED and HTTP requests
Ever since NodeRED is my favourite way of managing smart devices at home, I needed a better way of interacting between a Raspberry Pi and Tasker. Turns out, that NodeRED and HTTP requests are pretty powerful. While you can’t really send the HTTP request back to Tasker (I use AutoRemote based node – more on that in a moment) due to unknown IP, you always know your NodeRED server address. HTTP requests are powerful, as data is processed in a JSON style, available to be used in NodeRED.
Use HTTP POST action in Tasker.
Server:Port YOUR_IP or DNS:1880 Path: some/directory/ Data: key1=value1 key2=value2 Trust Any Certificate YES
The information is sent as JSON style values. For each value you have to specify the name of this value (key). If you want to send multiple values, use a new line.
To receive data in NodeRED, use HTTP POST node connected to a HTTP response node. The POST node has to have the same directory as the one specified in the Tasker HTTP request.
Data is received as as key specific payloads. If I was sending the data above, NodeRED would store it as:
msg.pyload.key1 = value1 msg.pyload.key2 = value2
You can use this data and pass it forward as a regular payload using a function node:
msg.payload = msg.pyload.key1; return msg;
To send the message back to Tasker, use HTTP POST node and AutoRemote link used in a previous method.
https://autoremotejoaomgcd.appspot.com/sendmessage?key=YOUR_AR_KEY&message={{payload}}
AutoTools and SSH
AutoTools is easier to use and set up, however, the main limitation is that you cannot message the Tasker enabled device directly. When the SSH message is initiated, you will obtain the response from the Raspberry Pi. You will have to forward the port 22 in your router (default one unless changed) and point it to the local IP of the Raspberry Pi. Mine is 192.168.0.12.
- Enter the IP or Hostname
- Enter the port number (22 is the default one for SSH)
- Enter your username (default: pi)
- Enter the password (default: raspberry)
- Enter a command to run (ie: date)
- Enter the name of the variable which will hold the response (ie: result)
- If you want to add files add local paths or online folders
Easy enough. Bear in mind, that if want the Raspberry Pi to send a message to the phone or tablet, it won’t be possible until you will add another solution – ie AutoRemote or NodeRED.
Conclusion
You have plenty of choices on how to send the data between Tasker and Raspberry Pi. Pick the best suited to your situation. I will be using NodeRED for sure, as this is a very approachable programming tool with great support for Alexa, Google Home and other devices.