Book Image

Hands-On Internet of Things with MQTT

By : Tim Pulver
Book Image

Hands-On Internet of Things with MQTT

By: Tim Pulver

Overview of this book

MQ Telemetry Transport (MQTT) is a lightweight messaging protocol for smart devices that can be used to build exciting, highly scalable Internet of Things (IoT) projects. This book will get you started with a quick introduction to the concepts of IoT and MQTT and explain how the latter can help you build your own internet-connected prototypes. As you advance, you’ll gain insights into how microcontrollers communicate, and you'll get to grips with the different messaging protocols and techniques involved. Once you are well-versed with the essential concepts, you’ll be able to put what you’ve learned into practice by building three projects from scratch, including an automatic pet food dispenser and a smart e-ink to-do display. You’ll also discover how to present your own prototypes professionally. In addition to this, you'll learn how to use technologies from third-party web service providers, along with other rapid prototyping technologies, such as laser cutting, 3D printing, and PCB production. By the end of this book, you’ll have gained hands-on experience in using MQTT to build your own IoT prototypes.
Table of Contents (16 chapters)
Title Page

Exploring smart homes

The most prominent category of Internet of Things (IoT) devices for end consumers is smart home—connected electronic devices that are used in your flat or house. They often replace traditional electronic devices by enhancing them with an internet connection and a way to control them digitally. In most cases, they offer a smartphone app or integration with a smart home app, such as Google Home, which makes it possible, for example, to turn the device on and off from your sofa, change its settings, and check its status.

