-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
162 lines (123 loc) · 4.32 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
from time import sleep
from machine import Pin
from utils.octopus_lib import w, getFree, getUid, randint
from microWebSrv import MicroWebSrv
from components.led import Led
from components.rgb import Rgb
import json
neo = 16 # 30 # number of Leds
np1 = Rgb(15,neo) # NeoPixel(Pin(15), 1) # SWLED
np2 = Rgb(16,neo) # NeoPixel(Pin(15), 1) # PWM/SERVO2
led = Led(2)
# pwm
print("-"*50)
print("ftp or WebServer - ESP32 control led/pwm/rgb")
print("-"*50)
def _httpHandlerINFOPost(httpClient, httpResponse):
infoDict = {}
infoDict["deviceUID"] = getUid()
infoDict["freeRAM"] = getFree()
httpResponse.WriteResponseJSONOk(infoDict)
cnt = 0
def _httpHandlerDATAPost(httpClient, httpResponse):
global cnt
val = 100
num = 20
cnt = cnt + 1
dataDict = {}
dataDict["cnt"] = cnt
dataDict["num"] = 30
dataDict["10"] = 200
dataDict["20"] = 100
dataDict["30"] = 0
for i in range(num):
val = val + randint(-10,10)
dataDict[i*10 + 40] = val
dataDict["val"] = val
httpResponse.WriteResponseJSONOk(dataDict)
def _httpHandlerLEDPost(httpClient, httpResponse):
content = httpClient.ReadRequestContent() # Read JSON color data
jsc = json.loads(content)
print("led: ", jsc)
led.value(jsc)
httpResponse.WriteResponseJSONOk()
sele = 1
def _httpHandlerSELEPost(httpClient, httpResponse):
global sele
content = httpClient.ReadRequestContent() # Read JSON color data
sel = json.loads(content)
print("select: ", sel)
sele = sel
#led.value(jsc)
httpResponse.WriteResponseJSONOk()
def _httpHandlerPWMPost(httpClient, httpResponse):
content = httpClient.ReadRequestContent()
jsc = json.loads(content)
print("pwm: ",jsc)
# todo
httpResponse.WriteResponseJSONOk()
def _httpHandlerCOMMPost(httpClient, httpResponse):
data = httpClient.ReadRequestContentAsJSON()
responseCode = 500
content = None
print(data)
if len(data) < 1:
responseCode = 400
content = "Missing data"
httpResponse.WriteResponse(code=400, headers = None, contentType = "text/plain", contentCharset = "UTF-8", content = content)
return
comm = data[0]
commval= data[1] if len(data) > 1 else ""
print(data[0], data[1])
responseCode = 201
httpResponse.WriteResponse( code=responseCode, headers = None, contentType = "text/plain", contentCharset = "UTF-8", content = content)
def _httpHandlerEXPANDPost(httpClient, httpResponse):
"""
if expander is None:
print("I2C expander is not initialized!")
httpResponse.WriteResponseOk(None)
return
"""
from components.i2c_expander import neg
data = httpClient.ReadRequestContent()
print("i2c_expander.data: " + str(data) + str(bin(int(data))))
try:
expander.write_8bit(neg(int(data)))
except Exception as e:
print("Exception: {0}".format(e))
raise
finally:
httpResponse.WriteResponseOk(None)
def _httpHandlerRGBPost(httpClient, httpResponse):
content = httpClient.ReadRequestContent()
colors = json.loads(content)
blue, green, red = [colors[k] for k in sorted(colors.keys())]
if sele == 1:
for i in range(neo):
np1.color((red, green, blue),i)
if sele == 2:
for i in range(neo):
np2.color((red, green, blue),i)
httpResponse.WriteResponseJSONOk()
btnum = 0
button = Pin(0, Pin.IN)
print("press button / CTRL+C or continue")
sleep(1)
for i in range(7):
print("-",end="")
if(not button.value()): btnum += 1
led.value(1)
sleep(0.1)
led.value(0)
sleep(0.2)
w()
print("-"*50)
if (btnum==0):
print("button0 -> start WebServer www/control/")
routeHandlers = [ ( "/control/expander", "POST", _httpHandlerEXPANDPost ),( "/command", "POST", _httpHandlerCOMMPost ),( "/info.json", "GET", _httpHandlerINFOPost ),( "/data.json", "GET", _httpHandlerDATAPost ),( "/control/led", "POST", _httpHandlerLEDPost ),( "/control/pwm", "POST", _httpHandlerPWMPost ), ( "/control/rgb", "POST", _httpHandlerRGBPost ),( "/control/select", "POST", _httpHandlerSELEPost ) ]
srv = MicroWebSrv(routeHandlers=routeHandlers, webPath='/www/control/')
srv.Start(threaded=False)
else:
print("button1 -> start FTP")
import ftp
print("="*50)