Raspberry Pi installs Flask service, and reads Gpio status and system time on the end page

Source: Internet
Author: User

Have done some works of the internet of things, because do not want to always do the app to control, because not everyone to download your own app, browser everyone is there, then everyone through the browser Web to access our server, it is not very simple and convenient, using Flask+python.
Flask is a lightweight WEB application framework written in Python that is simple to use and easy to get started with.
Install Flask:
sudo apt-get install Python-pip
sudo pip install flask

Then a simple server is set up, there are all our Raspberry Pi, estimated that we have to write a few programs, then:
mkdir Flask && CD Flask
mkdir read_gpio && CD Read_gpio

sudo nano hello-gpio.py

 fromFlaskImportFlask, Render_templateImportDatetime#Import System TimeImportRpi.gpio as GPIO#Import GpioApp = Flask (__name__) Gpio.setmode (GPIO. BCM)#set Gpio mode to BCM@app. Route ("/")defReadpin (): now= Datetime.datetime.now ()#Crawl TimeTimeString = Now.strftime ("%y-%m-%d%h:%m:%s")#Capture system Time function to TimeStringTry: Gpio.setup (The GPIO. IN)#Read Bcm_gpio_20      ifGpio.input (20) = =True:response="bcm_gpio_20 is high!"      Else: Response="bcm_gpio_20 is low!"   except: Response="there is an error reading pin"TemplateData= {      ' Time': TimeString'title':'Status of Pin'+Pin,'Response': Response}returnRender_template ('read_pin.html', **templatedata)#send templates to read_pin.html .if __name__=="__main__": App.run (Host='0.0.0.0', port=80, Debug=true)

sudo nano read_pin.html

<!DOCTYPE HTML>   <Head>      <title>{{title}}</title>   </Head>   <Body>      <H1>Pin Status</H1>      <H2>{{Response}}</H2>      <H2>{{Time}}</H2>   </Body></HTML>

Note: The HTML file to create a new subdirectory folder in the. Py Peer directory templates, or the template will not be found error;
The format is as follows:
Read_gpio (folder)--
---hello-gpio.py
---templates (folder)
---read_pin.html

Then a read Gpio state is built, we read the bcm_gpio_20, can be modified;
Then access your Raspberry Pi IP address in the browser ifconfig
You can access your Web page on your phone or PC as long as your Raspberry Pi is on the same LAN; Read the Raspberry Pi system time and Gpio status. Game_over, look at the state.


Finally say a little bit: maybe we also want to not live to watch our web page, just a page status, you can constantly refresh the page to get data, not too human, then set the page refresh time is good, according to the browser to choose, bloggers use Firefox. The name is Iceweasel on the Raspberry Pi.
Installation method is sudo apt-get install iceweasel;
After installation, then select the plugin reladevery install, restart the browser, the page you want to right-click Relad_every custom Time, the shortest one second, see reading effect can also. Koko

The following article describes the Web control Gpio

Raspberry Pi installs the Flask service, and reads Gpio status and system time on the end page

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.