Control your automated system
We are going to use heimcontrol.js as the control software for our automated system. This is a home automation web interface written in Node.js that runs on the Raspberry Pi, and sends low level serial commands to an Arduino connected via USB to control various hardware connected to it. Although some of the things the Arduino does can be done with the Raspberry Pi in theory, the Raspberry Pi does not have the ability to read analogue voltages, so temperature and light sensors would not be possible in this case. Also, the Raspberry Pi only has one hardware pulse width modulation output and three are needed for the LED strip.
Raspberry Pi prep
Use the latest Raspbian image as a starting point for this project. Log into the Pi using the default username of “pi” and default password “raspberry”. The first thing you’ll need to do is use sudo raspi-config to enable the camera and resize the root filesystem so the image uses all of the available space on the SD card. Once this is done, we can update our packages to the latest version and then install some extra packages that will be required later on:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libboost-dev arduino streamer
Next, we need to download a precompiled Node.js package and a MongoDB package (both are required by heimcontrol.js. Packages can be found at http://ift.tt/1pgBuLC, in case they go missing.
sudo dpkg -i node_0.10.36_armhf.deb
Check it has installed correctly:
pi@raspberrypi ~ $ node -v
Time to install MongoDB…
sudo dpkg -i mongodb_2.1.1_armhf.deb
# Start service:
sudo /etc/init.d/mongodb start
# Automatically start service at system startup:
sudo update-rc.d mongodb defaults
Now it’s time to install heimcontrol.js, which our expert had to fork on GitHub to fix a couple of issues.
npm config set python python2.7
git clone http://ift.tt/1c4zvf9
The install process will take a while as there’s quite a lot of stuff to compile. Before you can access heimcontrol.js, you will need to know the IP address of your Raspberry Pi. You can find this out using the ip addr command. Our address was 172.17.173.41. Run heimcontrol.js by typing:
Note that you have to be in the directory where you cloned the repository for this to work. This is probably /home/pi/heimcontrol.js. Heimcontrol runs on port 8080, so type the IP address of your Pi into your web browser followed by :8080 – in our case the correct URL was: http://ift.tt/1UpNpTp. We have applied a patch that disables authentication by default, because it gets annoying if you have to log in every time you want to use the web interface. You can re-enable authentication by editing config/development.js inside the heimcontrol.js directory.
Now that we know heimcontrol is working, Ctrl+C out of it because we have more work to do before we can start using it. We need to load the video for the Linux camera driver for the Raspberry Pi camera so that it can be used with the streamer software we installed earlier. To do this, you need to edit
/etc/modules using sudo and your favourite text editor (use nano if in doubt, so sudo nano /etc/modules). Add the line “bcm2835-v4l2” to the end of the file so the driver is loaded at boot. To load it instantly, run sudo modprobe bcm2835-v4l2.
Arduino prep and remote control scanning
We need to write some software to the Arduino called duino, which allows the ports to be controlled over serial from heimcontrol.js. This way of working is elegant because it allows more sensors to be added to the Arduino without any need to reprogram anything. We have already installed the Arduino software, so now we need to copy some libraries required by duino to the Arduino installation directory so that the software can be compiled:
sudo cp -r /home/pi/http://ift.tt/1pgBxqP
Before we write the duino software to the Arduino, we want to use the Arduino to sniff the messages sent by the remote for the remote control sockets. To do this, connect the 433MHz receiver module (the wider module of the two modules with four pins instead of three – see diagram to left) to the Arduino. Connect the data pin (either of the middle pins) to pin 2 of the Arduino, VCC to 5V, and GND to GND. Download the receiver software using:
… which is again mirrored over at http://ift.tt/1UpNsOP.
Now we have to start the Arduino software. If you are connecting to the Pi via SSH then you can enable X11 forwarding to your local machine by logging in with:
ssh -X firstname.lastname@example.org
Alternatively, you can type startx to start an x session if you have a screen and keyboard connected to the Pi. Once you have logged in with your desired method, type arduino into a terminal to start the Arduino programming software.
Open the ReceiveDemo_Simple.pde file that you just downloaded and upload it to the Arduino. Then open the serial monitor (Tools>Serial Monitor) and press the reset button on the Arduino. By pressing each button on your remote, you can see the code to switch each socket on and off. Make a note of the codes for each button because you will need to enter them later. Our output can be seen in the top-right image.
Once this is done, we can finally write the duino software to the Arduino. This process is the same as what you’ve just done except the file is located at /home/pi http://ift.tt/1pgBuLQ.
The software might take a minute to compile. Once it has been uploaded, you can exit the Arduino software and press the reset button on the Arduino. Now we can put everything together and start adding our sensors to heimcontrol.js.
Set up your control interface
Now is the time to start adding our sensors and devices to heimcontrol.js
1. Start heimcontrol.js on boot
Before we start adding devices, it makes sense to start heimcontrol.js on boot. To do this, we can add a line to /etc/rc.local which is a script that gets ran at boot by the root user. The file needs to be edited with sudo and your favourite editor, for example:
sudo nano /etc/rc.local
Add the following line before the line “exit 0”:
su pi -c “node /home/pi/http://ift.tt/1UpNsOR” &
Heimcontrol.js will be started automatically at boot from now on, but for now you can start it with node /home/pi/http://ift.tt/1UpNsOR.
2. Add the camera feed
Go to Settings and select Webcam. Set the method to Streamer and the devices as /dev/video0. Pick an interval; we picked two seconds but you can pick any interval you like. Shorter intervals are more feasible on a Raspberry Pi 2, as it is generally more responsive. Click Save and then go back to the home page.
3. Prepare remote control socket codes
This is where you need the codes that you sniffed from the remote earlier on. The heimcontrol.js web interface takes the code as a binary string of 1s and 0s. However, the code we sniffed is in a different format so you’ll need to convert it to binary using Python. Type python2 into the terminal to open a Python interpreter. Format the integer you captured as a binary string like so:
4. Add the remote control socket
Go to the Settings menu and go to the Arduino section. Click the Add button and set the method to RC Switch. Set the code type to binary. Give the switch a name, enter the pin that the RF transmitter is connected to (in our case, pin 2) and enter the two codes that you just worked out for the on/off buttons. Go back to the home page and test that the switch works. If it doesn’t, you might need to add an antenna to the transmitter by making a loop of wire. Check everything is connected correctly.
5. Add the temperature sensor
The temperature sensor can be tricky because it needs calibrating. A multimeter is a good idea so you can accurately read the analogue voltage. Go to the Arduino settings page and add a sensor. The formula for the TMP36 is: [(Vout in mV) – 500] / 10. We read 718mV with a multimeter, which would put the temperature at 21.8°C. Experiment with the formula by seeing what the raw value of x is from your sensor, but ours ended up as: (((x+45) * (5000/1023.0)) – 500) / 10. (5000/1023 is because the Arduino has a 10-bit analogue-to-digital converter, ie 0-1023 to read a voltage up to 5V.) Note that you have to ensure you have perfectly matched brackets, otherwise the software will crash because it simply tries to eval the string you put in.
6. Add the LED strip
Finally, it’s time to add the LED strip. Make sure the signal wires are connected to PWM-capable pins on the Arduino marked with a ~. We used pins 3, 5 and 6. Go to Settings>RGB Lights. Ensure you have the signal wires connected to the correct colours. We used 6 for red, 5 for green and 3 for blue. Click Save and turn on the 12V supply. Select a colour from the colour picker. Your strip should light up! Pick Red, Blue and Green to ensure everything is wired up correctly.
from Linux User & Developer – the Linux and FOSS mag for a GNU generation http://ift.tt/1pgBv26