-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (80 loc) · 2.5 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
import network
from machine import Pin
from logger import Logger
from microwebserver import MicroWebServer, WebHelper
import uasyncio
from motorcontroller import MotorController
# onboard led
led = Pin(2, Pin.OUT)
# left
in1 = Pin(15, Pin.OUT) # 1=0,2=1=forward, 1=1,2=0=backward
in2 = Pin(13, Pin.OUT) # 1=1,2=0=forward, 1=0,2=1=backward
# right
in3 = Pin(5, Pin.OUT)
in4 = Pin(4, Pin.OUT)
# Configure Logger
logger = Logger()
logger.info("main", "start")
# Connect to Wi-Fi
ssid = 'SSID'
password = 'PASSWORD'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while not station.isconnected():
pass
logger.info("main", "wlan Connection successful")
logger.info("main", "wlan configuration: {}".format(station.ifconfig()))
motor = MotorController(in1, in2, in3, in4)
def show_overview():
response = WebHelper.get_content_type_html()
content = WebHelper.get_web_content("website.html")
response += content.replace("{{SRV-ADDR}}", station.ifconfig()[0])
return response
def show_left():
motor.left()
response = WebHelper.get_content_type_html()
response += "left"
return response
def show_right():
motor.right()
response = WebHelper.get_content_type_html()
response += "right"
return response
def show_left_stop():
motor.left_stop()
response = WebHelper.get_content_type_html()
response += "left_stop"
return response
def show_right_stop():
motor.right_stop()
response = WebHelper.get_content_type_html()
response += "right_stop"
return response
def show_backwards():
motor.backward()
response = WebHelper.get_content_type_html()
response += "back"
return response
def show_forwards():
motor.forward()
response = WebHelper.get_content_type_html()
response += "forward"
return response
def show_stop():
motor.stop()
response = WebHelper.get_content_type_html()
response += "stop"
return response
webserver = MicroWebServer(station.ifconfig()[0], 80)
webserver.add_get_handler("/", show_overview)
webserver.add_get_handler("/forward", show_forwards)
webserver.add_get_handler("/backward", show_backwards)
webserver.add_get_handler("/left", show_left)
webserver.add_get_handler("/left_stop", show_left_stop)
webserver.add_get_handler("/right", show_right)
webserver.add_get_handler("/right_stop", show_right_stop)
webserver.add_get_handler("/stop", show_stop)
event_loop = uasyncio.get_event_loop()
uasyncio.run(webserver.start_server())
event_loop.run_forever()