Links

MKR WiFi 1010

Getting Started

Before getting started, make sure that required libraries are installed by visiting the setup page

Libraries

Include the required libraries and define objects on top of the page.
#include <QubitroMqttClient.h>
#include <WiFiNINA.h>
WiFiClient wifiClient;
QubitroMqttClient mqttClient(wifiClient);

Variables

Define the required variables.
char deviceID[] = "PASTE_DEVICE_ID_HERE";
char deviceToken[] = "PASTE_DEVICE_TOKEN_HERE";
char host[] = "_IP_";
int port = 1883;

Data Structure

Qubitro currently has support for key-value paired JSON objects as a data type. Nested values are not supported yet.
{
"Key1" : "Value1",
"Key2" : true,
"Key3" : 90
...
}
An example : "{\"Temp\":33}"

Setup method

  • Set client id for identification
  • Set device id and token for authentication
  • Connect to the Qubitro broker
  • Subscribe to the topic
  • Set onMessage method to listen subscribed topic
void setup()
{
mqttClient.setId(deviceID);
mqttClient.setDeviceIdToken(deviceID, deviceToken);
Serial.println("Connecting to Qubitro...");
if (!mqttClient.connect(host, port))
{
Serial.println("Connection failed! Error code = ");
Serial.println(mqttClient.connectError());
Serial.println("Visit docs.qubitro.com or create a new issue on github.com/qubitro");
while (1);
}
Serial.println("Connected to the Qubitro !");
mqttClient.subscribe(deviceID);
mqttClient.onMessage(receivedMessage);
}

loop method

  • Publish data to the subscribed topic
void loop()
{
mqttClient.poll();
mqttClient.beginMessage(deviceID);
mqttClient.print("{\"Temp\":33}");
mqttClient.endMessage();
delay(2000); //wait 2 seconds
}

receivedMessage method

  • Print whenever message received
void receivedMessage(int messageSize)
{
Serial.print("New message received:");
while (mqttClient.available())
{
Serial.print((char)mqttClient.read());
}
Serial.println();
}

Example

The following example :
  1. 1.
    Connects to the broker
  2. 2.
    Subscribes to the topic (the device itself in this example)
  3. 3.
    Publishes data in a loop with a delay of 2 seconds.
  4. 4.
    Prints message and subscribed topic (the device itself in this example)
Example output:
Connecting to WiFi...
Connected to the WiFi
Connecting to Qubitro...
Connected to the Qubitro !
New message received:{"Temp":33}
New message received:{"Temp":33}
New message received:{"Temp":33}

Support and Feedback

If you have further questions or suggestions, feel free to join
👇
-> Qubitro Community Discord via this invitation link.