Objective
- Building RESTful API with Raspberry Pi 3
Things
For this project, you will need,
- Raspberry Pi 3 [RPi3] (quantity: 1 no.),
- Micro SD card (quantity: 1 no.),
- Power Adapter (quantity: 1 no.),
- VNC Viewer software on your computer
Let’s Begin!
REST stands for Representational State Transfer. REST is about resources and how to represent resources in different ways on a server. It offers a simple, interoperable and flexible way of writing web services. REST is an architecture all about client-server communication using HTTP.
RESTful web services are web services which are REST based. With RESTful services, you can do anything you already do with normal web services. No severe restrictions on how the architectural model will be and what properties it will have.
Updating all packages
Let’s start by updating everything.
sudo apt-get update
sudo apt-get upgrade
Installing Python Flask
Next, we’re going to use Python Flask for the REST interface. Flask is written in Python and a very fast, powerful and stable way to connect.
mkdir source
cd source
apt-get install curl
curl -O http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
easy_install pip
pip install flask
This will install flask.
Testing Flask
Now we have to test this. We will create a web user for this.
adduser web
su web
cd ~
mkdir web-server
cd web-server
Now we need to create a python program app.py
sudo nano app.py
Then enter the below code there.
#!/usr/bin/python from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello, World!" if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)
Save it and close it. Now we have to set this as executable and execute it.
chmod a+x app.py
./app.py
Now we need to create a new SSH session on a new terminal and type the following. Do not close the other terminal which is running app.py
curl 127.0.0.1:5000
You will see the hello world message.
RPi Pinout
Interfacing the RPi with LEDs
Use the given schematic to make the necessary connections,
Interfacing the LEDs with Raspberry Pi
Final Code
Now we will add some action to app.py. We will use the flask to create a blinking LED.
Now again open app.py and copy paste the code given below.
Making the Webpage for the WebApp
HTML tags separated from your Python script is how you keep your project organized.
Flask uses a template engine called Jinja2 that you can use to send dynamic data from your Python script to your HTML file.
Create a new folder called templates:
pi@raspberrypi:~/web-server $ mkdir templates
pi@raspberrypi:~/web-server $ cd templates
pi@raspberrypi:~/web-server/templates $
Create a new file called main.html. This HTML will render a button to control LED.
pi@raspberrypi:~/web-server/templates $ nano main.html
Write the following code in the html file.
<!DOCTYPE html>
<head>
<title>RPi Web Server</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<h1>RPi Web Server</h1>
{% for pin in pins %}
<h2>{{ pins[pin].name }}
{% if pins[pin].state == true %}
is currently <strong>on</strong></h2><div class="row"><div class="col-md-2">
<a href="/{{pin}}/off" class="btn btn-block btn-lg btn-default" role="button">Turn off</a></div></div>
{% else %}
is currently <strong>off</strong></h2><div class="row"><div class="col-md-2">
<a href="/{{pin}}/on" class="btn btn-block btn-lg btn-primary" role="button">Turn on</a></div></div>
{% endif %}
{% endfor %}
</body>
</html>
There you go!
Launch the web server. To launch your Raspberry Pi web server move to the folder that contains the file app.py:
pi@raspberrypi:~/web-server/templates $ cd ..
Then run the following command:
pi@raspberrypi:~/web-server $ sudo python app.py
Your web server should start immediately! Here you can see IP address where the page will be rendered. Go to the browser and enter the IP address.
Now you will see two buttons to control LEDs.
Takeaway
