6.Web Servers

15
Pi Maker Workshop Powered by: 6.Web Servers http://www.inventrom.com/ http://www.robotechlabs.com/ Mayank Joneja botmayank.wordpress.com [email protected]

description

This is a part of the slide set used at the MakerSpace Noida (India) launch event, Pi Maker Workshop. This slide set is designed to help people get started with the Raspberry Pi and also serves as a collection of innovative projects and some core basic concepts that can aid anybody with their first few steps into the world of DIY electronics or maybe serve as a refresher for the experienced. Feel free to refer and share but please don't alter the watermarks :)

Transcript of 6.Web Servers

Page 1: 6.Web Servers

Pi Maker

Workshop

Powered by:

6.Web Servers

http://www.inventrom.com/http://www.robotechlabs.com/

Mayank Jonejabotmayank.wordpress.com

[email protected]

Page 2: 6.Web Servers

Mayank Joneja

Internet of Things

The Internet of Things (IoT) is a scenario in

which objects, animals or people are

provided with unique identifiers and the

ability to transfer data over a network

without requiring human-to-human or

human-to-computer interaction.

Page 3: 6.Web Servers

Mayank Joneja

IoT has evolved from the convergence of wireless technologies, micro-electromechanical

systems (MEMS ) and the Internet.

A thing, in the Internet of Things, can be a person with a heart monitor implant, a farm

animal with a biochip transponder , an automobile that has built-in sensors to alert the

driver when tire pressure is low -- or any other natural or man-made object that can be

assigned an IP address and provided with the ability to transfer data over a network.

So far, the Internet of Things has been most closely associated with machine-to-machine

(M2M) communication in manufacturing and power, oil and gas utilities. Products built

with M2M communication capabilities are often referred to as being smart.

Page 4: 6.Web Servers

Mayank Joneja

How to create a web server

One of the simplest ways to have an IoT type system is through setups like WebIOPi and IFTTT.

However when it comes to developing your own Web Applications, there are several approaches possible for hosting a server

Apache (LAMP) and MySQL : http://www.wikihow.com/Make-a-Raspberry-Pi-Web-Server

LigHTTPd :http://www.penguintutor.com/linux/light-webserver

http://raspberrywebserver.com/

Flask: A Django based web framework using Python

Page 5: 6.Web Servers

Mayank Joneja

Flask

http://mattrichardson.com/Raspberry-Pi-Flask/

http://flaskpi.com/tutorials

Page 6: 6.Web Servers

Mayank Joneja

Setup and Test

sudo apt-get install python-pip

sudo pip install flask

sudo nano hello-flask.py

sudo python hello-flask.py

from flask import Flask

app = Flask(__name__)

@app.route("/")

def hello():

return "Hello World!"

if __name__ == "__main__":app.run(host='0.0.0.0', port=80, debug=True

Page 7: 6.Web Servers

Mayank Joneja

HTML

If you want to send the browser a site formatted in proper HTML, it doesn't make a lot of

sense to put all the HTML into your Python script.

Flask uses a template engine calledJinja2 so that you can use separate HTML files with

placeholders for spots where you want dynamic data to be inserted.

Page 8: 6.Web Servers

Mayank Joneja

What time is it?

hello-template.pyfrom flask import Flask, render_template

import datetime

app = Flask(__name__)

@app.route("/")

def hello():

now = datetime.datetime.now()

timeString = now.strftime("%Y-%m-%d %H:%M")

templateData = {

'title' : 'HELLO!',

'time': timeString

}

return render_template('main.html',

**templateData)

if __name__ == "__main__":

app.run(host='0.0.0.0', port=80, debug=True)

Page 9: 6.Web Servers

Mayank Joneja

main.html

<!DOCTYPE html>

<head>

<title>{{ title }}</title>

</head>

<body>

<h1>Hello, World!</h1>

<h2>The date and time on the server is: {{ time }}</h2>

</body>

</html>

Page 10: 6.Web Servers

Mayank Joneja

The Return of the Buttons!

hello-gpio.py

from flask import Flask, render_template

import datetime

import RPi.GPIO as GPIO

app = Flask(__name__)

GPIO.setmode(GPIO.BCM)

@app.route("/")

def hello():

now = datetime.datetime.now()

timeString = now.strftime("%Y-%m-%d %H:%M")

templateData = {

'title' : 'HELLO!',

'time': timeString

}

return render_template('main.html', **templateData)

Page 11: 6.Web Servers

Mayank Joneja

@app.route("/readPin/<pin>")

def readPin(pin):

try:

GPIO.setup(int(pin), GPIO.IN)

if GPIO.input(int(pin)) == True:

response = "Pin number " + pin + " is high!"

else:

response = "Pin number " + pin + " is low!"

except:

response = "There was an error reading pin " + pin + "."

templateData = {

'title' : 'Status of Pin' + pin,

'response' : response

}

return render_template('pin.html', **templateData)

if __name__ == "__main__":

app.run(host='0.0.0.0', port=80, debug=True)

Page 12: 6.Web Servers

Mayank Joneja

HTML File for Buttons

<!DOCTYPE html>

<head>

<title>{{ title }}</title>

</head>

<body>

<h1>Pin Status</h1>

<h2>{{ response }}</h2>

</body>

</html>

Page 13: 6.Web Servers

Mayank Joneja

What else should I look up?

HTML basics and CSS (The look of the actual webpage and CSS for styling and buttons etc)

JavaScript (for mouse/keyboard events detections etc)

AJAX (to run things in the background like navigation to certain links)

Page 14: 6.Web Servers

Mayank Joneja

Some important notes

Folder structure is extremely important while deploying apps using Flask

Favicons, Background files, HTML code and Python code should be in proper places

Modify the python code to include Serial (PySerial) to communicate with Arduino, AVR

boards or other devices

Make sure that the variables passing between the Python code and the HTML code are

being shared properly

Page 15: 6.Web Servers

Mayank Joneja

Dynamically detecting current IP

Here’s a small piece of code that helps if you don’t want to find out your Pi’s IP to change

in the code

import socket

import subprocess

#Detect ip addr

arg='ip route list'

p=subprocess.Popen(arg,shell=True,stdout=subproce

ss.PIPE)

data = p.communicate()

split_data = data[0].split()

ipaddr = split_data[split_data.index('src')+1]

#ipaddr = "192.168.137.194"

#my_ip = 'Your ip is %s' % ipaddr