Ari Salmi at SnowGrains

Qt Cloud Messaging API Available for Embedded Systems

Published Tuesday January 2nd, 2018
6 Comments on Qt Cloud Messaging API Available for Embedded Systems
Posted in Cloud, cross-platform, Dev Loop, Embedded | Tags: , , , , ,

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 = <Path_to_Firebase_SDK>
  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.

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Cloud, cross-platform, Dev Loop, Embedded | Tags: , , , , ,

6 comments

Felix says:

Very awesome. I’ve been waiting for something like this to come. Good work! I know that firebase works for mobile only (sadly), but what about kaltiot? Can this be used for dekstop applications, too? If yes, this is going to be a groundbreaking addition to Qt.

2018 says:

Where I currently work, we use Windows as our backend server. This is also true of the last place I worked. Both places had remote Qt clients talking to some kind of C# or maybe they were webserver backends ( I was not involved with the communication) So wondering how this new module plays with Window servers running C#.

Also a lot of shops use nodejs for backend, does this new module play nicely with that. What are its advantages then just doing your own Rest API’s. Is the connection persistent to the server ? Any info you can provide here is appreciated.

Ari Salmi Ari Salmi says:

Hi Felix.
Thank you for the positive feedback 🙂

Currently Kaltiot is providing the SDK for desktop Linux desktop, Raspberry Pi, Mac desktop, Android and NodeJS environments. Getting the SDK for other environments like Windows would not be a problem if there’s a need for it.

-Ari

Foobar says:

Qt used to care for making easy to use APIs, but this is just horrible. I hope this is a exception and not the standard we can expect from Qt going forward. You really dropped the ball here!

I will stick with the C++ APIs for firebase for the time being, thanks.

Edward says:

What I can see here is that this is wrapper which uses firebase c++ API and hides that complexity into similar interfaces for all backends. Which is actually quite nice. Getting firebase to iOS and Android apps is now faster as developer (time to market) does not need to study firebase C++ SDK as well.

If you have integrated firebase c++ SDK with your Qt app you know it needs specific implementations as there’s only Cocos2d example in Google, which is close enough to c++.

Definitely configuration could be simpler and guess it will improve, but usage of this API is quite straightforward.

ivan says:

Code blocks are messed up in the article. In fact, only first one is fine.
Would you be so kind to fix it.

Commenting closed.

Get started today with Qt Download now