NAV Navbar
python java
  • ◀ Back to Doc's Home
  • MQTT Smart Meter API for High Resolution Data
  • Connecting and Publishing
  • Message Format
  • MQTT Smart Meter API for High Resolution Data

    This integration is a way for third parties to integrate their smart meter data in order to enable the services provided by the Voltaware Platform.

    This API is an MQTT message bus allowing devices to publish eletricity readings in an specific format. The number of services and.

    The messages must be exactly in the described format otherwise they will be discarded.

    The messages must be published to the exact topic otherwise they will be discarded.

    You can have multiple concurrent publishers.

    Connecting and Publishing

    Example of how to connect

    Dependencies [paho-mqtt](https://www.eclipse.org/paho) Version: 1.5.0
    
    import paho.mqtt.client as paho
    import ssl
    
    broker = "test-partner-broker.voltaware.com"
    port = 8883
    client_id = "python-example-client"
    username = "<your username>"
    passwd = "<your password>"
    message_topic = "<your topic>"
    message_json = "<json event format>"
    
    def on_publish(client,userdata,result):
        if result == 1:
            print("Data published!")
        else:
            print("Message not acknowledged!")
    
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected!")
            client1.publish(message_topic, message_json, 2)
        else:
            print("Connection failed!")
    
    client1= paho.Client(client_id)
    client1.on_publish = on_publish
    client1.on_connect = on_connect
    client1.tls_set_context(ssl.create_default_context())
    client1.username_pw_set(username, passwd)
    client1.connect(broker, port)
    
    try:
        rc = 0
        while rc == 0:
            rc = client1.loop()
        print("rc: " + str(rc))
    except KeyboardInterrupt:
        print("Exit")
    
    Dependencies [mqtt-client](https://mvnrepository.com/artifact/org.eclipse.paho/org.eclipse.paho.client.mqttv3/1.2.2)
    
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    
    import javax.net.ssl.SSLContext;
    
    import org.eclipse.paho.client.mqttv3.MqttClient;
    import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
    import org.eclipse.paho.client.mqttv3.MqttException;
    import org.eclipse.paho.client.mqttv3.MqttMessage;
    import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class PublisherExample {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(PublisherExample.class);
    
        public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, MqttException {
    
            final String broker = "ssl://test-partner-broker.voltaware.com:8883";
            final String clientId = "java-example-client";
            final String userName = "<your username>";
            final String passwd = "<your password>";
            final String topic = "<your topic>";
            final String messageJson = "<json event format>";
    
            final MqttClient mqttClient = new MqttClient(broker, clientId, new MemoryPersistence());
    
            try {
    
                final MqttConnectOptions mqttConnectionOptions = new MqttConnectOptions();
                mqttConnectionOptions.setKeepAliveInterval(30);
                mqttConnectionOptions.setCleanSession(true);
                mqttConnectionOptions.setUserName(userName);
                mqttConnectionOptions.setPassword(passwd.toCharArray());
                mqttConnectionOptions.setConnectionTimeout(5);
                final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
                sslContext.init(null, null, null);
                mqttConnectionOptions.setSocketFactory(sslContext.getSocketFactory());
                mqttClient.connect(mqttConnectionOptions);
    
                final MqttMessage mqttMessage = new MqttMessage(messageJson.getBytes());
                mqttMessage.setQos(2);
                mqttClient.publish(topic, mqttMessage);
                LOGGER.info("Message sent");
    
            } catch (final MqttException e) {
                LOGGER.error("Error to send to the topic!", e);
            } finally {
                mqttClient.disconnect();
                mqttClient.close();
            }
    
        }
    
    }
    

    You need these parameters to connect to the RabbitMQ Voltaware broker.

    See code samples.

    Message Format

    Message Format

    {
      "deviceId": "f1914578-6009-49de-9d06-3aada44e8e3e",
      "timestampUtc": "2019-07-10T16:04:48Z",
      "electricReadings": [
        {
          "activePowerW": 1234,
          "phaseShift": 180,
          "powerFactor": 0.1,
          "timestampUtc": "2019-07-10T16:04:48Z"
        },
        {
          "activePowerW": 1234,
          "phaseShift": 240,
          "powerFactor": -0.1,
          "timestampUtc": "2019-07-10T16:04:38Z"
        },
        ...
      ],
      "energyReading": {
        "totalConsumptionWh": 10883100,
        "readingTimeUtc": "2019-07-10T16:04:48Z"
      },
      "electricityCharges": {
        "billingMode": "PRE_PAYMENT|CREDIT",
        "tariff": {
          "activeTariffPriceMill": 16024,
          "nextTariffPresent": 0,
          "nextTariffPriceMill": 0,
          "nextTariffStartTime": 0
        },
        "cost": [
          {
            "period": "DAY",
            "amountMill": 29,
            "energyWh": 1
          },
          {
            "period": "WEEK",
            "amountMill": 302391,
            "energyWh": 10347
          },
          {
            "period": "MONTH",
            "amountMill": 1137970,
            "energyWh": 39004
          }
        ]
      }
    }
    

    These are the minimun set of parameters that must be provided:

    Attribute Description
    deviceId Unique identifier for your device
    timestampUtc ISO 8601 UTC Timestamp
    electricReadings Event list with Active Power in Watts, Power Factor and Phase shift
    energyReading Contains total consumption in watt/hour
    electricityCharges Billing information about packet
    billingMode Type of payment. PRE_PAYMENT or CREDIT
    activeTariffPriceMill Tariff price used to calculate cost in millicents

    Minimun set sample message

    {
      "deviceId": "f1914578-6009-49de-9d06-3aada44e8e3e",
      "timestampUtc": "2019-07-10T16:04:48Z",
      "electricReadings": [
        {
          "activePowerW": 1234,
          "powerFactor": 180,
          "timestampUtc": "2019-07-10T16:04:48Z"
        },
        {
          "activePowerW": 1234,
          "powerFactor": 180,
          "timestampUtc": "2019-07-10T16:04:38Z"
        }
      ],
      "energyReading": {
        "totalConsumptionWh": 10883100,
        "readingTimeUtc": "2019-07-10T16:04:48Z"
      },
      "electricityCharges": {
        "billingMode": "PRE_PAYMENT",
        "tariff": {
          "activeTariffPriceMill": 16024
        }
      }
    }