Using the bcm2835 Library on the Raspberry Pi

This article is part of a series with the name IoT Remote Power Switch – Project Part 11.

It describes what I had to do that the Remote Power Switch application could toggle a digital output pin on the Raspberry Pi (RPi) board. It also shows the wiring up of the Fotek solid state switch with the RPi and the heater.

Table of Contents

  1. Find a suitable GPIO Library
  2. Install the bcm2835 library on the Raspberry Pi
  3. Copy the bcm2835 Library from the RPi to the Development Host
  4. Configure Eclipse to Make Use of the bcm2835 Library
  5. Augment the Existing C Code with the Calls to the bcm2835 Library
  6. Do the Wiring
  7. Test the Remote Power Switch Application
  8. Summary and Outlook

Find a suitable GPIO Library

I searched the Internet and found a list of driver libraries here. Since I’m programming in C, only the first chapter is interesting. I decided for the bcm2835 library written by Mike McCauley. This is an arbitrary choice. For this simple application, any other library would do as well.

Install the bcm2835 library on the Raspberry Pi

I essentially followed the documentation on www.airspayce.com. This is the summarized procedure on an RPi console as user pi:

$> cd
$> mkdir Downloads
$> cd Downloads
$> wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.49.tar.gz
$> tar zxvf bcm2835-1.49.tar.gz
$> cd bcm2835-1.49/
$> ./configure
$> make
$> sudo make check
$> sudo make install

This simply installs the header file bcm2835.h under /usr/local/include and the archive file libbcm2835.a containing a library file for static linking under /usr/local/lib.

Copy the bcm2835 Library from the RPi to the Development Host

Using ifconfig in the RPi’s bash shell I figured out its IPv4 address. Changing to the console on the development host, I entered the following commands there to copy the libraries from the RPi to the host:

$> cd /usr/lib/arm-linux-gnueabihf
$> sudo rsync pi@192.168.0.11:/usr/local/lib/libbcm2835.a .
$> cd /usr/local/include/arm-linux-gnueabihf
$> sudo rsync pi@192.168.0.11:/usr/local/include/bcm2835.h .

Configure Eclipse to Make Use of the bcm2835 Library

All I had to do here is adding the libbcm2835.a library file to the list of object to be linked with. I opened the file main.c of the RPS project and opened the project’s properties dialog via the main menu:

  • Select DebugRaspbian as active configuration
  • C/C++ Build –> Settings –> Tool Settings –> Cross GCC Linker –> Miscellaneous –> Other objects: Add /usr/lib/arm-linux-gnueabihf/libbcm2835.a

A .a file is an archive that contains one or more object files that can be linked to the executable image at compile time.

Augment the Existing C Code with the Calls to the bcm2835 Library

Essentially I added a driver module called drvGPIO with the simplest possible implementation: it can only toggle one digital output. This module encapsulates the bcm2835 library. Then I made the data preparation module prpSwitch make use of the drvGPIO module in one single call: drvGPIO_setOutput(). Additionally, I cleaned up main.c a bit, but that’s it.
After letting Eclipse build the RPS application I copied the resulting executable file to the RPi using this command in the host shell: scp <path/to/RPS/executable/RemotePowerSwitch pi@192.168.0.11:~
You can find the source code on GitHub as usual.

Do the Wiring

Warning! Warning! Warning!
Domestic power outlets carry lethal voltage!
Don’t wire up the solid state switch with a power outlet unless you know what you are doing and possess the required technical skills!
Unintended contact with a hot wire leads to severe injuries or death!
Warning! Warning! Warning!

Wiring between the solid state relay and the Raspberry Pi

Note: This wiring is only suited for an experimental setup, since it exposes 230VAC at the yellow lab connectors.
Note the solid, professionally crimped cable lugs on the high power side of the solid state relay.

I used pin 9 of the P1 header as 0V and pin 11 as digital output 0V/3.3V. A 3.3V output is strong enough to drive the solid state relay’s internal opto-couplers and switch it on.
The pinout of the P1 header can be found here.

Test the Remote Power Switch Application

In order to test the whole chain, all links have to be up and running. I started all three applications (node server/app.js and Chrome on the development host and sudo ./RemotePowerSwitch http://192.168.0.15:9000/ on the RPi). Then I entered the URL http://localhost:9000/index.html in the address field of the browser.
The device identifier is still hard coded in the RPS application; it’s 1.
Finally I clicked the buttons ON and OFF in the browser window and checked if the status changed accordingly. With a delay of roughly 1 second it did.
More importantly, the LED on the solid state relay turned on when the ON button was clicked in the browser and off when the OFF button was clicked. This means that I can now switch a 230VAC/10A load via my browser.

Summary

I got everything working in the lab setup.

As usual, you find the source code here; it’s release v1.0. Note that the node_modules folder isn’t part of the repository. After downloading the repo or its zip file you’ll have to change directory to the top-level folder of the project and run $> npm install.

One final step is left: I’ve to push the web application into the cloud and equip the Raspberry Pi application with a very long, secret device identifier. It’s like a password. Only I know it, thus only I can switch the solid state relay on and off.
Once the server in the cloud works with my RPi, I will deploy the RPi and the solid state switch to my remote office. When that’s done I can control my office heater from any browser anywhere on this planet. That is the original goal of this project.

I am currently using RedHat’s OpenShift platform for another project, thus I consider using it for this project too. OpenShift is well documented, and it’s free of charge for small projects like this. Try it out!

That’s it then for this series. As already mentioned in a previous post, the task involved more work and know how than I had initially expected.

Thank you for your interest. Feedback welcome.