RaspberryPi real-world control with REST API

Finally found some time playing with a RaspberryPi and an attached PiFace Interface board to control some garden pumps and potentially an Aquaponics setup in the near future.

PiFace board

Requirements

Base Raspian (Debian Wheezy) Install (I prefer the clean minimal install via https://githutb.com/hifi/raspbian-ua-netins).

Configure the base system as per my previous base install.

Install Apache & PHP

sudo apt-get install apache2 php5 php5-dev php5-cli php5-mcrypt curl raspi-config

Ensure mod-rewrite is enabled

sudo a2enmod rewrite

Install PHP SPI Extension

More info: https://github.com/frak/php_spi

git clone https://github.com/frak/php_spi.git
cd php_spi
./phpize
./configure --enable--spi
make
make test
make install

Install PiFace REST API

cd /var/www/
git clone https://github.com/natefanaro/piface-rest-api
cd piface-rest-api
curl -sS https://getcomposer.org/installer | php
php composer.phar install

Thanks to Nate Fanaro for the excellent work on the API.

Make sure SPI is enabled on the RPi

raspi-config

Advanced Options –> A5 SPI –> yes

Install PiFace Modules

sudo apt-get install python3-pifacedigitalio python-pifacedigitalio
sudo usermod -a -G gpio www-data
sudo usermod -a -G spi www-data

For more info visit http://www.piface.org.uk/guides.

Apache2 API Configuration

Enable SPI extension

sudo vim /etc/php5/apache2/php.ini
#add the following extension
extension="spi.so"

Apache VHost config

sudo vim /etc/apache2/sites-available/piface

Paste the following VHost config

<Virtualhost *:80>
ServerName HOSTNAME
ServerAdmin webmaster@domain.com
DocumentRoot /usr/share/piface-rest-api/public_html
ErrorLog /var/log/apache2/piface_error.log
CustomLog /var/log/apache2/piface_access.log combined
<Directory />
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</Virtualhost>

Enable the newly created VHost

a2ensite piface
service apache2 restart

Testing

curl http://HOSTNAME/led/test/

Next up will be some modification for securing access to the PiFace REST API via some sort of API Key or digital certificate as it’s open by default at the moment.

Author: Leo Gaggl

ict business owner specialising in mobile learning systems. interests: sustainability, internet of things, ict for development, open innovation, agriculture

One thought on “RaspberryPi real-world control with REST API”

  1. Thanks for the article… it worked perfectly. However, if I turn off the power and have a cold reboot, the rest API is behaving really odd, it does not let you GET or SET. Until you logon to the shell, run some .py script related to pi (such as the blink.py). Then, the rest API starts to work. If you do a sudo reboot, it’s fine. Problem is only when doing a cold reboot. Any idea what is wrong?

Leave a Reply