Can't subscribe to MQTT topic



  • Hardware used is Atom S3, software - UiFlow2.
    My UiFlow2 code:
    0_1683721000559_uiflow2.png
    Without "MQTT subscribe" block all else works.
    Error when running on device:
    0_1683721028662_error.jpeg

    Python code generated by UiFlow:

    import os, sys, io
    import M5
    from M5 import *
    from umqtt import *

    circle0 = None
    mqtt_client = None

    def mqtt_mqttDebugIncoming_event(data):
    global circle0, mqtt_client
    circle0.setColor(color=0x009900, fill_c=0x009900)

    def setup():
    global circle0, mqtt_client
    mqtt_client.subscribe('mqttDebugIncoming', mqtt_mqttDebugIncoming_event, qos=0)

    M5.begin()
    circle0 = Widgets.Circle(62, 62, 50, 0xffffff, 0xffffff)

    mqtt_client = MQTTClient('mqttDebug', 'homeassistant.local', port=1883, user='mqttdebug', password='mqttdebug', keepalive=300)
    mqtt_client.connect(clean_session=True)
    mqtt_client.publish('mqttDebugOutgoing', 'This works, broker receives message!', qos=0)
    circle0.setColor(color=0x3333ff, fill_c=0x3333ff)

    def loop():
    global circle0, mqtt_client
    M5.update()

    if name == 'main': # Forum message formatter messes this up
    try:
    setup()
    while True:
    loop()
    except (Exception, KeyboardInterrupt) as e:
    try:
    from utility import print_error_msg
    print_error_msg(e)
    except ImportError:
    print("please update to latest firmware")



  • Well that explains it. MQTT in UIFlow is a bit broken as UIFlow2 is for testing only



  • @ajb2k3In that case, is there an option to use UIFlow with S3 while UIFlow2 is "for testing only"? I did not see any warnings saying "things will not work" while buying it. Is there any timeframe when the already enabled functionality might work? I'm currently not asking about new features or support for more hardware, just for the things that already are available in UI.



  • Hello @Oskars

    FWIW: global variable mqtt_client is used in setup to subscribe before it is initialized. Unfortunately in UIFlow2 the Python tab is non functional yet, so the sequence cannot be fixed on the fly.

    Thanks
    Felix



  • @felmue Thank You, after I changed the code via Thonny and moved mqtt_client.subscribe after initialization the error disappeared. However the device still does not react to MQTT topics it has subscribed. So the bug is not only in UIFlow2 (incorrectly placed subscribe call) but also in MQTT library or firmware?



  • Any news here?



  • Hello guys

    just tested again. MQTT subscribe is still broken for me in alpha 14. Please fix.

    Thanks
    Felix



  • @felmue Somewhat annoying that so much seems to be broken at the moment. We have no buttons and I cannot work out how to do anything with the microsd card. Would like to play a wav alarm file when specific MQTT message is received. Neither seems possible yet.



  • Its is still alpha so not the finale item and ma have code changed.



  • @ajb2k3 It would be good if M5stack had some sort of chart or spreadsheet that stated what currently worked and what didn't. Also an eta for things coming etc. Compared with the number of M5stack youtube "how to" videos that appeared for previous "Core" products, the CoreS3 is suffering. Maybe I got on the Core2/Fire bandwagon after most teething troubles were fixed and most features were no longer "Alpha"!



  • MQTT topic subscribe still seems to be not working in Alpha 16 - no error but no activity on message receiving either. Anyone else has the same issue?



  • @Oskars

    version: Alpha-17

    0_1687146705149_962d9af4-e8c5-4fd8-97bb-ee12201bde39-image.png

    import os, sys, io
    import M5
    from M5 import *
    from umqtt import *
    
    
    label0 = None
    label2 = None
    label1 = None
    label3 = None
    mqtt_client = None
    
    
    def mqtt_testtopic_event(data):
      global label0, label2, label1, label3, mqtt_client
      label0.setText(str(((str('Topic:') + str((data[0]))))))
      label1.setText(str(((data[1]).decode())))
    
    
    def mqtt_testtopic1_event(data):
      global label0, label2, label1, label3, mqtt_client
      label2.setText(str(((str('Topic:') + str((data[0]))))))
      label3.setText(str(((data[1]).decode())))
    
    
    def setup():
      global label0, label2, label1, label3, mqtt_client
    
      M5.begin()
      label0 = Widgets.Label("Text", 4, 3, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
      label2 = Widgets.Label("Text", 4, 55, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
      label1 = Widgets.Label("Text", 4, 29, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
      label3 = Widgets.Label("Text", 3, 81, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
    
      mqtt_client = MQTTClient('umqtt_client', '192.168.2.50', port=1883, user='m5stack', password='m5stack', keepalive=0)
      mqtt_client.connect(clean_session=True)
      mqtt_client.subscribe('testtopic', mqtt_testtopic_event, qos=0)
      mqtt_client.subscribe('testtopic1', mqtt_testtopic1_event, qos=0)
      label0.setFont(Widgets.FONTS.DejaVu12)
      label1.setFont(Widgets.FONTS.DejaVu12)
      label2.setFont(Widgets.FONTS.DejaVu12)
      label3.setFont(Widgets.FONTS.DejaVu12)
    
    
    def loop():
      global label0, label2, label1, label3, mqtt_client
      M5.update()
      mqtt_client.wait_msg()
    
    
    if __name__ == '__main__':
      try:
        setup()
        while True:
          loop()
      except (Exception, KeyboardInterrupt) as e:
        try:
          from utility import print_error_msg
          print_error_msg(e)
        except ImportError:
          print("please update to latest firmware")
    
    

    test_mqtt.m5f2

    {"version":"V2.0","type":"atoms3","components":[{"name":"screen","type":"screen","layer":0,"screenId":"","screenName":"","id":"__atoms3_screen","createTime":1681463262614,"x":0,"y":0,"width":280,"height":280,"backgroundColor":"#000","size":0},{"name":"label0","type":"label","layer":1,"screenId":"builtin","screenName":"","id":"v^k&_0&lzY#a-pVu","createTime":1681787932237,"x":4,"y":3,"color":"#ffffff","backgroundColor":"#222222","text":"Text","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0},{"name":"label1","type":"label","layer":2,"screenId":"builtin","screenName":"","id":"lg-u6bmP&KAb-BFg","createTime":1681787940036,"x":4,"y":29,"color":"#ffffff","backgroundColor":"#222222","text":"Text","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0},{"name":"label2","type":"label","layer":1,"screenId":"builtin","screenName":"","id":"m+Am3fMqYyQ$+VEm","createTime":1681788301006,"x":4,"y":55,"color":"#ffffff","backgroundColor":"#222222","text":"Text","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"width":37,"height":20},{"name":"label3","type":"label","layer":2,"screenId":"builtin","screenName":"","id":"fj^rW#mTo@K0dL5r","createTime":1681788303358,"x":3,"y":81,"color":"#ffffff","backgroundColor":"#222222","text":"Text","engine":"gfx","font":"Widgets.FONTS.DejaVu18","rotation":0,"width":37,"height":20}],"resources":[{"software":["mqtt"]}],"units":[],"hats":[],"i2cs":[],"blockly":"<block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"490\" y=\"70\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_update\" id=\"system_m5_update\"><next><block type=\"mqtt_wait_msg\" id=\"T1rVj/Q%g){|@a=eic@f\"></block></next></block></statement></block><block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"130\" y=\"110\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"system_m5_begin\"><next><block type=\"mqtt_set_client\" id=\"g+(|o=#m4jpn7OLPkw)J\"><value name=\"ID\"><shadow type=\"text\" id=\"?[M!b8*f#bx*SpMXH!!5\"><field name=\"TEXT\">umqtt_client</field></shadow></value><value name=\"SERVER\"><shadow type=\"text\" id=\"oNzSR(TF-,inxj~YS2}#\"><field name=\"TEXT\">192.168.2.76</field></shadow></value><value name=\"PORT\"><shadow type=\"math_number\" id=\"e~GXuZ!~qfASA;IYe3f=\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">1883</field></shadow></value><value name=\"USER\"><shadow type=\"text\" id=\"3PbUi]|zTswyRzP*h=Zm\"><field name=\"TEXT\">m5stack</field></shadow></value><value name=\"PASSWORD\"><shadow type=\"text\" id=\"d.(iU%~um%@j5=N8C!9:\"><field name=\"TEXT\">m5stack</field></shadow></value><value name=\"KEEPALIVE\"><shadow type=\"math_slider\" id=\"4lCv3Yqkt^hJHwEHH,R)\"><mutation max=\"65535\" min=\"0\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">0</field></shadow></value><next><block type=\"mqtt_connect\" id=\"N$GCr?UqrYp~!@[yZfZ~\"><field name=\"CLEAN\">True</field><next><block type=\"label_set_font\" id=\"sBjX6,JwP2hIWbkZU]#L\"><field name=\"NAME\">label0</field><field name=\"FONT\">Widgets.FONTS.DejaVu12</field><next><block type=\"label_set_font\" id=\"l{(uGTFfhsb)s(6iS/hU\"><field name=\"NAME\">label1</field><field name=\"FONT\">Widgets.FONTS.DejaVu12</field><next><block type=\"label_set_font\" id=\"Rn6r~[cLiSQf+]nM8sTq\"><field name=\"NAME\">label2</field><field name=\"FONT\">Widgets.FONTS.DejaVu12</field><next><block type=\"label_set_font\" id=\"oaK+AUCEaQtl@kR^|jP;\"><field name=\"NAME\">label3</field><field name=\"FONT\">Widgets.FONTS.DejaVu12</field></block></next></block></next></block></next></block></next></block></next></block></next></block></statement></block><block type=\"mqtt_subscribe\" id=\"w7Ov@_:I,|/99:[91gQ+\" x=\"90\" y=\"570\"><value name=\"MSG\"><shadow type=\"text\" id=\"5+0D;yAI;M9Kab.KdV([\"><field name=\"TEXT\">testtopic</field></shadow></value><statement name=\"FUNC\"><block type=\"label_set_text\" id=\"qN{uc|lEU4,^0u_YYo@7\"><field name=\"NAME\">label0</field><value name=\"TEXT\"><shadow type=\"text\" id=\"5~k^hoGX6L}naX9o!pt=\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_add_str\" id=\"tAsN]8zdz!{wa]J5fQRj\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"NTGIBKM?0;M)9Q$?/F/E\"><field name=\"TEXT\">Topic:</field></shadow></value><value name=\"VALUE2\"><block type=\"mqtt_get_topic\" id=\"O)j9sIdBDlw?k(Y4#E{]\"></block></value></block></value><next><block type=\"label_set_text\" id=\"AIj|L5*V}(Na=i~]!Wr{\"><field name=\"NAME\">label1</field><value name=\"TEXT\"><shadow type=\"text\" id=\"9gNvISVfV@]35[tz`S6-\" disabled=\"true\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_decode_str\" id=\"}[Ilzb9o=Qd12AOz*Q/k\"><value name=\"VALUE\"><block type=\"mqtt_get_msg\" id=\"p48aW7MN4QcC/]H_RqLv\"></block></value></block></value></block></next></block></statement></block><block type=\"mqtt_subscribe\" id=\":g;Bc#f6bCJv3D=P,C24\" x=\"690\" y=\"550\"><value name=\"MSG\"><shadow type=\"text\" id=\"p!BP(rRWfzb8%w2;)|Z/\"><field name=\"TEXT\">testtopic1</field></shadow></value><statement name=\"FUNC\"><block type=\"label_set_text\" id=\"+{@FB5tyv:a,[(0)S^?^\"><field name=\"NAME\">label2</field><value name=\"TEXT\"><shadow type=\"text\" id=\"5~k^hoGX6L}naX9o!pt=\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_add_str\" id=\"MOXaUlFRx59,!q}DPD;6\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"ABnF09VK)vlEkGQ|B9P*\"><field name=\"TEXT\">Topic:</field></shadow></value><value name=\"VALUE2\"><block type=\"mqtt_get_topic\" id=\"3w2H7IRX7ZEIXgo$ZPFE\"></block></value></block></value><next><block type=\"label_set_text\" id=\"sAd`+qhCgZXd{Kt,+h$r\"><field name=\"NAME\">label3</field><value name=\"TEXT\"><shadow type=\"text\" id=\"9gNvISVfV@]35[tz`S6-\" disabled=\"true\"><field name=\"TEXT\">Label</field></shadow><block type=\"text_decode_str\" id=\"(3lEfMbcJYt7tw_fl=%3\"><value name=\"VALUE\"><block type=\"mqtt_get_msg\" id=\"YAAO(q1I]VjM1vGhYh2R\"></block></value></block></value></block></next></block></statement></block>","screen":[{"simulationName":"Built-in","type":"builtin","width":128,"height":128,"scale":1.3,"screenName":"","blockId":"","id":"builtin","createTime":1681463262611}]}
    


  • @lbuque Thanks a million to You, I finally got the code working! Looks like the thing I was missing was to use "MQTT apply for messaging" block in the loop. And changing the default 0 keepalive value (after some searching I found it caused problems with my Mosquitto broker).