” This is a hobby home automation project build using Django,Raspberry Pi,Xbees with aim to learn build mobile-server connection from scratch, working with somepopular elctronic modules (Raspberry Pi and XBees). “
This is a complete flowchart of underlying operations performed.
So clearly the important tasks of development are :
- Making Django Server
- Deploying to Heroku
- Connecting MasterNode, Raspberry Pi to Server
- Connecting Electronic components via Relays/ Xbees to Raspberry Pi
Making a Django Server
I assume you have a little bit knowledge of working in linux systems and you are familiar with django and pthon’s virtual environment.
let’s begin by creating a new virtual environment and activating the environment and then install our required dependencies.
The dependencies that we will be using are:
- Django (1.10)
- Django Rest Framework (3.5.0)
To install them simply type:
We’ll then proceed by creating Project and app files.
Just to see if you have build things correctly try to run server by:
Ignore if you get errors of unapplied Migrations. Make sure that you see the “Congrats on your first django-powered page” default page on http://localhost:8000/
If you have reached here, It means you have successfully intialized the project. Let’s then proceed with actual coding.
Then create a superuser for our project using :
Make login-logout system
Start by creating a templates folder inside the Accounts directory. Then create a folder named Accounts inside the previously created templates folder ( the directory structure will be something like this Accounts->templates->Accounts) and login.html
and logout.html
files inside it.
login.html
<form method="post" action="{% url 'accounts:login' %}">
{% csrf_token %}
Username: <input type='text' name='username' />
<br>
Password: <input type='password' name='password' />
<br>
<input type='submit' value='Login' />
</form>
logout.html
<script>
document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/";
location.href="/accounts/auth/";
</script>
Then create views for login, logout logic as :
views.py
Basically We try to validate the requesting user by looking up for him in our user data model. And if foung redirect him to another place.
Try logging in using the created user’s username and password.
(NOTE : Here we’ve redirected the user to api/list which till now does not exist. Do not get confused for now we’ll see to this soon)
Making Complete Model structure
(I have decided to put in API application however you may put in whichever you like and change files accordingly.)
model.py
(We’ll be dealing with data as user, the electric component’s state vlaue : its bool value and the intensity : the numerical Value)
What we did here is we created a table with name UserModel and added colums as User
- Django’s generic user model , user_name
- test type, MainSwitch
- bool type data, ItemBool
-bool type data , ItemValue
- num type data. Though I have written example for 2 components only, you can expand it to as many you like.
We’ll now register this table to database using -
Since we are using Django’s Rest framework for our project, let’s first add serializers.py
and then create views later .
serializers.py
views.py
For complete understanding of how the rest_framework’s generics work, refer to DRF generic views
Creating Paths
Now Since the model and views have been created, lets create the url paths.
Accounts/urls.py
API/urls.py
We added the routing paths to /list
- for complete list details, /create_user
- to create a new user with username, email and password, get-auth-token
- to get an auth token for the user to be used in his/her mobile app, get-id
- to get the id of user.
Finally register the API model to be visible under the admin dashboard as :
API/admin.py
Let’s now configure the settings.py for verifying locally.
Settings.py
Try running the server locally. If it works then we got our app working fine locally. Next we need to deploy our app to heroku.
Deploying to Heroku
- Create your heroku account if you do not have one by visiting https://www.heroku.com.
- Create a new app in the dash board and visit the deploy section.
- next Install their terminal heroku-cli using heroku-cli.
- We need to generate these required files Expected files for successful deployment of django to heroku.
- Since we’ll require storage for our application, go to resources and then in addons search-select heroku-postgresql.
- Next retrieve the database credentials
URI
from heroku-postgresql settings. Let it be XXXXX.
install some depependencies :
Next list out the dependencies using in the project home directory:
Then generate Pipfile using :
This will generate the Pipfile
and Pipfile.lock
.
To tell heroku that our project is a WSGI type application, we need to generate a Procfile
as :
Procfile
web: gunicorn HomeAutomation.wsgi --log-file -
We are now ready to change settings file for deployment. settings.py
Now lets upload the code to heroku.
run migration and creating superuser commands as :
Now the project should be working fine. Visit the project’s site to view it in action.
Connecting Pi to Server
For now we are simply connecting raspberry pi to Server and looping through to update the data every second.
(NOTE : This functionality however should be upgraded to make asynchronous calls. I have some part of this done at Advanced-Feature-Branch on https://github.com/SatyendraBanjare/Project-HomeAutomation )
install some dependencies in raspberry using:
(NOTE : I used raspbian os in raspberry pi while working in this and configured usb based connection of XBEE(master_node) with pi and also previously configured the XBEE(master_node) in AT MODE )
The final python script for our raspberry pi is :
This script demonstrates controlling 2 electric components corresponding to Item5
and Item6
via XBEES. Therefore there are accordingly 2^2 =4 cases possible. accordingly, we will send one out of those 4 case value to xbee.
There are also directly connected 4 elecctric components that can be controlled directly from raspberry pi corresponding to Item1
, Item2
, Item3
, Item4
.
Connecting Pi XBee Arduino
We will have arduino connected on the other side with that will perform relay operation based on the information received from XBEE(child node).
The relevant arduino code is :
Compile this code on arduino module using their IDE .
Make sure the relays are connected to correct Pin of arduino and Pi. Run the script on Pi and turn on supply for XBees and relays to see things in action.