IoT Lecture #2: proceeding data from RasPi to a cloud

IoT Lecture #2: proceeding data from RasPi to a cloud

Last week we created an embedded solution with an Arduino kit that measures temperature and sends temperature values to a serial port - This week let’s create a software for RasPi that reads data from serial port and ingests it to Predix server. If you still have not setup your Predix server, you can read this article on how to do that -

So, what do we need to create this kind of solution? There are many instruments that can help us, like Python, Java, C/C++ and Qt. As for me, the best framework to do so is Qt since it’s crossplatform having super-high performance, but it’s only my personal opinion. And to communicate with Predix Timeseries service we will use QPredix SDK So let’s do that!

First, let’s create a new project. As we won’t use any display, let’s make our app console only.

New project in QT creator

Then let’s name it.

Naming in QT creator

Now let’s choose a kit.

Kit selection in QT creator

And always use Git or svn to keep soursecode versioned.

Git in QT creator

Good. This is what you can see now.

New project in QT creator

The Template project ші created so let’s add the required functionality. Our application should read data from a serial port continuously and sends data to Predix server, so my proposition is to run a timer, that fires every 3 seconds, reads 10 samples, finds average temperature and sends it to cloud.

Class QTimer implements the timer functionality, but in order to be able to use its signal timeout() that fires in due time, we need a class that implements slots. So let’s create our class named Handler.

Class creation in QT creator


C++ in QT creator

To use signals slots, we should inherit our class from basic class QObject.

Qobject in QT creator

QT creator window

To be able to work with serialport, websocket and with network you should go to your *.pro file and make your one step as it is indicated hereinafter:

QT += core serialport network websockets

And to start using QPredix sdk add

include (./QPredix/QPredix.pri)


To *.pro file as the second line.

Now we can start writing code.


#ifndef HANDLER_H
#define HANDLER_H
#include <QObject>
#include <QList>
class QTimer;
class QSerialPort;
class QUaa;
class QTimeSeries;
class Handler : public QObject
  explicit Handler(QObject *parent = 0);

private slots:
  void readyRead();
  void startReading();

  void sendDataToCloud();
  void initPredix();

  QList <double>mDataList;
  QTimer *mTimer;
  QSerialPort *mSerialPort;

  QUaa *mUaa;
  QTimeSeries *mTimeSeries;



#endif // HANDLER_H

#include "handler.h"

#include <QTimer>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>

#include "QPredix/qpredix.h"

#include <QJsonDocument>

Handler::Handler(QObject *parent) : QObject(parent)

  //get all connected devices to USB
  QList <QSerialPortInfo> lList = QSerialPortInfo::availablePorts();
  QSerialPortInfo lInfo;

  //find if there is connected arduino device
  foreach (QSerialPortInfo info, lList) {
  if (info.manufacturer().contains("Arduino")) {
    // first of all intialize setial port to be able read data
    lInfo = info;
    mSerialPort = new QSerialPort;
    //connect serialport to readyread to read data received from serial port
    connect(mSerialPort, &QSerialPort::readyRead, this, &Handler::readyRead);

    // start timer that will start reading each 2 seconds
    mTimer = new QTimer(this);
    connect(mTimer, &QTimer::timeout, this, &Handler::startReading);

    //leave constructor after initialisation

  //stop and delete mTimer to clear memmory
  if (mTimer->isActive()) {

  delete mTimer;

  //close and delete mSerialPort to clear memmory
  if (mSerialPort->isOpen()) {

  delete mSerialPort;

void Handler::readyRead()
  // read received data from serial port
  QByteArray lArray = mSerialPort->readAll();
  //all temperature values are devided with new line \n, so let's separate all temperatures by this symbol
  QStringList lList = QString(lArray).split("\n");

  //delete first and last symbol as it could be corrupted
  if (lList.count() > 1) {
  } else {

  //convert all values to data and store in global list to be able use it in different method
  foreach (QString value, lList) {

  // check if we have enough values to send to cloud
  if (mDataList.count() > 10) {

void Handler::startReading()
  //clear global list with vlaues and open port to start reading data
  if (!mSerialPort->open(QIODevice::ReadOnly)) {
    qDebug() << "Couldn't start device";

void Handler::sendDataToCloud()
  //let's find the evarage value of temperature
  double lTmp = 0;
  foreach (double value, mDataList) {
    lTmp += value;

  lTmp = lTmp/mDataList.count();
  // after we have avarage temperature we can clear global temp list

  qDebug() << lTmp;

  // let's add attributes and send data to cloud
  QMap <QString, QString>lAtt;
  lAtt.insert("city", "Lviv");
  mTimeSeries->sendData("temp", lTmp, 3, lAtt);

void Handler::initPredix()
  // initialising Predix UAA service to have access to cloud
  QUaaInfo lInfo;
  lInfo.setUaaUrl("your UAA Uri");
  lInfo.setClientCredential("login", “secret”);

  QUaaUserInfo lUser;

  mUaa = new QUaa(lInfo);

  //set timeseries zineid to get access to data storage
  mTimeSeries = new QTimeSeries(mUaa);



#include <QCoreApplication>

#include "handler.h"

int main(int argc, char *argv[])
  QCoreApplication a(argc, argv);

  Handler h;

  return a.exec();


Predix tool kit

You can check your results by using Predix toolkit!/tsQuery

You can find the code here

So now all your data is read and stored in Cloud. Next time we are going to read this data in order to display it(PC or phone).