Machines talking to each other? Notifications that trigger if your elderly parents change their routine? Having fun by making a dinosaur nod in your child’s bedroom when it’s time for dinner? The Internet of Things is opening a world of possibilities.
It has become simpler than ever to connect whatever you want to whatever you want, to read sensors remotely or to enchant an object – without the need for advanced degrees in computing, electronics or magic.
This article describes the concepts behind the Internet of Things (IoT). It also explains why a low cost, credit-card sized computer called a Raspberry Pi, the MQTT messaging protocol and Node-RED – a visual flow programming language – are the ideal tools for building your own IoT device.
Internet of Things
The Internet of Things (IoT) has been with us for a long time – just under different names – Pervasive Computing, Ubiquitous Computing (“UbiComp”), Tier-Zero (one layer down from the desktop computers dubbed Tier-One), Sensor Networks, Motes, Machine-to-Machine (M2M) etc.
In essence, any system which brings data from some kind of sensor, across a network, to a different location, can be thought of as IoT. Likewise, sending data or instructions to something in a different location can also be thought of as IoT.
Many areas of our lives have already been touched by IoT technology. Oil pipeline monitoring systems, vehicle tracking, monitoring heart rates and blood pressure of hospital patients, tracking milk yield from dairy cows and improving the in-branch experience for banking customers, all have taken advantage of the IoT.
What happens when you combine data analytics with the application of sensor and actuator systems? Integrate it into existing IT systems of a company and the IoT becomes a powerful tool to enable existing processes to be augmented, channels to market to be improved, supply chains to be optimised, and many other aspects of business and our everyday lives to be made better.
The foundations of the IoT are devices that are Instrumented, Interconnected and appear Intelligent.
Instrumented
We have been relying on instruments and sensors for many years to tell us things. For example meters and smoke detectors tell us through displays and sound how much electricity we have used and whether there is a fire in the house. Originally, all of these were local – the electricity meter and display are in the house and someone has to come and read it before a bill is produced. The fire alarm sounded in the room where the fire was, and it is up to us to get out and call the fire brigade. More sophisticated systems were then introduced – for example, fire alarms can be linked directly to fire stations.
A key attribute of IoT systems is that data is delivered in a timely fashion to those who need to act on it. For example, there is very little value in receiving a report on a Friday afternoon which informs you that a machine broke down three days earlier. It would be far more useful to get an SMS message to your phone saying “machine number 4 has just stopped working”. Better still would be a message the day before, saying “based on the way it’s behaving, it looks like machine number 4 will break down tomorrow”. That would give time to proactively attend to the machine and repair the failing components before bringing the production process to a costly halt.
IoT systems provide insights into what is happening at a remote location, such as a house, through monitoring the energy use of the whole house and individual electrical appliances within the house, such as the washing machine, cooker, television, etc.
Details will vary widely, and the network may or may not be “The Internet”, but it’s still an IoT system for the purposes of this discussion.
Interconnected
Nowadays, we think nothing of being able to change TV channel from the comfort of our sofa, open a garage door or gate, or unlock a car, just with the press of a button – by remote control.
These systems that control a remote device – which also includes the ability to turn on a light, start a motor or close a valve, are also examples of IoT systems.
IoT can Send and Receive
Sensor systems bring data from remote devices. But all the arrows can be reversed and data can also be sent to remote devices to control things in the physical world.
The IoT is starting to make a difference to the lives of what we might term “ordinary people” – to differentiate from the enthusiastic home-automation geeks who have been playing with this technology in their homes for years.
We are seeing emerging products such as Nest and Hive which give remote control, via your smartphone, of your heating system.
You can avoid coming home to a cold house by setting a geo-fence, an imaginary boundary a few miles away from your house, and have your phone automatically send an “on” signal to your heating when you cross the boundary of that geo-fence. This gives your house time to reach a pleasant temperature by the time you arrive home. Some smart heating controllers are learning to recognise occupancy of the house, and learn the daily comings and goings of the family, and then, after a few weeks of observation, modify the on/off cycle settings of the heating and hot water to ensure warmth when people are at home, and a return to a lower temperature when the house is empty.
The IoT is a fascinating area for considerations of User eXperience (UX). The “out of box” experience is particularly important for an IoT device. If the function of a Thing is to notify you when the bus is coming up the road towards the bus-stop near your house, you want it to just start working when you get it home, not have to spend hours with manuals or online tutorials trying to work out how to connect it to your WiFi. This is particularly key for Things that don’t have any keyboard or display: if you have to convey your home WiFi SSID and security credentials to the device before it will do anything at all, you will already be feeling negatively towards the device, even if you are an enthusiastic IoT hacker! For someone less familiar with computers, this might be the point at which you take it back to the shop or just put it in a drawer and forget about it.
Engineers are great at solving problems. But we need to be sensitive to the idea that what we think is obvious or natural, may not be for everyone else in the world. Lots of flashing coloured LEDs and arcane command-line options to be typed to get something working are not the most effective user experience for most people.
Intelligent
At a higher level the IoT is about the data produced by the things – and this can allow companies and organisations to optimise their processes or develop better products and services for their customers.
Over the years it has been quite astounding how many areas of human endeavour IoT technology has touched. There are plenty of “obvious” use cases, such as home automation and industrial pipeline monitoring. There are also logical extrapolations of that to monitoring other remote things, such as washing machines, lifts and vehicles. But some of the most exciting aspects are the unexpected applications of IoT such as analysing energy use in the home to infer that an elderly relative is up and about each morning. Increasingly, it’s the power of the data analytics that provides the impressive insights, rather than the sensors themselves. This leads us to conclude that “it’s all about the data”, which has significant implications for the end-to-end infrastructure needed to create an IoT solution, rather than just the “Thing”.
The Internet of Things is all about the data
Cloud-based connectivity for devices, device management services, and basic translation and transformation and storage of data are becoming key components in IoT solutions.
IoT data has been called the “new oil” or “new natural resource”. We presume this does not mean it will pollute the atmosphere and ultimately run out, but that, in its crude, initial form, it is of limited value. However, through processing and refining it becomes increasingly valuable – just as crude oil is refined to provide products such as tar, oil, diesel, petrol and gas.
The IoT has a lot to contribute to the areas of travel and transportation. We are seeing the emergence of connected cars which send a constant stream of data back to their manufacturer to let them know how they’re performing in the field. This enables predictive analytics to be used to let you know when a part needs to be serviced before it deteriorates to the point of breaking.
IoT solutions might make your journey smoother, by advising you when your train is delayed, or a ferry isn’t running due to bad weather, or because of a late-running bus you’ll miss a connecting train and so need to get a different one 20 minutes later. Sensors on the vehicles that make up our transport system feed data about location and speed back to processing systems which can compare actual progress against the timetable and advise on significant deviations. For the companies that run transport systems, feedback from this data can be used to adjust timetables to better fit observed performance, and can give statistics on progress towards on-time targets and also help with driver training and vehicle maintenance planning. The opportunities for improving integration of multi-modal journeys is something that could tangibly lead to people being more inclined to take public transport rather than using private cars.
Whether for your home or business, when you’re out and about or caring for others, the Internet of Things can make a huge difference to lifestyles, efficiency, time management and information.
Why Raspberry Pi?
A tiny and affordable computer, originally designed for children, has become an amazing tool for the IoT. The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV. It uses a standard keyboard and mouse. However, once programmed, it will also work “headless” – without a monitor, mouse or keyboard. It can be used as a “normal” computer – for word processing, spreadsheets and video games, but also can be programmed in a variety of computer languages.
But it is its ability to interact with the outside world that makes the Raspberry Pi really useful for IoT projects.
The GPIO (General Purpose Input/Output) pins can be used as a real-world interface in amazing ways. Inputs are not just physical switches – they could be a sensor or a signal from another device or computer. The output can also do anything, from lighting an LED to sending a signal to turn a motor or even send data to another device. If the Raspberry Pi is on a network, devices attached to it can be controlled from anywhere – and information from these devices can be sent anywhere.
Connectivity and control of physical devices over the internet is an impressive and exciting thing – the Raspberry Pi gives you this power.
In the early 2000’s, Eben Upton, Rob Mullins, Jack Lang and Alan Mycroft, based at the University of Cambridge’s Computer Laboratory in the UK, saw a year-on-year decline in the number and skill level of students applying to study Computer Science at University. They decided to do something about it.
Home PCs and games consoles had replaced the Amigas, BBC Micros, Spectrum ZX and Commodore 64 machines on which people of an earlier generation had learned to program. These new PCs and consoles were expensive and what went on inside them was a mystery, so parents often forbade children to mess about with them.
Between 2006 to 2008 the team designed several versions of what has now be- come the Raspberry Pi. It was called Raspberry because many computers are named after fruit – Apple, Blackberry, Apricot etc. and because Raspberry was the rudest sounding fruit. Pi was from Python, the programming language. Within two years it had sold over two million units. On their fourth birthday, 29th February 2016, the Raspberry Pi Foundation announced over 8 million units had sold, making them the UK’s all-time best-selling computer. By 29th February 2020 over 15 million units had been sold.
As the founders themselves say, they don’t believe that the Raspberry Pi is a fix to all of the world’s computing issues; but they do believe that it is a catalyst. We suspect they did not envisage it would also be a catalyst for the Internet of Things. About a third of the Pis sold are used by hobbyists making their own projects, and a third are used in industry – the remaining third are used, as per the original intention, for education. Hobbyist projects include electronic mousetraps and Christmas tree lights that change colour to a tweet. In industry they have been used to automate factory flows to controlling shop window displays or even animatronic dinosaurs in theme parks, as discussed above.
Prototyped projects using a Pi often result in no other control system being necessary. The main considerations for not using a Pi in the final implementation are environmental considerations such as vibration, humidity and temperature extremes. Almost everywhere else, they are suitable. There are over 15 million Pis in circulation – the manufacturers have ridden the “bug curve” and the Pi is very reliable for use in many embedded applications.
Blackgang Chine
One of our favourite stories of non-experts using Raspberry Pi and Node-RED is at Blackgang Chine on the Isle of Wight, UK. (See also The Engineering Edge podcast on Blackgang and the article)
In 1842 Alexander Dabell created the World’s first “Park of Themes” or Theme Park at Blackgang Chine on the Isle of Wight – a small island off the south coast of England. In the early 1970’s Alexander’s great-grandson decided to add a new theme – and as many fossils were being found locally, what better than to install some dinosaurs?
Installation of fibreglass dinosaurs – airlift. (1972). Image credit: Blackgang Chine
In 2012, the next generation took control of the Park – another Alexander Dabell. This Alexander wanted MORE dinosaurs – ones that moved – and so “Area 5” was born, where you can roam through a jungle of animatronic dinosaurs.
Area 5 at Blackgang Chine 2014. Image Credit: Blackgang Chine
The Theme Park is small and staff have to multi-task – from fibreglassing and carpentry to gardening and catering. Not many of the employees had an interest in becoming “programmers” but they did want to be able to fix and control the dinosaurs themselves. As they were working in the park every day and talking with visitors, the staff would imagine exciting new interactions amongst the dinosaurs and the public. But they needed a way to programme those interactions.
They could already fix structural damage but the electronics needed parts to be shipped across from China – sometimes taking weeks to arrive. With the “Busy Six” – the six weeks of the year when 80% of the visitors pass through the park – imminent, I got a phone call … “Lucy, come and hack our dinosaurs”
One of Blackgang’s technical people, Mark Butler, had already fixed a storm-damaged dinosaur control box by using a Raspberry Pi and teaching himself just enough Python. This managed to keep the dinosaur going, but it wasn’t a complete fix. I was asked to do three things:
1. Fix the broken dinosaurs so they worked during the Busy Six weeks.
2. Train the staff to be able to fix and adapt them in the future.
3. Create a better long term solution.
The dinosaurs we were working with each had up to eight motors, some limit switches and a sound recording. Just one Pi could be used to control them all. Knowing that budgets were tight at the Theme Park, and that the maker community love to tinker with fun things, I made a plan.
An offer of “Come and Hack Robot Dinosaurs” went out to various members of the maker community, tinkerers, coders and electronic engineers – most of whom I had met on Twitter. In return for food and cake, they happily hacked the dinos, and helped each other and the staff. Some people were from the Island, others from the Mainland – not many had met each other before, but the “community” and cake formed good bonds. We called the event “BlackgangPi”.
It was discovered that the control board that had failed used obsolete parts that could not be replaced. However, the Raspberry Pi was ideal for the job of controlling the dinos. The control boards connected to a power-driver board, and this drove the large motors. The power-driver boards could accept low voltage (3.3V) logic level inputs, and so they could be driven directly from the Pi’s GPIO pins. The original power-driver board had not yet failed – and so this was relegated into the “longer term solution” list.
Now that a Raspberry Pi was used to control the dinosaurs, they could be programmed to act in other ways, and not just in a pre-programmed sequence.
This was also useful as the dinosaur roar and mouth opening had got out of sync, and a roar often occurred when the mouth was closed.
Andy Stanford-Clark and small T-Rex at BlackgangPi 2014. Image Credit: Lucy Rogers
One of the items the staff wanted to control was the choreography of the dinosaurs. For example “lift head for five seconds, then wag tail, then begin to open mouth and roar, close mouth” etc. The process control algorithm can be thought about in “logical” English (first do this, then do that) – which many people, who are not programmers, already use on a daily basis. This type of process control is an ideal application for Node-RED. The pre-installed GPIO pin on / off and time delay nodes can be dragged onto a sheet in the user interface and simply wired together.
One of the other things the staff wanted to change was the order in which the Park dinos reacted to the motion sensors. Originally, they just followed a pre-programmed sequence – that got repetitive. They wanted it to effectively “roll some dice” to decide what to do. Andy Stanford-Clark worked with a group of staff on this, and also online with some people at IBM and, during the hack session, created a “random” node, which is now part of the standard Node-RED installation package.
There’s a video of the first BlackgangPi on Raspberry Pi’s blog pages
Blackgang Chine is just one of the many examples of non-technical experts using a Raspberry Pi and Node-RED for their own projects. Keeping spares of the low cost microcomputer is economically viable, making the system robust. The logical flow programming language of Node-RED can incorporate JavaScript functions using a text editor if desired, but the library of nodes readily available on the palette can provide a solution for most projects for the non-programmer.
The only limitation is your imagination.
MQTT
MQTT used to stand for “Message Queuing Telemetry Transport”, although through evolution, it is now no longer an acronym and so does not stand for anything. It is a messaging protocol for the Internet of Things, which makes it easy to get your Things connected. It looks after getting your message from A to B – you don’t need to know or worry about how. You just decide “what will I send?” and “what will I do with it when it gets to the other end?”
MQTT is an ISO standard (ISO/IEC 20922) – which means that wherever you are, MQTT messages will use the same protocol – there won’t be any compatibility issues. It is used by IBM, Microsoft, Amazon and many other IoT companies – and you can use it too. The protocol is free for all to use, clearly defined, simple to understand, and non-proprietary (i.e. not owned by one controlling company). You can learn more about MQTT and read the specification through links on https://mqtt.org. MQTT was designed for low bandwidth networks and so is ideally suited for the Internet of Things.
MQTT uses a publish–subscribe messaging pattern. This is similar to magazine publishing – the publisher produces a magazine, and a subscriber places a subscription. When a new magazine is published, it is automatically sent to all the subscribers, without the subscribers having to ask for it each time.
In MQTT, the senders of messages are called publishers. The publishers do not know who, or if anyone, is subscribing to their messages. It characterises its messages into different topics (think sport, health, science etc. in the magazine world) and just sends out the message. The subscribers subscribe to the topics they are interested in, and receive the messages when they are published. The subscribers do not know (nor do they need to) who is publishing.
In this way, MQTT supports a “one-to-many” distribution pattern; one sensor device can publish data that one, ten or a thousand applications could subscribe to without having to change the code running on the device.
Also, you can subscribe to as many different topics as you wish, and a device or application can be both a publisher and a subscriber at the same time. For example, a device could be publishing sensor data and subscribing to control commands.
This makes MQTT a powerful yet simple messaging system – whether you want to be alerted when the International Space Station is overhead, allow anyone to change the colour of your Christmas tree lights or monitor the temperature of your greenhouse, MQTT is the system to use.
More info on the history of MQTT and how to use it are here and more advanced usage here
Node-RED
Node-RED provides a browser-based drag-and-drop programming environment. This makes it easy to create applications by wiring together “nodes” of functionality selected from a wide range in the palette. It is an open-source project, free to download, install and use, and runs on devices as small as the Raspberry Pi, on laptops, and also in cloud-hosted environments. Application “flows” can be then deployed to run with a single-click.
For many of the years we have been using MQTT for sending messages from devices around the internet. However, there have very often been times when we want to receive a message, change it a bit, and send it somewhere else. For example you might receive a message from a temperature sensor representing a temperature in Fahrenheit, want to convert it to Centigrade and then send it to a display. That pattern of subscribe, modify, publish is a very common one.
You might want to route a message to another destination, depending on its content (for example, is the value greater than 100?). You might want to receive data from a variety of sources: internet connections, a serial port, the digital input pin of a GPIO interface. After processing you might want to post the data to a web site, or publish an MQTT message, or write the data to a file or database.
What was needed was a nice easy graphical way to wire up these kinds of processing steps. When Nick O’Leary – a Senior Software Engineer in IBM’s Emerging Technology Solutions team – had some free time in March 2013 he started working on the idea of: what if you could have a graphical tool for expressing the idea of wiring building blocks together to make an application?
In the temperature sensor example described above, you could drag an MQTT input node onto the screen, with the broker and topic to subscribe to, connect that with a line to a processing node that would contain a fragment of program- ming language to convert the value from Fahrenheit to Centigrade, and then connect that to an MQTT output node that would publish the message to its final destination on a topic on a specified broker.
The elegance of Node-RED is that the underlying code for an MQTT subscriber, or to communicate with a serial port, or to drive a GPIO output pin on a Raspberry Pi, has often already been written by someone else. Node-RED packages that piece of code, which someone else has written, and allows you to re-use it simply by dragging it into your application.
Software engineers could write the code to subscribe to MQTT or talk to a serial port, but time spent reinventing that wheel is time not spent on solving the actual problem in hand. Node-RED provides a way to encapsulate code that has been written by someone else, and re-use it easily, setting configuration options and parameters through an easy to use web interface. You then write your application by dragging the required input, processing and output blocks onto a blank canvas in a web browser, and then wiring the blocks together by dragging lines between them.
Changing the flow of an application is as simple as deleting a line connecting a block to one destination and replacing it with a line to another destination block.
With the help of IBM’s Dave Conway-Jones, Nick O’Leary grew Node-RED to accommodate other needs that were found when it was used in real-life: the first use was on a customer project putting sensors on a production line in a factory, using Raspberry Pis and Node-RED to program their actions.
The standard Raspberry Pi “Raspbian” operating system image includes Node- RED in the programming section of the Start menu – making it very easy to access. Once Node-RED has started running, you use a web browser to access the graphical user interface, either locally on the Pi, or remotely from a laptop across the network.
When you have written your application in the browser by dragging and dropping nodes from the palette and wiring them together, you “deploy” your application to the Node-RED server by clicking the deploy button. At that point, your application is running on the server, wherever that happens to be, and you can close your browser window. Seeing it on the browser is only for application development and debugging, and is not essential for the application to run. Hence, if you write a Node-RED application and deploy it to a Raspberry Pi or a cloud-based server, then it continues to run there after your browser has closed and disconnected.
Node-RED is also attractive to people who are not heavy-duty software engineers – a lot of the things you want to do have been anticipated by the Node-RED developers and have already been provided as a pre-packaged node, ready to use.
If you want to delay a message for a few seconds, or roll dice to get a random number, or convert a message from XML into JSON format, or send a message to a websocket, or receive a message from MQTT… there’s already a node for that.
Since the project was contributed to open-source in September 2014, there has been rapid growth of Node-RED, with many people contributing to the development of new nodes. There are libraries of contributed nodes where you can find new nodes to add into your palette of available nodes so you can use them in your applications. Node-RED is written in JavaScript – it runs in the node.js run-time. JavaScript functions can be created within the editor to create more sophisticated applications.
Node-RED has been used by teachers and children to help make games, Santa alerts and tweetable hamster feeders, by a Theme Park to control robot dinosaurs, and by industry to control and monitor factory automation. It’s simple enough for children to use, but sophisticated enough for most industrial applications.
Conclusion
Raspberry Pi, Node-RED and MQTT are powerful yet simple tools. Powerful – they can be used to control very complex IoT devices, and yet simple, in that anyone who can work out a sequence of operations, such as “fill kettle, turn kettle on, put teabags in teapot, wait until water boils, pour water into teapot” can use them to make their own IoT device.
This article has given a taste of what a Raspberry Pi, MQTT and Node-RED can do and what each of them are. For more info see the articles on MQTT and MQTT-fu