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 Example - Publish to Beebotte, subscribe with Mqtt Dash
#1
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

  1. In 'Channels' we create 'Topics' to publish and subscribe to. A 'test' topic is already created (3).
  2. In 'Dashboards' we can create widgets to interact with the topics and visually represent the data (subscribe and publish)
  3. 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.
  4. Click on 'Create New' to create a new Channel

Mqtt broker Beebotte - Create new channel
   

  1. Give the channel a name, let's call it LM
  2. Give a resource name, like Nodon
  3. Select a data-type that is expected to be persisted to the resource, choose any if you are not sure
  4. 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'

  1. Give the dashboard a name, here we call it Home
  2. Add a widget and choose 'Text Area'
  3. 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/)

  1. Click on the plus-sign to add a new connection
  2. Give the connection a name, enable Validate certificate, enable Encryption, host, port and username/password (password is the same as username, thus channel token)
  3. Click on Advanced

Mqtt Explorer - Advanced settings
   

  1. In the topic field type: LM/Nodon and click on Add
  2. Click on 'Certificates'

Mqtt Explorer - Advanced settings certificates
   

  1. Click on 'server certificate' and select the certificate you downloaded earlier
  2. When done click on 'back', again on 'Back and 'Save'
  3. 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.

  1. Give a name to the new dashboard, here Beebotte
  2. Fill in the server address, port and enable the two checkboxes.
  3. Fill in your channel token collected earlier
  4. 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

  1. Give the new metric a name, in this example test
  2. Fill in the topic, in this example LM/Nodon (beware this is case sensitive)
  3. 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)
  4. 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)
  5. 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

  1. Click on 'system config'
  2. Click on 'services' > 'FTP server'
  3. Enable server status, leave the rest alone (no ftps, port 21)
  4. Fill in a password (1 uppercase, 1 lowercase, 1 digit and 8 characters long)

Filezilla - Upload certificate to LogicMachine
   

Download and fire up FileZilla.

  1. Fill in the IP-address of your LogicMachine, username ftp, password you choose earlier, port 21 and click 'Quickconnect'
  2. Browse to the folder where you downloaded the server certificate earlier
  3. Click and drag the file to the window at the right (4)
  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

  1. Give the script a name
  2. 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
   

  1. 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)
  2. 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.
  3. 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.
  4. 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

  1. In Mqtt Explorer you see the payload as it is send by the script
  2. In Beebotte, because it's a json object you see [object Object]
  3. 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.
Reply


Forum Jump: