Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pip3 install httpserver
- import os
- import time
- import threading
- from http.server import BaseHTTPRequestHandler, HTTPServer
- HOST = '192.168.6.150'#Direccion de la Raspberry Pi
- PORT = 8000
- def velocidad(habilita):
- print ("HABILITA_A",habilita)
- time.sleep(1)
- hilo = threading.Thread(target=velocidad, args=(not habilita,))
- hilo.start()
- def setupGPIO():
- print ("Setup Gpio")
- #Control de los dos motores del robot
- def Motores(HABILITA_A=False,MOTOR_A1=False,MOTOR_A2=False,HABILITA_B=False,MOTOR_B1=False,MOTOR_B2=False):
- #Simula motores
- print (HABILITA_A,MOTOR_A1,MOTOR_A2,HABILITA_B,MOTOR_B1,MOTOR_B2)
- class ServidorWeb(BaseHTTPRequestHandler):
- VEL=True
- 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>ICARO</h1>
- <p> Control Robot </p>
- <form action="/" method="POST">
- CONTROL :
- <input type="submit" name="submit" value="Avanza">
- <input type="submit" name="submit" value="Retrocede">
- <input type="submit" name="submit" value="Derecha">
- <input type="submit" name="submit" value="Izquierda">
- <input type="submit" name="submit" value="Stop">
- </form>
- </body>
- </html>
- '''
- self.do_HEAD()
- self.wfile.write(html.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]
- setupGPIO()
- #VEL=not VEL #Un pwm al 50% para reducir la velocidad
- if post_data == 'Avanza':
- Motores(self.VEL,False,True,self.VEL,False,True)
- if post_data == 'Retrocede':
- Motores(self.VEL,True,False,self.VEL,True,False)
- if post_data == 'Derecha':
- Motores(self.VEL,False,True,False,False,False)
- if post_data == 'Izquierda':
- Motores(False,False,False,self.VEL,False,True)
- if post_data == 'Stop':
- Motores(False,False,False,False,False,False)
- self._redirect('/') # Redirect back to the root url
- # # # # # Main # # # # #
- if __name__ == '__main__':
- http_server = HTTPServer((HOST, PORT), ServidorWeb)
- print("Server Starts - %s:%s" % (HOST, PORT))
- hilo = threading.Thread(target=velocidad, args=(False,))
- hilo.start()
- try:
- http_server.serve_forever()
- except KeyboardInterrupt:
- http_server.server_close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement