Difference between revisions of "MQTT-flex"

From wiki.netio-products.com
Jump to navigation Jump to search
(Extended MQTT flex JSON config examples)
(Extended MQTT flex JSON config examples)
Line 489: Line 489:
  
 
= Extended MQTT flex JSON config examples =
 
= Extended MQTT flex JSON config examples =
<code>
+
 
 +
== Default NETIO MQTT-flex configuration ==
 +
This example demonstrates setup for connection to public <code>broker.hivemq.com</code>, measurement reporting and output control.
 +
 
 +
The device cabe be controlled via <code>netio/${DEVICE_NAME}/output/1/action topic</code> by sending standard NETIO action values:
 +
* 0 - OFF
 +
* 1 - ON
 +
* 2 - SHORT OFF
 +
* 3 - SHORT ON
 +
* 4 - TOGGLE
 +
* 5 - NO ACTION.
 +
 
 +
The device report measurements:
 +
 
 +
* Output state - every 15 minutes or by change
 +
* Voltage [V]- every 5 minutes or by change of 1 V
 +
* Current [mA]- every 5 minutes or by change of 50 mA
 +
* Load(Power) [W]- every 5 minutes or by change of 10 W
 +
* Energy(Consumption) [Wh]- every 15 minutes or by change of 1Wh
 +
* True Power Factor (TPF) [-]- every 5 minutes or by change of 0.02
 +
* Frequency [Hz]- every 1 hour or by change of 0.1 Hz
 +
 
 +
For register all values, you can use wildcard <code>netio/${DEVICE_NAME}/output/1/#</code>
 +
 
 
  {
 
  {
      "config":{
+
  "config":{
        "broker":{
+
      "broker":{
            "url":"broker.hivemq.com",
+
        "url":"broker.hivemq.com",
            "protocol":"mqtt",
+
        "protocol":"mqtt",
            "port":1883,
+
        "port":1883,
            "ssl":false,
+
        "ssl":false,
            "type":"generic",
+
        "type":"generic",
            "username":"freedom",
+
        "username":"",
            "password":"peace|LOVE|empathy4ALL"
+
        "password":"",
 +
        "clientid":"moje${DEVICE_NAME}${DEVICE_MAC}",
 +
        "keepalive":30
 +
      },
 +
      "subscribe":[
 +
        {
 +
            "topic":"netio/${DEVICE_NAME}/output/1/action",
 +
            "qos":0,
 +
            "target":"OUTPUTS/1/ACTION",
 +
            "action":"${payload}"
 
         },
 
         },
         "subscribe":[{
+
         {
                "topic":"netio/${DEVICE_NAME}/output/1/action",
+
            "topic":"netio/${DEVICE_NAME}/output/1/switch",
                "qos":0,
+
            "qos":0,
                "target":"OUTPUTS/1/ACTION",
+
            "target":"OUTPUTS/1/ACTION",
                "action":"${payload}"
+
            "action":4
             }
+
        }
        ],
+
      ],
        "publish":[
+
      "publish":[
             {
+
        {
                "topic":"netio/${DEVICE_NAME}/output/1/state",
+
            "topic":"netio/${DEVICE_MAC}/output/1/state",
                "qos":0,
+
            "qos":0,
                "retain":true,
+
            "retain":true,
                "payload":"${OUTPUTS/1/STATE}",
+
            "payload":"${OUTPUTS/1/STATE}",
                "events":[
+
             "events":[
                    {
+
              {
                        "type":"change",
+
                  "type":"change",
                        "source":"OUTPUTS/1/STATE"
+
                  "source":"OUTPUTS/1/STATE"
                    }
+
              },
                ]
+
              {
             },
+
                  "type":"timer",
             {
+
                  "period":900
                "topic":"netio/${DEVICE_NAME}/output/1/energy",
+
              }
                "qos":0,
+
            ]
                "retain":false,
+
        },
                "payload":"${OUTPUTS/1/ENERGY}",
+
        {
                "events":[
+
            "topic":"netio/${DEVICE_NAME}/output/1/voltage",
                    {
+
            "qos":0,
                        "type":"timer",
+
            "retain":false,
                        "period":3600
+
            "payload":"${OUTPUTS/1/VOLTAGE}",
                    },
+
            "events":[
                    {
+
              {
                        "type":"delta",
+
                  "type":"timer",
                        "source":"OUTPUTS/1/ENERGY",
+
                  "period":300
                        "delta":1
+
              },
                    }
+
              {
                ]
+
                  "type":"delta",
             }
+
                  "source":"OUTPUTS/1/VOLTAGE",
        ]
+
                  "delta":1
    }
+
              }
 +
             ]
 +
        },
 +
        {
 +
            "topic":"netio/${DEVICE_NAME}/output/1/current",
 +
            "qos":0,
 +
            "retain":false,
 +
            "payload":"${OUTPUTS/1/CURRENT}",
 +
            "events":[
 +
              {
 +
                  "type":"timer",
 +
                  "period":300
 +
              },
 +
              {
 +
                  "type":"delta",
 +
                  "source":"OUTPUTS/1/CURRENT",
 +
                  "delta":50
 +
              }
 +
            ]
 +
        },
 +
        {
 +
            "topic":"netio/${DEVICE_NAME}/output/1/load",
 +
            "qos":0,
 +
            "retain":false,
 +
            "payload":"${OUTPUTS/1/LOAD}",
 +
             "events":[
 +
              {
 +
                  "type":"timer",
 +
                  "period":300
 +
              },
 +
              {
 +
                  "type":"delta",
 +
                  "source":"OUTPUTS/1/LOAD",
 +
                  "delta":10
 +
              }
 +
             ]
 +
        },
 +
        {
 +
            "topic":"netio/${DEVICE_NAME}/output/1/energy",
 +
            "qos":0,
 +
            "retain":false,
 +
            "payload":"${OUTPUTS/1/ENERGY}",
 +
            "events":[
 +
              {
 +
                  "type":"timer",
 +
                  "period":900
 +
              },
 +
              {
 +
                  "type":"delta",
 +
                  "source":"OUTPUTS/1/ENERGY",
 +
                  "delta":1
 +
              }
 +
            ]
 +
        },
 +
        {
 +
            "topic":"netio/${DEVICE_NAME}/output/1/tpf",
 +
            "qos":0,
 +
            "retain":false,
 +
            "payload":"${OUTPUTS/1/POWER_FACTOR}",
 +
             "events":[
 +
              {
 +
                  "type":"timer",
 +
                  "period":900
 +
              },
 +
              {
 +
                  "type":"delta",
 +
                  "source":"OUTPUTS/1/POWER_FACTOR",
 +
                  "delta":0.02
 +
              }
 +
            ]
 +
        },
 +
        {
 +
            "topic":"netio/${DEVICE_NAME}/output/1/frequency",
 +
            "qos":0,
 +
            "retain":false,
 +
            "payload":"${OUTPUTS/1/FREQUENCY}",
 +
            "events":[
 +
              {
 +
                  "type":"timer",
 +
                  "period":3600
 +
              },
 +
              {
 +
                  "type":"delta",
 +
                  "source":"OUTPUTS/1/FREQUENCY",
 +
                  "delta":0.1
 +
              }
 +
            ]
 +
        }
 +
      ]
 +
  }
 
  }
 
  }
</code>
 

Revision as of 10:30, 14 September 2019

There is a lot of approaches in integration of MQTT in devices and systems. One vendor prefer one topic with structured data payload, the another one prefer tree topic structure with simple value in data payload. MQTT-flex is a standard MQTT with specific configuration approach. The goal of MQTT-flex is to give a user opportunity to integrate NETIO device with any system with different data approach.

