-
Notifications
You must be signed in to change notification settings - Fork 0
/
robonova_async.py
100 lines (81 loc) · 2.55 KB
/
robonova_async.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
# ------------------------------------------------------------
# Developping with MicroPython in an async way
#
# ------------------------------------------------------------
# === application: neopixel klok ===
# ------------------------------------------------------------
print("== module robonova_async.py")
import wifi
import webserver
import http_server
import logging
import asyncio
import gc
import config
import utime as time
import ujson as json
import sys
import robonova_control
logging.setGlobal(logging.DEBUG)
htec = logging.getlogger("htec")
sche = logging.getlogger("sche")
http = logging.getlogger("http")
webs = logging.getlogger("webs")
log = logging.getlogger("nova")
sche.setLevel(logging.INFO)
http.setLevel(logging.INFO)
webs.setLevel(logging.DEBUG)
htec.setLevel(logging.INFO)
log.setLevel(logging.DEBUG)
import sys
log.info("Current platform: id:%s",sys.platform )
# Two tasks
def heap():
yield
while True:
cpuload = (10000 - asyncio.sched.idlecount) / 100
asyncio.sched.idlecount = 0
log.info ("Memory free: %d cpu load: %d %% " , gc.mem_free() , cpuload )
yield
def wlanConnect():
tick = 10
yield
wifi.startap('robonova','12345678')
yield
connected = wifi.wlan.isconnected()
if not connected:
wifi.connect2ap()
timeout = 10
while timeout > 0 and not connected:
log.info(" Try %d",timeout)
timeout -= 1
connected = wifi.wlan.isconnected()
if connected:
log.info("Connected to station.")
#if sys.platform != "linux":
#ntptime.settime()
# pass
#now = time.localtime()
#klok.sync(now)
#klok.checkSummerWinter(now)
yield
if not connected:
log.warn("Unable to connect to ssid:%s. Starting AP led_klok ")
else:
pass
http = http_server.HttpServer("web")
port = 80
if sys.platform == "linux":
port = 8080
server = webserver.WebServer(http)
log.warn("Config tasks in the scheduler")
DATA_PORT = 1024
sched = asyncio.sched
sched.task(heap(), name = "heap", period = 10000)
sched.task(wlanConnect(), name = "wlan", period = 1000, time2run = 500)
sched.task(http.listen(port),name = "webServer",prio= 5)
sched.task(robonova_control.servoTask(),name = "servos", period=1000,time2run = 112,prio=20)
sched.enablePolling(100)
sched.enableGC(100)
log.info("All tasks loaded.. Start scheduler")
sched.mainloop()