Atom DTU NBIoT2

这是 ATOM DTU NBIoT2 的驱动程序库,用于接收和发送来自 DTU NBIoT 的数据。

支持以下产品:

Atom DTU NBIoT2

备注

请在使用前确认设备是否支持您所在地区的 NB-IoT 频段。

备注

请确保 SIM7028 的固件版本大于或等于 2110B07SIM7028

可以使用 nbiot_get_version.png 来检查固件版本。

UiFlow2 示例

NBIoT HTTP 示例

在 UiFlow2 中打开 atoms3_base_nbiot2_http_example.m5f2 项目。

此示例展示了如何使用 Atom DTU NBIoT2 发送 HTTP 请求。

UiFlow2 代码块:

atoms3_base_nbiot2_http_example.png

示例输出:

通过串口接收到的 NBIoT 消息输出数据。

MQTT 示例

在 UiFlow2 中打开 atoms3_base_nbiot2_mqtt_example.m5f2 项目。

此示例展示了如何使用 Atom DTU NBIoT2 发送 MQTT 消息。

UiFlow2 代码块:

atoms3_base_nbiot2_mqtt_example.png

示例输出:

屏幕上显示接收到的NBIoT消息数据。

MicroPython 示例

NBIoT HTTP 示例

此示例展示了如何使用 Atom DTU NBIoT2 发送 HTTP 请求。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import UART
 9from base import AtomRS485
10from base import AtomDTUNBIoT2
11
12
13label0 = None
14uart2 = None
15base_rs485 = None
16base_nbiot2 = None
17base_nbiot2_http_req = None
18
19
20def setup():
21    global label0, uart2, base_rs485, base_nbiot2, base_nbiot2_http_req
22
23    M5.begin()
24    Widgets.fillScreen(0x000000)
25    label0 = Widgets.Label("label0", 8, 7, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26
27    uart2 = UART(2, baudrate=115200, bits=8, parity=None, stop=1, tx=5, rx=6)
28    base_rs485 = AtomRS485(
29        1,
30        baudrate=115200,
31        bits=8,
32        parity=None,
33        stop=1,
34        tx=7,
35        rx=8,
36        txbuf=256,
37        rxbuf=256,
38        timeout=0,
39        timeout_char=0,
40        invert=0,
41        flow=0,
42    )
43    base_nbiot2 = AtomDTUNBIoT2(uart2, verbose=False)
44    base_nbiot2.connect(apn="cmnbiot")
45    while not (base_nbiot2.isconnected()):
46        pass
47    base_nbiot2_http_req = base_nbiot2.post(
48        "http://httpbin.org/post",
49        json={"message": "Hello from M5Stack!", "status": "active"},
50        headers={
51            "Content-Type": "application/json",
52            "Custom-Header": "MyHeaderValue",
53        },
54    )
55    print((str("status code: ") + str((base_nbiot2_http_req.status_code))))
56    print((str("content: ") + str((base_nbiot2_http_req.content))))
57
58
59def loop():
60    global label0, uart2, base_rs485, base_nbiot2, base_nbiot2_http_req
61    M5.update()
62
63
64if __name__ == "__main__":
65    try:
66        setup()
67        while True:
68            loop()
69    except (Exception, KeyboardInterrupt) as e:
70        try:
71            from utility import print_error_msg
72
73            print_error_msg(e)
74        except ImportError:
75            print("please update to latest firmware")

示例输出:

通过串口接收到的 NBIoT 消息输出数据。

MQTT 示例

此示例展示了如何使用 Atom DTU NBIoT2 发送 MQTT 消息。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4import os, sys, io
 5import M5
 6from M5 import *
 7from hardware import UART
 8from base import AtomRS485
 9from base import AtomDTUNBIoT2
10
11
12label0 = None
13base_nbiot2 = None
14uart2 = None
15base_rs485 = None
16base_nbiot2_mqtt = None
17
18
19def base_nbiot2_testtopic_a_event(data):
20    global label0, base_nbiot2, uart2, base_rs485, base_nbiot2_mqtt
21    label0.setText(str(data[1]))
22
23
24def setup():
25    global label0, base_nbiot2, uart2, base_rs485, base_nbiot2_mqtt
26
27    M5.begin()
28    Widgets.fillScreen(0x000000)
29    label0 = Widgets.Label("label0", 8, 7, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
30
31    uart2 = UART(2, baudrate=115200, bits=8, parity=None, stop=1, tx=5, rx=6)
32    base_rs485 = AtomRS485(
33        1,
34        baudrate=115200,
35        bits=8,
36        parity=None,
37        stop=1,
38        tx=7,
39        rx=8,
40        txbuf=256,
41        rxbuf=256,
42        timeout=0,
43        timeout_char=0,
44        invert=0,
45        flow=0,
46    )
47    base_nbiot2 = AtomDTUNBIoT2(uart2, verbose=False)
48    base_nbiot2.connect(apn="cmnbiot")
49    while not (base_nbiot2.isconnected()):
50        pass
51    base_nbiot2_mqtt = base_nbiot2.MQTTClient(
52        "uiflow2-client", "mqtt.m5stack.com", port=1883, user="", password="", keepalive=0
53    )
54    base_nbiot2_mqtt.connect(clean_session=False)
55    base_nbiot2_mqtt.subscribe("testtopic/a", base_nbiot2_testtopic_a_event, qos=0)
56
57
58def loop():
59    global label0, base_nbiot2, uart2, base_rs485, base_nbiot2_mqtt
60    M5.update()
61    base_nbiot2_mqtt.check_msg()
62
63
64if __name__ == "__main__":
65    try:
66        setup()
67        while True:
68            loop()
69    except (Exception, KeyboardInterrupt) as e:
70        try:
71            from utility import print_error_msg
72
73            print_error_msg(e)
74        except ImportError:
75            print("please update to latest firmware")

示例输出:

屏幕上显示接收到的NBIoT消息数据。

API参考

AtomDTUNBIoT2

class base.dtu_nbiot2.AtomDTUNBIoT2(uart, verbose=False)

基类:SIM7028

创建一个 AtomDTUNBIoT2 对象。

参数:
  • uart (machine.UART) – 要使用的UART对象。

  • verbose (bool) – 是否打印调试信息。

UiFlow2 代码块:

nbiot_init.png

MicroPython 代码块:

from base import AtomDTUNBIoT2
from hardware import UART

uart2 = UART(2, baudrate=115200, bits=8, parity=None, stop=1, tx=22, rx=19)
base_nbiot2 = AtomDTUNBIoT2(uart2, verbose=False)

备注

更多详细信息请参阅 NBIOT2Unit

AtomRS485

备注

更多详细信息请参阅 AtomRS485