Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Control Raspberry Pi GPIO via http web server: code designed for Cytron Maker pHAT
#Control Raspberry Pi GPIO via http web server
#Original code from: https://github.com/e-tinkers/simple_httpserver
#==================================================================
#Code designed for Cytron Maker pHAT
#===================================
#Added:
#support control for all 8 LEDs ("/ledX/" URL endpoints)
#support control for buzzer
#some minnor HTML code changes
#=============================
#To do:
#LED blinking
#=============================
#Contact me: https://github.com/odolezal
#=======================================
import RPi.GPIO as GPIO
import os
from time import sleep
from http.server import BaseHTTPRequestHandler, HTTPServer
host_name = '10.0.0.12' # Change this to your Raspberry Pi IP address
host_port = 8000
class MyServer(BaseHTTPRequestHandler):
""" A special implementation of BaseHTTPRequestHander for reading data from
and control GPIO of a Raspberry Pi
"""
def do_HEAD(self):
""" do_HEAD() can be tested use curl command
'curl -I http://server-ip-address:port'
"""
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
""" do_GET() can be tested using curl command
'curl http://server-ip-address:port'
"""
html = '''
<html>
<head>
<title>GPIO web control</title>
</head>
<body style="width:960px; margin: 20px auto;">
<h1>GPIO web control</h1>
<p>Current GPU temperature is {}</p>
<p><strong>Control:</strong></p>
<p>Turn LED1: <a href="/led1/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led1/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn LED2: <a href="/led2/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led2/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn LED3: <a href="/led3/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led3/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn LED4: <a href="/led4/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led4/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn LED5: <a href="/led5/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led5/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn LED6: <a href="/led6/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led6/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn LED7: <a href="/led7/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led7/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn LED8: <a href="/led8/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led8/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn all LEDs: <a href="/led-all/on"><font color="green"><strong>ON</strong></font></a> or <a href="/led-all/off"><font color="red"><strong>OFF</strong></font></a></p>
<p>Turn Buzzer: <a href="/buzzer/on"><font color="green"><strong>ON</strong></font></a> or <a href="/buzzer/off"><font color="red"><strong>OFF</strong></font></a></p>
<p><strong>Status:</strong></p>
<div id="led-status"></div>
<script>
document.getElementById("led-status").innerHTML="{}";
</script>
</body>
</html>
'''
temp = os.popen("/opt/vc/bin/vcgencmd measure_temp").read()
self.do_HEAD()
status = ''
if self.path=='/':
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
elif self.path=='/led1/on':
GPIO.output(17, GPIO.HIGH)
status='LED1 is ON'
elif self.path=='/led1/off':
GPIO.output(17, GPIO.LOW)
status='LED1 is OFF'
elif self.path=='/led2/on':
GPIO.output(18, GPIO.HIGH)
status='LED2 is ON'
elif self.path=='/led2/off':
GPIO.output(18, GPIO.LOW)
status='LED2 is OFF'
elif self.path=='/led3/on':
GPIO.output(27, GPIO.HIGH)
status='LED3 is ON'
elif self.path=='/led3/off':
GPIO.output(27, GPIO.LOW)
status='LED3 is OFF'
elif self.path=='/led4/on':
GPIO.output(22, GPIO.HIGH)
status='LED4 is ON'
elif self.path=='/led4/off':
GPIO.output(22, GPIO.LOW)
status='LED4 is OFF'
elif self.path=='/led5/on':
GPIO.output(25, GPIO.HIGH)
status='LED5 is ON'
elif self.path=='/led5/off':
GPIO.output(25, GPIO.LOW)
status='LED5 is OFF'
elif self.path=='/led6/on':
GPIO.output(12, GPIO.HIGH)
status='LED6 is ON'
elif self.path=='/led6/off':
GPIO.output(12, GPIO.LOW)
status='LED6 is OFF'
elif self.path=='/led7/on':
GPIO.output(13, GPIO.HIGH)
status='LED7 is ON'
elif self.path=='/led7/off':
GPIO.output(13, GPIO.LOW)
status='LED7 is OFF'
elif self.path=='/led8/on':
GPIO.output(19, GPIO.HIGH)
status='LED8 is ON'
elif self.path=='/led8/off':
GPIO.output(19, GPIO.LOW)
status='LED8 is OFF'
elif self.path=='/buzzer/on':
GPIO.output(26, GPIO.HIGH)
status='Buzzer is ON'
elif self.path=='/buzzer/off':
GPIO.output(26, GPIO.LOW)
status='Buzzer is OFF'
elif self.path=='/led-all/on':
GPIO.output(17, GPIO.HIGH)
GPIO.output(18, GPIO.HIGH)
GPIO.output(27, GPIO.HIGH)
GPIO.output(22, GPIO.HIGH)
GPIO.output(25, GPIO.HIGH)
GPIO.output(12, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
status='All LEDs are ON'
elif self.path=='/led-all/off':
GPIO.output(17, GPIO.LOW)
GPIO.output(18, GPIO.LOW)
GPIO.output(27, GPIO.LOW)
GPIO.output(22, GPIO.LOW)
GPIO.output(25, GPIO.LOW)
GPIO.output(12, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
status='All LEDs are OFF'
self.wfile.write(html.format(temp[5:], status).encode("utf-8"))
if __name__ == '__main__':
http_server = HTTPServer((host_name, host_port), MyServer)
print("Server Starts - %s:%s" % (host_name, host_port))
try:
http_server.serve_forever()
except KeyboardInterrupt:
http_server.server_close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.