Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


10der

HC(x) + NODE-RED

Recommended Posts

  • Topic Author
  • Posted (edited)

    sorry but MQTT have relate to node-red like rastaman to no ganja smoking :) 

    MQTT server (service) it's independed service. it settuped / installed separately from NODE-RED, configured separately from NODE_RED.

    Edited by 10der

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • Please login or register to see this link.

     
    commented 

    Please login or register to see this link.

    Quote

    how can i disable user and password in mqtt?

     

     

     

    Please login or register to see this link.

     
    commented 

    Please login or register to see this link.

      

    edited 

    I use mosquitto and I just leave in the mqtt.cfg file the 2 lines for user PW commented:
    #.user=
    #.pwd=
    You can of course also delete the 2 lines.

    But the question belongs rather to a MQTT area than to this area.

    If your question is regarding sonoff then leave in the user_config.h the 2 lines below as they are:
    #define MQTT_USER "DVES_USER" // [MqttUser] Optional user
    #define MQTT_PASS "DVES_PASS" // [MqttPassword] Optional password
    Like this you have no PW set in the server and you do not care, what sonoff is sending.

     

    Please login or register to see this link.

     
    commented 

    Please login or register to see this link.

    on mosquitto configuration file there is an option for anonymous allowed. If this is set to TRUE you can come with any password and any user and even empty ones and have full access.

    Please login or register to see this link.

    Please login or register to see this code.

    Share this post


    Link to post
    Share on other sites
    Posted (edited)

    Tanks for the flow ctyd

    I am trying to get heating node to turn on/off my wallplug . But as i can se in debug it only get command false, and i dont understand where that command coming from. Trying to upload flow here.

    But i dont understand how i should get the commands true or false to be sent to the node wallplug.   Should it be sent from heating node or are i missing somthing here 

    Quote
    Quote
    Quote

    Please login or register to see this code.

     

     

     

     

    Edited by oddmn

    Share this post


    Link to post
    Share on other sites
    Posted (edited)
    On 3/27/2020 at 3:46 PM, 10der said:

    sorry but MQTT have relate to node-red like rastaman to no ganja smoking :) 

    MQTT server (service) it's independed service. it settuped / installed separately from NODE-RED, configured separately from NODE_RED.

    Ok, Mosquitto installed on my RPI and now the WD is working properly.
    Only a question :
    Colud you suggest the right way to manage the DV loop crash after this exception error (see screenshot)?
    This happens when the RPI rebbot or if the HC2 <-> RPI connection has been lost.

    Many thanks

    Please login or register to see this attachment.

    Edited by anonymous_66

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • 1) check via scebe/device warchdog (search by forum)

    2) cover VD code via pcall

    Share this post


    Link to post
    Share on other sites
    3 hours ago, 10der said:

    1) check via scebe/device warchdog (search by forum)

    2) cover VD code via pcall

    I meant a your VD source solution.
    The fish ? Now is too expensive

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • 2 hours ago, anonymous_66 said:

    meant a your VD source solution.

    yes. in my VD.

    you may wrap code via pcall 

     

    Please login or register to see this code.

     

    not tested,

     

    i am not use HC2 at this time as developer. all functionality ported to NODE-RED

     

    new node (ported from HA) 

    trigger-state

     

    Please login or register to see this image.

    /monthly_2020_03/image.png.c5dec0df7e5e9e52c66fb4b4f111e66a.png" />

     

    turn on off by remotec keyfob

     

    image.png.cdee26c663c5a54f3b82606c3b78a37e.png 

     

    trigger event for more sources

    image.png.743824b51307366c4391a96e3f5e55d4.png

     

    motion event triggerd when all lights is off

     

     

     

     

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • about a new node

     

    I want to turn ON/OFF my lighting (969) according my light lux sensor (1449 )

    it's very easy

    Please login or register to see this image.

    /monthly_2020_04/image.png.9291401c052b8fcaf94b8afb81527418.png" />

     

     

    image.png.f00fb4e8970acf9ceda48e74c2bb2f14.png

     

    Please login or register to see this code.

     

    Share this post


    Link to post
    Share on other sites
    On 3/30/2020 at 9:16 PM, 10der said:

    yes. in my VD.

    you may wrap code via pcall 

     

    Please login or register to see this code.

     

    not tested,

     

    i am not use HC2 at this time as developer. all functionality ported to NODE-RED

     

     

     

    Thanks to your suggestion , let me post the VD code that avoids the main loop crash in case of RPI connection lost.

     

    local MQTT_TOPIC = "home/fibaro"


    function callback(topic, payload)
        fibaro:debug(topic .. "=>" .. payload)
    end

    --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==

    local function shift_left(value, shift)
        return (value * 2 ^ shift)
    end

    local function shift_right(value, shift)
        return (math.floor(value / 2 ^ shift))
    end

    local function encode_utf8(input)
        local output
        output = string.char(math.floor(#input / 256))
        output = output .. string.char(#input % 256)
        output = output .. input

        return (output)
    end

    function message_write(tcp_socket, message_type, payload)
        local message = string.char(shift_left(message_type, 4))

        if (payload == nil) then
            message = message .. string.char(0) -- Zero length, no payload
        else
            local remaining_length = #payload

            repeat
                local digit = remaining_length % 128
                remaining_length = math.floor(remaining_length / 128)
                if (remaining_length > 0) then
                    digit = digit + 128
                end -- continuation bit
                message = message .. string.char(digit)
            until remaining_length == 0

            message = message .. payload
        end

        return tcp_socket:write(message)
    end

    function connect(hostname, port)
        local function uuid()
            local random = math.random
            local template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
            return string.gsub(
                template,
                "[xy]",
                function(c)
                    local v = (c == "x") and random(0, 0xf) or random(8, 0xb)
                    return string.format("%x", v)
                end
            )
        end

        local payload
        payload = encode_utf8("MQIsdp")
        payload = payload .. string.char(0x03)
        identifier = encode_utf8(uuid())

        -- Connect flags (byte 10)
        -- ~~~~~~~~~~~~~
        -- bit    7: Username flag =  0  -- recommended no more than 12 characters
        -- bit    6: Password flag =  0  -- ditto
        -- bit    5: Will retain   =  0
        -- bits 4,3: Will QOS      = 00
        -- bit    2: Will flag     =  0
        -- bit    1: Clean session =  1
        -- bit    0: Unused        =  0

        if (will_topic == nil) then
            payload = payload .. string.char(0x02) -- Clean session, no last will
        else
            local flags
            flags = shift_left(will_retain, 5)
            flags = flags + shift_left(will_qos, 3) + 0x06
            payload = payload .. string.char(flags)
        end

        -- Keep alive timer
        payload = payload .. string.char(math.floor(60 / 256)) -- seconds (maximum is 65535)
        payload = payload .. string.char(60 % 256)

        -- Client identifier
        payload = payload .. identifier

        -- Send MQTT message
        local tcpSocket = Net.FTcpSocket(hostname, port)
        tcpSocket:setReadTimeout(250)
        local bytes, errorCode = message_write(tcpSocket, 0x01, payload)
        if (errorCode == 0) then
            -- all OK
            return tcpSocket
        else
            fibaro:debug("Error on connect")   
            return nil
        end
    end

    function subscribe(connection, topic)
        local message_id = 1

        local message
        message = string.char(math.floor(message_id / 256))
        message = message .. string.char(message_id % 256)

        message = message .. encode_utf8(topic)
        message = message .. string.char(0) -- QOS level 0

        return message_write(connection, 0x08, message)
    end

    function unsubscribe(connection, topic)
        local message_id = 1

        local message
        message = string.char(math.floor(message_id / 256))
        message = message .. string.char(message_id % 256)

        message = message .. encode_utf8(topic)
        return message_write(connection, 0x0a, message)
    end

    function parse_message_publish(message_type_flags, remaining_length, message)
        local topic_length = string.byte(message, 1) * 256
        topic_length = topic_length + string.byte(message, 2)
        local topic = string.sub(message, 3, topic_length + 2)
        local index = topic_length + 3
        local qos = shift_left(message_type_flags, 1) % 3

        if (qos > 0) then
            local message_id = string.byte(message, index) * 256
            message_id = message_id + string.byte(message, index + 1)
            index = index + 2
        end

        local payload_length = remaining_length - index + 1
        local payload = string.sub(message, index, index + payload_length - 1)
        callback(topic, payload)
    end

    function parse_message(message_type_flags, remaining_length, message)
        local message_type = shift_right(message_type_flags, 4)
        if message_type == 0x02 then -- CONACK
            fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "CONACK")
        elseif message_type == 0x09 then -- SUBACK
            fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "SUBACK")
        elseif message_type == 0x03 then -- PUBLISH
            fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "PUBLISH")
            parse_message_publish(message_type_flags, remaining_length, message)
        elseif message_type == 0x0d then -- PINGRESP
            fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "PINGRESP")
        else
            -- print(message_type)
            -- unknown
            fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "DATA")
        end
    end

    function handle_response(buffer)
        if (buffer ~= nil and #buffer > 0) then
            local index = 1

            -- Parse individual messages (each must be at least 2 bytes long)
            -- Decode "remaining length" (MQTT v3.1 specification pages 6 and 7)

            while (index < #buffer) do
                local message_type_flags = string.byte(buffer, index)
                local multiplier = 1
                local remaining_length = 0

                repeat
                    index = index + 1
                    local digit = string.byte(buffer, index)
                    remaining_length = remaining_length + ((digit % 128) * multiplier)
                    multiplier = multiplier * 128
                until digit < 128 -- check continuation bit

                local message = string.sub(buffer, index + 1, index + remaining_length)

                if (#message == remaining_length) then
                    parse_message(message_type_flags, remaining_length, message)
                else
                    print("Incorrect remaining length: " .. remaining_length .. " ~= message length: " .. #message)
                end

                index = index + remaining_length + 1
            end

            -- Check for any left over bytes, i.e. partial message received
            if (index ~= (#buffer + 1)) then
                --- why I am here?
                print("Partial message received" .. index .. " ~= " .. (#buffer + 1))
            end
        end
    end

    function handler(connection)
        local bytes, errorCode = message_write(connection, 0x0c, nil)
        if (errorCode ~= 0) then
            fibaro:debug("MQTT.client:message_write(): " .. tostring(errorCode))
            return errorCode
        else
            local response, errorCode = connection:read()
            if (errorCode ~= 0) then
                return errorCode
            else
                if (string.len(response or "") > 0) then
                    handle_response(response)
                else
                    fibaro:debug("Error: Invalid response. response length: " .. string.len(state or ""))
                end
            end
        end
    end

    function publish(connection, topic, payload)
        local message = encode_utf8(topic) .. payload
        return message_write(connection, 0x03, message)
    end

    --if not connection then
    --    connection = connect(fibaro:get(fibaro:getSelfId(), "IPAddress"), fibaro:get(fibaro:getSelfId(), "TCPPort"))
    --    local bytes, errorCode = subscribe(connection, MQTT_TOPIC)
    --end

    --fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "OK")

    while true do
     
    local ok, result =
    pcall(
      function()

    --local echo_topic = "home/fibaro"

    local connection = connect(fibaro:get(fibaro:getSelfId(), "IPAddress"), fibaro:get(fibaro:getSelfId(), "TCPPort"))
    local bytes, errorCode = subscribe(connection, MQTT_TOPIC)
    local bytes, errorCode = subscribe(connection, "prova")      
    --print(bytes, errorCode)

    fibaro:debug("MQTT Loop started")      
    --local bytes, errorCode = publish(connection, MQTT_TOPIC, "Hello from HC2!!!")
    --print(bytes, errorCode)

    local error_message = nil
    while (error_message == nil) do
        fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "OK")    
        fibaro:sleep(500)
        error_message = handler(connection)
        fibaro:sleep(500)
    end

    if error_message == 2 then
           fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", "AUTH ERR")
           fibaro:debug("Disable password authorisation in mosquitto or set alow_anonymous true")
        else
           fibaro:call(fibaro:getSelfId(), "setProperty", "ui.lblStatus.value", error_message)
    end


    fibaro:debug("Error code "..error_message)

    unsubscribe(connection, MQTT_TOPIC)
    connection = nil

      end
    )

    fibaro:sleep(30000);
    end

     

    On 3/30/2020 at 9:16 PM, 10der said:

     

     

     

     

     

    Share this post


    Link to post
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.


    ×
    ×
    • Create New...