-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi.py
61 lines (49 loc) · 1.5 KB
/
wifi.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
import network
import requests
import utime
import logger as log
CONNECT_TIMEOUT = 5000 # ms
wifi = None
ipaddr = ""
def connect(ssid, password):
global CONNECT_TIMEOUT
global wifi
global ipaddr
wifi = network.WLAN(network.STA_IF)
if wifi.isconnected() == True:
wifi.disconnect()
wifi.active(True)
wifi.connect(ssid, password)
connect_time = utime.ticks_ms()
while wifi.isconnected() == False:
if utime.ticks_ms() - connect_time >= CONNECT_TIMEOUT:
log.error("Connect time out!")
return False, ""
else:
pass
ipaddr = wifi.ifconfig()[0]
log.info("Connection successful, IP Address: {}".format(ipaddr))
return True, ipaddr
def disconnect(self):
global wifi
wifi.disconnect()
# def _ping_baidu():
# rq = requests.get("http://www.baidu.com")
# if rq.status_code != 200 or "Example Domain" not in rq.text:
# log.info("Unconnected to the Internet!")
# rq.close()
# return False
# else:
# log.info("Connected to the Internet!")
# rq.close()
# return True
def ping():
rq = requests.get("http://www.example.com")
if rq.status_code != 200 or "Example Domain" not in rq.text:
log.info("Unconnected to the Internet!")
rq.close()
return False
else:
log.info("Connected to the Internet!")
rq.close()
return True