Cback Nbiot
案例程序
该程序通过MQTT连接服务器,订阅特定主题,在按钮A被按下时生成一个随机数并发布到主题中,同时显示在串口上。
from m5stack import *
from m5ui import *
from uiflow import *
import hat
setScreenColor(0xffffff)
nb = hat.get(hat.CBACK_NBIOT)
nb_topic = None
nb_msg = None
counter = None
previous = None
import random
def hat_cbacknbiot_mqtt_cb(nb_mq_topic, nb_mq_payload):
global nb_topic, nb_msg, counter, previous
nb_topic = nb_mq_topic
nb_msg = nb_mq_payload
print(nb_topic)
print(nb_msg)
pass
def buttonA_wasPressed():
global nb_topic, nb_msg, counter, previous
counter = random.randint(100000, 999999)
print(counter)
label2.setText(str(counter))
pass
btnA.wasPressed(buttonA_wasPressed)
counter = 0
previous = 0
nb.mqtt_to_connect('mqtt.m5stack.com', 1883, 'm5_mqtt999', '', '', 120)
if nb.mqtt_check_connection():
print('Connected')
while not (nb.mqtt_subscribe('SubTopic', hat_cbacknbiot_mqtt_cb, 0)):
nb.mqtt_unsubscribe('SubTopic')
print('Subscribed')
while True:
nb.mqtt_poll()
if counter != previous:
nb.mqtt_publish('PubTopic', str(counter), 0)
previous = counter
wait_ms(2)
功能说明
- uiflow_block_hat_cbacknbiot_coap_init_ip
- uiflow_block_cbacknbiot_ezdata_async_get_value
- uiflow_block_hat_cbacknbiot_check_gprs_network_registration
- uiflow_block_hat_cbacknbiot_check_network_registration
- uiflow_block_hat_cbacknbiot_check_single_quality
- uiflow_block_hat_cbacknbiot_check_status
- uiflow_block_hat_cbacknbiot_coap_destroy
- uiflow_block_hat_cbacknbiot_coap_get
- uiflow_block_hat_cbacknbiot_coap_post
- uiflow_block_hat_cbacknbiot_coap_put
- uiflow_block_hat_cbacknbiot_destroy_https
- uiflow_block_hat_cbacknbiot_disconnect_server
- uiflow_block_hat_cbacknbiot_ezdata_remove
- uiflow_block_hat_cbacknbiot_ezdata_save
- uiflow_block_hat_cbacknbiot_get_ccid
- uiflow_block_hat_cbacknbiot_get_imei
- uiflow_block_hat_cbacknbiot_http_services
- uiflow_block_hat_cbacknbiot_modem_power
- uiflow_block_hat_cbacknbiot_mqtt_check_connection
- uiflow_block_hat_cbacknbiot_mqtt_connect
- uiflow_block_hat_cbacknbiot_mqtt_disconnect
- uiflow_block_hat_cbacknbiot_mqtt_poll
- uiflow_block_hat_cbacknbiot_mqtt_publish
- uiflow_block_hat_cbacknbiot_mqtt_sub
- uiflow_block_hat_cbacknbiot_mqtt_sub_cb
- uiflow_block_hat_cbacknbiot_mqtt_unsubscribe
- uiflow_block_hat_cbacknbiot_power_down_module
- uiflow_block_hat_cbacknbiot_reset_module
- uiflow_block_hat_cbacknbiot_set_echo_mode