Python is used for simple WEB control of Raspberry Pi, and python Raspberry Pi web

Source: Internet
Author: User

Python is used for simple WEB control of Raspberry Pi, and python Raspberry Pi web

First, we will show you the effect. I feel very satisfied. Please continue to read the full text:

 

Knowledge used: Python Bottle HTML Javascript JQuery Bootstrap AJAX, of course, linux

I'm going, so many ...... Let me start ......

First paste the final source code:

#! /Usr/bin/env python3from bottle import get, post, run, request, template @ get ("/") def index (): return template ("index ") @ post ("/cmd") def cmd (): print ("press the button:" + request. body. read (). decode () return "OK" run (host = "0.0.0.0 ")

That's right. I just want to explain 10 sentences:

1 .#! /Usr/bin/env python3: Tell shell that the file is Python source code, and let bash call python3 to explain the code.

2. from bottle import get, post, run, request, template, the methods and objects I used are imported from the bottle framework.

The following sentence defines two routes, one is "/" and the other is "/cmd". The former is of the get type (decorated with @ get ), the latter is POST type (decorated with @ post)

The first route is very simple, that is, reading the index template (the template is html) and sending it to the client (browser), because the path is "/", that is, for example, the IP address of Raspberry Pi is 192.168.0.10.

Use http: // 192.168.0.10: 8080 to access our "/" route (the default port of the bottle is 8080)

Similarly, the second route path is "/cmd", that is, access http: // 192.168.0.10: 8080/cmd to access the second route.

The last sentence: run (host = "0.0.0.0") is to call the run method of bottle to create an http server so that we can access our interface through a browser.

Below I will explain in detail the functions of these codes:

The role of the first route is to throw the browser an HTML (index. tpl) document to display this interface:

 

The source code of this file is as follows:

<! DOCTYPE html> 

This content is a bit more, but it is very simple. It refers to the two front-end frameworks of jquery bootstrap and adds 5 buttons (code between <body> </body> ). Of course, I used bootstrap's built-in icons, such as up, down, left, right, and stop. The IDS of these five buttons are defined as up, down, left, right, and stop. Then the following key code is written:

$(function(){$("button").click(function(){$.post("/cmd",this.id,function(data,status){});});});

That's right. There are three codes ......

Line 2 binds a click event to all buttons, and line 3 calls the post method of jquery to call this. id (the id of the clicked button) is sent to the path "/cmd". At this time, the second route of the python code takes effect and receives the id of the clicked button on the webpage, and printed the "pressed Button: XXX"

Of course, you can write a few if statements here to make some actual control as needed. Well, for example, you can call wiringpi2 for python to control the GPIO of Raspberry Pi.

I would like to introduce so much about the simple implementation of Raspberry Pi's WEB Control Using Python, and hope to help you!

Articles you may be interested in:
  • Example of python getting GY-85 9-Axis Module information in Raspberry Pi
  • Tutorial on installing Python and OpenCV on Raspberry Pi 2 or Raspberry Pi B +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.