The Complete Magazine on Open Source

Qt programming for HTTP REST clients

1.01K 0

This article discusses Qt support for connectivity to IoT platforms like ThingSpeak using HTTP REST APIs. It also focuses on handling JSON data in terms of encoding, parsing, URL encoding and forming query strings.

Qt is a cross-platform development framework designed to provide eye candy GUI features and to give rich API support for Web communication, graph plotting, data exchange, 3D visualisation, multimedia handling, location tracking, sensor interfacing, etc. Qt applications can be built for various desktop platforms like Linux, Mac OS and Windows; for embedded platforms like Raspberry Pi and QNX, and also for mobile platforms like Android, iOS and Windows Mobile.
This article explores Qt support for connectivity to IoT platforms like ThingSpeak using HTTP REST APIs, and also focuses on handling JSON data in terms of encoding, parsing, URL encoding, forming query strings, etc. The assumption is that readers have basic familiarity with Qt and RESTful operations using HTTP support. If you are new to the Qt environment, do refer to some of the previous articles published in earlier editions of OSFY, as listed in the References, before proceeding.

About HTTP REST and ThingSpeak

ThingSpeak is an open source platform for building IoT applications, which supports connectivity using HTTP REST APIs. One can send data using the POST method with a request payload or the GET method with a query string, and retrieve data in JSON or XML formats using the GET method. Recently, support for sending data using MQTT Publish has also been added. ThingSpeak has good integration support for MATLAB from Mathworks, which helps it to offer better analysis and visualisation of data. It provides SDKs in various languages for connectivity or one can try to connect using any HTTP client of the preferred language.

In ThingSpeak, data is stored in terms of channels, and each channel is associated with an ID, name, description and various applicable fields along with some tags, geo-location, etc. For simple and better authentication, API keys are provided for read/write operations and a channel key for meta operations. The API keys of a specific channel can be embedded in a query string or post data for authentication. Secure HTTP (https) is preferred to prevent unauthorised access of API keys by eavesdroppers. A channel can also be made public during the creation or by changing the settings later, in which case no read key is required for viewing the data.

One can log in to ThingSpeak using a Mathworks account, which is free of cost, and create any number of channels and send maximum feeds—the only limitation on updating channels is an interval of 15 seconds. Once the channel is created with a suitable description, applicable fields and meta information, you can identify the read or write API keys under Channel settings and the key for channel meta operations in the Profile section.

Let’s now look at how a REST client for connecting with ThingSpeak can be built.

Qt HTTP connectivity

In order to talk to any network server using the HTTP protocol, Qt provides the QNetworkAccessManager class, which has asynchronous methods like get, post, put and deleteResource to perform various REST operations. These methods take the QNetworkRequest object as an argument, as well as an additional argument for request payload in the form of QByteArray for POST, PUT operations. QNetworkRequest mainly contains the URL in the form of a QUrl object, which encapsulates the protocol, host name, port, path, query string, etc. The QUrlQuery class can be used to form a QueryString effectively. These methods return the response in the form of a QNetworkReply, which needs to be explicitly destroyed using the deleteLater method in the slot connected to the finished signal of QNetworkAccessManager.

Let’s consider a channel with three fields representing temperature, humidity and pressure. Let us assume that the various API keys and channel numbers are initialised as follows:

Qstring CHNum = “xxxxxx”;

Figure 1: Sending a feed using POST

To send a feed, we need to perform the POST method with the steps that follow, using the URL and the following JSON data as request payload:


Step 1: Let’s prepare the data for POST in JSON format, assuming that tval, hval and pval represent the temperature, humidity and pressure values in integer form.

QVariantMap feed;
QByteArray payload=QJsonDocument::fromVariant(feed).toJson();

You may verify the prepared JSON data in QString format as follows:

qDebug() << Qvariant(payload).toString();

Or you can display it in a QLineEdit or QTextEdit widget for testing purposes.

Step 2: Prepare the URL as follows:

QUrl myurl;
myurl.setScheme(“http”); //https also applicable

We are skipping the user name and password as we are using API keys for authentication. We are also skipping setPort as the service is running on the default port 80. You can verify the prepared URL as follows:

qDebug() << myurl.toString();

Step 3: Prepare the network request, as follows:

QNetworkRequest request;

Figure 2: Sending a feed using GET

Step 4: Perform the POST operation using the following code in the slot connected to the Publish PushButton click.

QNetworkAccessManager *restclient; //in class
restclient = new QNetworkAccessManager(this); //constructor
QNetworkReply *reply = restclient->post(request,payload);
qDebug() << reply->readAll();

Restclient can be declared in the MainWindow or the Dialog class and the QNetworkAccessManager object can be created in constructor, as one object is sufficient to perform all the operations.

The same update operation can be achieved using the GET method and data encoded in the URL as a query string:

QUrlQuery querystr; 
reply = restclient->get(myurl);
qDebug() << reply->readAll();

Alternatively, data encoded in the URL query string format can be used to perform the POST operation using the same URL

//Prepare querystr similar to above steps
request.setHeader(QNetworkRequest::ContentTypeHeader, “application/x-www-form-urlencoded”);
QByteArray postdata = Qvariant(querystr).toByteArray();

Retrieving data from ThingSpeak

To retrieve the feeds from ThingSpeak, we need to perform the GET operation on and api_key=XXX..XX as the query string. For this purpose, use the following code in the slot connected to the Retrieve PushButton click. You can append results=10 to the query string to limit the number of results to the last 10 feeds.

QUrl url;

To process the responses holding all feeds in the JSON format, connect the finished signal of restclient to a suitable custom slot.

QObject::connect(restclient, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply *)));

Parsing JSON data

The returned JSON data has two primary fields with the names channel and feeds, and the value of feeds is an array of all feeds, where each array element consists of fields like entry_id, field1, field2, field3, created_at, etc.

To traverse all array elements and retrieve the specific fields in each, you can use the following code to parse the JSON data in the replyFinished slot:

QJsonDocument jsdoc;
jsdoc = QJsonDocument::fromJson(reply->readAll());
QJsonObject jsobj = jsdoc.object();
QJsonArray jsarr = jsobj[“feeds”].toArray();
foreach (const QJsonValue &value, jsarr) {
QJsonObject jsob = value.toObject();
qDebug() << jsob[“entry_id”].toInt();
qDebug() << jsob[“field1”].toString();
qDebug() << jsob[“field2”].toString();
qDebug() << jsob[“field3”].toString();
qDebug() << jsob[“created_at”].toString();

Figure 3: Retreiving data and displaying it in tabular form

Rendering data in tabular form

To display the data in tabular form, create a TableWidget through TableView with the name tableFeeds and add the following code in the slot connected to the Retrieve PushButton click.

foreach (const QJsonValue &value, jsarr) {
QJsonObject jsob = value.toObject();
new QTableWidgetItem(jsob[“entry_id”].toString()));
ui->tableFeeds->setItem(k,1, new QTableWidgetItem(jsob[“field1”].toString()));
ui->tableFeeds->setItem(k,2, new QTableWidgetItem(jsob[“field2”].toString()));
ui->tableFeeds->setItem(k,3, new QTableWidgetItem(jsob[“field3”].toString()));
ui->tableFeeds->setItem(k,4, new QTableWidgetItem(jsob[“created_at”].toString()));

You can also plot the graph with the above feeds using the third party library, QCustomPlot or with the help of Qt Charts introduced in version 5.7.0.

As an example, we have discussed connectivity to ThingSpeak in this article. You can apply these concepts to other IoT platforms and database servers like InfluxDb, which support HTTP REST based connectivity.