Before getting started, make sure that required libraries are installed by visiting the setup page
Include the required libraries and define objects on top of the page.
#include <QubitroMqttClient.h>#include <WiFi101.h>WiFiClient wifiClient;QubitroMqttClient mqttClient(wifiClient);
Define the required variables.
char deviceID[] = "PASTE_DEVICE_ID_HERE";char deviceToken[] = "PASTE_DEVICE_TOKEN_HERE";char host[] = "_IP_";int port = 1883;
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}"
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);}
Publish data to the subscribed topic
void loop(){mqttClient.poll();mqttClient.beginMessage(deviceID);mqttClient.print("{\"Temp\":33}");mqttClient.endMessage();delay(2000); //wait 2 seconds}
Print whenever message received
void receivedMessage(int messageSize){Serial.print("New message received:");while (mqttClient.available()){Serial.print((char)mqttClient.read());}Serial.println();}
The following example :
Connects to the broker
Subscribes to the topic (the device itself in this example)
Publishes data in a loop with a delay of 2 seconds.
Prints message and subscribed topic (the device itself in this example)
Example output:
Connecting to WiFi...Connected to the WiFiConnecting to Qubitro...Connected to the Qubitro !New message received:{"Temp":33}New message received:{"Temp":33}New message received:{"Temp":33}
Visit our GitHub page for libraries or documentation issues and examples.
You may ask any question through the chatbot or share a feedback by visiting the Qubitro Feedback platform.