Home

Awesome

AM7020 MicroPython 範例程式碼

AM7020 (SIMCOM SIM7020E) 窄頻物聯網(NBIoT)模組 MicroPython 範例程式碼

AM7020

Installation

To install am7020:

import upip
upip.install('micropython-am7020')

or

$ ampy -p COMX put am7020

MQTT 教學說明

Declare nb and mqtt client instance using:

nb = AM7020NB(uart_num=0, baudrate=115200, tx_pin=16, rx_pin=17, reset_pin=5, dump_at_cmd=False)
mqtt = AM7020MQTT(nb)

Initialize nb and connect to NBIOT base station:

while((not nb.init() or (not nb.nbiotConnect(apn, band)))):
    print(".")

while(not nb.waitForNetwork()):
    print(".")
    sleep(5)

Check MQTT connection status and connect to Broker:

if(not mqtt.chkConnBroker()):
    mqtt.connBroker(MQTT_BROKER, 1883, mqtt_id="MY_AM7020_TEST_MQTTID")

Publish and subscribe:

mqtt.publish(TEST_TOPIC, "Hello MQTT")

def callback1(msg):
    print(TEST_TOPIC, ":", msg)
mqtt.subscribe(TEST_TOPIC, callback1)

Listen for messages from the broker:

mqtt.procSubs()

HTTP 教學說明

Declare nb and http client instance using:

nb = AM7020NB(uart_num=0, baudrate=115200, tx_pin=16, rx_pin=17, reset_pin=5, dump_at_cmd=False)
http = AM7020HTTP(nb, HTTP_SERVER)

Initialize nb and connect to NBIOT base station:

while((not nb.init() or (not nb.nbiotConnect(apn, band)))):
    print(".")

while(not nb.waitForNetwork()):
    print(".")
    sleep(5)

HTTP GET and POST:

http.get(HTTP_GET_API)
state_code = http.responseStatusCode()
body = http.responseBody()

http.post(HTTP_POST_API, content_type="application/json", body="{\"value\": \"POST\"}")
state_code = http.responseStatusCode()
body = http.responseBody()