# Examples

# Configuration

Before starting integration process Public MQTT API should be configured. See User Interface Options chapter of Gateway Programming Guide.

To do this - enter configuration mode on gateway shell and set basic parameters.

# Setup MQTT Port and Authentication

Public MQTT port is 9883.

set mqtt public authentication username "public"
set mqtt public authentication password "password"
commit
save

Resulting configuration tree will be:

{
    mqtt {
        public {
            authentication {
                username "public"
                passsword ********
            }
        }
    }
}

# Configure custom telemetry sending interval

set mqtt public telemetry-interval 5
commit
save

Resulting configuration tree will be:

{
    mqtt {
        public {
            telemetry-interval 5
        }
    }
}

# Enable public API for devices

set mqtt public devices enapter-electrolyser-v21 <EL_1_DEVICE_ID> enabled true
set mqtt public devices enapter-electrolyser-v21 <EL_2_DEVICE_ID> enabled true
set mqtt public devices enapter-dryer <DRYER_DEVICE_ID> enabled true
commit
save

Resulting configuration tree will be:

{
    mqtt {
        public {
            devices {
                enapter-electrolyser-v21 8BF3C483286E348B0045E9F15EBCB67EFC268B4A {
                    enabled true
                }
                enapter-electrolyser-v21 A9CCB145472FF183535E93427D007B6844E369BB {
                    enabled true
                }
                enapter-dryer 2B2E42A378DB505BDBD0A9F41F2D4603D30A51E5 {
                    enabled true
                }
            }
        }
    }
}

# Define variables

set variables variable <VARIABLE_NAME> type float
commit
save

Resulting configuration tree will be:

{
    variables {
        variable target-pid-pressure {
            type float
        }
    }
}

# Receiving device telemetry

  1. Connect to mqtt on gateway ip address, specified port, with specified username & password,
  2. Subscribe to telemetry topic
mosquitto_sub -d -v -p 9883 -h 192.168.1.1 -u public -P password -t "telemetry/v1/<DEVICE_ID>"

# Set variable value

  1. Connect to mqtt on gateway ip address, specified port, with specified username & password,
  2. Publish message to set variable topic
mosquitto_pub -p 9883 -h 192.168.1.1 -u public -P password -t "variables/v1/set/<FLOAT_VARIABLE_NAME>" -m "{ \"value\": 30.5 }"
mosquitto_pub -p 9883 -h 192.168.1.1 -u public -P password -t "variables/v1/set/<BOOL_VARIABLE_NAME>" -m "{ \"value\": false }"

# Command variables

Variables can be used to execute commands.

For example electrolysers can be started when start-els variable set to true.

  1. Define variable
set variables variable start-els type bool
commit
save
  1. Activate ELs plugin
set rule-engine plugin electrolyser-v21 enabled true
commit
save
exit
  1. Configure devices in rule engine
set rule-engine devices electrolyser-v21 <EL_1_DEVICE_ID> enabled true
set rule-engine devices electrolyser-v21 <EL_2_DEVICE_ID> enabled true
  1. Add rule to activate ELs when variable set to true
set rule-engine rule 1-start-els condition 1 variable start-els compare-operator eq
set rule-engine rule 1-start-els condition 1 variable start-els value "true"
set rule-engine rule 1-start-els action 1 start-electrolyser-v21 device <EL_1_DEVICE_ID>
set rule-engine rule 1-start-els action 2 start-electrolyser-v21 device <EL_2_DEVICE_ID>
set rule-engine rule 1-start-els action 3 set-variable start-els value "false"

Resulting configuration tree will be:

{
    variables {
        variable start-els {
            type bool
        }
    }
    rule-engine {
        rule 1-start-els {
            condition 1 {
                variable run-all-els {
                    compare-operator eq
                    value "true"
                }
            }
            action 1 {
                start-electrolyser-v21 {
                    device 8BF3C483286E348B0045E9F15EBCB67EFC268B4A
                }
            }
            action 2 {
                start-electrolyser-v21 {
                    device A9CCB145472FF183535E93427D007B6844E369BB
                }
            }
            action 3 {
                set-variable start-els {
                    value "false"
                }
            }
        }
    }
}

# Using variable value in condition

For example electrolysers can be stopped once system reaches target pressure.

  1. Define variable
set variables variable target-pressure type float
commit
save
  1. Activate ELs and dryers plugins
set rule-engine plugin electrolyser-v21 enabled true
set rule-engine plugin dryer enabled true
commit
save
exit
  1. Configure devices in rule engine
set rule-engine devices electrolyser-v21 <EL_DEVICE_ID> enabled true
set rule-engine devices dryer <DRYER_DEVICE_ID> enabled true
  1. Add rule to stop EL when system's target pressure reaches value defined in target-pressure variable
set rule-engine rule 1-stop-el condition 1-dryer-pressure dryer <DRYER_DEVICE_ID> if "device.output_pressure_avg(60) > var(\"target-pressure\")"
set rule-engine rule 1-stop-el action 1 stop-electrolyser-v21 device <EL_DEVICE_ID>

Resulting configuration tree will be:

{
    variables {
        variable target-pressure {
            type float
        }
    }
    rule-engine {
        rule 1-stop-el {
            condition 1-dryer-pressure {
                dryer 2B2E42A378DB505BDBD0A9F41F2D4603D30A51E5 {
                    if "device.output_pressure_avg(60) > var(\"target-pressure\")"
                }
            }
            action 1 {
                stop-electrolyser-v21 {
                    device 8BF3C483286E348B0045E9F15EBCB67EFC268B4A
                }
            }
        }
    }
}