Skip to content

Commit be9a268

Browse files
committed
update Pycom NBIoT example to use microATsocket project for data communication
1 parent 3b22ba2 commit be9a268

File tree

6 files changed

+88
-136
lines changed

6 files changed

+88
-136
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "examples/pycom/nbiot/microATsocket"]
2+
path = examples/pycom/nbiot/microATsocket
3+
url = https://github.com/insighio/microATsocket

examples/pycom/nbiot/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Requires submodule initialization:
2+
3+
```
4+
git submodule update --init --recursive
5+
```
6+
7+
The implementation with the custom sockets that utilize AT commands uses project [microATsocket](https://github.com/insighio/microATsocket) to be able to send data using IPv6 addresses. In case of CoAP messages, microATsocket is advised to be used with custom build firmware with [Pull Request 429](https://github.com/pycom/pycom-micropython-sigfox/pull/429) than enables long message to be send over AT commands.

examples/pycom/nbiot/microATsocket

Submodule microATsocket added at d82948d

examples/pycom/nbiot/pycom_at_socket.py

Lines changed: 0 additions & 131 deletions
This file was deleted.

examples/pycom/nbiot/pycom_nbiot_coap_client.py renamed to examples/pycom/nbiot/pycom_nbiot_coap_client_custom_sock.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import binascii
55
import microcoapy
66

7-
from pycom_at_socket import PycomATSocket
7+
import microATsocket.microATsocket as socket
88

99
_NBIOT_MAX_CONNECTION_TIMEOUT_MSEC=30000
1010
_NBIOT_APN="iot"
@@ -41,7 +41,7 @@ def disconnectNBIoT():
4141

4242
def sendPostRequest(client):
4343
# About to post message...
44-
messageId = client.post(_SERVER_IP, _SERVER_PORT, _COAP_POST_URL, '[{"bn":"09876543","n":"batt","u":"V","v":12}]',
44+
messageId = client.post(_SERVER_IP, _SERVER_PORT, _COAP_POST_URL, '[{"bn":"09876543aacc","n":"batt","u":"V","v":12}]',
4545
"authorization=123456789", microcoapy.COAP_CONTENT_FORMAT.COAP_APPLICATION_JSON)
4646
print("[POST] Message Id: ", messageId)
4747

@@ -62,13 +62,14 @@ def receivedMessageCallback(packet, sender):
6262
client.resposeCallback = receivedMessageCallback
6363

6464
# Initialize custom socket
65-
customSocket = PycomATSocket(lte)
65+
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
66+
sock.setModemInstance(lte)
6667

6768
# Use custom socket to all operations of CoAP
68-
client.setCustomSocket(customSocket)
69+
client.setCustomSocket(sock)
6970

7071
sendPostRequest(client)
7172

72-
customSocket.close()
73+
sock.close()
7374

7475
disconnectNBIoT()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from network import LTE
2+
import machine
3+
import utime
4+
import binascii
5+
import microcoapy
6+
7+
_NBIOT_MAX_CONNECTION_TIMEOUT_MSEC=30000
8+
_NBIOT_APN="iot"
9+
10+
_SERVER_IP="2001:4860:4860::8888"
11+
_SERVER_PORT=5683
12+
_COAP_POST_URL="path/to/post/service"
13+
14+
print("Initializing LTE...")
15+
lte = LTE()
16+
lte.init()
17+
18+
def connectNBIoT(timeout):
19+
print("Connecting LTE...")
20+
if(not lte.isattached() or not lte.isconnected()):
21+
lte.send_at_cmd('AT+CFUN=0')
22+
lte.send_at_cmd('AT+CMEE=2')
23+
lte.send_at_cmd('AT+CGDCONT=1,"IPV6","' + _NBIOT_APN + '"')
24+
lte.send_at_cmd('AT+CFUN?')
25+
26+
start_time_activation = utime.ticks_ms()
27+
28+
lte.send_at_cmd('AT+CFUN=1')
29+
while not lte.isattached() and (utime.ticks_ms()-start_time_activation < timeout):
30+
print(".", end="")
31+
machine.idle()
32+
33+
con_start_time_activation = utime.ticks_ms()
34+
lte.connect()
35+
while not lte.isconnected() and (utime.ticks_ms()-con_start_time_activation < timeout):
36+
print(",", end="")
37+
machine.idle()
38+
print("")
39+
40+
return lte.isattached() and lte.isconnected()
41+
42+
def disconnectNBIoT():
43+
LTE().detach()
44+
45+
46+
def sendPostRequest(client):
47+
# About to post message...
48+
messageId = client.post(_SERVER_IP, _SERVER_PORT, _COAP_POST_URL, '[{"bn":"09876543","n":"batt","u":"V","v":13}]',
49+
"authorization=123456789", microcoapy.COAP_CONTENT_FORMAT.COAP_APPLICATION_JSON)
50+
print("[POST] Message Id: ", messageId)
51+
52+
# wait for respose to our request for 2 seconds
53+
client.poll(10000)
54+
55+
56+
def receivedMessageCallback(packet, sender):
57+
print('Message received:', packet.toString(), ', from: ', sender)
58+
59+
60+
61+
connected = connectNBIoT(_NBIOT_MAX_CONNECTION_TIMEOUT_MSEC)
62+
print("LTE ok: " + str(connected))
63+
64+
if(connected):
65+
client = microcoapy.Coap()
66+
# setup callback for incoming respose to a request
67+
client.start()
68+
69+
sendPostRequest(client)
70+
71+
disconnectNBIoT()

0 commit comments

Comments
 (0)