Configuration of MQTT in devices supporting MQTT flex is done by JSON (JavaScript Object Notation). With MQTT-flex you can easily define the MQTT topics, payloads and device behaviour via text configuration in JSON object format. Since there is then lot of possibilities with such freestyle flexibility allowed, you can find bellow full set of all implemented attributes.

Supported devices:
PowerCable MQTT, PowerCable OEM (DevKit), PowerPDU 4PS, PowerDIN 4PZ, PowerBox 3Px


Contents

Simple MQTT-flex configuration + explanation

{
     "config":{
        "broker":{
            "url":"broker.hivemq.com",
            "protocol":"mqtt",
            "port":1883,
            "ssl":false,
            "type":"generic",
            "username":"freedom",
            "password":"peace|LOVE|empathy4ALL"
        },
        "subscribe":[{
               "topic":"netio/${DEVICE_NAME}/output/1/action",
               "qos":0,
               "target":"OUTPUTS/1/ACTION",
               "action":"${payload}"
           }
       ],
       "publish":[{
               "topic":"netio/${DEVICE_NAME}/output/1/state",
               "qos":0,
               "retain":true,
               "payload":"${OUTPUTS/1/STATE}",
               "events":[
                   {
                       "type":"timer",
                       "period":"1000"
                   }
               ]
           }
       ]
   }
}

Client Configuration (broker section)

Put here MQTT client connection setup

This configuration example set NETIO device to connect to broker.hivemq.com with mqtt protocol, no ssl connection with credetials freedom/peace|love|empathy4ALL.

        "broker":{
            "url":"broker.hivemq.com",
            "protocol":"mqtt",
            "port":1883,
            "ssl":false,
            "type":"generic",
            "username":"freedom",
            "password":"peace|LOVE|empathy4ALL"
        },

Device Control (subscribe section)

Put here array of topics where to subscribe

Device output 1 is controlled with action received by subscription to netio/${DEVICE_NAME}/output/1/action topic with QOS 0

        "subscribe":[{
               "topic":"netio/${DEVICE_NAME}/output/1/action",
               "qos":0,
               "target":"OUTPUTS/1/ACTION",
               "action":"${payload}"
           }
       ],

Output State monitoring (publish section)

Put here array of topics where to publish.

Device send every 1000 ms message to topic with QOS 0, retain true information with output 1 state

       "publish":[{
               "topic":"netio/${DEVICE_NAME}/output/1/state",
               "qos":0,
               "retain":true,
               "payload":"${OUTPUTS/1/STATE}",
               "events":[
                   {
                       "type":"timer",
                       "period":"1000"
                   }
               ]
           }
       ]

Config sections

Broker

here you have to define URL or IP address of machine hosting MQTT broker, level of security used for protocol, auth data and optionally some specific MQTT attributes, which are in the spec, but not mandatory required

Attributes table overview

Item Presence Possible Values Description JSON Config Usage
url mandatory MQTT broker URL "url":"broker.hivemq.com"
port mandatory 1 - 65535 MQTT broker port "port":1883
ssl mandatory true, false Use SSL crypted communication selector "ssl":false
type mandatory generic Communication/setup type option "type":"generic"
username mandatory regexp+len Credentials for MQTT broker "username":"freedom"
password mandatory regexp+len Credentials for MQTT broker "password":"peace|LOVE|empathy4ALL"
clientid optional regexp+len MQTT clientid. Max. 32 characters. Variables ${DEVICE_MAC} or ${DEVICE_NAME} can be used "clientid":"myEcoTable01"
keepalive optional 1 - 65535 MQTT keep alive period in seconds "keepalive":90

Examples

Standard setup

SSL secured setup

Variables used setup

Subscribe

Hints to usage

When receive message in topic do action on target.

actiondef: When message in topic match actiondef payload do action on target.

Attributes table overview

