Adding LUA library to SHAC/NAC

Discussion in 'C-Bus Automation Controllers' started by Damaxx, Apr 4, 2019.

  1. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    171
    Likes Received:
    17
    Was going over some more ideas for scripts and come across this post from Daniel in Jan 2018

    So that go me wondering - The LUA MQTT library is a very simple library with standard dependencies.

    https://github.com/geekscape/mqtt_lua/blob/master/lua/mqtt_library.lua

    Before I go dive into the deep end and brick my controller, are there any known caveats with adding functions?
    If we could get simple MQTT implementation, it would open a trove of possibilities.
     
    Damaxx, Apr 4, 2019
    #1
    1. Advertisements

  2. Damaxx

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    28
    Likes Received:
    4
    Location:
    New Zealand
    Pie Boy, Apr 15, 2019
    #2
    1. Advertisements

  3. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    171
    Likes Received:
    17
    Can you elaborate on how you got MQTT going?
     
    Damaxx, Apr 16, 2019
    #3
  4. Damaxx

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    28
    Likes Received:
    4
    Location:
    New Zealand
    What are you trying to achieve exactly? this will bring in messages from an all ready configured broker on the same network as the SHAC making the SHAC a Mqtt client, i have mqtt talking to cbus both ways like this with a few esp8266 units and such

    The lib is already loaded in the SHAC firmware somewhere so you just need to require it etc, i stumbled on this by accident...
    Create a resident script with 0 delay to connect to the broker and topics see below, the lib is well documented as per link above

    mqtt = require("mosquitto")
    client = mqtt.new()

    client.ON_CONNECT = function()
    log("MQTT connected")
    client:subscribe("Some Topic")
    local mid = client:subscribe(Some Topic", 2)
    end

    client.ON_MESSAGE = function(mid, topic, payload)

    if topic == "Some topic" then

    if payload == "Some Payload" then
    -- Do something
    end

    end

    log(topic, payload)
    end

    broker = 'xxx.xxx.xxx.xxx' -- Broker ip adress
    client:connect(broker)
    client:loop_forever()
     
    Pie Boy, Apr 17, 2019
    #4
    Damaxx likes this.
  5. Damaxx

    Damaxx

    Joined:
    May 12, 2008
    Messages:
    171
    Likes Received:
    17
    Awesome - that's what I was looking for.
    I was trying different ways of mqtt - all of which required an "mqtt" library.
    Didn't even think to try mosquitto.
    Much appreciated.
     
    Damaxx, Apr 17, 2019
    #5
  6. Damaxx

    garethadams

    Joined:
    Oct 25, 2015
    Messages:
    12
    Likes Received:
    0
    Location:
    Perth
    I have been looking at implementing a MQTT client on my SHAC to my mosquitto broker. Reading this thread I see others have been successful with this integration, I seem to be failing....

    Looking at https://github.com/xHasKx/luamqtt and also https://github.com/geekscape/mqtt_lua/blob/master/lua/mqtt_library.lua documentation, the library used by SHAC does not follow these. I see the library being required is 'mosquitto' but can't find any documentation.

    I have added the following resident script with a 0 delay;

    Code:
    -- load mqtt module
    mqtt = require("mosquitto")
    
    -- create new mqtt client
    client = mqtt.new()
    
    log("created MQTT client", client)
    
    client.ON_CONNECT = function()
      log("MQTT connected")
      client:subscribe("dev/test")
      local mid = client:subscribe("dev/test", 2)
    end
    
    client.ON_MESSAGE = function(mid, topic, payload)
    
        if topic == "dev/test" then
    
          if payload == "hello" then
         
     log(topic, payload)
         
    end
     
    end
    
    log(topic, payload)
    end
    
    broker = '192.168.2.35'
    client:connect(broker)
    client:loop_forever()
    The mosquito broker is working as proven with MQTT-Spy and Putty. I can sub/pub to topic dev/test using these but have had no success with the SHAC. I am getting the following error from the log on mosquitto, 192.168.2.211 is the SHAC.

    1562734124: New connection from 192.168.2.211 on port 1883.
    1562734124: Socket error on client <unknown>, disconnecting.

    Any help on this would be appreciated.
     
    garethadams, Jul 10, 2019
    #6
  7. Damaxx

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    28
    Likes Received:
    4
    Location:
    New Zealand
    Just an idea.
    Do you have any security/ password broker side? Username/ password etc if so, you will need to specify log In as below

    client:login_set("user","password")

    before client:connect (broker)
     
    Pie Boy, Jul 10, 2019
    #7
  8. Damaxx

    garethadams

    Joined:
    Oct 25, 2015
    Messages:
    12
    Likes Received:
    0
    Location:
    Perth
    @Pie Boy , Thank You. It worked. Added the client:logion_set("user","password") to the script and managed to subscribe to a topic to receive the payload message.

    Do you have any documentation on how the library is implemented? I did wonder about the security on the broker side but didn't know how to implement in the SHAC, now I do!
     
    garethadams, Jul 10, 2019
    #8
  9. Damaxx

    Pie Boy

    Joined:
    Nov 21, 2012
    Messages:
    28
    Likes Received:
    4
    Location:
    New Zealand
    Pie Boy, Jul 11, 2019
    #9
  10. Damaxx

    garethadams

    Joined:
    Oct 25, 2015
    Messages:
    12
    Likes Received:
    0
    Location:
    Perth
    That's the one, thanks for your help.
     
    garethadams, Jul 12, 2019
    #10
  11. Damaxx

    garethadams

    Joined:
    Oct 25, 2015
    Messages:
    12
    Likes Received:
    0
    Location:
    Perth
    I am trying to read and write several topics/payloads. Is it better to create a resident script for each one or read all values in the same script?
     
    garethadams, Jul 13, 2019
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.