Skip to content

Commit 1961a7a

Browse files
committed
updated wifi and s2m
1 parent b84fd8e commit 1961a7a

2 files changed

Lines changed: 205 additions & 14 deletions

File tree

Lines changed: 121 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from microbit import *
2121

22-
2322
# This is a script used to control a micro:bit from s2m
2423

2524
# This loop continuously polls the sensors and then prints a reply string.
@@ -111,12 +110,83 @@ def loop():
111110
image_key = cmd_list[1]
112111
except IndexError:
113112
continue
114-
if image_key in image_dict:
115-
display.show(image_dict.get(image_key),[0,0,255])
113+
try:
114+
r_value = int(cmd_list[2])
115+
except ValueError:
116+
continue
117+
except IndexError:
118+
continue
119+
120+
if r_value < 0:
121+
r_value = 0
122+
if r_value > 255:
123+
r_value = 255
124+
try:
125+
g_value = int(cmd_list[3])
126+
except ValueError:
127+
continue
128+
except IndexError:
129+
continue
130+
131+
if g_value < 0:
132+
g_value = 0
133+
if g_value > 255:
134+
g_value = 255
135+
try:
136+
b_value = int(cmd_list[4])
137+
except ValueError:
138+
continue
139+
except IndexError:
140+
continue
141+
142+
if b_value < 0:
143+
b_value = 0
144+
if b_value > 255:
145+
b_value = 255
146+
147+
#if image_key in image_dict:
148+
display.show(image_dict.get(image_key),[r_value,g_value,b_value])
116149

117150
# scroll text command
118151
elif cmd_id == 's':
119-
display.scroll(str(cmd_list[1]),[0,0,255])
152+
try:
153+
r_value = int(cmd_list[2])
154+
except ValueError:
155+
continue
156+
except IndexError:
157+
continue
158+
159+
if r_value < 0:
160+
r_value = 0
161+
if r_value > 255:
162+
r_value = 255
163+
164+
try:
165+
g_value = int(cmd_list[3])
166+
except ValueError:
167+
continue
168+
except IndexError:
169+
continue
170+
171+
if g_value < 0:
172+
g_value = 0
173+
if g_value > 255:
174+
g_value = 255
175+
176+
try:
177+
b_value = int(cmd_list[4])
178+
except ValueError:
179+
continue
180+
except IndexError:
181+
continue
182+
183+
if b_value < 0:
184+
b_value = 0
185+
if b_value > 255:
186+
b_value = 255
187+
188+
display.scroll(str(cmd_list[1]),[r_value,g_value,b_value])
189+
120190

121191
# write pixel command
122192
elif cmd_id == 'p':
@@ -147,17 +217,46 @@ def loop():
147217
if y > 4:
148218
y = 4
149219
try:
150-
value = int(cmd_list[3])
220+
r_value = int(cmd_list[3])
151221
except ValueError:
152222
continue
153223
except IndexError:
154224
continue
155225

156-
if value < 0:
157-
value = 0
158-
if value > 9:
159-
value = 9
160-
display.set_pixel(x, y, value)
226+
if r_value < 0:
227+
r_value = 0
228+
if r_value > 255:
229+
r_value = 255
230+
231+
try:
232+
g_value = int(cmd_list[4])
233+
except ValueError:
234+
continue
235+
except IndexError:
236+
continue
237+
238+
if g_value < 0:
239+
g_value = 0
240+
if g_value > 255:
241+
g_value = 255
242+
243+
try:
244+
b_value = int(cmd_list[5])
245+
except ValueError:
246+
continue
247+
except IndexError:
248+
continue
249+
250+
if b_value < 0:
251+
b_value = 0
252+
if b_value > 255:
253+
b_value = 255
254+
255+
#display.set_pixel(x, y, value)
256+
from pixel import Pixel
257+
View = Pixel()
258+
View.LoadXY(x, y, [r_value,g_value,b_value])
259+
View.Show()
161260

162261
# clear display command
163262
elif cmd_id == 'c':
@@ -217,9 +316,9 @@ def loop():
217316
sensor_string = ""
218317

