This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

MqTT in Wiser for KNX
#21
(26.08.2022, 12:55)admin Wrote: You can create a scheduled script that runs every minute and publishes all values. Each message has to specified manually due to a variable format. Add as many addmessage calls as needed and change grp.getvalue addresses. The script will automatically disconnect once all messages are published.

Code:
broker = 'mqtt.eniris.be'

username = 'xxx'
password = 'xxx'

port = 1883

messages = {}
time = os.time()

function addmessage(topic, data)
  messages[ #messages + 1 ] = {
    topic = topic,
    data = require('json').encode(data),
  }
end

addmessage('standard1/rp_one_m/waterMetrics/' .. username, {
  time = time,
  extraTags = {
    subId = 'T19',
  },
  fields = {
    absVolume_m3 = grp.getvalue('1/1/1'),
    flow_m3ph = grp.getvalue('1/1/2'),
  }
})

addmessage('standard1/rp_one_m/weatherMetrics/' .. username, {
  time = time,
  extraTags = {
    subId = 'T19',
  },
  fields = {
    outdoorTemperature_degC = grp.getvalue('1/1/3'),
    outdoorBrightness_lux = grp.getvalue('1/1/4'),
    windSpeed_kmph = grp.getvalue('1/1/5'),
  }
})

addmessage('standard1/rp_one_m/gasMetrics/' .. username, {
  time = time,
  extraTags = {
    subId = 'T19',
  },
  fields = {
    absVolume_Nm3 = grp.getvalue('1/1/6'),
  }
})

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

mids = {}
pubcount = 0

client.ON_CONNECT = function(status, rc, msg)
  if status then
    log('mqtt cloud connected')

    for _, message in ipairs(messages) do
      local mid = client:publish(message.topic, message.data)

      if mid then
        pubcount = pubcount + 1
        mids[ mid ] = true
      end
    end

    if pubcount == 0 then
      client:disconnect()
    end
  else
    log('mqtt cloud connect failed ' .. tostring(msg))
    client:disconnect()
  end
end

client.ON_PUBLISH = function(mid)
  if mids[ mid ] then
    mids[ mid ] = nil
    pubcount = pubcount - 1

    if pubcount == 0 then
      log('disconnecting')
      client:disconnect()
    end
  end
end

client:login_set(username, password)
status, rc, msg = client:connect(broker, port)

if status then
  client:loop_forever()
else
  log('cloud connect failed: ' .. tostring(msg))
end
Thank you very much admin. Always a life saver  Smile
Reply
#22
Hello

I revive this topic about wiser and mqtt for the following question.
using the script from the first page of the thread, it works fine both ways (mqtt to object and object to mqtt).

I have observed the following if I use the same object, for example 1/0/0 (1 bit), for in/topic1 and out/topic1.

When I update it from wiser, the out/topic1 is posted and the value is updated, ok.

When I update it from mqtt using mqtt explorer, the in/topic1 is posted and the object in the Wiser is updated, ok.

The issue is that the output out/topic1 is not updated, having updated the object from mqtt using in/topic1.

So in the mqtt explorer I have one, for example in true and the other in false and they are not synchronized.

Shouldn't the output topic be updated when the object is also updated from mqtt?

Greetings
Reply
#23
This is done to prevent a possible loop when the same object is mapped to the same topic in both directions.
You can remove this check like this:
Code:
function publishvalue(event)
    -- message from us or client is not connected
    -- if event.sender == 'mq' or not mclientfd then
    if not mclientfd then
      return
    end
Reply
#24
Ok, you have your logic.

It works perfectly with the proposed solution.

Thank you.

Greetings
Reply
#25
Hello

I would like to use TLS security for the thread script.

I have seen a thread talking about the certificates but it does not clarify much.

How could I do it?

Greetings
Reply
#26
When only server certificate is needed for testing purposes you can simply skip the validation by setting client:tls_insecure_set(true).
If this works then you can disable insecure mode and profile server certificate: client:tls_set(path_to_pem_file)
Certificate file can be uploaded via FTP, the path will be /data/ftp/mqtt.pem or similar. Note that this file must be in PEM format and it must contain the whole certificate chain for the server.

For both client and server certificates the use the following code:
Code:
client:tls_set(path_to_server_pem_file, nil, path_to_client_pem_file, path_to_client_key_file)
Client private key must not have an additional password.
Reply
#27
Hello
Thanks for the reply.
I understand that I only have to convert to PEM, the certificates and the key that I already have, generated for the mosquitto server.

Greetings
Reply


Forum Jump: