-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathboot_wifi.py
More file actions
73 lines (56 loc) · 1.81 KB
/
boot_wifi.py
File metadata and controls
73 lines (56 loc) · 1.81 KB
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
Boot script
Do initial stuff here, similar to the setup() function on Arduino
Connect to network, create an AccessPoint if connection failed otherwise
"""
# system packages
import gc
import machine
import network
import time
# set clock speed to 240MHz instead of default 160MHz
# import machine
# machine.freq(240000000)
station = network.WLAN(network.STA_IF)
if station.active() and station.isconnected():
station.disconnect()
time.sleep(1)
station.active(False)
time.sleep(1)
station.active(True)
station.connect('SSID', 'PASSWORD')
time.sleep(1)
connection_timeout = 10 * 1000 # WiFi connection timeout in milliseconds
start_ms = time.ticks_ms()
while (time.ticks_diff(time.ticks_ms(), start_ms) <= connection_timeout):
print('Waiting for WiFi connection...')
if station.isconnected():
print('Connected to WiFi')
print(station.ifconfig())
break
time.sleep(1)
result = station.isconnected()
# force an accesspoint creation
# result = False
if result is False:
# disconnect as/from station and disable WiFi for it
station.disconnect()
station.active(False)
time.sleep(1)
# create a true AccessPoint without any active Station mode
accesspoint = network.WLAN(network.AP_IF)
# activate accesspoint if not yet enabled
if not accesspoint.active():
accesspoint.active(True)
accesspoint_name = "MicroPython AP"
accesspoint.config(essid=accesspoint_name,
authmode=network.AUTH_OPEN,
password='',
channel=11)
print('Created Accesspoint: {}'.format(accesspoint_name))
print('Restart cause: {}'.format(machine.reset_cause()))
# run garbage collector at the end to clean up
gc.collect()
print('System booted successfully!')