Item Presence Possible Values Description JSON Config Usage
topic mandatory see table below specification of the topic, which MQTT-flex device listens from broker and act accordingly "topic":"netio/${DEVICE_NAME}/output/1/action"
qos mandatory 0, 1 or 2 MQTT's Quality of Sevice - definition {here|http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html#_Toc442180912} "qos":0
target mandatory internal definition of action, which can be performed above the specified output "target":"OUTPUTS/1/ACTION"
action mandatory a payload or a value the action attribute needs to have passed to "action":"${payload}"
actiondefNot supported yet optional
jsonNot supported yet optional

Examples

Toggle output with standard payload

Toggle output with custom payload

Subscribe to more than one topic

Subscribe topic payload values specification for control of the output:

netio/<DEVICE_NAME>/output/1/action with payload for output control : (0 – off, 1 – on, 2 – short off, 3 – short on, 4 – toggle, 5 – no change)

Publish

when you need to send some data from device towards the broker, you have to specify it in the publish section - you can send measurement data as they are implemented, or you can use translation tables to send some pre-defined strings or even send JSON object. these attributes can be send periodicaly every time the timer reached specified value in seconds or based on value change (something like delta principle, but using only value differrence within defined period)

Hints to usage

When event send message with payload to topic.

Attributes table overview

Item Presence Possible Values Description JSON Config Usage
topic mandatory see table below definition of the topic, how it is registered towards MQTT broker "topic":"netio/${DEVICE_NAME}/output/1/load"
qos mandatory see table below MQTT's Quality of Sevice - definition {here|http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html#_Toc442180912}
retain mandatory boolean: true or false MQTT's attribute to specify to broker how to store last received information - section 3.3.1.3 of {MQTT doc|http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html#_Toc442180841} "retain":true
payload mandatory see table below definition of the topic, how it is registered towards MQTT broker "topic":"netio/${DEVICE_NAME}/output/1/load"
events mandatory see table below definition of events used for triggering sending of data via mqtt (change, delta, timer)
type
source
delta
period
payloaddefNot supported yet

Examples

Publish energy periodically (event timer)

Publish current on change (delta)

Publish output state

Publish to more than one topic with different events

Target / Source

In MQTT-flex we use targets and sources specify where to apply some change or get value. Practically it means addressing of INPUTS/OUTPUTS and adding command or variable.

  • source - specifies from where the value will be read (specifies the source of data for payload, change detection, delta calculation)
  • target - specifies where the data should be sent or command applied (e.g. specifies the target for ACTION)

INPUT / OUTPUT (Target) selection

NETIO Devices can have inputs and outputs which can be different type. When you want to work with some variable you have to specify on which I/O. For doing that in MQTT-flex is implemented structured addressing with "/" separator. See the steps below how to compose source or target.

  1. At first specify if requested variable is from/for INPUT or OUTPUT using keywords "OUTPUTS" or "INPUTS" .
  2. Add "/" separator
  3. Specify the number of INPUT/OUTPUT to 1(or TOTAL for summary statistics if available)
  4. Add "/" separator
  5. Specify requested variable or command

Note: Not all of OUTPUTS/INPUTS support all variables. Available variables are closely connected with the device functions and I/O features.

Note2: INPUTS not yet implemented

Examples:

adressing controll of output 3

OUTPUTS/3/ACTION

getting voltage on output 1

OUTPUTS/1/VOLTAGE

Commands

Variables

In some parameters is nescessary to use variables to get current state and measurements. Variables are separated in two categories:

  • Target/Source variables
  • Payload Topic variables

Target Source Variables

The list of available variables for each

OUTPUTS variables

VOLTAGE

Unit: V

Type: integer

Allowed in: "payload" "source"

Example 1: "payload":"Present RMS voltage on output 1 is ${OUTPUTS/1/VOLTAGE}"

Example 2: "source":"${OUTPUTS/1/VOLTAGE}"

CURRENT

Unit: mA

Type: integer

Allowed in: "payload" "source"

Example 1: "payload":"Present RMS current on output 1 is ${OUTPUTS/1/CURRENT}"

Example 2: "source":"${OUTPUTS/1/CURRENT}"