219318
# accelerometer
220-
sensor_string += str(int(accelerometer.get_x())) + ','
221-
sensor_string += str(int(accelerometer.get_y())) + ','
222-
sensor_string += str(int(accelerometer.get_z())) + ','
319+
sensor_string += str(int(accelerometer.get_x()*20)) + ','
320+
sensor_string += str(int(accelerometer.get_y()*20)) + ','
321+
sensor_string += str(int(accelerometer.get_z()*20)) + ','
223322

224323
# buttons
225324
sensor_string += str(button_a.is_pressed()) + ','
@@ -254,9 +353,17 @@ def loop():
254353
sensor_string += '0' + ','
255354

256355
if not digital_outputs[2]:
257-
sensor_string += str(pin2.read_analog())
356+
sensor_string += str(pin2.read_analog())+','
258357
else:
259358
sensor_string += '0' + ','
359+
###temperature
360+
sensor_string += str(temperature()) +','
361+
#L R light
362+
import light
363+
R = light.Intensity(39)
364+
L = light.Intensity(36)
365+
sensor_string += str(int(L.read()))+','
366+
sensor_string += str(int(R.read()))
260367

261368
print(sensor_string)
262369
sleep(10)

12.network/wifi.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import network
2+
3+
from button import Button
4+
5+
wlan = network.WLAN(network.STA_IF)
6+
7+
if wlan.active() is False:
8+
wlan.active(True)
9+
10+
def disconnect():
11+
global wlan
12+
wlan.disconnect()
13+
14+
def close():
15+
disconnect()
16+
global wlan
17+
wlan.active(False)
18+
19+
def isconnected():
20+
global wlan
21+
return wlan.isconnected()
22+
23+
def smartconfig():
24+
close()
25+
print(network.smartconfig())
26+
27+
def try_connect():
28+
import wifi_cfg
29+
wlan.disconnect()
30+
wlan.connect(wifi_cfg.WIFI_SSID, wifi_cfg.WIFI_PSWD)
31+
32+
__button__, __last_button__ = False, False
33+
34+
def __check_button(button):
35+
global __button__, __last_button__
36+
if __last_button__ != button.is_pressed():
37+
__button__ = True
38+
39+
def start(led_delay=150, button=Button(35)):
40+
41+
if isconnected():
42+
print('Wifi connected.')
43+
return
44+
45+
global wlan, __button__, __last_button__
46+
__button__, __last_button__ = False, button.is_pressed()
47+
48+
if wlan.active() is False:
49+
wlan.active(True)
50+
51+
from machine import Timer
52+
from display import Display, Purple, Yellow, Green, Blue
53+
print('Press "A" to enter the smartconfig mode while the led is rolling')
54+
try:
55+
timer = Timer(-100)
56+
timer.init(period = 250, mode = Timer.PERIODIC, callback = lambda t : (__check_button(button)))
57+
mac = wlan.config('mac')
58+
view = ('%X%X' % (mac[4], mac[5]))
59+
try:
60+
import wifi_cfg
61+
except:
62+
default = "WIFI_SSID = 'webduino.io'\nWIFI_PSWD = 'webduino'\nHOST_NAME='bit%s'\n" % view
63+
print("create default wifi_cg.py : \n" + default + "or run smartconfig restart config ")
64+
with open("wifi_cfg.py", "w") as f:
65+
f.write(default)
66+
import wifi_cfg
67+
if hasattr(wifi_cfg, 'HOST_NAME'):
68+
wlan.config(dhcp_hostname=wifi_cfg.HOST_NAME)
69+
70+
Display().scroll(view, [Purple, Yellow, Green, Blue], led_delay)
71+
72+
if __button__:
73+
print("Started wifi in smartconfig mode")
74+
smartconfig()
75+
else:
76+
print("Started wifi in normal mode")
77+
try_connect()
78+
79+
except Exception as e:
80+
print(e)
81+
finally:
82+
Display().clear()
83+
timer.deinit()
84+

0 commit comments

Comments
 (0)