Qt Cloud Messaging API Available for Embedded Systems

Challenges with cloud messaging for embedded devices has inspired the Kaltiot & SnowGrains teams to create a cross-platform Qt API which enables easy push messaging from and to embedded devices. The API is called the Qt Cloud Messaging API and it is built with flexibility and extensibility in mind.

We have decided to target other Qt areas, too, and make the API easily extensible to any service provider instead of being for embedded only. This enables developers to use the same API for both mobile and desktop development.

There is a vast number of push messaging providers for mobile and web development nowadays. Yet in industries like automation, automotive, robotics and for other embedded devices there has not really been any providers for this type of service. The need is increasing at a rapid pace as embedded and IoT devices are pushed more and more messages by the device owners, car service providers, telemetry, cloud and many others.

The Qt Cloud Messaging API is simple to adopt and take into use.

Let’s take a sneak peek at how to utilize the API for embedded systems and Android mobile platforms. We’ve integrated the Kaltiot Smart IoT SDK and the Firebase C++ SDK into the backend, and with the Qt Cloud Messaging API we can easily create e.g. a chat application on top of it.

Kaltiot Smart IoT is a service for scalable and secure messaging between devices and services. It provides bi-directional and always online, yet battery optimized communication. The client SDK is designed to work even in constrained low-end devices in poor wireless network conditions. Kaltiot Smart IOT is available for Linux32, Linux64, Raspberry Pi, Mac, Android.

How to use the Qt Cloud Messaging API for embedded devices:

 First, clone the qtcloudmessaging repository: git clone https://codereview.qt-project.org/qt/qtcloudmessaging

Service provider

Pre-requirements:

  1. Get the Kaltiot SDK to your embedded device from (e.g. Linux or Raspberry Pi SDK) https://console.torqhub.io
  2. Get the API key for sending channels or/and creating server-side implementation
  3. Add following line to your application .pro file: QT += cloudmessagingembeddedkaltiot
  4. Define KALTIOT_SDK path to your application.pro
  5. Add following includes to main.cpp
#include <QtCloudMessaging>
#include <QtCloudMessagingEmbeddedKaltiot>
  1. Add following QtCloudMessaging configs to main.cpp
// Instantiate CloudMessaging library
QCloudMessaging *pushServices = new QCloudMessaging();

// Add provider for Kaltiot Embedded systems QCloudMessagingEmbeddedKaltiotProvider *kaltiotPushService = new QCloudMessagingEmbeddedKaltiotProvider()

// Provider based init parameters are given with QVariantMap QVariantMap provider_params;

provider_params["API_KEY"] = "Your API key from the Kaltiot console for server communication"; // Creating name for provider which can be used cross your app.

pushServices->registerProvider("KaltiotService", kaltiotPushService, provider_params);

QVariantMap client_params;

client_params["address"] = "IOTSensor1"; client_params["version"] = "1.0"; client_params["customer_id"] = "Kaltiot";

// Creating default channels to listen QVariantList channels; channels.append("weather_broadcast_channel"); client_params["channels"] = channels;

// Connect IoT sensor client to system pushServices->connectClient("KaltiotService", "IOTSensor1", client_params);

//! Automatically subcribe to listen one more channel e.g. WindInfo. pushServices->subsribeToChannel("WindInfo", " KaltiotService ", " IOTSensor1");

  1. Provide context of cloud messaging to QML in main.cpp
//! Provide context to QML

engine.rootContext()->setContextProperty(“pushServices”, pushServices);

QML Part:

in main.qml catch the messages coming from the Kaltiot service.

  1. Receiving messages
