Controlling Raspberry Pi with Django

Embedded System Ristek
RISTEK Fasilkom UI

--

It is quite easy and fun playing with Raspberry Pi. We could build anything, blinking LEDs for the example. But most of the time, our interaction is limited with just the Raspberry Pi itself. Wouldn’t it be more fun if we can control those LEDs with our smartphone? So in this post, let’s create LEDs that we can control with our smartphone.

The basic idea is creating web server running on Raspberry Pi, make other devices can communicate with Raspberry. Then you can control our Raspberry with this connection.

So what you need is

  • Raspberry Pi 3
  • LEDs
  • Resistors
  • Jumper
  • Django

Setup LEDs

First thing, you need to setup the LEDs. It’s quite easy, as you can easily find it on google. You only need to connect ground and pin to LEDs. Don’t forget to use resistor between positive LEDs and VCC so LEDs won’t burn out. You can freely choose which pin you want to control our LEDs.

Install Dependencies

You want django running on our raspberry pi as a server. To do that, You need to install all dependencies needed. To do that,

  1. Update local package:

sudo apt-get update

Install django with pip:

sudo apt-get install python-pip

sudo pip install django

If you prefer using python3:

sudo apt-get install python3-pip

sudo pip3 install django

2. Install virtualenv to run django

sudo pip install virtualenv

3. Install apache2, phpmyadmin, and mysql

Install python mysqldb

sudo apt-get install python-mysqldb -y

Install LAMP server

sudo apt-get install apache2 -y

sudo apt-get install mysql-server mysql-client -y

sudo apt-get install php5 libapache2-mod-php5 php5-mysql -y

sudo service apache2 restart

Install phpmyadmin and add it to apache configuration

sudo apt-get install phpmyadmin -y

sudo nano /etc/apache2/apache2.conf

Add following code:

Include /etc/phpmyadmin/apache.conf

And restart apache service

sudo service apache2 restart

Create Django Project

After installing django, now you can create a new django app.

Try

django-admin startproject blinkled

This creates new directory called “blinkled”, with structures like

Now setup our django app

python manage.py startapp led

It will create new directory called “led”, and the structures become

Open settings.py under blinkled directory, and add our app into INSTALLED_APPS:

Now let’s build the app. Open views.py under led directory. Then add this following code:

Don’t forget to set your LED_PIN GPIO to output mode.

Okay, you already created our views. To call those function from other devices, you will create endpoints which run those methods. Now, open urls.py under led directory, then add

Great, now if you hit /led/turnOn/ or /led/turnOff/ it will run turnOn or turnOff methods on views.py

Run Django

Now our project are ready to run. To do that, you need to:

  1. Create virtualenv to run django.Go to your root project, then:

virtualenv env

Run env:

source env/bin/activate

Install all requirements needed by your project:

pip install -r requirements.txt

Run django

python manage.py runserver

And tadaa… your project is listening on port 8000. You can try hitting localhost:8000/led/turnOn/ and localhost:8000/led/turnOff/ and wow, your led is blinking.

Creating Raspberry Pi as a Server

But, it only works on your local. To make it accessable by other devices on the same network, you need to

  1. Edit ALLOWED_HOST in settings.py under blinkled directory:
    ALLOWED_HOSTS = [“localhost”,<Your_RaspberryPi_IP>]
  2. Run server on port 8080

python manage.py runserver 0.0.0.0:8080

Alright, now you can control your LED from other devices on the same network with http://<Your_RaspberryPi_IP>:8080.

And that’s it, you can control your Pi with any device you can imagine!

--

--