Raspberry Poll

csmith

Created by: csmith

Published Mar. 12, 2013

For this project at Caktus we created a simple voting poll that allows users to vote over SMS and displays the results as the votes come in. To reproduce this project, you'll need a Raspberry Pi and something to handle SMS messages. Our project uses a GSM modem, but the project could be configured to use Twilio or similar services. The project leverages three great Python projects: Django, RapidSMS, and Pygame.

The backend is a simple RapidSMS app that can be found here on Github: RapidSMS-RaspberryPi The backend handles the receiving, processing, and sending of SMS messages and provides the Django admin interface for creating and configuring polls. RapidSMS also provides a web interface for watching SMS messages come in or for faking the receipt of an SMS message.

The frontend is a Pygame project that can be seen here: RaspberryPoll. RaspberryPoll queries the database for the vote totals of the current poll and displays these in a bar chart. It is capable of cycling through multiple polls, though for our purposes we only enable a single poll at a time.

We ran both the backend and frontend on the same Raspberry Pi without any problems, but the two could be split over multiple Pi's on a network if desired.

Related categories: PyGame , Web

Step 1: Install Requirements

On a Raspberry Pi running the Raspbian distribution, navigate to a terminal or use SSH to install the requirements listed on the Rapidsms-RaspberryPi Github page. These requirements include Git, Python, Pip, Virtualenv, and Virtualenvwrapper. You should be able to install these following the steps below:

sudo apt-get install git-core python-dev python-setuptools
sudo easy_install pip
sudo pip install virtualenv virtualenvwrapper

 

Step 2: Clone RapidSMS-RaspberryPi

In a terminal, or over ssh run:

git clone git@github.com:caktus/rapidsms-raspberrypi.git

In a browser, navigate to the RapidSMS-RaspberryPi Github page and follow the directions under Getting Started. Those steps are summarized below for your convenience. You may also want to change the database name in raspberrypi-rapidsms/settings/base.py to point to a different location on your file system. The location isn't that important but you'll need to remember it for setting up the frontend project.

mkvirtualenv --distribute rapidsms_raspberrypi
$VIRTUAL_ENV/bin/pip install -r $PWD/requirements/dev.txt
cp rapidsms_raspberrypi/settings/local.example.py rapidsms_raspberrypi/settings/local.py
echo "export DJANGO_SETTINGS_MODULE=rapidsms_raspberrypi.settings.local" >> $VIRTUAL_ENV/bin/postactivate
echo "unset DJANGO_SETTINGS_MODULE" >> $VIRTUAL_ENV/bin/postdeactivate
deactivate
workon rapidsms_raspberrypi
python manage.py syncdb
python manage.py migrate
python manage.py runserver

The final step should run the Django runserver successfully.

Step 3: Setup a RapidSMS backend

If you have a GSM modem:

First, we'll need to install kannel on the Pi with:

sudo apt-get install kannel

Now, you'll need to configure the modem to work with RapidSMS using the RapidSMS Kannel backend as described here: RapidSMS Kannel Backend Documentation.The default settings file contains a `INSTALLED_BACKENDS` declaration that you may need to configure for your setup.

If you don't have a GSM modem, one option is using Twilio. A RapidSMS backend for Twilio is available on Github as RapidSMS-Twilio. You'll need to install python twilio with:

pip install python-twilio

Now, follow the Usage section of the RapidSMS-Twilio Readme to configure RapidSMS to use Twilio

Step 4: Clone RaspberryPoll

To setup the frontend of the project, start by cloning RaspberryPoll. You can accomplish this with:

git clone git@github.com:caktus/raspberrypoll.git

The raspberrypolldisplay.py file contains the code needed for the frontend. It takes a single argument for the location and filename of the sqlite database to use. The repository comes with a test.sqlite file for testing the frontend on its own before hooking it up to a backend. To run, simply use:

python raspberrypolldisplay test.sqlite

fakepoll.py is a program that runs separately and updates the data in a given sqlite database file in order to simulate changes to the data made by the backend. fakepoll also takes the location and filename of the sqlite database as an argument. While raspberrypolldisplay is running, run the following to see the database change with random data:

python fakepoll.py test.sqlite

At this point, we want to run the frontend with the same database that the backend uses. The backend project doesn't come with a default poll, so we'll need to set one up. To accomplish this, we need to setup a Poll in the RapidSMS-RaspberryPi backend. While running the rapidsms-raspberrypi project, navigate to the Pi in a web browser into the Django admin. You'll need to create a Poll that is marked active and give it two or more Options. Options should not have spaces in their names.

Now you're ready to run the frontend off of the same database. To do this, simply run raspberrypolldisplay with an argument for the SQLite database that the backend is configured to use in rapidsms-raspberrypi/settings/base.py. By default, this is '/var/tmp/rapidsms_raspberrypi' as in the following example:

python raspberrypolldisplay.py /var/tmp/rapidsms_raspberrypi

Feel free to experiment with different fonts or images to show in the corners. You'll probably want to keep Razzy though.

Comments

comments powered by Disqus