MQTT subscribes on UiFlow



  • @world101 Your example does not read the topics, it's being set when you push the buttons....



  • @skysurfer said in MQTT subscribes on UiFlow:

    @world101 Your example does not read the topics, it's being set when you push the buttons....

    You are correct. I'm having the same issues as you.



  • Is there really no one that can help us? We are already 3 with the same problem.
    I don't think that we did the same wrong.
    Is there a other way to get the values from my mqtt broker or homeassistant? Processing by IFTT or other web service and read the values a different way onto the M5?



  • @buhi there is one more option: you can read / write data from the channel using REST API queries: tab ADVANCED -> HTTP-> HTTP REQUEST. Most MQTT brokers allow this option.

    When i send http request:
    https://io.adafruit.com/api/v2/robalstona/feeds/feed01

    i receive something like that in json format:

    {"username":"robalstona","owner":{"id":300855,"username":"robalstona"},"id":1207296,"name":"feed01","description":null,"license":null,"history":true,"enabled":true,"visibility":"public","unit_type":null,"unit_symbol":null,"last_value":"20.7","created_at":"2019-10-27T19:40:47Z","updated_at":"2020-01-23T12:01:53Z","key":"feed01","writable":false,"group":{"id":198443,"key":"default","name":"Default","user_id":300855},"groups":[{"id":198443,"key":"default","name":"Default","user_id":300855}]}

    in my case the "last_value" field with assigned value "20.7" is what I need.

    You can use JSON->loads json and ADVANCED -> HTTP -> Get Data to store this data in variable as list.

    only then do you need to know the structure of this list to get to the field with interesting data values



  • Okay, so I was really challenged and determined by this thread to get the setup working. I installed Home Assistant and set up the Mosquitto MQTT add-on. Long story short, I was able to get multiple topics subscribed and working on 3 different devices. Two M5Stacks and one iPhone app called MQTTool. All three devices are publishing and receiving data to/from MQTTT.

    Here are my uiFlows:
    0_1579798423204_Screen Shot 2020-01-23 at 11.51.34 AM.png
    0_1579798433528_Screen Shot 2020-01-23 at 10.32.17 AM.png

    M5Stack #1 has an ENV unit providing Temperature, Pressure, and Humidity to MQTT, is reading the status of the LIGHT unit from M5Stack #2, and is reading the status of the iPhone app providing fake sensor/thermostat data. M5Stack #2 has a LIGHT unit providing the digital status of the light sensor (on/off) to MQTT, is reading the T/P/H values provided by M5Stack #1, and is reading the status of the iPhone app providing fake sensor/thermostat data.
    0_1579798854625_IMG_0916.jpg

    Troubleshooting the black screen: REPL is your friend for errors. That is ultimately how I figured out what was not working. On a Mac, I opened Terminal and typed this to get to the REPL:
    screen -port /dev/tty.SLAB_USBtoUART -b 115200

    As I was building the setup, trying a slightly modified flow on each device, I forgot to connect the unit on the other M5stack. If you have a unit added in uiFlow UI (left side panel) and the unit blocks added to your flow, but the unit is not physically connected to the M5stack, this causes a black screen when attempting to connect to MQTT. No on screen error at all, but the REPL said something like "env0 not found" when I rebooted the M5stack and looked at the startup logs. It would just sit there and hang.

    Another issue is that the Unit number might be changing on you when you add/remove it from your device or program (i.e. env0 changes to env1 or light0 changes to light1), especially when you don't refresh the entire uiFlow webpage before connecting each new device.

    Anyway, I hope this helps someone. If you have other scenarios you want me to test, let me know.



  • @world101 Thank you so far for your hard work. I really have to try again.



  • I have some important discoveries... If you have another client device (i.e. MQTTlens, MQTTool, etc.) that sets the retain flag = True on the publish message, that will cause an error on the m5stack MQTT client when you subscribe to multiple topics. Here is the Traceback error.

    Internal FS (FatFS): Mounted on partition 'internalfs' [size: 2031616; Flash address: 0x210000]
    ----------------
    Filesystem size: 1978368 B
               Used: 585728 B
               Free: 1392640 B
    ----------------
    I (497) [TFTSPI]: attached display device, speed=8000000
    I (497) [TFTSPI]: bus uses native pins: false
    [ M5 ] node id:d8a01d698f30, api key:32532B1C
    I (4783) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
    I (4784) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
    I (4935) phy: phy_version: 4100, 2a5dd04, Jan 23 2019, 21:00:07, 0, 0
    I (4942) modsocket: Initializing
    Connect Wi-Fi: SSID:xxxxxxxx PASSWD:xxxxxxxx network...
    ......
    Connected. Network config: ('10.0.0.5', '255.255.255.0', '10.0.0.1', '10.0.0.1')
    Warning: Comparison between bytes and str
    Warning: Comparison between bytes and str
    Warning: Comparison between bytes and str
    Traceback (most recent call last):
      File "main.py", line 48, in <module>
      File "flowlib/m5mqtt.py", line 100, in subscribe
      File "flowlib/simple.py", line 193, in subscribe
      File "flowlib/simple.py", line 243, in wait_msg
      File "flowlib/m5mqtt.py", line 52, in _on_data
    KeyError: b'sensor/light'
    MicroPython v1.11-319-g54c3f2613-dirty on 2020-01-15; ESP32 module with ESP32
    Type "help()" for more information.
    >>> 
    

    So, subscribing to multiple topics seems to be okay in general, but setting the retain flag = True on more than one of those topics seems to cause the MQTT client to throw an error. Also, with lots of debugging, I found that you can only have one topic that has the retain flag = True and it has to be the last one in your micropython code (I think because the code is processed from the bottom up???).

    This code works. Topics aa, bb, cc, and dd have messages with retain=True. Topics aaa, bbb, ccc, and ddd have messages with retain=False. Topic dd is at the bottom.

    from m5stack import *
    from m5ui import *
    from uiflow import *
    from m5mqtt import M5mqtt
    
    setScreenColor(0x000000)
    
    
    m5mqtt = M5mqtt('m5stickC', '10.0.0.60', 1883, 'mqtt', 'password', 300)
    
    label0 = M5TextBox(2, 17, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label1 = M5TextBox(2, 57, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label2 = M5TextBox(2, 96, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label3 = M5TextBox(2, 132, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    
    a = None
    b = None
    c = None
    d = None
    
    
    def fun_aaa_(topic_data):
      global a, b, c, d
      a = topic_data
      label0.setText(str(a))
      pass
    m5mqtt.subscribe(str('aaa'), fun_aaa_)
    
    def fun_bbb_(topic_data):
      global a, b, c, d
      b = topic_data
      label1.setText(str(b))
      pass
    m5mqtt.subscribe(str('bbb'), fun_bbb_)
    
    def fun_ccc_(topic_data):
      global a, b, c, d
      c = topic_data
      label2.setText(str(c))
      pass
    m5mqtt.subscribe(str('ccc'), fun_ccc_)
    
    def fun_dd_(topic_data):
      global a, b, c, d
      d = topic_data
      label3.setText(str(d))
      pass
    m5mqtt.subscribe(str('dd'), fun_dd_)
    
    
    m5mqtt.start()
    while True:
      wait(1)
      wait_ms(2)
    

    This does not work since topic cc (retain=True) is not at the bottom.

    from m5stack import *
    from m5ui import *
    from uiflow import *
    from m5mqtt import M5mqtt
    
    setScreenColor(0x000000)
    
    
    m5mqtt = M5mqtt('m5stickC', '10.0.0.60', 1883, 'mqtt', 'password', 300)
    
    label0 = M5TextBox(2, 17, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label1 = M5TextBox(2, 57, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label2 = M5TextBox(2, 96, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label3 = M5TextBox(2, 132, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    
    a = None
    b = None
    c = None
    d = None
    
    
    def fun_aaa_(topic_data):
      global a, b, c, d
      a = topic_data
      label0.setText(str(a))
      pass
    m5mqtt.subscribe(str('aaa'), fun_aaa_)
    
    def fun_bbb_(topic_data):
      global a, b, c, d
      b = topic_data
      label1.setText(str(b))
      pass
    m5mqtt.subscribe(str('bbb'), fun_bbb_)
    
    def fun_cc_(topic_data):
      global a, b, c, d
      c = topic_data
      label2.setText(str(c))
      pass
    m5mqtt.subscribe(str('cc'), fun_cc_)
    
    def fun_ddd_(topic_data):
      global a, b, c, d
      d = topic_data
      label3.setText(str(d))
      pass
    m5mqtt.subscribe(str('ddd'), fun_ddd_)
    
    
    m5mqtt.start()
    while True:
      wait(1)
      wait_ms(2)
    

    However, if you move the cc topic to the bottom it works again.

    from m5stack import *
    from m5ui import *
    from uiflow import *
    from m5mqtt import M5mqtt
    
    setScreenColor(0x000000)
    
    
    m5mqtt = M5mqtt('m5stickC', '10.0.0.60', 1883, 'mqtt', 'password', 300)
    
    label0 = M5TextBox(2, 17, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label1 = M5TextBox(2, 57, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label2 = M5TextBox(2, 96, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    label3 = M5TextBox(2, 132, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
    
    a = None
    b = None
    c = None
    d = None
    
    
    def fun_aaa_(topic_data):
      global a, b, c, d
      a = topic_data
      label0.setText(str(a))
      pass
    m5mqtt.subscribe(str('aaa'), fun_aaa_)
    
    def fun_bbb_(topic_data):
      global a, b, c, d
      b = topic_data
      label1.setText(str(b))
      pass
    m5mqtt.subscribe(str('bbb'), fun_bbb_)
    
    def fun_ddd_(topic_data):
      global a, b, c, d
      d = topic_data
      label3.setText(str(d))
      pass
    m5mqtt.subscribe(str('ddd'), fun_ddd_)
    
    def fun_cc_(topic_data):
      global a, b, c, d
      c = topic_data
      label2.setText(str(c))
      pass
    m5mqtt.subscribe(str('cc'), fun_cc_)
    
    m5mqtt.start()
    while True:
      wait(1)
      wait_ms(2)
    

    Hope this helps. Also, I hope @m5stack will update the m5mqtt client to support the Retain flag, QoS, and the Last Will and Testament (LWT) messages soon to solve some of these limitations I have uncovered.



  • Ok that could be they key. I will try that out.
    So many thanks!



  • @world101 Makes no difference here...



  • @skysurfer

    I can’t help much without knowing your setup. What are you seeing on the REPL or serial monitor? What devices and firmware version are you running? Post your code and I’ll see if I can figure out your issue.