In this tutorial, we're gonna learn how to publish sensor readings from XDK BOSCH to Qubitro IoT platform through MQTT protocol.
Qubitro is the fastest way to build IoT applications with predictable pricing, developer-friendly features, and scalability you’ll love. You can connect your hardware to Qubitro, collect the data and create data visualization on your own dashboard. For more information about Qubitro you can check on its official site: qubitro.com.
We're gonna use the BOSCH XDK, an all in one IoT solution from BOSCH based on 32-Bit microcontroller (ARM Cortex M3) which has Bluetooth 4.0 low energy, Wireless Local Area Network, and Internal Lithium-Ion battery. It also has several built-in sensors like accelerometer, gyroscope, magnetometer, temperature sensor, air pressure, humidity and light sensor. For more information about BOSCH XDK you can check on its official site: developer.bosch.com.
Step 1. Create Qubitro ProjectSignup or login to Qubitro Portal then create a new project. Click New Project button, add 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. Then 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 Qubitro MQTT broker. Please note these values because we'll use them on our code later. You can check these values on device Settings menu. Your username & clientId are your Qubitro Device ID and your password is your Qubitro Device Token.
Open the XDK Workbench then connect our XDK on bootloader mode. To do that, connect our XDK to the computer using micro USB cable while pressing button 1 (button with a dot on top of it). Then switch the XDK on. The status of our XDK will be shown on XDK Workbench.
On application.mita editor, write these lines:
package main;
import platforms.xdk110;
// resource for accelerometer
setup accelerometer {
bandwidth = BW_500Hz;
range = Range_8G;
}
// resource for gyroscope
setup Gyroscope_BMI160 {
bandwidth = BW_10_7Hz;
range = Range_250s;
}
// resource for light sensor
setup light {
manual_mode = false;
}
// resource for environment sensor
setup environment {
power_mode = Normal;
standby_time = 1000;
temperature_oversampling = OVERSAMPLE_1X;
}
// resource for WiFi connectivity
setup wifi : WLAN {
ssid = 'YOUR_WIFI_NAME';
authentication = Personal(psk = 'YOUR_WIFI_PASSWORD');
}
// resource for Qubitro MQTT Broker
setup qubitroBroker : MQTT {
transport = wifi;
cleanSession = true;
url = 'mqtt://broker.qubitro.com:1883';
clientId = 'YOUR_QUBITRO_DEVICE_ID';
authentication = Login('YOUR_QUBITRO_DEVICE_ID', 'YOUR_QUBITRO_DEVICE_TOKEN');
var qubitroTopik = topic('YOUR_QUBITRO_DEVICE_ID', 0);
}
// create event: every 5 sec read sensors & send to Qubitro
every 5 seconds {
// read sensors
var accx = accelerometer.x_axis.read();
var accy = accelerometer.y_axis.read();
var accz = accelerometer.z_axis.read();
var gyx = gyroscope.x_axis.read();
var gyy = gyroscope.y_axis.read();
var gyz = gyroscope.z_axis.read();
var light = light.intensity.read() / 1000.0;
var temperature = environment.temperature.read() / 1000.0;
temperature = temperature - 6.5975;
var humidity = environment.humidity.read() / 1000.0;
var pressure = environment.pressure.read() / 1000.0;
// create JSON string
var json = `{
"accx": ${accx}, "accy": ${accy}, "accz": ${accz},
"gyx": ${gyx}, "gyy": ${gyy}, "gyz": ${gyz},
"light": ${light}, "temperature": ${temperature},
"humidity": ${humidity}, "pressure": ${pressure}
}`;
// publish data to qubitro
qubitroBroker.qubitroTopik.write(json);
println(json);
}
Build the project then flash the code. Your XDK's activities can be checked on XDK Workbench console. Now every 5 seconds, the XDK will publish the data to Qubitro.
ResultsOn Qubitro device Data tab you'll see the data stream from your XDK. You can create data visualization on Analytics tab or create your own centralized Dashboard on Monitoring section.
Comments
Please log in or sign up to comment.