According to a study by Zion Market Research (https://www.zionmarketresearch.com/news/smart-home-market), the global smart home market is about to reach around USD 53.45 billion by 2022, with big tech companies such as Samsung and Google being at the forefront. Let's look at some examples of these smart home devices:

  • Smart fridges
  • Smart door locks
  • Smart thermostats
  • Smart scales
  • Smart lights
  • Smart pet food dispensers

These are only a few of the smart home devices available today. The potential of such smart devices to influence our lives is huge.

Let's look at an example of a smart device—a smart fridge—and see what benefits it brings over a non-smart device.

The main problems people tend to have with a regular fridge are the following:

  • Food goes to waste because you have no clear overview of expiration dates for each item.
  • You run out of a certain food because you did not keep track of your supply.

Smart fridges keep track of your groceries and their expiration dates and warn you when you might run out of your favorite food. When opening the fridge, it could also present you with recommendations for what to cook with the available ingredients.

To minimize food waste, it might detect that you have, for example, tomatoes, curry sauce, and onions that will go bad soon. It might then automatically run an online search and present you with recipes that include these ingredients for you to prepare.

If it is connected to third-party shopping providers, it might also take care of ordering food automatically for you—for example, when your milk is running low, it might order a new bottle for you, which will then either be delivered to your doorstep or be ready for pickup at your local grocery store, together with other items that you are running low on.

While you cannot expect that level of comfort from a smart device just yet, more and more devices in our households will have similar features that make our lives easier.

How smart devices connect to the internet

In most cases, smart home devices need internet access to function correctly, which means that you must tell your device how it can connect to your local network via a network name and password.

When you first set up a new smart device in your household, the process often looks like this:

  1. You connect the smart device to a power source.
  2. The smart device goes into setup mode.
  3. The smart device opens an ad hoc network (see https://en.wikipedia.org/wiki/Wireless_ad_hoc_network).
  4. You connect to the network provided by the smart device via your smartphone. 
  5. On your smartphone, you visit a special website that is served by the smart device, and which is only accessible when you are connected to its network.
  6. You specify the network username and password on the website.
  1. The information is transmitted to the smart device.
  2. The smart device will close the temporary ad hoc network and connect to your normal network via the username and password you provided.
  3. The smart device can go online via your regular wireless network.

The very same technique can be used for creating IoT prototypes with microcontrollers, such as Arduino, Raspberry Pi, or Particle Photon, to create a convenient setup routine. We will not cover this technique in this book, but this is something to keep in mind if you're thinking about giving a prototype to friends and family or with batch production.

For example, if you were to bring your prototype to your friend's house, it would not be able to connect to the internet there, because it does not know the network name and password to connect with.

Without a setup routine like the one described previously, the source code of the device has to be modified each time the device is placed into a different environment to successfully connect to the other network.

Useful and unnecessary use cases

If you have ever lived in a single apartment without too much knowledge about how to separate your clothes when doing your laundry, you might have ended up with a bunch of pink shirts because a red sock found its way into your collection of white shirts that were to be washed. If you don't know what I am talking about, it is one of the mistakes a lot of single people make when using a washing machine. Washing a red sock with your white shirts is not a good idea, and might result in your shirts taking on the color of the sock, thereby becoming pink.

Other things can go wrong. You might do something wrong when setting the temperature, accidentally washing your favorite Norwegian Christmas sweater at 90 degrees, causing it to shrink. Now you might wear it while exposing your belly on cold summer days.

Chances are high that you don't want these things to happen.

Imagine that your shirts had a voice and that, when you asked them about their color and washing preferences, they answered, "Hi, my color is light-blue and I enjoy being washed at 30 °C. I will get sick when you tumble dry me, so please don't!". Now your washing machine could ask each and every textile lying in the washing machine drum about their washing preferences and set the program accordingly, or warn you that the red sock might better be off with the other red socks.

This can be made possible by using smart tags, such as RFID (short for Radio-Frequency Identification).

Many such use cases are about to bring value to our lives and make our lives easier. However, many companies forget about their product's original use case. One example is a smart light bulb that cannot be used as a light source when it is having a firmware update (https://twitter.com/BalrogGameRoom/status/1036644958973960192), which lasts for up to one hour. A firmware update probably does not need to be done very often, but if the light bulb is going through a firmware update in the evening, when it is dark, you will question their update policy and want to go back to using a normal light bulb. 

I think updating the firmware on devices is important to keep the device secure and safe from hackers, but, in this case, the company producing these smart light bulbs could have spent more time developing a better update routine to improve the user experience.

Often, smart devices only work under perfect conditions, in this case, with updated firmware. Compared to non-smart devices (in this case, a normal light bulb), this is a huge step backward. A normal light bulb has one purpose: provide light when it has power. Either it works or it does not. Adding internet capabilities to a device might not always be the best decision, as it produces problems such as the one mentioned previously. The device should be created in a way that means that using its core feature is not disturbed if there's no internet connection or outdated firmware.

Many companies that develop smart devices overemphasize the smartness of their products and forget about their original use case, reducing the usability of the device, which was its main purpose. Not all things need to be smart, and if they are made smart, then their main feature should work even without internet access.

In another example, two security researchers discovered a vulnerability in a smart vacuum cleaner modela robot equipped with a camera cleaning your flat autonomously. According to their study, it is not too hard to get access to the vacuum cleaner with admin privileges, allowing an attacker to misuse the vacuum cleaner as a 360º spying device. Definitely not what you signed up for when buying a smart device to save yourself some time. In this case, the manufacturer should have spent more time securing the device properly to make sure it is safe from hacker attacks.

There definitely is a need for autonomous vacuum-cleaning robots, smart light bulbs, and probably smart coffee machines, but when building new smart devices, you should always critically decide whether adding internet access to the device really adds something to its utility. If it does, then you should bear in mind the following questions when thinking about these aforementioned edge cases:

  • What happens when the device is not able to reach the internet? Is it still usable?
  • Can the device be updated? If so, when will it be updated? Does an update block its main functionality?
  • Is the device secure enough? Or will it be easy for hackers to access the device and use it for their purposes?

If you want a few hours of entertainment (and education), you should check out the Twitter account https://twitter.com/internetofshit, which collects IoT fails and available smart devices of little use. It will not only bring a smile to your face but also make you realize that things went too far for some companies when dealing with smart home devices. The first question to ask yourself should always be: Is the thing I am about to build useful? Just making something smart certainly does not make it more useful, but instead might make it vulnerable to hacker attacks or completely useless when the internet connection does not work.