-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnbiot.py
More file actions
636 lines (482 loc) · 18.7 KB
/
nbiot.py
File metadata and controls
636 lines (482 loc) · 18.7 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import time
import ubinascii
import sys
import json
import machine
class NbiotCommandError(Exception):
def __init__(self, message, resp=None):
# Call the base class constructor with the parameters it needs
super().__init__(message)
# Add the response data
self.resp = resp
class NbiotCommandTimeout(Exception):
def __init__(self, message, resp=None):
# Call the base class constructor with the parameters it needs
super().__init__(message)
# Add the response data
self.resp = resp
class NBIoT:
# NOTE: Pins re really GPIO numbers, not the physical pin numbers
nbiotEnablePin = 14 # The pico pin connected to enable on the NB-IoT chip
nbiotEnable = None # This is the pin object that holds the PinState
nbiotDtrPin = 17
nbiotDtr = None
uartPort = 0 # The pico UART used to communicate with the NB-IoT chip
baudRate = 115200
uart = None # The uart object
keepalive_interval = 600
clean_session = 1
will_flag = 0
will_options = None
qos = 1
retained = 0
dup = 0
user = "mimos"
password = "efd73d953f20838073a7a5b17aaad315"
# apn = "telstra.internet"
enabled = False
client_id = None
cid = None
mqtt_id = None
def __init__(self):
# Timeout is in (ms)
self.uart = machine.UART(self.uartPort, self.baudRate, bits=8, parity=None, stop=1, timeout=1000)
self.nbiotEnable = machine.Pin(self.nbiotEnablePin, machine.Pin.OUT)
self.nbiotEnable.low()
self.nbiotDtr = machine.Pin(self.nbiotDtrPin, machine.Pin.OUT)
self.nbiotDtr.value(1)
self.client_id = ubinascii.hexlify(machine.unique_id())
def enable(self):
print('--- NB-IoT Enable')
if self.enabled:
print("ERROR: enable() reentered - calling disable()")
self.disable()
time.sleep(3)
self.wakeup() # enable the chip
try:
# # Get the signal strength
# print("Get Signal strength...")
# resp = self.send_cmd("AT+CSQ", "Get Signal Strength")
# data = self.parse_for("+CSQ:", resp)
# if data:
# ss, _other = data.split(",")
# print(f"Signal Strength: {ss}")
# else:
# print ("ERROR: no data")
# return False
# Wait for PDP to be activated
print("Wait for PDP activation...")
retry = 30
while retry:
# AT+CGACT PDP Context Activate or Deactivate
resp = self.send_cmd("AT+CGACT?", comment="Get the PDP activated state")
if resp:
result = self.parse_for("+CGACT:", resp)
if result:
self.cid, state = result.split(",")
if state == "1":
print(f'cid: {self.cid}')
break
retry -= 1
time.sleep(3)
if not retry:
print("Unable to activate to the PDP")
return False
except (NbiotCommandError, NbiotCommandTimeout):
self.cid = None
return False
self.enabled = True
return True
def disable(self):
print('--- NB-IoT Disable')
self.enabled = False
self.goto_sleep()
def factory_reset(self):
print('--- NB-IoT Factory Reset')
self.wakeup()
try:
self.send_cmd("AT&F", "Reset NB-IoT NVRAM")
time.sleep(1)
self.wait_for_at()
self.send_cmd('ATE1', "Enable Echo")
self.send_cmd('AT*MCGDEFCONT="IP"', 'Setting PDP Type: IP')
self.send_cmd('AT+CNMI=0,0,0,0,0', 'Disable SMS messages')
except (NbiotCommandTimeout, NbiotCommandError):
return False
try:
# save the reset config
self.send_cmd("AT&W", "Save the reset config")
except NbiotCommandTimeout:
pass
except NbiotCommandError:
return False
return True
def send_mqtt(self, server, topic, json_message, port=1883, version=4):
print('--- NB-IoT MQTT Send')
mqtt_delay = 1
retry_cnt = 10
print(f"msg: {json_message}")
print(f"topic: {topic}")
while retry_cnt:
mqtt_id = None
retry_cnt -= 1
try:
# Create a new connection
resp = self.send_cmd(f'AT+CMQNEW="{server}",{port},12000,1024', comment="Create an MQTT client")
mqtt_id = self.parse_for("+CMQNEW:", resp)
if not mqtt_id:
self.disconnect_mqtt(mqtt_id)
continue
print (f"mqtt_id: {mqtt_id}")
time.sleep(mqtt_delay)
# Open a connection to the server
# This may not connect - i.e.: timeout
command = f'AT+CMQCON={mqtt_id},{version},"{self.client_id}",{self.keepalive_interval},{self.clean_session},{self.will_flag},"{self.user}","{self.password}"'
self.send_cmd(command, comment="Connect to the MQTT server")
# Timeout and Error will raise and exceptions
# if we get here we got an OK
time.sleep(mqtt_delay)
payload = json.dumps(json_message).encode().hex()
length = len(payload)
# send the message to the mqtt server
command = f'AT+CMQPUB={mqtt_id},"{topic}",{self.qos},{self.retained},{self.dup},{length},"{payload}"'
self.send_cmd(command, comment="Send MQTT message")
# Timout and Error will both generate exceptions
# if we get here we have received OK
# TODO - subscribe and wait for the message to be sent back
self.disconnect_mqtt(mqtt_id)
return True
except (NbiotCommandError, NbiotCommandTimeout):
self.disconnect_mqtt(mqtt_id)
continue
# Exit due to many retries
return False
def send_http(self, server, path, json_message, port=80):
print('--- NB-IoT HTTP Send')
try:
resp = self.send_cmd(f'AT+CHTTPCREATE="http://{server}:{port}/"', "New HTTP instance")
http_id = self.parse_for('+CHTTPCREAT:', resp)
if not http_id:
print ("No http_id")
return False
print(f'http_id: {http_id}')
self.send_cmd(f'AT+CHTTPCON={http_id}', "Connect to HTTP server")
time.sleep(3)
header = None
content_type = "application/json"
payload = json.dumps(json_message).encode().hex()
self.send_cmd(f'AT+CHTTPSEND={http_id},2,"{path}",{header},"{content_type}",{payload}', "Send POST request")
except (NbiotCommandError, NbiotCommandTimeout):
return False
return True
def check_settings(self):
print('--- NB-IoT Check Settings')
try:
self.send_cmd('AT+CGMR', "Get Firmware Version")
self.send_cmd('AT+CPIN?', "Check if pin required")
self.send_cmd('AT+CSQ', "Received Signal Strength")
self.send_cmd('AT+CEREG?', "Network Status")
self.send_cmd('AT+CGACT?', "Get the cid")
self.send_cmd('AT+COPS?', "Operator Selector")
self.send_cmd('AT+CGCONTRDP=', "Get the IP addresses assigned")
self.send_cmd('AT+CREVHEX?', "Data Mode 0:Raw 1:Binary")
self.send_cmd('AT+CFUN?', "Radio Functionality 0:Off 1:Full")
self.send_cmd('AT+CGATT?', "Network attachment")
self.send_cmd('AT+CGDCONT?', "PDP context")
self.send_cmd('AT+CEREG?', "Network Status")
self.send_cmd('AT+CMQNEW?', "Get the MQTT clients")
self.send_cmd('AT+CMQCON?', "Get the MQTT connections")
except (NbiotCommandError, NbiotCommandTimeout):
return False
return True
def update_firmware(self):
print('--- NB-IoT Firmware Update')
try:
resp = self.send_cmd("AT+CGMR")
print(f"Current firmware: {resp[1]}")
self.send_cmd("AT+CFOTA=1", "Start Firmware update")
except (NbiotCommandError, NbiotCommandTimeout):
return False
while True:
stats, data = self.read_uart()
print(data)
result = self.parse_for("+CFOTA:", data)
if result == "No update package":
print("Error: No update package")
return False
if result == "Update successfully":
print("Success")
break
try:
self.send_cmd("AT+CFOTA=4", "Save Update")
print(f"Current firmware: {resp[1]}")
self.send_cmd("AT+CFOTA=1", "Start Firmware update")
except (NbiotCommandError, NbiotCommandTimeout):
return False
def get_http(self, url, path):
print('--- NB-IoT Get HTTP')
content = None
try:
resp = self.send_cmd(f'AT+CHTTPCREATE="{url}"', f"HTTP create {url}")
http_id = self.parse_for("+CHTTPCREATE:", resp)
if http_id is None:
return False
self.send_cmd(f'AT+CHTTPCON={http_id}', "HTTP connect")
self.send_cmd(f'AT+CHTTPSEND={http_id},0,"{path}"', f"HTTP Get {path}")
client_id, code, length, *rubbish = self.wait_for("+CHTTPNMIH:").split(',')
if client_id == http_id and code == "200":
client_id, flag, t_len, c_len, content_hex = self.wait_for("+CHTTPNMIC:").split(',')
content = bytes.fromhex(content_hex).decode('utf-8')
self.send_cmd(f'AT+CHTTPDISCON={http_id}', "Disconnect the HTTP socket")
self.send_cmd(f'AT+CHTTPDESTROY={http_id}', "Destroy the http config")
except (NbiotCommandError, NbiotCommandTimeout):
return None
return content
def dns_lookup(self, host):
try:
resp = self.send_cmd(f'AT+CDNSGIP="{host}"', f"DNS Lookup: {host}")
data = self.parse_for("+CDNSGIP:", resp)
if not data:
data = self.wait_for("+CDNSGIP:")
if not data:
return False
params = data.split(",")
if len(params) < 3:
return False
status = params[0]
host_name = params[1].strip('"')
ip = params[2].strip('"')
if not status == "1":
return None
if not host_name == host:
return None
return ip
except (NbiotCommandError, NbiotCommandTimeout):
return False
def set_time(self):
try:
self.send_cmd('AT+CSNTPSTART="au.pool.ntp.org"', 'Start time service')
data = self.wait_for('+CSNTP:')
if not data:
return None
print(f"data: {data}") # data: 25/07/23,10:37:16:41
# Set the Pico clock
date, time = data.split(",")
yy, mon, dd = date.split("/")
hh, mm, ss, ms = time.split(":")
dt_tuple = ((2000 + int(yy)), int(mon), int(dd), 0, int(hh), int(mm), int(ss), int(ms),)
machine.RTC().datetime( dt_tuple )
except (NbiotCommandError, NbiotCommandTimeout):
return None
finally:
self.send_cmd('AT+CSNTPSTOP', 'Stop time service')
return data
##################################################################
def disconnect_mqtt(self, mqtt_id):
if mqtt_id:
time.sleep(1)
try:
self.send_cmd(f"AT+CMQDISCON={mqtt_id}", "Disconnect from MQTT server")
except (NbiotCommandError, NbiotCommandTimeout):
pass
time.sleep(1)
def awake(self):
return self.nbiotEnable.value() == 1
def wakeup(self):
print("Wake up NB-IoT")
if self.awake():
print("NB-IoT is already awake - put it to sleep first")
self.goto_sleep()
time.sleep(1)
# wake up the chip
self.nbiotEnable.high()
self.wait_for_at()
resp = self.wait_for("+CPIN:", attempts=20)
return resp == "READY"
def asleep(self):
return not self.awake()
def goto_sleep(self):
print("Setting NB-IoT to sleep")
self.nbiotEnable.low() # disable the sim7020e
# Returns a tuple (status, data[])
# May need to read multiple lines
# Exits on OK or Error or Timeout
def read_uart(self):
data = []
while True:
line = self.uart.readline()
if line:
line = line.decode().strip()
data.append(line)
if line.startswith("OK"):
return True, data
if line.startswith("ERROR"):
return False, data
else:
# Line is None or empty Timout reading readline
return None, data
# reads UART untill timeout
def flush_uart(self):
print("Flushing UART...", end="" )
while True: # keep flushing untill status = None (timoeout)
status, data = self.read_uart()
print(f"Flushed data: {data}")
if status is None:
return
def send_cmd(self, cmd, comment=None, check_echo=True):
if comment:
print(comment + " ... ", end="")
self.uart.write((cmd+'\r\n').encode())
while True:
status, data = self.read_uart()
if not check_echo:
break
if status is None:
break
if data:
if not self.parse_for(cmd, data) is None:
break
if status: # True
if comment:
print(f"OK: {data}")
return data
elif status == False:
if comment:
print(f"ERROR: {data}")
raise NbiotCommandError(f"nbiot command error: {cmd}", data)
else: # None
if comment:
print(f"TIMEOUT: {data}")
raise NbiotCommandTimeout(f"nbiot command timeout: {cmd}", data)
# Waits for an unsolicited message
# returns the value to the right of the prefix: (stripped)
def wait_for(self, prefix, attempts=15):
print(f"Wait until chip responds with {prefix}")
while attempts:
status, data = self.read_uart()
print(f"WaitFor: status: {status}, data: {data}")
if data:
for line in data:
if line.startswith(prefix):
print(f"Found: {prefix}")
offset = len(prefix)
return line[offset:].strip()
attempts -= 1
print(f"Failed to find {prefix}")
return None
# This will loop waiting for AT command to respond
def wait_for_at(self, attempts=10):
print("Wait until chip responds to AT commands")
while attempts:
try:
self.send_cmd("AT", "Attention")
break
except (NbiotCommandError, NbiotCommandTimeout):
attempts -= 1
continue # loop again
# self.flush_uart() # remove any extra OK's
if attempts:
return True
print("Failed to receive OK")
return False
def parse_for(self, prefix, resp):
print(f"parseFor {prefix} in {resp}")
if resp is None:
return None
for line in resp:
if line.startswith(prefix):
offset = len(prefix)
return line[offset:].strip()
return None
# report the current RSSI
def rssi(self):
try:
resp = self.send_cmd("AT+CSQ", comment="Get RSSI")
except (NbiotCommandError, NbiotCommandTimeout):
return False
result = self.parse_for('+CSQ:', resp)
if not result:
print("Unable to get rssi ...")
return None
ss = result.split(",")[0]
if ss:
if ss == "99":
print("Error: No Signal")
return None
if ss == "0":
print("Warning: Signal week -113dBm or less")
else:
try:
rssi = (int(ss) * 2) - 113
print(f"rssi: {rssi} dBm")
return rssi
except:
return None
return None
def firmware_version(self):
resp = self.send_cmd("AT+CGMR", "Get firmware revision")
return resp[1] # already striped
#### Tests ############################
if __name__ == "__main__":
import utils
print("##### Tests #####")
print("Initialise NBIoT")
nbiot = NBIoT()
print("Wait 5 secs to allow the chip to stablise")
time.sleep(3)
print("##### Get Time #####")
if nbiot.enable():
nbiot.set_time()
else:
nbiot.check_settings()
nbiot.disable()
sys.exit(-1)
print("##### Wake Up #####")
if not nbiot.wakeup():
nbiot.goto_sleep()
sys.exit(-1)
nbiot.goto_sleep()
print("sleeping 5sec to ensure that the chip is fully asleep")
time.sleep(3)
print("##### Factory Reset #####")
if not nbiot.factory_reset():
nbiot.disable()
sys.exit(-1)
time.sleep(1)
print("##### Enable #####")
if not nbiot.enable():
nbiot.check_settings()
nbiot.disable()
sys.exit(-2)
# print("##### Firmware Update #####")
# if not nbiot.update_firmware():
# nbiot.disable()
# sys.exit(-2)
print("##### Check Settings #####")
if not nbiot.check_settings():
nbiot.disable()
sys.exit(-3)
print("##### DNS Lookup #####")
ip = nbiot.dns_lookup("mqtt.at.martin.cc")
print(f"DNS returned: {ip}")
print("##### Send Data #####")
t = time.gmtime()
json_data = {
"u": utils.uid(),
"t": 25,
"p": 50,
"rssi": nbiot.rssi(),
"utc": "{}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}".format(t[0], t[1], t[2], t[3], t[4], t[5]),
}
if not nbiot.send_mqtt(ip, "sensors/circulait/data", json_data):
nbiot.disable()
sys.exit(-4)
print("##### HTTP GET #####")
rails_server = "accelerate-advantage-b6d76071507e.herokuapp.com"
nbiot.dns_lookup(rails_server)
uid = utils.uid()
content = nbiot.get_http(f"http://{rails_server}", f"api/sensors/{uid}")
print(f"content: {content}")
print("##### Disable #####")
nbiot.disable()
sys.exit(0)