![]() |
|
Mqtt Example - Publish to Beebotte, subscribe with Mqtt Dash - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Mqtt Example - Publish to Beebotte, subscribe with Mqtt Dash (/showthread.php?tid=3223) |
Mqtt Example - Publish to Beebotte, subscribe with Mqtt Dash - tigi - 13.03.2021 This example is a proof of concept for publishing data to the online Mqtt broker Beebotte and subscribe to it using Mqtt Dash on Android. Below information is based on my own search, struggle and learning process. For newbies as myself I think this example will be usefull. I did not write the script myself but found it here on the forum and changed it to work for my example based on info I found scattered around. Credits and Kudos to all of them! What is used in this example:
Mqtt broker Beebotte - setting up Surf to https://beebotte.com/ and register yourself a free account. Login into your account and you will see an overview as in the screenshot. We will use 'Channels' and 'Dashboards' in the most basic form
Mqtt broker Beebotte - Create new channel
Mqtt broker Beebotte - Dashboard create widget This step is not necessary in the setup but is used as visual feedback when fiddeling around. Click on 'Dashboards' and then on 'Create Dashboard'
Mqtt broker Beebotte - Gathering the necessary information To communicate with the Beebotte in a secure way we use SSL on port 8883 and the Beebotte server certificate
For authentication we use the 'channel token' (recommended by Beebotte) as username
The topic path to subscribe to
Beebotte mqtt broker is now ready. Mqtt Explorer - Basic setup Download and install Mqtt Explorer (http://mqtt-explorer.com/)
Mqtt Explorer - Advanced settings
Mqtt Explorer - Advanced settings certificates
Mqtt Explorer is now ready. Mqqt Dash on Android - Beebotte server settings Search,install and open Mqtt Dash on your Android device. Click in the upper right corner on the plus-sign.
Tip: Long pressing on your new added dashboard will bring up a menu where you can edit the settings. Mqqt Dash on Android - Subscribe to topic Click on the new dashboard you just created. Click in the upper right corner on the plus-sign to add a new type, choose Switch/button
Mqtt Dash is now ready. LogicMachine - Enable FTP server In order to upload the certificate to the LogicMachine, we have to enable the FTP service first. After the upload you can disable this again. Login to your LogicMachine
Filezilla - Upload certificate to LogicMachine Download and fire up FileZilla.
Close FileZilla and disable the FTP service on your Logicmachine LogicMachine - Sensor In the Objects list choose an object that has a boolean value such as 01.009 open/close. In this example we select an Enocean Nodon Sensor which is mapped to the knx groupaddress 1/1/1 Click on the scripting tab, select event-based and click on 'Add new Script' in the lower left corner
Logicmachine - LUA Script Code: -- Event-Based GroupAddress 1/1/1 - LM Nodon Sensor
require('json')
broker = "mqtt.beebotte.com"
port = 8883
username = "token_xxxxxxxxxxxxxx"
password = ""
pemfile = '/data/ftp/mqtt.beebotte.com.pem'
topic = 'LM/Nodon'
--data = tostring(event.getvalue())
data = json.encode({
state = event.getvalue(),
})
--log(data)
mqtt = require("mosquitto")
client = mqtt.new()
client:tls_insecure_set(false)
client:tls_set(pemfile)
client.ON_CONNECT = function(status, rc, msg)
if status then
log("mqtt cloud connected")
client:publish(topic, data)
log("data send")
else
log("mqtt cloud connect failed " .. tostring(msg))
client:disconnect()
end
end
client.ON_PUBLISH = function()
client:disconnect()
log("disconnected")
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))
endLogicMachine - LUA Script edits
Logicmachine - LUA Script enable and test it out
Beebotte - Mqtt Explorer - Mqtt Dash When changing the sensor state, is it physically or in LogicMachine though run script, you will see the changes appear in Mqtt Explorer, Beebotte Dashboard and Mqtt Dash on android
|