6.Web Servers
Upcoming SlideShare
Loading in...5
×
 

6.Web Servers

on

  • 162 views

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 ...

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 :)

Statistics

Views

Total Views
162
Views on SlideShare
158
Embed Views
4

Actions

Likes
0
Downloads
4
Comments
0

2 Embeds 4

https://twitter.com 2
http://www.slideee.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

6.Web Servers 6.Web Servers Presentation Transcript

  • Pi Maker Workshop Powered by: 6.Web Servers http://www.inventrom.com/http://www.robotechlabs.com/ Mayank Joneja botmayank.wordpress.com botmayank@gmail.com
  • 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.
  • 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.
  • 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
  • Mayank Joneja Flask  http://mattrichardson.com/Raspberry-Pi-Flask/  http://flaskpi.com/tutorials
  • 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
  • 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.
  • Mayank Joneja What time is it?  hello-template.py from 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)
  • 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>
  • 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)
  • 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)
  • Mayank Joneja HTML File for Buttons <!DOCTYPE html> <head> <title>{{ title }}</title> </head> <body> <h1>Pin Status</h1> <h2>{{ response }}</h2> </body> </html>
  • 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)
  • 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
  • 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