Commit 435bcf06 authored by P.M.P.C Bandara's avatar P.M.P.C Bandara

Http control

parent 5f800a4b
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import os
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
host_name = '192.168.8.104' # IP Address of Raspberry Pi
host_port = 8000
def setupGPIO():
#GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False)
#GPIO.setup(18, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
def forward(sec):
setupGPIO()
GPIO.output(17, True)
GPIO.output(22, False)
GPIO.output(23, True)
GPIO.output(24, False)
time.sleep(sec)
GPIO.cleanup()
def reverse(sec):
setupGPIO()
GPIO.output(17, False)
GPIO.output(22, True)
GPIO.output(23, False)
GPIO.output(24, True)
time.sleep(sec)
GPIO.cleanup()
def right(sec):
setupGPIO()
GPIO.output(23, True)
GPIO.output(24, False)
time.sleep(sec)
GPIO.cleanup()
def left(sec):
setupGPIO()
GPIO.output(23, False)
GPIO.output(24, True)
time.sleep(sec)
GPIO.cleanup()
def getTemperature():
temp = os.popen("/opt/vc/bin/vcgencmd measure_temp").read()
return temp
class MyServer(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def _redirect(self, path):
self.send_response(303)
self.send_header('Content-type', 'text/html')
self.send_header('Location', path)
self.end_headers()
def do_GET(self):
html = '''
<html>
<body
style="width:960px; margin: 20px auto;">
<h1>Robot Navigator</h1>
<p>Current GPU temperature is {}</p>
<form action="/" method="POST">
<table>
<tr>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 1-1"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 1-2"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 1-3"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
</tr>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 2-1"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 2-2"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 2-3"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
</tr>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 3-1"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 3-2"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
<td style="vertical-align: center;;text-align: center;"> <input type="submit" name="submit" value="Plant 3-3"><img src="https://www.clipartkey.com/mpngs/m/33-330352_transparent-soil-png-little-plant-transparent-background.png" style="width:40%"/></td>
</tr>
</table>
</form>
<br/><br/>
<h4>View dashboard information</h4>
<iframe src="dashboard (2).xlsx"></iframe>
</body>
</html>
'''
temp = getTemperature()
self.do_HEAD()
self.wfile.write(html.format(temp[5:]).encode("utf-8"))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
post_data = post_data.split("=")[1]
if post_data == 'Plant 1-1':
forward(1)
elif post_data == 'Plant 1-2':
forward(2)
elif post_data == 'Plant 1-3':
forward(3)
elif post_data == 'Plant 2-1':
forward(1)
elif post_data == 'Plant 2-2':
forward(2)
elif post_data == 'Plant 2-3':
forward(3)
elif post_data == 'Plant 3-1':
forward(1)
elif post_data == 'Plant 3-2':
forward(2)
elif post_data == 'Plant 3-3':
forward(3)
else:
forward(2)
exec(open('distance.py').read())
print("Plant no is {}".format(post_data))
self._redirect('/') # Redirect back to the root url
# # # # # Main # # # # #
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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment