Hi,
Has anybody had success with this: http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.lua.git/ talking to AirVantage?
I’ve had the example from the earlier version (https://github.com/geekscape/mqtt_lua) of the lib connecting to a local mosquitto broker & pubs & subs working fine but that did not need a call to the auth() method (it didn’t exist in that version), which this Paho version does.
My code:
function callback(
topic, -- string
payload) -- string
print("mqtt_test:callback(): " .. topic .. ": " .. payload)
if (payload == "quit") then running = false end
end
local MQTT = require "paho.mqtt"
MQTT.Utility.set_debug(true)
local mqtt_client = MQTT.client.create("eu.airvantage.net", 1883, callback)
mqtt_client:connect("")
mqtt_client:auth("<imei>","<mqtt password from AV>")
mqtt_client:subscribe({"Station/density"}) -- one of my variables
local error_message = nil
local running = true
while (error_message == nil and running) do
error_message = mqtt_client:handler()
if (error_message == nil) then
socket.sleep(1.0) -- seconds
end
end
if (error_message == nil) then
mqtt_client:unsubscribe({ args.topic_s })
mqtt_client:destroy()
else
print(error_message)
end
The data section of the app model:
...
<asset default-label="MSC" id="Station">
<variable default-label="Temperature" path="temperature" type="double"/>
<variable default-label="Density" path="density" type="double"/>
...
My Lua script output is:
MQTT.client:connect():
MQTT.client:subscribe(): Station/density
MQTT.client:handler()
MQTT.client:handler()
MQTT.client:handler()
MQTT.client:destroy()
MQTT.client:disconnect()
socket_client:receive(): closed
socket_client:receive(): closed
Thanks,
Steve
Hi Steve,
You can find a tutorial MQTT+Lua (for our GX gateway though) here: doc.airvantage.net/av/howto/har … -mqtt-lua/
I have to update this tutorial with the correct link to the sample on github which is here now: github.com/rjacolin/GX440-mqtt-lua
You may find some help.
With a first quick review, your subscription to the topic is not correct: if you want to send data to AirVantage, you have to use a topic like: /messages/json (for instance)
More information here: doc.airvantage.net/av/reference … s/mqtt-av/
Regards,
Robert
Hi Robert,
Thanks for the reply.
I see the library is very similar to the one I used.
I’m now using the Lua library SW are using (https://github.com/rjacolin/GX440-mqtt-lua) . I based my test code below on the main.lua having removed modbus and replacing log() calls with print() for my purposes in a small test running in ZeroBrane’s IDE.
My code:
local mqtt = require 'mqtt_library'
local utils = require 'utils'
-- ----------------------------------------------------------------------------
-- CONSTANTS
-- ----------------------------------------------------------------------------
local ASSET_NAME = "Station"
local MQTT_DATA_PATH = "<imei>/messages/json"
local MQTT_COMMAND_PATH = "<imei>/tasks/json"
local MQTT_ACK_PATH = "<imei>/acks/json"
local MQTT_BROKER = "eu.airvantage.net"
local MQTT_PORT = 1883
local mqtt_client
local function process_mqtt(topic, val)
--local data = utils.split(topic, "/")[4]
print("Receive mqtt "..topic.." : ".. val)
end
local function main()
print("Application started")
mqtt.Utility.set_debug(true)
mqtt_client = mqtt.client.create(MQTT_BROKER, MQTT_PORT, process_mqtt)
print("MQTT Client - OK")
-- I changed the first parameter from LOG_NAME
local r = mqtt_client:connect("Station", nil, nil, nil, nil, "<imei>", "<password>")
-- is DATA_PATH correct for subscribing????
r = mqtt_client:subscribe({MQTT_DATA_PATH.."#"})
print("Init done")
socket.sleep(2)
while true do
mqtt_client:handler()
socket.sleep(1)
end
end
main()
It fails after the second call to handler():
Application started
MQTT Client - OK
MQTT.client:connect(): Station
MQTT.client:subscribe(): <imei>/messages/json#
Init done
MQTT.client:handler()
MQTT.client:handler()
MQTT.client:destroy()
MQTT.client:disconnect()
socket_client:receive(): closed
Debugging session completed (traced 2 instructions).
Program completed in 7.22 seconds (pid: 2884).
D:\TRUST\ZBS\lua\mqtt_library.lua:303: MQTT.client:handler(): Not connected
stack traceback:
[C]: in function 'error'
D:\TRUST\ZBS\lua\mqtt_library.lua:303: in function 'handler'
mqttAV.lua:75: in function 'main'
mqttAV.lua:81: in main chunk
I can send data ok to AV using:
mosquitto_pub -h eu.airvantage.net -t <imei>/messages/json -f "..DATAFILE.." -u <imei> -P <password>
And the DATAFILE like this:
[
{ "Station.density": [ {"timestamp": 1479917448000, "value":62.456973670156} ] },
{ "Station.temperature": [ {"timestamp": 1479917448000, "value":46.777345538194} ] }
]
Am I doing something more fundamentally wrong in the Lua?
Steve
Steve,
It is not easy for me as I don’t have lua on my computer. What you can do, it is the following:
- check the device is connected to AirVantage after mqtt-client:connect call: in the timeline (Monitor > Systems > go in the systems details page > Timeline button on the top of the page), you must see a connection item
- use a mqtt client to connect to /errors in order to receive any notification if something gets wrong in the payload with AirVantage
- be aware, if a second mqtt client uses the same client-id, your first client will be disconnected. So use a different client-id to connect to errors topic. So you may avoid to use “Station” but something like “Stations-”.
Regards.
Robert
Hi Robert,
I never got any errors and the timeline indicated it had a connection and didn’t indicate otherwise even when the code crashed. I eventually tracked it down to this line:
r = mqtt_client:subscribe({MQTT_DATA_PATH.."#"})
it should be:
r = mqtt_client:subscribe({MQTT_DATA_PATH})
Thanks for the pointers and tips though. Back to the good stuff now…
Regards,
Steve
Thanks for the feedback. I will update the sample app.
Continuing the discussion from MQTT Lua lib:
Hi
I have to implement a MQTT client into a RV50X, able to handle Qos level 1.
I checked the example described in this topic, unfortunately it seems that it is designed only for Qos level 0. Do you confirm ?
Can you advice if there is any other MQTT client available for Aleos dev studio which could feed my needs in term of qos ?
I have tried to implement this one https://github.com/xHasKx/luamqtt, but without success.
thanks for your help
Olivier