Skip to content

Commit 385b017

Browse files
fix wifi stuff
1 parent f770250 commit 385b017

File tree

1 file changed

+32
-35
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.wifi/assets

1 file changed

+32
-35
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import mpos.ui
99
import mpos.config
1010

11+
have_network = True
12+
try:
13+
import network
14+
except Exception as e:
15+
have_network = False
16+
1117
# Global variables because they're used by multiple Activities:
1218
access_points={}
1319
last_tried_ssid = ""
@@ -20,7 +26,6 @@ class WiFi(Activity):
2026
scan_button_scanning_text = "Scanning..."
2127

2228
ssids=[]
23-
havenetwork = True
2429
keep_running = True
2530
busy_scanning = False
2631
busy_connecting = False
@@ -53,15 +58,6 @@ def onCreate(self):
5358
self.scan_button.add_event_cb(self.scan_cb,lv.EVENT.CLICKED,None)
5459
self.setContentView(main_screen)
5560

56-
def onStart(self, screen):
57-
self.havenetwork = True
58-
try:
59-
import network
60-
wlan=network.WLAN(network.STA_IF)
61-
wlan.active(True)
62-
except Exception as e:
63-
self.havenetwork = False
64-
6561
def onResume(self, screen):
6662
global access_points
6763
access_points = mpos.config.SharedPreferences("com.micropythonos.system.wifiservice").get_dict("access_points")
@@ -82,13 +78,15 @@ def show_error(self, message):
8278
timer.set_repeat_count(1)
8379

8480
def scan_networks_thread(self):
81+
global have_network
8582
print("scan_networks: Scanning for Wi-Fi networks")
86-
global ssids, busy_scanning, scan_button_label, scan_button
87-
if self.havenetwork and not wlan.isconnected(): # restart WiFi hardware in case it's in a bad state
88-
wlan.active(False)
89-
wlan.active(True)
83+
if have_network:
84+
wlan=network.WLAN(network.STA_IF)
85+
if not wlan.isconnected(): # restart WiFi hardware in case it's in a bad state
86+
wlan.active(False)
87+
wlan.active(True)
9088
try:
91-
if self.havenetwork:
89+
if have_network:
9290
networks = wlan.scan()
9391
self.ssids = list(set(n[0].decode() for n in networks))
9492
else:
@@ -120,6 +118,7 @@ def start_scan_networks(self):
120118
_thread.start_new_thread(self.scan_networks_thread, ())
121119

122120
def refresh_list(self):
121+
global have_network
123122
print("refresh_list: Clearing current list")
124123
self.aplist.clean() # this causes an issue with lost taps if an ssid is clicked that has been removed
125124
print("refresh_list: Populating list with scanned networks")
@@ -130,20 +129,20 @@ def refresh_list(self):
130129
print(f"refresh_list: Adding SSID: {ssid}")
131130
button=self.aplist.add_button(None,ssid)
132131
button.add_event_cb(lambda e, s=ssid: self.select_ssid_cb(s),lv.EVENT.CLICKED,None)
133-
if self.havenetwork and wlan.isconnected() and wlan.config('essid')==ssid:
134-
status="connected"
135-
elif last_tried_ssid==ssid: # implies not connected because not wlan.isconnected()
136-
status=last_tried_result
137-
elif ssid in access_points:
138-
status="saved"
139-
else:
140-
status=""
141-
if status:
142-
print(f"refresh_list: Setting status '{status}' for SSID: {ssid}")
143-
label=lv.label(button)
144-
label.set_text(status)
145-
label.align(lv.ALIGN.RIGHT_MID,-10,0)
146-
132+
status = ""
133+
if have_network:
134+
wlan=network.WLAN(network.STA_IF)
135+
if wlan.isconnected() and wlan.config('essid')==ssid:
136+
status="connected"
137+
if status != "connected":
138+
if last_tried_ssid == ssid: # implies not connected because not wlan.isconnected()
139+
status=last_tried_result
140+
elif ssid in access_points:
141+
status="saved"
142+
label=lv.label(button)
143+
label.set_text(status)
144+
label.align(lv.ALIGN.RIGHT_MID,0,0)
145+
147146
def scan_cb(self, event):
148147
print("scan_cb: Scan button clicked, refreshing list")
149148
self.start_scan_networks()
@@ -173,11 +172,12 @@ def start_attempt_connecting(self, ssid, password):
173172
_thread.start_new_thread(self.attempt_connecting_thread, (ssid,password))
174173

175174
def attempt_connecting_thread(self, ssid, password):
176-
global last_tried_ssid, last_tried_result
175+
global last_tried_ssid, last_tried_result, have_network
177176
print(f"attempt_connecting_thread: Attempting to connect to SSID '{ssid}' with password '{password}'")
178177
result="connected"
179178
try:
180-
if self.havenetwork:
179+
if have_network:
180+
wlan=network.WLAN(network.STA_IF)
181181
wlan.disconnect()
182182
wlan.connect(ssid,password)
183183
for i in range(10):
@@ -189,7 +189,7 @@ def attempt_connecting_thread(self, ssid, password):
189189
if not wlan.isconnected():
190190
result="timeout"
191191
else:
192-
print("Warning: not trying to connect because not havenetwork, just waiting a bit...")
192+
print("Warning: not trying to connect because not have_network, just waiting a bit...")
193193
time.sleep(5)
194194
except Exception as e:
195195
print(f"attempt_connecting: Connection error: {e}")
@@ -209,7 +209,6 @@ def attempt_connecting_thread(self, ssid, password):
209209

210210

211211
class PasswordPage(Activity):
212-
213212
# Would be good to add some validation here so the password is not too short etc...
214213

215214
selected_ssid = None
@@ -220,7 +219,6 @@ class PasswordPage(Activity):
220219
connect_button=None
221220
cancel_button=None
222221

223-
224222
def onCreate(self):
225223
self.selected_ssid = self.getIntent().extras.get("selected_ssid")
226224
print("PasswordPage: Creating new password page")
@@ -268,7 +266,6 @@ def onCreate(self):
268266
self.setContentView(password_page)
269267

270268
def hide_keyboard(self):
271-
#global keyboard,connect_button,cancel_button
272269
print("keyboard_cb: READY or CANCEL or RETURN clicked, hiding keyboard")
273270
self.keyboard.set_height(0)
274271
self.keyboard.remove_flag(lv.obj.FLAG.CLICKABLE)

0 commit comments

Comments
 (0)