Blog

How to publish an Event to the Wia MQTT API

Hi All,
Today I'm going to show you how to publish an Event to the Wia MQTT API. Before we start, here's a few of the main details you will need:

  • MQTT API Endpoint:
    • Unsecure: mqtt://api.wia.io
    • Secure: mqtts://api.wia.io
  • MQTT API Port
    • Unsecure: 1883
    • Secure: 8883
  • Device ID: This can be found in Device > Settings. Should begin with dev_
  • Username: Use your device secret key. This can be found in Device > Settings. Should begin with d_sk_.
  • Password: A blank space character

Now that we have all the details to get authenticated, the message should contain the following objects:

  • Topic: devices/DEVICE_ID/events/EVENT_NAME
    • DEVICE_ID is the same identifier mentioned above.
    • EVENT_NAME is the name of your event e.g. 'motionDetected'. The name of the event must be in both the topic and the payload.
  • Payload (may need to convert to a string):
    {
      "name": "motionDetected":,
      "data": "true"
    }
    

Here's an example using the mqtt Node.js module:

var options = {
  port: 8883,
  username: "Username or device secret key",
  password: " ",
};

var mqtt = require('mqtt');
var client  = mqtt.connect('mqtts://api.wia.io', options);

client.on('connect', function () {
  client.publish('devices/DEVICE_ID/events/EVENT_NAME', 
    '{"name":"motionDetected","data":"true"}'
  );
});

That's it! You should see the Event coming through on our Dashboard.

If you need any help just drop us an email to support@wia.io or tweet us at @wiaio

CL


Recommended article

Ready to get started?
Talk to our sales team or setup an account.