Connections{
target : pushServices
onMessageReceived:{

//! Message is received as string and needs parsing to JSON console.log(message) } }

  1. Sending messages:
//! Sending a broadcast message e.g. from the weather station:
//! Message structure for embedded devices is easy:
//! define payload json:
//! {
//!   “payload_type”:”STRING”,
//!   “payload”: encodeURI(JSON.stringify(payload))
//! }
//! Payload is your application specific. E.g:
var payload =
{
msgType:”NEW_WEATHER_INFO”,
city: “Oulu”,
forecast: “full sunlight for whole next week”
}

//! Capsulate payload to message and send it via QtCloudMessaging API: var payload_array = [{“payload_type”:”STRING”,”payload”: encodeURI(JSON.stringify(payload))}]

pushServices.sendMessage(JSON.stringify(data), ”KaltiotService”, ”IOTSensor1”, ””, ”weather_broadcast_channel”);

Using the Qt Cloud Messaging API for Android / iOS mobile development with Google Firebase service provider

Pre-requirements:

  1. Create new project into Google Firebase console: https://firebase.google.com/
  2. Download Google Firebase C++ SDK  and add the Firebase configuration file for Android Gradle:
DISTFILES += android/google-services.json
  1. Add following line to your application .pro file:
QT += cloudmessagingfirebase

Define Google firebase path into your application.pro file with

GOOGLE_FIREBASE_SDK = &lt;Path_to_Firebase_SDK&gt;
  1. Add following includes to your main.cpp
#include <QtCloudMessaging>
#include <QtCloudMessagingFirebase>
  1. Add following QtCloudMessaging setup to main.cpp:
// Instantiate CloudMessaging library
QCloudMessaging *pushServices = new QCloudMessaging();
QCloudMessagingFirebaseProvider *firebaseService = new QCloudMessagingFirebaseProvider();
QVariantMap provider_params;

// Server API key is not recommended to store inside to the application code due security reasons. // But if you do, make sure it is inside compiled C file or if you are doing a server side implementation with C++ & Qt. // SERVER_API_KEY Is needed to be able to send topic messages from the client without Firebase application server.

provider_params["SERVER_API_KEY"] = "Get your SERVER API KEY from the google firebase console";

// Registering the Google firebase service component. pushServices->registerProvider("GoogleFireBase", firebaseService, provider_params);

/*! Connected client is needed for mobile device. \param Service name "GoogleFireBase" \param Client identifier name to be used inside the application \param Parameters for the client. No params for firebase client. */ pushServices->connectClient("GoogleFireBase", "MobileClient", QVariantMap());

//! Automatically subscribe to listen one example topic pushServices->subsribeToChannel("ChatRoom", "GoogleFireBase", "MobileClient");

  1. Provide context of cloud messaging to QML in main.cpp
//! Provide context to QML
engine.rootContext()->setContextProperty(“pushServices”, pushServices);

QML Part:

in main.qml catch the messages coming from the google firebase

  1. Receiving messages
Connections{
target : pushServices
onMessageReceived:{
//! Message is received as string and needs parsing to JSON
console.log(message)
}
}
  1. Sending messages:
//! For firebase, message structure needs to have data as whole message.
//! Notifications are shown in the Android/iOS notification center.
function sendMessage(notification_titile, notification_msg, msg){
var data = { “data”:{
“message”: {“text”:msg } },
“notification” : {
“body” : notification_msg,
“title” : notification_titile
}
}

//! Give data and service provider identifier as well as client name defined in the C++ code. pushServices.sendMessage(JSON.stringify(data),”GoogleFireBase”,”MobileClient”,””,”ChatRoom”); }

You can play around with the Qt Cloud Messaging API + firebase from the sample

https://github.com/snowgrains/qtcloudmessaging-examples

The Qt Cloud Messaging API provides cross-platform API to enable us to include new providers and still keep the development API as same.

The Qt Cloud Messaging API has been developed to be easily extensible to any service provider. Take a sneak peek at ongoing development from the https://codereview.qt-project.org/qt/qtcloudmessaging and contribute your own backend service to the community.

Kaltiot has been developing and hosting IoT cloud services since 2008. Trusted e.g. by Microsoft, Kaltiot passes through 10M messages every hour. (www.kaltiot.com)

SnowGrains provides Full Stack Software Solutions On The Rocks.(www.snowgrains.com)

Ari Salmi is multi-technology developer and Qt enthusiast.


Blog Topics:

Comments