I need to Link 5 LM (GSM) machine's back to a central LM Lite, (This a demo system to prove concept to a client)
Was looking how I can send various Object values between then all, sending alerts back to the Central and the Central LM sending required control cammands like operating relays etc.
Thinking to use as a temporary quick fix for proof of concept Remote Services, but I would like to establish a more scalable and secure solution going forward, as there could be a significant amount of remote LM's on sites (100+)
So at the present all LM will sit on a OPEN VPN Network, so using Remote Service is OK for now.
Been reading on the forum about MQTT and Broker based solutions, which appears to be the way forward.
Just seeing if there is any information or examples of how to get ALL LM's communicating between remote site and the central LM as above.
The web services would work but MQTT might be better option for such bigger installation.
For your demo you can install MQTT broker app, in attachment
You need RC2 fw but if you use VPN then you already have it.
As for clients you can use this script https://forum.logicmachine.net/showthrea...6#pid10926
On the client with broker in use ip 172.0.0.1
For installation for 100LMs it would be recommended to install MQTT broker on your VPN server.
Hard to say how many clients broker on LM can handle.
Daniel, thank you for the example.
Have tested the solutions (thank you) and works well. Just what I was looking for.
Now tested, I am going to move the broker onto a cloud, done and works great. but now need to secure connection to the broker. How can I implement a user name a password for each client to use. Stopping unauthorized use of broker,
If you use it inside the VPN then it is already secured. Only devices which are able to connect to VPN will be able to access broker. Do you want to do it outside of VPN?
11.05.2020, 11:01 (This post was last modified: 11.05.2020, 12:12 by sjfp.)
Yes please.
The VPN is on one clients site, but another project isn't, so securing the MQTT is important as well.
Plus how can add info on which LM is connected.
On the broker I can see active clients (only a number like 4 client), but cant see which ones.
Is there a way to find out, and also no if the client disappears ??
Sorry if this is an issue.
For connection with encryption use this example. Enable MQTT broker with encryption and add a user and password in the Broker app
You may need to reboot LM with broker after changing settings.
Code:
if not broker then
broker = '192.168.0.10'
username = 'user'
password = 'password'
socket = require('socket')
port = 8883
function multiply(mult)
return function(value)
local num = tonumber(value)
if num then
return num * mult
else
return value
end
end
end
Daniel, just tried the provided code, change IP address and port number (1883) but wont connect to the broker.
Tried the connection with a app on android phone with the same credentials, and connect with no issue. So I think there may be an issue in the code provided. Sorry :-(
if res then
for topic, _ in pairs(mqtt_to_object) do
mclient:subscribe(topic)
end
else
mclient:disconnect()
end
end
mclient.ON_MESSAGE = function(mid, topic, payload)
local map = mqtt_to_object[ topic ]
if map then
if map.convert then
payload = map.convert(payload)
end
grp.write(map.addr, payload)
end
end
mclient.ON_DISCONNECT = function(...)
log('mqtt disconnect', ...)
mclientfd = nil
end
function mconnect()
local status, rc, msg, fd
status, rc, msg = mclient:connect(broker, port)
if not status then
log('mqtt connect failed ' .. tostring(msg))
end
fd = mclient:socket()
if fd then
mclientfd = fd
end
end
mclient:tls_insecure_set(true)
mclient:login_set(username, password or '')
mconnect()
function publishvalue(event)
-- message from us or client is not connected
if event.sender == 'mq' or not mclientfd then
return
end
local addr = event.dst
local dpt = datatypes[ addr ]
local map = object_to_mqtt[ addr ]
-- unknown object
if not dpt or not map then
return
end
local value = busdatatype.decode(event.datahex, dpt)
if value ~= nil then
if map.convert then
value = map.convert(value)
elseif type(value) == 'boolean' then
value = value and 1 or 0
end
mclient:publish(map.topic, tostring(value), map.qos or 0, map.retain)
end
end
I see in your last sample that you have added the field "qos" (quality of service), i have a couple of questions about that:
For the qos, when i set it to 1 how do we handle the PUBACK packet? If we set it to 2 how do we handle the PUBREC, PUBREL and PUBCOMP packages? Is this handled in the lib already and the re-sending of a topic value is automatically handled and we don't have to take care of this in the client script?
Another question is: Can we simply set a extra field 'lwt' in the object to mqtt table to set the last will and testament message?
You can set 'Last will' like this
client:will_set(topic, message, qos = 0, retain = false)
So in example script it will looks like that.
mclient:will_set('in/topic2', 'disconnected', 0, true)
Add this after line
mclient:login_set(username, password or '')
16.05.2020, 16:30 (This post was last modified: 16.05.2020, 16:32 by Erwin van der Zwart.)
Hi Daniel,
Thanks!
Works perfect, now that we have a MQTT client in Ecostruxure Building Operation 3.2 (still BETA) i have a nice testing broker and it opens up a lot of options for integration.
It's possible to send a null value (retained) to a topic to clear the retained value, but is there also a command to clear retained values from a topic including all subtopics or even all topics? Otherwise it's quite a pita to clear retained values if needed (for example during testing).
A PUBLISH Packet with a RETAIN flag set to 1 and a payload containing zero bytes will be processed as normal by the Server and sent to Clients with a subscription matching the topic name. Additionally any existing retained message with the same topic name MUST be removed and any future subscribers for the topic will not receive a retained message. “As normal” means that the RETAIN flag is not set in the message received by existing Clients. A zero byte retained message MUST NOT be stored as a retained message on the Server.
I've been testing this code using one LM as broker+publisher and a second one as publisher.
I realized that when rebooting the LM that is not the broker, it can't reconnect until I disable and enable the script...