Sunday, April 12, 2015

Control GPIO Pins on Raspberry Pi 2 using Webiopi

I recently started working on a robot controlled with my Raspberry Pi , it is at the moment kind of rover which is attached to my Raspberry Pi through a long umbellical cord of wires. There are 2 servos to which my Raspberry Pi sends out data through an L298N H-Bridge which makes the Robot "Rover" wheels  move either backward or forward .

I managed to also write a python code to send signals to the servers which looks a bit like this :

-------------
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)

GPIO.output(7,True)
time.sleep(1)
GPIO.output(7,False)
GPIO.output(11,True)
time.sleep(1)
GPIO.output(11,False)
GPIO.output(13,True)
time.sleep(1)
GPIO.output(13,False)
GPIO.output(15,True)
time.sleep(1)
GPIO.output(15,False)
GPIO.cleanup()


--------------

Now that is great but what would have been better would be to access to the GPIO pin remotely via a REST API for example and here is where Webiopi kicks in which basically provides you a WEB and a REST interface to connect to you GPIO pins of your PI :
https://code.google.com/p/webiopi/wiki/INSTALL  

As a first setup you need to follow the instructions from the Webiopi install page above .

Note: that current version ,  WebIOPi-0.7.1 ,didn't work straight off with my Raspberry Pi 2 I had to do the following changes to the following c files  once the Webiopi setup was completed:

1.python/native/cpuinfo.c,change "BCM2708" to "BCM2709";
2.python/native/gpio.c, change "#define BCM2708_PERI_BASE 0x20000000" to "#define BCM2708_PERI_BASE 0x3f000000";
3.run setup.sh again.

The above solution was found from RaspberryPi forums .

Now you should be up and running after having re-run the setup for WebIOPI. You launch the server using command:

sudo /etc/init.d/webiopi start

Then you go to navigate to the following location on your PI :

http://IP_ADDRESS_OF_YOUR_PI:8000/

Note that you can get the IP_ADDRESS_OF_YOUR_PI using ifconfig command .

To quickly test the setup navigate to :
http://IP_ADDRESS_OF_YOUR_PI:8000/app/gpio-header

For those pins on which servos are connected change them from IN to OUT  and click on the number corresponding to the pin to execute .


Weaved

Note that WebIOPI is supported by Weaved which provides you to install a package on your Raspberry PI that allows you to control your RaspberryPi WebIOPI from anywhere as long as  you and your PI are connected to the internet .

What nice is that this is a free service and I justed it works .

https://developer.weaved.com/portal/members/betapi.php