UIFlow Guide
The program connects to the server through MQTT, subscribs to A particular topic, generates a random number when button A is pressed and publishes it to the topic, while displaying it on the serial port.
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)