"Go" Pcduino read gpio data with Python

Source: Internet
Author: User

1 Install request (Requests is a Python HTTP client library): $ sudo apt-get install python-requests

2 Installing Python-pip (PIP is a tool that can replace Easy_install's installation and pipe Python packages): $sudo apt-get Install python-imaging PYTHON-IMAGING-TK Python-pip Python-dev git Note: Sometimes running this command will fail to install, then you need to enter: $sudo apt-get Update and then enter this command.

3 Install flask (Flask is a lightweight Web application framework, written in Python): $sudo pip Install flask

4to theGitHubDownload "Python-pcduino "This library file is placed inUbuntuunder, openSample, copy theblink_led"renamed to"Hello-gpio"and then in the inside will“blink_led"renamed to"hello-gpio.py ", "hello-gpio.pythe code is as follows:

  1. From flask import flask, render_template
  2. Import datetime
  3. Import Gpio
  4. App = Flask (__name__)
  5. Channel = {0: ' gpio0 ', 1: ' Gpio1 ', 2: ' Gpio2 ', 3: ' Gpio3 ', 4: ' Gpio4 ',
  6. 5: ' Gpio5 ', 6: ' Gpio6 ', 7: ' Gpio7 ', 8: ' Gpio8 ', 9: ' Gpio9 ',
  7. Ten: ' Gpio10 ', one: ' Gpio11 ', and: ' Gpio12 ', ' gpio13 '
  8. }
  9. @app. Route ("/")
  10. def hello ():
  11. now = Datetime.datetime.now ()
  12. TimeString = Now.strftime ("%y/%m/%d%h:%m:%s")
  13. TemplateData = {
  14. ' title ': ' Hello! ',
  15. ' Time ': timestring
  16. }
  17. Return render_template (' main.html ', **templatedata)
  18. @app. Route ("/readpin/<pin>")
  19. def readpin (PIN):
  20. Gpio.pinmode (Channel[int (PIN)],gpio. INPUT)
  21. Value = ""
  22. if (Gpio.digitalread (Channel[int (pin)) = = Gpio. High):
  23. Value = "Read GPIO" + Pin + "is high!"
  24. else:
  25. Value = "Read GPIO" + Pin + "is low!"
  26. TemplateData = {
  27. ' title ': ' Status of GPIO ' + PIN,
  28. ' Value ': value
  29. }
  30. Return render_template (' pin.html ', **templatedata)
  31. if __name__ = = "__main__":
  32. App.run (host= ' 0.0.0.0 ', port=80,debug=true)
Copy Code
5 Create a new folder named "templates" in the folder "hello-gpio", and the folder contains 2 files: " main.html, pin.html " <ignore_js_op> <ignore_js_op>
main.html content is as follows:
    1. <! DOCTYPE html>
    2. <title>{{Title}} </title>
    3. <body>
    4. <center>
    5. </center>
    6. </body>
Copy Codepin.html content is as follows:
  1. <! DOCTYPE html>
  2. <title>{{Title}} </title>
  3. <body>
  4. <center>
  5. nbsp <a href= "http://www.pcduino.org" >pcDuino.org</a>
  6. </center>
  7. </body>
Copy Code6 input : $sudo python./hello-gpio.py Run code: <ignore_js_op>
7 View pcduino IP address:$ifconfig : <ignore_js_op>
8 on another PC that shared the same network with Pcduino, open the browser input 192.168.35 you will see the following information:

"Go" Pcduino read gpio data with Python

Related Article

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.