POWER_FACTOR

Unit: [-]

Type: float (2 decimals)

Allowed in: "payload" "source"

Example 1: "payload":"True Power Factor on output 1 is ${OUTPUTS/1/POWER_FACTOR}"

Example 2: "source":"${OUTPUTS/1/POWER_FACTOR}"

LOAD

Unit: W

Type: integer

Allowed in: "payload" "source"

Example 1: "payload":"Present load on output 1 is ${OUTPUTS/1/ENERGY}Wh"

Example 2: "payload":"Device's power connected to output 1 is ${OUTPUTS/1/ENERGY}Wh"

Example 3: "source":"${OUTPUTS/1/ENERGY}"

STATE

Type: bool(integer) - 0 or 1

Allowed in: "payload" "source"

Example 1: "payload":"Present output 1 state is ${OUTPUTS/1/STATE}"

Example 2: "source":"${OUTPUTS/1/STATE}"

ACTION

Type: integer

Possible actions:

  • 0 - set state OFF
  • 1 - set state ON
  • 2 - short OFF (restart)
  • 3 - short ON
  • 4 - toggle (switch)
  • 5 - no change

Allowed in: "target"

Example: "target":"OUTPUTS/1/ACTION"

DELAY [ms]

NAME

FREQUENCY

Unit: Hz

Type: integer

Allowed in: "payload" "source"

Example: "payload":"Frequency in power grid is ${OUTPUTS/1/ENERGY}Hz"

ENERGY

Unit: Wh

Type: integer

Allowed in: "payload" "source"

Example 1: "payload":"Consumed energy on output 1 is ${OUTPUTS/1/ENERGY}Wh"

Example 2: "source":"${OUTPUTS/1/ENERGY}"

ENERGY_START

Unit: unix timestamp

Type: integer

Allowed in: "payload"

Example 1: "payload":"Energy measured since ${OUTPUTS/1/ENERGY_START} unix time"

Example 2: "payload":"${OUTPUTS/1/ENERGY_START}" is output payload 1567654639

ENERGY_START_FMT

Unit: fmt date format

Type: string

Allowed in: "payload"

Example 1: "payload":"Energy measured since ${OUTPUTS/1/ENERGY_START}"

Example 2: "payload":"${OUTPUTS/1/ENERGY_START_FMT}" is output payload 2019-09-05T03:37:19+00:00

INPUT variables Not yet implemented

Not yet implemented

TOTALs - for future use with devices having more than 1 measured output

  • OUTPUTS/TOTAL/ENERGY
  • OUTPUTS/TOTAL/LOAD
  • OUTPUTS/TOTAL/CURRENT
  • OUTPUTS/TOTAL/VOLTAGE
  • OUTPUTS/TOTAL/POWER_FACTOR
  • OUTPUTS/TOTAL/FREQUENCY

Payload/topic variables

List

DEVICE_NAME

Can be used in: "payload", "topic"

DEVICE_MAC
UPTIME Not yet implemented
PAYLOAD

Can be used in: "action"

OUTPUT_ID Not yet implemented
WIFI_SIGNALNot yet implemented

Examples

"payload":"${DEVICE_NAME}"
"topic":"netio/${DEVICE_NAME}/output/1/action"
"action":"${PAYLOAD}"

Extended MQTT flex JSON config examples

Default NETIO MQTT-flex configuration

This example demonstrates setup for connection to public broker.hivemq.com, measurement reporting and output control.

The device cabe be controlled via netio/${DEVICE_NAME}/output/1/action topic by sending standard NETIO action values:

  • 0 - OFF
  • 1 - ON
  • 2 - SHORT OFF
  • 3 - SHORT ON
  • 4 - TOGGLE
  • 5 - NO ACTION.

