As we discussed before, there are two broad categories of physical objects on the Web of Things: tagged objects and connected objects. The first category comprises various tagging technologies that are attached to a product, such as barcodes, QR codes, NFC or RFID tags, and so on. In this case, objects aren’t connected directly to the web, only passively, because there’s a need for another device or application to interact with the product. Connected objects are directly connected to the Web of Things and are the world of embedded systems and embedded devices, which are essentially small, relatively inexpensive, low-power computers with limited resources and capabilities. You can apply the techniques and architecture you’ll
This chapter covers
■ Understanding the various categories of embedded systems
■ An introduction to working with the Raspberry Pi ■ An introduction to setting up and using Node.js
for IoT prototyping
■ Learning to connect sensors and actuators using GPIOs
84 CHAPTER 4 Getting started with embedded systems
learn about in this book to both tagged and connected objects, but the focus of this book is mainly on connected objects.
In chapter 2, you learned how to consume services from a real embedded device— a Raspberry Pi located in our office—to get a first glimpse of the Web of Things. But this wasn’t very physical because the device you interacted with was neither yours nor next to you.
In this chapter, we’ll show you how to set up and configure your very own IoT device. By the end of this chapter, you’ll have a real device connected to the Web of Things, and you’ll have all the tools at hand to be able to program it and implement all the concepts presented in the next chapters of this book. You’ll start by choosing a hardware platform. There are many options out there, so we’ll make sure to help you. You’ll then make your device fit for the Web of Things by installing various software packages and libraries. You’ll also learn the basics of IoT prototyping with a hint of electronics by connecting real sensors and actuators directly to your Raspberry Pi.
If this is your first encounter with embedded devices and electronics, this chapter will be a gentle (yet challenging) and fun crash course!
4.1
The world of embedded devices
Literally thousands of embedded platforms types are available, ranging from small pro- duction runs of general-purpose sensor nodes built for researchers or hackers to cheap and mass-produced circuits built specifically for smoke alarms, microwave ovens, and alarm clocks. Obviously, we won’t have time to get into a deep review of these platforms in this book. What you should remember is that there are two big leagues of embedded devices: those targeted at hobbyists (less specific and optimized but more reusable and flexible) and those meant to be built into real-world industrial products (more opti- mized for specific use cases, so harder to extend and use in other contexts).
4.1.1 Devices for hobbyists vs. industrial devices
The idea of embedding computers into everyday objects isn’t so novel: our washing machines have contained integrated circuits for decades. But they didn’t connect to the internet, nor were they designed to be easily accessible or reprogrammable by application developers or customers. The emergence of the Internet of Things changes the game quite a bit. First, IoT devices are connected to the internet, which can be challenging for low-power devices. This constraint gave birth to a number of industrial-grade embedded platforms that support various networking protocols out of the box, ready to be used for commercial applications. Second, the research com- munity and hobbyists started to get increasingly interested in tiny computers that not only were easy to program but also could support all sorts of sensors or actuators.
These two trends gave birth to a myriad of platforms for both real-world and indus- trial use cases as well as for hobbyists and DIY projects. The main difference between these two categories is the focus of their users. For industrial platforms, the objective has been to reduce the costs so they could be embedded in all kinds of consumer products while maintaining a high level of stability (you wouldn’t want to have to
85
The world of embedded devices
reboot your washing machine every now and then). Hobbyists, however, were less sen- sitive to industrial-grade performance and robustness and preferred platforms that were open and easier to use and extend and that also came with an elaborate and easy-to-use tool suite.
Having said that, the traction around the Internet of Things in the recent years blurred the line between the two worlds, and you can observe industrial platforms manufacturers working hard to make their devices more accessible with better tools. Likewise, hobbyist platforms are getting more robust and cheaper and hence are now also embedded in real-world products.
Providing a detailed overview of those platforms could easily consume the rest of this book, so we describe only some of the most popular ones in table 4.1. Note that this table is an oversimplification of the brands and their offerings and is provided as a high-level overview of some of the big players. Please refer to the respective sites of the platforms for more details.
4.1.2 Real-time operating systems vs. Linux
In table 4.1, you probably noticed the Type column showing RTOS and Linux devices. There are basically two categories of operating system (OS) used for embedded devices: real-time OS (RTOS) and, well, not so real-time OS!
Table 4.1 An overview of some IoT embedded platforms. Platforms targeting hobbyists usually cost more but also have more resources (RAM, CPU, and so on). Industrial platforms tend to offer lower specifications but the costs are usually lower.
Brand Models CPU RAM + Price Type Connectivity
Arduino 20+ and many clones (Spark, Intel, and so on)
ATmega, 8–64 MHz, Intel Curie, Linino 16 KB– 64 MB Largest community
~30 USD RTOS, Linux, hobbyists
Pluggable exten- sion boards (Wi- Fi, GPRS, BLE, ZigBee, and so on) Raspberry Pi A, A+, B, B+, 2, 3, Zero ARMv6 or v7, 700 MHz -1.2 GHz 256–1 GB Full Linux, GPU, large community ~5-35 USD Linux, hobbyists Ethernet, extension through USB, BLE (Pi3) Intel Edison Intel Atom 500
MHz 1 GB X86, full Linux ~50 USD Linux, hobbyist to industrial Wi-Fi, BLE BeagleBoard BeagleBone Black, X15, and so on AM335x 1 GHz ARMv7 512 MB– 2 GB Stability, full Linux, SDK ~50 USD Linux, hobbyist to industrial Ethernet, exten- sion through USB and shields Texas Instru- ments CC3200, SoC IoT, and so on ARM 80 MHz, etc. from 256 KB
Cost, Wi-Fi <10 USD RTOS, industrial
Wi-Fi, BLE, ZigBee
Marvell 88MC200, SoC IoT, and so on ARM 200 MHz, etc. from 256 KB Cost, Wi-Fi, SDK
<10 USD RTOS, indus- trial
Wi-Fi, BLE, ZigBee
Broadcom WICED, and so on (also at the heart of the Raspberry PIs) ARM 120 MHz, and so on from 256 KB Cost, Wi-Fi, SDK <10 USD RTOS, industrial
Wi-Fi, BLE, Zig- Bee, Thread
86 CHAPTER 4 Getting started with embedded systems
In essence, what makes an OS real-time is its ability to respond quickly and predict- ably to data that comes in. Real-time OSs are necessary for applications that control “big and hairy things” such as nuclear power plants, manufacturing chains, and air- planes, where determinism and response time are more critical selection factors than anything else. They also usually lead to lower power consumption or at least quite pre- dictable power consumption.
When it comes to embedded devices, the world of RTOS is dominated by Free- RTOS,1 an open source real-time operating system, although some viable alternatives
exist, such as Contiki,2 TinyOS,3 mbed OS,4 and the commercial VxWorks.5
One of the drawbacks of a real-time OS is that it isn’t very good at operating many tasks in parallel, which makes it hard to build complex layers offering simple abstrac- tions. This is where a non-real-time OS can help. It becomes particularly helpful for things that are not so mission-critical where user experience and features are more important than a constant, very fast response time. In this world—even more than in the RTOS world—one operating system rules them all: Linux.6
Because of its large community, plethora of tools, abstractions, and supported architectures, Linux is the ideal environment in which to start tinkering and innovat- ing with IoT devices. But don’t get it wrong; it’s also increasingly a solid candidate for real-world and robust IoT applications such as for home automation or for building application gateways as described in chapter 5.
1 http://www.freertos.org/ 2 http://www.contiki-os.org/ 3 http://www.tinyos.net/
4 https://www.mbed.com/en/development/software/mbed-os/ 5 http://www.windriver.com/products/vxworks/
6 Note that a number of projects offer modified versions of the Linux kernel to transform it into a real-time OS; see, for instance, http://www.osadl.org/Realtime-Linux.projects-realtime-linux.0.html.
The nerd corner—I want more operating systems!
Over the past few years, Linux has become such a popular operating system used on embedded devices that a project from the Linux Foundation called Yoctoa is now ded-
icated to creating custom Linux distributions for embedded devices. Similarly, Google is working on Brillo,b an extension of Android (which is built on Linux as well) for the IoT,
and Ubuntu launched Ubuntu Core for the IoT.c Although Linux is massively dominating,
there are a few alternatives to Linux in this space, such as Windows 10 for IoT.d We’ll
be using Linux Raspbian on the Pi, but you can also try Yocto, Ubuntu Core, or Windows 10, all of which run on the Pi (from the Pi 2 onward).
a https://www.yoctoproject.org/
b https://developers.google.com/brillo/?hl=en c http://www.ubuntu.com/internet-of-things d https://dev.windows.com/en-us/iot
87
The world of embedded devices
4.1.3 Summary and beyond the Pi
In this section, we offer a condensed introduction to the world of embedded devices. Obviously, entire books have been written on this topic, so we didn’t try to be exhaus- tive in our search, but rather tried to give you a general understanding of the options and elements to consider when choosing a hardware platform for your project.7
We decided to use the Raspberry Pi as the reference platform for the remainder of this book. Why the Raspberry Pi? First, because it’s arguably the most popular embed- ded platform along with the Arduino. But unlike the Arduino,8 it was built for Linux
and was designed from the beginning to be a powerful and accessible platform for automation and the Internet of Things. Basically it will let you dig into the web and the Internet of Things without having to deal with all the challenges (for example, C programming, limited RAM, and rough tools) that come with RTOS platforms.
Moreover, as you’ll see in the next section, it’s inexpensive and is available in many countries, and it comes with several USB ports for accessories and an HDMI output to connect it to your TV or screen. Because it’s based on Linux and offers direct access to the OS and therefore Node.js, all the required dependencies can be easily installed on it as you would install things on your PC.
This does not mean the Pi is the only option to run the code provided in this book or to learn the Web of Things. As long as you pick a device that can run Node.js, you’re pretty much good to go.
FROM PROOF OF CONCEPT TO INDUSTRIAL PROTOTYPE
The Pi is definitely one of the easiest ways to get started with the WoT and to build all sorts of prototypes. But for anything more serious, such as industrial and commercial- grade prototypes or actual products, the classic version of the Pi isn’t the best choice. There are a number of reasons for this, but the two main ones are the use of an SD card as storage and the form factor of the Pi.
Relying on an SD card doesn’t work well in the real world. SD cards have a limited lifespan. They also occasionally break, get corrupted, or could be easily dislodged. A more practical way to store the operating system and the data you need is to rely on on-board flash memory. Like SD cards, flash memories are persistent, meaning that the data will be kept even if the device isn’t powered. They’re soldered to the boards and are also faster and more stable than SD cards.
Furthermore, the Pi wasn’t primarily designed to be used in commercial applica- tions and therefore has more components and connectors that might not be required for most use cases.
Does that mean you won’t be able to apply what you learn here to building a com- mercial-strength prototype? Not at all! Real-world-ready platforms are available that are compatible with what you’ll learn in this book and with the code you’ll write.
7 A good roundup of popular hardware platforms can be found here: http://postscapes.com/internet-of-
things-hardware.
88 CHAPTER 4 Getting started with embedded systems
First, there’s a version of the Pi that’s much more realistic for the real world: the Pi Compute Module.9 The Compute Module is an embedded platform similar to the
classic Pi, but with a much smaller form factor and an on-board flash memory. Also, the Pi Zero is the latest addition to the Pi family and the smallest (65mm x 30mm). Add to that a shocking $5 price tag and it also becomes a viable candidate to be embedded within a commercial solution, although it lacks network connectivity or on- board flash memory. Similarly to the Pi Zero, the C.H.I.P10 has a small form factor and
for $9 also offers Wi-Fi and Bluetooth connectivity.
Other very good (but slightly more expensive) alternatives exist, such as the Intel Edison,11 which, like the Pi 3, also supports Bluetooth Low Energy (see chapter 5) and
Wi-Fi out of the box but is much smaller and has an onboard flash.
If you’re looking for a device to build more mechanically stable prototypes without having to solder too much, you should consider the BeagleBoard12 platform from
BeagleBone, a platform similar to the Pi but known for its robustness and stability. Moreover, the BeagleBone offers both SD card and flash-based storage, making it easy to move your prototype from a concept to a real-world trial.
Plenty of other platforms are available, but what’s interesting with the Pi, Beagle- Bone, and Edison is that they all run on Linux and support Node.js. This means that most of the examples will run out of the box on these three platforms (and many oth- ers), but some more advanced examples, such as those using GPIOs, might need a slightly different setup or alternative libraries. The appendix gives you a number of pointers on using the architecture, concepts, and code examples of this book with other devices such as the BeagleBone Black, the Intel Edison, and the Arduino boards.
4.2
Set up your first WoT device—Raspberry Pi
So far, this chapter has described the world of embedded systems and the various aspects to consider when choosing the most appropriate hardware platform for your own project. Next, we briefly cover the software layer running on top of your hardware platform by discussing the operating system on the device and describing how Java- Script and particularly Node.js are a very interesting application-development ecosys- tem for building Web of Things devices.
4.2.1 Meet the Raspberry Pi
The Raspberry Pi is a popular series of single-board computers: think of an entire com- puter not much larger than the credit card you used to buy it (see figure 4.1). These devices were developed by the Raspberry Pi foundation primarily as educational tools for more people to learn about basic computer science and physical computing.
9 https://www.raspberrypi.org/products/compute-module/ 10http://getchip.com
11http://www.intel.com/content/www/us/en/do-it-yourself/edison.html 12http://beagleboard.org/BLACK
89
Set up your first WoT device—Raspberry Pi
The most disruptive model to date is the Raspberry Pi Zero. This device kicked off a little revolution: a full Linux computer for $5, a price tag usually reserved to the resource limited, low-cost RTOS devices out there. The Pi Foundation went so far as to give it away for free, attached to the December 2015 issue of The MagPi magazine,13
showing that the days when computers will be attached to any object for a ridiculously low cost aren’t that far away!
In terms of performance, the Pi Zero is pretty similar to the Pi A, but its ARMv6 CPU is overclocked at 1 GHz and it boasts twice as much RAM. For only 9 grams, it also features a micro-SD card slot, a mini-HDMI socket, and two micro USB ports (one for power, the other for data).
The latest Pi model at the time of writing is the Pi 3. It’s more expensive than the Pi Zero, but it also has significantly more to offer. The Pi 3 boasts a quad-core 1.2 GHz CPU, 1 GB RAM, a micro-SD slot, and a Broadcom VideoCore IV graphic unit. In terms of connectivity, the Pi 3 has four USB ports (and a Micro USB, which is used to power it), an HDMI port, a 3.5mm jack, an Ethernet connector, and 40 general-purpose input/output ports (GPIO). Finally, unlike its predecessors, the Pi 3 also offers out-of- the-box Wi-Fi and Bluetooth connectivity, making a fully WoT-ready device. All of that for a mere 45 grams in total weight!
4.2.2 Choosing your Pi
All the examples in this book were tested on the Pi B+, Pi 2, Pi 3 and Pi Zero. Which one should you grab?
13https://www.raspberrypi.org/magpi/raspberry-pi-zero-out-today-get-it-free-with-the-magpi-40/ Micro HDMI Micro USB (data) Micro USB (power) GPIOs CPU, RAM, cache
GPIOs SD card slot USB (4x) Micro USB (power) Pi 3 Pi Zero HDMI Audio out Ethernet
90 CHAPTER 4 Getting started with embedded systems
If you’re just beginning with the IoT and embedded devices, the Pi 3 (or any suc- cessor) is a safe choice: it offers all the required connectivity out of the box and doesn’t require any soldering to connect sensors and actuators to the GPIOs. But the Pi 3 is significantly larger and also more power hungry (4 watts for the Pi 2 or 3 versus 0.8 watts for the Pi Zero).