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
LogicMachine - 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
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:
- An enocean door-sensor from Nodon, which is mapped to a group-address and is used to trigger an event. (https://nodon.fr/en/nodon/enocean-door-window-sensor/)
- An online Mqtt broker to publish and subscribe to, here Beebotte (https://beebotte.com/)
- A windows app Mqtt Explorer as an extra way to see what's going on (http://mqtt-explorer.com/)
- FileZilla FTP client to upload the server certificate to the LogicMachine (https://filezilla-project.org/)
- Mqtt Dash android app to subscribe to the Mqtt topic and visualize the result (https://play.google.com/store/apps/detai...l=en&gl=US)
- The event-based script
- Super-duper LogicMachine 5
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
- In 'Channels' we create 'Topics' to publish and subscribe to. A 'test' topic is already created (3).
- In 'Dashboards' we can create widgets to interact with the topics and visually represent the data (subscribe and publish)
- A pre-made test channel. Click on 'test' and it will show the underlying 'resources', one is defined 'res'. Click on 'Channels' to go back to the overview.
- Click on 'Create New' to create a new Channel
Mqtt broker Beebotte - Create new channel
- Give the channel a name, let's call it LM
- Give a resource name, like Nodon
- Select a data-type that is expected to be persisted to the resource, choose any if you are not sure
- Click on Create 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'
- Give the dashboard a name, here we call it Home
- Add a widget and choose 'Text Area'
- Click on 'save changes'
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
- Beebotte server: mqtt.beebotte.com
- Port: 8883
- SSL: Yes
- Server certificate mqtt.beebotte.com.pem can be downloaded from this page: https://beebotte.com/docs/mqtt
For authentication we use the 'channel token' (recommended by Beebotte) as username
- username: token_xxxxxxxxxxxxxxxxx (you can find the token when clicking on the channel name, in this case LM, number 3 in the screenshot)
- password: (no password is necessary)
The topic path to subscribe to
- LM/Nodon (This is a combination of the Channel (1) and resource (2) name) Attention names are case-sensitive!)
Beebotte mqtt broker is now ready.
Mqtt Explorer - Basic setup
Download and install Mqtt Explorer (http://mqtt-explorer.com/)
- Click on the plus-sign to add a new connection
- Give the connection a name, enable Validate certificate, enable Encryption, host, port and username/password (password is the same as username, thus channel token)
- Click on Advanced
Mqtt Explorer - Advanced settings
- In the topic field type: LM/Nodon and click on Add
- Click on 'Certificates'
Mqtt Explorer - Advanced settings certificates
- Click on 'server certificate' and select the certificate you downloaded earlier
- When done click on 'back', again on 'Back and 'Save'
- You can now click connect, if all is good you'll see mqtt.beebotte.com in the upper left corner of Mqtt Explorer
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.
- Give a name to the new dashboard, here Beebotte
- Fill in the server address, port and enable the two checkboxes.
- Fill in your channel token collected earlier
- Click in the upper right corner on the 'save' icon
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
- Give the new metric a name, in this example test
- Fill in the topic, in this example LM/Nodon (beware this is case sensitive)
- Our payload is in json format, so we have to fill in here $.state (state is the keyname of the payload we use in the script further on)
- Disable publishing (we are not going to send any data, only receive). In the field on type true, in the field off type false. (these are the values of the payload with keyname state)
- Click on save
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
- Click on 'system config'
- Click on 'services' > 'FTP server'
- Enable server status, leave the rest alone (no ftps, port 21)
- Fill in a password (1 uppercase, 1 lowercase, 1 digit and 8 characters long)
Filezilla - Upload certificate to LogicMachine
Download and fire up FileZilla.
- Fill in the IP-address of your LogicMachine, username ftp, password you choose earlier, port 21 and click 'Quickconnect'
- Browse to the folder where you downloaded the server certificate earlier
- Click and drag the file to the window at the right (4)
- The file will be uploaded to the root of the LogicMachine
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
- Give the script a name
- Select the group-address of the sensor you want to use, here 1/1/1 Enocean Nodon
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))
end
LogicMachine - LUA Script edits
- Change the username in line 7 to the channel token. All other settings should be fine if you didn't change the certificate filename and used the same channel and resource name LM/Nodon (case-sensitive)
- The data is encoded to json format. Here the keyname for the event.getvalue() is state, you can change that but you have to change it in Mqtt Dash too then.
- For debug purposes you can enable log(data) or convert the boolean data to a string (in this case disable line 14 to 16 by adding -- in front.
- These lines enable a secure connection to the online broker tls_insecure_set is set to false and tls_set loads the pemfile we uploaded earlier.
Logicmachine - LUA Script enable and test it out
- Save the script and enable it. Open the logs and Error log window.
- Click on run script.
- You should now see the state of the object if log(data) is enabled in the script.
- You should also see a successful connection to the mqtt cloud, data send and disconnection as seen in the screenshot.
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
- In Mqtt Explorer you see the payload as it is send by the script
- In Beebotte, because it's a json object you see [object Object]
- In Mqtt Dash you see a checkmark (in this example also green) if the state is true, and no checkmark if the state is false. You can change color and icons in Mqtt Dash.