Publish built-in sensors’ Wio Terminal data to Qubitro IoT platform through MQTT protocol.
Step 1. Create Qubitro Project
Signup or login to Qubitro Portal, then create a new project. Click the New Project button, add the project name & description, then click Create Project button.
Then create a new device inside your project. Choose your device connectivity method, in this case, MQTT. Enter device details from name, description, brand, model, and location.
After that, you’ll get your credentials (host, port, username, password & clientId) to connect to the Qubitro MQTT broker. Please note these values because we’ll use them on our code later. You can check these values on the device Settings menu. Your username & clientId are your Qubitro Device ID, and your password is your Qubitro Device Token.
Step 2. Code the Wio Terminal
Connect Wio Terminal to the computer on bootloader mode. To do that, connect Wio using USB type-C, then slide the switch twice. Next, open the Arduino IDE, and select the appropriate board and port. Then create a WioQubitro.ino and credential.h file with the following contents:
**credential.h**
```h
#define WIFI_SSID "YOUR_WIFI_SSID"
#define WIFI_PASS "YOUR_WIFI_PASSWORD"
#define MQTT_BROKER "broker.qubitro.com"
#define MQTT_USER "YOUR_QUBITRO_DEVICE_ID"
#define MQTT_PASS "YOUR_QUBITRO_DEVICE_TOKEN"
#define MQTT_CLID "YOUR_QUBITRO_DEVICE_ID"
#define MQTT_PORT 1883
#define MQTT_TOPIC "YOUR_QUBITRO_DEVICE_ID"
```**WioQubitro.ino**
```cpp
// import libraries
#include "credential.h"
#include "TFT_eSPI.h"
#include "LIS3DHTR.h"
#include "rpcWiFi.h"
#include <ArduinoMqttClient.h>// objects
TFT_eSPI tft;
LIS3DHTR<TwoWire> lis;
int button = 0;WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASS;void setup() {
// TFT LCD setup
tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);// sensors setup
lis.begin(Wire1);
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);
lis.setFullScaleRange(LIS3DHTR_RANGE_2G);
pinMode(WIO_LIGHT, INPUT);
pinMode(WIO_MIC, INPUT);
pinMode(WIO_5S_PRESS, INPUT_PULLUP);// connect WiFi
WiFi.mode(WIFI_STA);
WiFi.disconnect();
tft.setTextSize(2);
tft.drawString("Processing...", 20, 20);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
WiFi.begin(ssid, password);
}
tft.setTextSize(2);
tft.drawString("WiFi connected!", 20, 50);
// connect to Qubitro mqtt broker
mqttClient.setId(MQTT_CLID);
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
if (!mqttClient.connect(MQTT_BROKER, MQTT_PORT)){
tft.setTextSize(2);
tft.drawString("Failed connect to Qubitro", 20, 80);
}
tft.setTextSize(2);
tft.drawString("Qubitro connected!", 20, 80);delay(3000);
tft.fillScreen(TFT_BLACK);
}void loop() {
int light = analogRead(WIO_LIGHT);
int mic = analogRead(WIO_MIC);
float x, y, z;
x = lis.getAccelerationX();
y = lis.getAccelerationY();
z = lis.getAccelerationZ();// event: if button pressed, publish data to Qubitro
if (digitalRead(WIO_5S_PRESS) == LOW) {
button += 1;// publish data
mqttClient.poll();
mqttClient.beginMessage(MQTT_TOPIC);// send JSON string
mqttClient.print("{");
mqttClient.print("\"accx\":");
mqttClient.print(x);
mqttClient.print(",");
mqttClient.print("\"accy\":");
mqttClient.print(y);
mqttClient.print(",");
mqttClient.print("\"accz\":");
mqttClient.print(z);
mqttClient.print(",");
mqttClient.print("\"light\":");
mqttClient.print(light);
mqttClient.print(",");
mqttClient.print("\"mic\":");
mqttClient.print(mic);
mqttClient.print(",");
mqttClient.print("\"button\":");
mqttClient.print(button);
mqttClient.print("}");
mqttClient.endMessage();
}// show sensor values on TFT LCD
tft.setTextSize(2);
tft.drawString("Acc X:", 20, 20);
tft.drawString("Acc Y:", 20, 95);
tft.drawString("Acc Z:", 20, 165);
tft.drawString("Light:", 180, 20);
tft.drawString("Microphone:", 180, 95);
tft.drawString("Button:", 180, 165);
tft.setTextSize(4);
tft.drawString(String(x), 20, 45);
tft.drawString(String(y), 20, 120);
tft.drawString(String(z), 20, 190);
tft.drawString(String(light), 180, 45);
tft.drawString(String(mic), 180, 120);
tft.drawString(String(button), 180, 190);
delay(100);
}
```
Verify and upload the sketch. Once done, Wio Terminal will connect to WiFi, connect to Qubitro MQTT broker, and then display the built-in sensors’ values on its TFT LCD screen. In addition, Wio Terminal will publish data to the Qubitro MQTT broker if the button is pressed.
Results
On the Qubitro device Data tab, you’ll see the data stream from your Wio Terminal. In addition, you can create data visualization on the Analytics tab or create your centralized Dashboard in the Monitoring section.