-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (167 loc) · 4.2 KB
/
main.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import socket
import network
import machine
from secrets import secrets
from machine import Pin,PWM,UART,WDT
import time
ssid = secrets['ssid']
password = secrets['pw']
led = machine.Pin("LED",Pin.OUT)
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
#wdt = WDT(timeout=8000)
while ap.active() == False:
pass
print('Connection successful')
print(ap.ifconfig())
def generateHTML():
html = """<!DOCTYPE html>
<html>
<head>
<title>Hackspace Bot</title>
<style>
button {
width:175px;
height:100px;
background-color:#0000ff;
color:#ffffff;
font-size:25pt
}
</style>
</head>
<body>
<div>
<form action="" method="post">
<table class="controller">
<tr>
<td></td>
<td>
<button type="submit" formaction="forwards">↑</button>
</td>
<td></td>
</tr>
<tr>
<td><button type="submit" formaction="left">←</button></td>
<td>
<button type="submit" formaction="stop">Stop</button>
</td>
<td>
<button type="submit" formaction="right">→</button>
</td>
</tr>
<tr>
<td></td>
<td>
<button type="submit" formaction="backwards" >↓</button>
</td>
<td></td>
</tr>
</table>
</div>
"""
html = html + f"""
<div>
<table>
<tr>
<td>SSID</td>
<td>PASSWORD</td>
<td>IP</td>
</tr>
<tr>
<td>{ssid}</td>
<td>{password}</td>
<td>{ap.ifconfig()}</td>
</tr>
</table>
</div>
</form>
</body>
</html>
"""
return html
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
In1=Pin(6,Pin.OUT) #IN1
In2=Pin(7,Pin.OUT) #IN2
#OUT3 and OUT4
In3=Pin(4,Pin.OUT) #IN3
In4=Pin(3,Pin.OUT) #IN4
EN_A=PWM(Pin(8))
EN_B=PWM(Pin(2))
# Defining frequency for enable pins
EN_A.freq(1500)
EN_B.freq(1500)
# Setting maximum duty cycle for maximum speed (0 to 65025)
EN_A.duty_u16(65025)
EN_B.duty_u16(65025)
def restart():
stop()
# Left
def turn_left():
print("Left I go")
In1.high()
In2.low()
In3.low()
In4.high()
def turn_right():
print("Right I go")
In1.low()
In2.high()
In3.high()
In4.low()
# Backward
def move_backward():
print("Back up Back up")
In1.low()
In2.high()
In3.low()
In4.high()
# Forward
def move_forward():
print("Onwards!")
In1.high()
In2.low()
In3.high()
In4.low()
# Stop
def stop():
print("All Stop")
In1.low()
In2.low()
In3.low()
In4.low()
print('listening on', addr)
# Listen for connections
while True:
try:
led.toggle()
#wdt.feed()
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
data = str(request)
direction = (data[1:20].split(' ')[1].replace('/',''))
if(direction =='forwards'):
move_forward()
elif(direction == 'left'):
turn_left()
elif(direction == 'right'):
turn_right()
elif(direction == 'backwards'):
move_backward()
elif(direction == 'stop'):
stop()
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
time.sleep(0.2)
cl.send(generateHTML())
cl.close()
except OSError as e:
cl.close()
s.close()
print(f'{e}')
blink_error(e)
print('connection closed')
restart()