-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
69 lines (65 loc) · 2.06 KB
/
server.py
File metadata and controls
69 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import RPi.GPIO as GPIO
from http.server import BaseHTTPRequestHandler, HTTPServer
#Define pins for controlling motor driver L298N
IN1=2
IN2=3
IN3=4
IN4=17
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)
#Setting all required pins to low so that robot does
#not run on bootup of raspberry pi
GPIO.output(IN1,False)
GPIO.output(IN3,False)
GPIO.output(IN2,False)
GPIO.output(IN4,False)
Request = None
#HTTP handles requests sent by app
class RequestHandler_httpd(BaseHTTPRequestHandler):
def do_GET(self):
global Request
messagetosend = bytes('Hello World',"utf")
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Content-Length', len(messagetosend))
self.end_headers()
self.wfile.write(messagetosend)
Request = self.requestline
Request = Request[5 : int(len(Request)-9)]
print(Request)
if Request == 'w': #FWD: IN1 & IN3 High, IN2 & IN4 Low
GPIO.output(IN1,True)
GPIO.output(IN3,True)
GPIO.output(IN2,False)
GPIO.output(IN4,False)
if Request == 'a': #LFT: IN2 & IN3 High, IN1 & IN4 Low
GPIO.output(IN2,True)
GPIO.output(IN3,True)
GPIO.output(IN1,False)
GPIO.output(IN4,False)
if Request == 's': #REV: IN1 & IN3 Low, IN2 & IN4 High
GPIO.output(IN2,True)
GPIO.output(IN4,True)
GPIO.output(IN1,False)
GPIO.output(IN3,False)
if Request == 'd': #RGT: IN1 & IN4 High, IN2 & IN3 Low
GPIO.output(IN1,True)
GPIO.output(IN4,True)
GPIO.output(IN2,False)
GPIO.output(IN3,False)
if Request == 'b': #STP: IN1 & IN3 low, IN2 & IN4 Low
GPIO.output(IN1,False)
GPIO.output(IN3,False)
GPIO.output(IN2,False)
GPIO.output(IN4,False)
return
#server runs on port 7000
#IP address of raspberry pi: 192.168.0.110
server_address_httpd = ('192.168.0.110',7000)
httpd = HTTPServer(server_address_httpd, RequestHandler_httpd)
print('Starting server')
httpd.serve_forever()
GPIO.cleanup()