The device report measurements:

  • Output state - every 15 minutes or by change
  • Voltage [V]- every 5 minutes or by change of 1 V
  • Current [mA]- every 5 minutes or by change of 50 mA
  • Load(Power) [W]- every 5 minutes or by change of 10 W
  • Energy(Consumption) [Wh]- every 15 minutes or by change of 1Wh
  • True Power Factor (TPF) [-]- every 5 minutes or by change of 0.02
  • Frequency [Hz]- every 1 hour or by change of 0.1 Hz

For register all values, you can use wildcard netio/${DEVICE_NAME}/output/1/#

{
  "config":{
     "broker":{
        "url":"broker.hivemq.com",
        "protocol":"mqtt",
        "port":1883,
        "ssl":false,
        "type":"generic",
        "username":"",
        "password":"",
        "clientid":"moje${DEVICE_NAME}${DEVICE_MAC}",
        "keepalive":30
     },
     "subscribe":[
        {
           "topic":"netio/${DEVICE_NAME}/output/1/action",
           "qos":0,
           "target":"OUTPUTS/1/ACTION",
           "action":"${payload}"
        },
        {
           "topic":"netio/${DEVICE_NAME}/output/1/switch",
           "qos":0,
           "target":"OUTPUTS/1/ACTION",
           "action":4
        }
     ],
     "publish":[
        {
           "topic":"netio/${DEVICE_MAC}/output/1/state",
           "qos":0,
           "retain":true,
           "payload":"${OUTPUTS/1/STATE}",
           "events":[
              {
                 "type":"change",
                 "source":"OUTPUTS/1/STATE"
              },
              {
                 "type":"timer",
                 "period":900
              }
           ]
        },
        {
           "topic":"netio/${DEVICE_NAME}/output/1/voltage",
           "qos":0,
           "retain":false,
           "payload":"${OUTPUTS/1/VOLTAGE}",
           "events":[
              {
                 "type":"timer",
                 "period":300
              },
              {
                 "type":"delta",
                 "source":"OUTPUTS/1/VOLTAGE",
                 "delta":1
              }
           ]
        },
        {
           "topic":"netio/${DEVICE_NAME}/output/1/current",
           "qos":0,
           "retain":false,
           "payload":"${OUTPUTS/1/CURRENT}",
           "events":[
              {
                 "type":"timer",
                 "period":300
              },
              {
                 "type":"delta",
                 "source":"OUTPUTS/1/CURRENT",
                 "delta":50
              }
           ]
        },
        {
           "topic":"netio/${DEVICE_NAME}/output/1/load",
           "qos":0,
           "retain":false,
           "payload":"${OUTPUTS/1/LOAD}",
           "events":[
              {
                 "type":"timer",
                 "period":300
              },
              {
                 "type":"delta",
                 "source":"OUTPUTS/1/LOAD",
                 "delta":10
              }
           ]
        },
        {
           "topic":"netio/${DEVICE_NAME}/output/1/energy",
           "qos":0,
           "retain":false,
           "payload":"${OUTPUTS/1/ENERGY}",
           "events":[
              {
                 "type":"timer",
                 "period":900
              },
              {
                 "type":"delta",
                 "source":"OUTPUTS/1/ENERGY",
                 "delta":1
              }
           ]
        },
        {
           "topic":"netio/${DEVICE_NAME}/output/1/tpf",
           "qos":0,
           "retain":false,
           "payload":"${OUTPUTS/1/POWER_FACTOR}",
           "events":[
              {
                 "type":"timer",
                 "period":900
              },
              {
                 "type":"delta",
                 "source":"OUTPUTS/1/POWER_FACTOR",
                 "delta":0.02
              }
           ]
        },
        {
           "topic":"netio/${DEVICE_NAME}/output/1/frequency",
           "qos":0,
           "retain":false,
           "payload":"${OUTPUTS/1/FREQUENCY}",
           "events":[
              {
                 "type":"timer",
                 "period":3600
              },
              {
                 "type":"delta",
                 "source":"OUTPUTS/1/FREQUENCY",
                 "delta":0.1
              }
           ]
        }
     ]
  }
}