├── README.md ├── http_webserver.py └── led.py /README.md: -------------------------------------------------------------------------------- 1 | # YouTube Tutorial 2 | 3 | [![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/owgRkU_-4lw/0.jpg)](https://www.youtube.com/watch?v=owgRkU_-4lw) 4 | 5 | ## Overview 6 | 7 | Stock-Simulator-9 8 | 9 | -------------------------------------------------------------------------------- /http_webserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import RPi.GPIO as GPIO 4 | import os 5 | from http.server import BaseHTTPRequestHandler, HTTPServer 6 | 7 | host_name = '10.0.0.184' # IP Address of Raspberry Pi 8 | host_port = 8000 9 | 10 | 11 | def setupGPIO(): 12 | GPIO.setmode(GPIO.BCM) 13 | GPIO.setwarnings(False) 14 | 15 | GPIO.setup(18, GPIO.OUT) 16 | 17 | 18 | def getTemperature(): 19 | temp = os.popen("/opt/vc/bin/vcgencmd measure_temp").read() 20 | return temp 21 | 22 | 23 | class MyServer(BaseHTTPRequestHandler): 24 | 25 | def do_HEAD(self): 26 | self.send_response(200) 27 | self.send_header('Content-type', 'text/html') 28 | self.end_headers() 29 | 30 | def _redirect(self, path): 31 | self.send_response(303) 32 | self.send_header('Content-type', 'text/html') 33 | self.send_header('Location', path) 34 | self.end_headers() 35 | 36 | def do_GET(self): 37 | html = ''' 38 | 39 | 41 |

Welcome to my Raspberry Pi

42 |

Current GPU temperature is {}

43 |
44 | Turn LED : 45 | 46 | 47 |
48 | 49 | 50 | ''' 51 | temp = getTemperature() 52 | self.do_HEAD() 53 | self.wfile.write(html.format(temp[5:]).encode("utf-8")) 54 | 55 | def do_POST(self): 56 | 57 | content_length = int(self.headers['Content-Length']) 58 | post_data = self.rfile.read(content_length).decode("utf-8") 59 | post_data = post_data.split("=")[1] 60 | 61 | setupGPIO() 62 | 63 | if post_data == 'On': 64 | GPIO.output(18, GPIO.HIGH) 65 | else: 66 | GPIO.output(18, GPIO.LOW) 67 | 68 | print("LED is {}".format(post_data)) 69 | self._redirect('/') # Redirect back to the root url 70 | 71 | 72 | # # # # # Main # # # # # 73 | 74 | if __name__ == '__main__': 75 | http_server = HTTPServer((host_name, host_port), MyServer) 76 | print("Server Starts - %s:%s" % (host_name, host_port)) 77 | 78 | try: 79 | http_server.serve_forever() 80 | except KeyboardInterrupt: 81 | http_server.server_close() 82 | -------------------------------------------------------------------------------- /led.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import RPi.GPIO as GPIO 4 | import os 5 | 6 | 7 | def setupGPIO(): 8 | GPIO.setmode(GPIO.BCM) 9 | GPIO.setwarnings(False) 10 | 11 | GPIO.setup(18, GPIO.OUT) 12 | 13 | 14 | def printTemperature(): 15 | temp = os.popen("/opt/vc/bin/vcgencmd measure_temp").read() 16 | print("GPU temperature is {}".format(temp[5:])) 17 | 18 | 19 | def controlLED(): 20 | try: 21 | while True: 22 | user_input = input( 23 | "Turn LED On or Off with 1 or 0 (Ctrl-C to exit): ") 24 | if user_input is "1": 25 | GPIO.output(18, GPIO.HIGH) 26 | print("LED is on") 27 | elif user_input is "0": 28 | GPIO.output(18, GPIO.LOW) 29 | print("LED is off") 30 | except KeyboardInterrupt: 31 | GPIO.cleanup() 32 | print("") 33 | 34 | 35 | setupGPIO() 36 | printTemperature() 37 | 38 | controlLED() 39 | --------------------------------------------------------------------------------