-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlte2.py
150 lines (131 loc) · 3.7 KB
/
lte2.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
import socket
import time
import binascii
from network import LTE
import os
import machine
print("sys", os.uname().sysname)
print("unique_id", binascii.hexlify(machine.unique_id()))
print("release", os.uname().release)
APN="spe.inetd.vodafone.nbiot"
APN=""
BAND=20
BAND=""
disconnect_detach = True
def sleep(s):
print("sleep(", s, ")", end="")
while s > 0:
print(".", end="")
time.sleep(1)
s -= 1
print("")
def disconnect(lte):
if ( lte.isconnected() ):
print("disconnect ", end="")
t = time.time()
lte.disconnect()
print(" disconnected after", time.time() - t, "seconds")
def connect(lte):
disconnect(lte)
print("connect ", end="")
t = time.time()
lte.connect()
while not lte.isconnected():
print(".", end="")
time.sleep(0.25)
print(" connected after", time.time() - t, "seconds")
try:
# older sqns fw does not support pppsuspend
ifconfig_suspend(lte)
except:
pass
def detach(lte):
disconnect(lte)
if ( lte.isattached() ):
print("detach")
# try:
lte.detach()
#except Exception as ex:
# print("detach failed:", ex)
def ifconfig(lte):
for attempt in range(0,3):
try:
print(lte.send_at_cmd('AT+CGCONTRDP=1'))
# '\r\n+CGCONTRDP: 1,5,"spe.inetd.vodafone.nbiot.mnc028.mcc901.gprs","10.175.213.177.255.255.255.255","","10.105.16.254","10.105.144.254","","",,,1430\r\n\r\nOK\r\n'
# lte.send_at_cmd('AT+CGDCONT?')
# '\r\n+CGDCONT: 1,"IP","spe.inetd.vodafone.nbiot",,,,0,0,0,0,0,0,1,,0\r\n\r\nOK\r\n'
print(lte.send_at_cmd('AT!="ifconfig"'))
return
except Exception as ex:
print("ifconfig Exception:", ex)
raise Exception("Could not ifconfig")
def ifconfig_suspend(lte):
lte.pppsuspend()
ifconfig(lte)
lte.pppresume()
def attach(lte):
detach(lte)
print("attach ", end="")
t = time.time()
if BAND:
lte.attach(band=BAND, apn=APN) #, type=LTE.IP)
else:
lte.attach()
while not lte.isattached():
print(".", end="")
time.sleep(0.25)
print(" attached after", time.time() - t, "seconds")
def http_get(url="http://detectportal.firefox.com/"):
print("http_get(", url, ")")
_, _, host, path = url.split('/', 3)
#print("host", host)
#print("path", path)
addr = socket.getaddrinfo(host, 80)[0][-1]
#print("addr", addr)
s = socket.socket()
#print("connect")
s.connect(addr)
#print("send")
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
s.settimeout(15)
while True:
data = s.recv(100)
if data:
# print("http_get: data")
# print(str(data, 'utf8'), end='')
pass
else:
break
# print("close")
s.close()
# print("done")
def dns():
print("accessing internet ", end="")
host = "detectportal.firefox.com"
t = time.time()
ip = socket.getaddrinfo(host, 80)[0][4][0]
print(" completed after", time.time() - t, "seconds")
print(host, ip)
def lte_connect():
print("init")
t = time.time()
try:
# try to detach, in case the script was Ctrl-C-ed and is rerun
detach(lte)
print("detached")
except:
pass
lte = LTE() # debug=True) # carrier=None, cid=1)
# print("imei", lte.imei())
attach(lte)
# lte.init(debug=True)
connect(lte)
# host = 'google.com'
# print(host, socket.getaddrinfo(host,80))
return True
############################# main ##########################
if __name__ == "__main__":
lte_connect()
if False:
disconnect(lte)
detach(lte)