This document applies to device makers that would like to connect their device to the Smartenit Cloud.
In order to get access to the MQTT Broker, a valid client_id, client_secret, and device_id are required. The first two fields are normally created or assigned per type of device from a Smartenit application. This will allow the cloud to limit the usage of the broker or to distinguish between devices using the API.
The main steps to get a connection to the broker for real communication are:
- Register a device
- Discover a device
- Link a device to an account
- Request access to broker
- Connect to the broker
- Subscribe to allowed topics
- Publish messages
Register a device
Before the client is able to register a new device, it is important to get a valid access token from the OAuth 2.0 endpoint in the API. This step will provide a token with enough scope for registering devices using a unique hwId as an identifier, so the cloud can return the same ID consistently, very important for backup and restore and for avoiding duplicated devices in the database.
The following command can be used to get an access token
Now using the token the device can be registered:
It's important to include the device interfaces information when registering, this will allow users to connect with the devices using a local network.
Once the device is registered in the API it can be discovered by a client application.
Notice that at this point the accountId attribute is set to null, this means that the device is not linked to any account and will not be able to connect real-time. In order to link this device to an account, a user using a client application must discover the device and link it to its account.
Discover a device
Now, the device is registered and can be discovered by a user. The basic strategy will use the external IP to detect that the user and the device are connected to the same local network. As it is shown above, the cloud will respond with the deviceId acquired and the accountId associated to this device. As in the example, it will return null if it is not associated yet to any account. In this scenario, the device can not use real-time communication until it is fully associated. This is intended for limiting the number of devices creating traffic in the broker, as they will send information that will not be used at all.
Link a device to an account
In order to start using a device in real time, the device must be linked to an account. In order to link the account, clients can use the device discovery service to list devices in their same local network.
This will return a list of devices
Once you have this list you can link one of the devices of the list to a specific account. When called, the link service will use the access_token data to link the device to a specific accountId.
This will reurn the linked device
Request access to broker
The device must keep trying to register until an accountId is returned. This will indicate that the device has been linked and can request access to the broker for providing real-time communication. This step requires a new access token specifically for this device, this can be done providing the deviceId obtained while registering the device.