One Million Monkeys

…because 999,999 just isn't enough.

Archive for the category “Computers”

Temperature Monitoring With Tasker and Raspberry Pi

So I wanted to be able to use my Android phone to check the temperature in my house. I have a  temperature sensor attached to my Raspberry Pi. I use Tasker plugins AutoRemote to send commands to the Raspberry Pi, and AutoNotification to format the respose as an Android notification. I’m using a DS18B20 digital temperature sensor, that uses the 1-Wire protocol to send data to the Pi. I wrote a python script that queries the sensor and returns the temperature in degrees Fahrenheit. To see how to set up the sensor on the Pi, you can follow Adafruit’s tutorial. The problem is that accessing that data requires root access. On the Pi terminal, you would just run scripts with the sudo command and it’s no problem. However, to do this via AutoRemote requires sending your root password in clear text over the internet–not something I like to do, as a matter of course.

I came up with a solution that involves splitting the process of querying the temperature sensor and returning the data into two parts: a server and client. You can then start the server script on the Pi using sudo. Then you can use AutoRemote to run the client, which gets the temperature from the server and returns it to AutoNotification.

Raspberry Pi Python Scripts

The server and client scripts are pretty small and simple. You can get them from my Bitbucket repository on your Raspberry Pi. First change into the directory where you want to keep the files. Then issue the command

git clone https://SDreher71@bitbucket.org/SDreher71/tempserver.git

Once you have the files, you’ll need to make them executable with the command chmod u+x filename.

Start the temperature server with sudo ./temp_server.py

AutoRemote Setup

AutoRemote is a plugin for Tasker. Both Tasker and the plugins used here are available in the Google Play store.

Follow the instructions for connecting your Raspberry Pi to AutoRemote. Then open Tasker and create a new task. Add an action to the task: Plugins > AutoRemote Message. Configure the message: Device: your Raspberry Pi, Message: cd path/to/your/scripts && ./get_temp.py.

On my phone’s home screen, I added a Tasker Task widget, and connected it to this task. Now, pressing the task Icon on my home screen sends the get_temp command to my Raspberry Pi with a listening temp_server running. The message returned by the Pi to the phone is ‘linux=:=70.3’ or whatever the temperature is.

Now, create a new Tasker Profile. I called mine Temperature. Add a State context, and select Plugin > AutoRemote. Select the configuration icon and check ‘Event Behavior.’ Under Message Filter enter ‘linux=:=’ (without the quotes). This will allow the task to filter only messages coming from your linux machine. Then select Advanced on the configuration screen and touch Command. Enter ‘temp’ (again, without quotes). This tells Tasker to assign whatever is to the right of the =:= in the incoming message to the variable %temp.

AutoNotification Setup

Now create a new task for this profile and give it a name (mine is Read Temp). Add an action: Plugin > AutoNotification. Open the configuration and enter settings. Most of these are up to you, depending on what you want the notification to say. The main thing is in the Text setting, I have ‘House temperature is %temp F’ (without quotes). Yours can say whatever you like, but the actual temperature is substituted for the variable %temp. Play around with the other settings until you get it the way you want. This Pocketables tutorial does a nice job of explaining the various options.

That’s it. Now when you touch the Task widget on your home screen, you should get a message in your notifications telling you what the temperature is.

Advertisements

Post Navigation