One Million Monkeys

…because 999,999 just isn't enough.

Archive for the tag “Raspberry Pi”

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

Raspberry Pi Laser Tripwire

So I’ve been playing around with my Raspberry Pi computer, especially with the general purpose input/output pins available for controlling external hardware. Before getting my Raspberry Pi, I had exactly no experience with electronics, but thanks to the various forums at the Raspberry Pi website, and especially the excellent tutorials provided by Adafruit, I am finding my way around both the Raspberry Pi as well as Arduino microcontrollers.

My latest project has involved setting up a laser tripwire that will do something when the laser beam is broken. Ultimately, I plan to wire up a motorized Nerf dart gun to fire a couple of darts when the beam is broken. For now, though, it sounds a buzzer. Here’s how it works:

image

Laser diode powered by 2 AA batteries. Beam is adjusted to shine on the LDR on the other side of the doorway.

image

Breadboard layout of LDR connected to Pi with MCP3008 ADC. The buzzer is connected to Pi pin 22.

The basic principle is straightforward. The laser beam, which is powered by two AA batteries, is adjusted so that it shines on a CdS light-dependent resistor (LDR). If the voltage across the LDR falls below some threshold value, determined by experimentation, then some action is initiated, in this case a buzzer is powered on for two seconds.

One small complication is that the Raspberry Pi only has digital IO pins, and reading the voltage on the LDR requires an analog input. The way I have dealt with this is to use an MCP3008 analog-to-digital converter. Adafruit sells these for $3.75, and has a good tutorial on using it with an analog temperature sensor. In this case just remove the temperature sensor, and connect one lead from the LDR to an ADC input pin, and the other LDR lead to the Pi’s 3.3 volt power. I also put a 10K Ohm pull-down resistor on the ADC input pin. I connected my LDR to two of the four wires inside an old telephone cable to give some extra space, and mounted the business end of the LDR inside a hole I drilled in a small block of wood. This helps block out some of the ambient light that might otherwise mask the breaking of the laser beam.

I’m also a big fan of Adafruit’s Pi Cobbler for connecting things on the breadboard to the Pi’s GPIO pins. If you don’t have one of these, do yourself a favor and spend the 8 bucks. For me, it had the added benefit of making me learn how to solder.

At this point the only consequence to breaking the laser beam is a small buzzer that sounds for two seconds. It’s a garden variety piezoelectric buzzer with a positive lead connected to one of the Pi’s digital pins, set as an output, and the ground lead connected to the Pi’s ground. The output pin is set to low, unless the LDR voltage falls below the threshold, at which point the buzzer output pin is set to high for two seconds, and then returned to low. The code is Python, and can be downloaded here:

git clone https://SDreher71@bitbucket.org/SDreher71/rpi-tripwire.git

The ADCpy.py file is used to read the data from the ADC, and is modified from Adafruit’s MCP3008 tutorial code.

Let me know in the comments if you have any problems or questions. Enjoy!

Update: In response to a comment, here’s a couple of photos of the the LDR mounted in a small block of wood. The phone cable has four wires in it. Two are as soldered to the leads on the LDR. At the other end, I soldered a couple of male header pins for plugging into the breadboard.
image

image

Post Navigation