Reader small image

You're reading from  Hands-On Embedded Programming with Qt

Product typeBook
Published inJul 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789952063
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
John Werner
John Werner
author image
John Werner

John Werner is an internationally published author, engineer, consultant, and conference speaker with more than 15 years' experience. He has earned patents for inventions ranging from Inkjet printers to automotive ignition systems. John was one of the early users of Qt on QNX and contributed to the early knowledge of how to make it work. He is a contributor to the Qt-related information on Stack Exchange. He is currently a senior software engineer, specializing in Qt development, for Caliber Imaging & Diagnostics.
Read more about John Werner

Right arrow

Exploring the IoT with Qt

In this chapter, we will be exploring how Qt works with the Internet of Things (IoT). We will start by to understand what the IoT is comprised of, and then we will look at some examples, using Qt.

The main topics we will cover are the following:

  • Examining the IoT and Qt
  • Retrieving the weather using a Web API
  • Reading the temperature sensor in Qt
  • Publishing our status using Message Queuing Telemetry Transport (MQTT)

As we continue to uncover new requirements for BigProject, you will learn the following:

  • How IoT came about and what you can use it for
  • How Qt can help you with IoT
  • How to use Qt to access online data using a web-based API
  • How to use the cross-platform Qt Sensor classes to read sensors in Qt
  • How to use Qt support for MQTT

Technical requirements

In this chapter, we will be continuing to build BigProject, so you will need to refer to your work from Chapter 7, Adding More Features. You will also need your Target as we will be running code on it.

We also will be using the Raspberry Pi Sense HAT as we learn about Qt Sensor support.

Since we will be working with a weather API in this chapter, you will want to obtain a free API ID for OpenWeatherMap.org. Follow the instructions at https://openweathermap.org/appid to get a free API ID. We will only be using free features.

The source code for BigProject and other solutions in this chapter can be found on GitHub at https://github.com/PacktPublishing/Hands-On-Embedded-Programming-with-Qt/tree/master/Chapter09.

Examining the IoT and Qt

As we start this chapter, why don't we go back in time and look at how five or six computers talking to one another has now turned into a world where everything is connected? Let's also look at where Qt fits into that world.

The IoT – a brief history

When I graduated from college, shared university computers were networked computers connected together in various ways, and computer addresses always included a path from a well-known machine to the machine you were on. For instance, this was my email signature back in 1988; it contained three different ways to reach me:

The Wumpus     UUCP: {cmcl2!decvax}!rochester!ur-tut!aptr
BITNET: aptrccss@uorvm
Internet...

Forecasting the weather using a Web API

Members of the marketing team have come up with another idea. They would like BigProject to be able to display the current outside temperature for where we are, so they wrote this:

Req. 21: Display the current temperature for a city.

This might seem a difficult requirement to fulfill, but Qt comes to the rescue: QtNetworkAccessManager makes it incredibly easy to retrieve data from internet sources that support HTTP or HTTPS access, so we will use it.

We will start by finding a weather API we can use, and then we will move on to retrieving weather information using QtNetworkAccessManager. Finally, we will test the code and make sure it really does what we want.

Searching for a weather API

...

Reading the temperature sensor in Qt

A thermostat is really not a thermostat if it does not know the temperature in the room. Up until now, we have relied on a mock class to provide the temperature, but now it is time to measure the temperature in the room. The Raspberry Pi Sense HAT board has a multitude of sensors, including temperature, pressure, and humidity sensors.

To read the sensors, we will use the QSensor class and a QSensor plugin that reads the sensors on the Sense HAT board.

First, we will need to install the board on our target. Then, we will build the QSensor plugin for the Sense HAT board, along with the required drivers.

Installing the Sense HAT board

Way back in Chapter 1, Setting Up the Environment, you...

Publishing our status using MQTT

The following new requirement has come in: Req 22: The product must integrate with an IoT protocol such as MQTT or KNX.

In Chapter 7, Adding More Features, we looked at how easily Qt makes working with WebSockets; now we are going to take a look at a couple of more specific IoT protocols—MQTT and KNX. Both of these protocols are supported directly in Qt's commercial product, Qt for Automation (see Chapter 8, Qt in the Embedded World), and are made available as a source code extension under the (L)GPL license. Since we are using the (L)GPL version of Qt, we will be building them ourselves.

Building IoT extensions

If you can build Qt for the Raspberry Pi, you can build a couple of...

Summary

As we have worked through this chapter, we have built out our BigProject and learned a few things on the way.

First, we took a quick look at the history of IoT and where Qt fits into that. Then, we started transforming BigProject into an IoT device—not a small point device, but an IoT controller. As part of that transformation, we learned how to access web services from within Qt so that we could retrieve weather conditions.

We then learned how to use QSensor to access sensors in our system. In this particular case, it was the temperature sensor on the Sense HAT.

Finally, we took a look at a couple of IoT and automation protocols that are available in Qt and transformed BigProject from a simple display-controller to an MQTT client that publishes updates.

In Chapter 10, Using More Qt-Related Technologies, we will explore a few more Qt-related technologies, including...

Questions

  1. Is the IoT a new thing or a new name for something that has been around for almost two decades?
  2. Can Qt be used without a GUI?
  3. When accessing a web API, is the result returned synchronously or asynchronously?
  4. How should asynchronous results be handled?
  1. What does auto mean in C++11 (and later) versions of C++? Why would you use auto?
  2. What issues could occur if separate power supplies were to be used for the Raspberry Pi and Touchscreen driver board and panel?
  3. Qt is described as cross-platform. How does it handle getting information from sensors on different platforms and operating systems?
  4. What IoT protocols have been implemented for Qt?
  5. What general steps would you take to add the reporting of humidity and pressure published status from BigProject?
  6. What would you change in MainWindow if you wanted to publish status updates only every 5 minutes instead of every second...

Further reading

As with many topics in this book, we can only touch on a few aspects of several topics in this chapter without creating a volume whose size would rival that of an encyclopedia. Here are some books that should help you learn more about some of the topics we have covered:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Embedded Programming with Qt
Published in: Jul 2019Publisher: PacktISBN-13: 9781789952063
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
John Werner

John Werner is an internationally published author, engineer, consultant, and conference speaker with more than 15 years' experience. He has earned patents for inventions ranging from Inkjet printers to automotive ignition systems. John was one of the early users of Qt on QNX and contributed to the early knowledge of how to make it work. He is a contributor to the Qt-related information on Stack Exchange. He is currently a senior software engineer, specializing in Qt development, for Caliber Imaging & Diagnostics.
Read more about John Werner