Chapter Goal: Understand What the IOT Is and How to Prototype IOT Devices
Topics Covered in This Chapter:
What is IOT
Choosing a Raspberry Pi Model
Choosing your IOT Device
Characterization of IOT Devices
Buying the right tools to deal with Hardware
Writing code in Python and in the Arduino IDE
The IOT is a name for the vast collection of “things” that are being networked together in the home and workplace (up to 20 billion by 2020 according to Gardner, a technology consulting firm). That is a very vast collection. And they may be underestimating it.
We all have large numbers of computers in a modern house. I just did a walkthrough of my house, ignoring my office (which is filled with another ∼100 computers). I found 65 different devices having embedded computers. I’m sure I missed some of them. Now of those computer-based devices, I counted 20 of them that have IP addresses, although I know that I am missing a few (such as the thermostat). So in a real sense, this house has 20 IOT devices. And it is only 2016 as of the writing of this book. With over 100 million households in the United States alone, 20 billion IOT devices somehow don’t seem so many.
So what are the three defining characteristics of the IOT?
Networking - these IOT devices talk to one another (M2M communication) or to servers located in the local network or on the Internet. Being on the network allows the device the common ability to consume and produce data.
Sensing - IOT devices sense something about their environment.
Actuators - IOT devices that do something. Lock doors, beep, turn lights on, or turn the TV on.
Of course, not every IOT device will have all three, but these are the characteristics of what we will find out there.
Is the IOT valuable? Will it make a difference? Nobody is sure what the killer application will be, but people are betting huge sums of money that there will be a killer application. Reading this book and doing the projects will teach you a lot about the technology and enable you to build your own IOT applications.
The Raspberry Pi family of single board computers (see Figure 1-1) is a product of the Raspberry Pi Foundation (RaspberryPi.org). They have sold over 9 million of these small, inexpensive computers. The Raspberry Pi runs a number of different operating systems, the most common of which is the Raspian release of Unbuntu Linux.
Figure 1-1. Raspberry Pi 2
Like Windows, Linux is a multitasking operating system, but unlike Windows, it is an open source system. You can get all the source code and compile it if you wish, but I would not recommend that to a beginner.
One of the best parts of the Raspberry Pi is that there are a huge number of device and sensor drivers available, which makes it a good choice for building IOT projects, especially using it as a server for your IOT project. The Raspberry Pi is not a low-power device, which limits its usage as an IOT device. However, it is still a great prototyping device and a great server.
There is a rather bewildering variety of Raspberry Pi boards available. I suggest for this book that you get a Raspberry PI 2 or Raspberry Pi 3. While the $5.00 Raspberry Pi Zero is tempting, it takes quite a bit of other hardware to get it to the point where it is usable. While the Raspberry Pi 3 is more expensive ($35), it comes with a WiFi interface built in and extra USB ports.
Note that we are using the Raspberry Pi A+ for building the IOTWeatherPi weather station later in this book. The reason for that is power consumption: one-half to one-third of the power used by the more powerful Raspberry Pi models.
There are many great tutorials on the Web for setting up your Raspberry Pi and getting the operating system software running.
If you think the list of Raspberry Pi boards available is bewildering, then wait until you look at the number of IOT devices that are available. While each offering is interesting and has unique features, I am suggesting the following devices for your first projects in the IOT. Note that I selected these based upon the ability to customize the software and to add your own devices without hiding ALL the complexity, hence reducing the learning involved. That is why I am not using Lego-type devices in this book.
We will be using the following:
ESP8266-based boards (specifically the Adafruit Huzzah ESP8266)
Arduino Uno and Arduino Mega2560 boards
When looking at a new project, the first thing to do to understand an IOT project is to look at the six different aspects for characterizing an IOT project.
Communications
Processor Power
Local Storage
Power Consumption
Functionality
Cost
When I think about these characteristics, I like to rate each one on a scale from 1–10, 1 being the least suitable for IOT and 10 being the most suitable for IOT applications. Scoring each one forces me to think carefully about how a given project falls on the spectrum of suitability.
Communications are important to IOT projects. In fact, communications are core to the whole genre. There is a trade-off for IOT devices. The more complex the protocols and higher the data rates, the more powerful processor you need and the more electrical power the IOT device will consume.
TCP/IP base communications (think web servers; HTTP-based commutation (like REST servers); streams of data; UDP - see Chapter 2) provide the most flexibility and functionality at a cost of processor and electrical power.
Low-power BlueTooth and Zigbee types of connections allow much lower power for connections with the corresponding decrease in bandwidth and functionality.
IOT projects can be all over the map with requirements for communication flexibility and data bandwidth requirements.
IOT devices having full TCP/IP support are rated the highest in this category, but will probably be marked down in other categories (such as Power Consumption).
There are a number of different ways of gauging processor power. Processor speed, processor instruction size, and operating system all play in this calculation. For most IOT sensor and device applications, you will not be limited by processor speed as they are all pretty fast. However, there is one exception to this. If you are using encryption and decryption techniques (see Chapter 7), then those operations are computationally expensive and require more processor power to run. The trade-off can be that you have to transmit or receive data much more slowly because of the computational requirements of encrypting/decrypting the data. However, for many IOT projects, this is just fine.
Higher processor power gives you the highest ratings in this category.
Local storage refers to all three of the main types of storage: RAM, EEPROM, and Flash Memory.
RAM (Random Access Memory) is high-data rate, read/writable memory, generally used for data and stack storage during execution of the IOT program. EEPROM (Electrically Erasable Programmable Read Only Memory) is used for writing small amounts of configuration information for the IOT device to be read on power up. Flash Memory is generally used for the program code itself. Flash is randomly readable (as the code executes, for example), but can only be written in large blocks and very slowly. Flash is what you are putting your code into with the Arduino IDE (see Chapter 2).
The amount of local storage (especially RAM) will add cost to your IOT Device. For prototyping, the more the merrier. For deployment, less is better as it will reduce your cost.
Power consumption is the bane of all IOT devices. If you are not plugging your IOT device in the wall, then you are running off of batteries or solar cells and every single milliwatt counts in your design. Reducing power consumption is a complex topic that is well beyond the introductory projects in this book. However, the concepts are well understood by the following:
Put your processor in sleep mode as much as possible.
Minimize communication outside of your device.
Try to be interrupt driven and not polling driven.
Scour your design looking for every unnecessary amount of current.
The higher the number in this category, the less power the IOT unit uses.
This is kind of a catch-all category that is quite subjective. For example, having additional GPIOs (General Purpose Input Outputs) available is great for flexibility. I am continuously running into GPIO limitations with the Adafruit Huzzah ESP8266 as there are so few pins available. Having additional serial interfaces are very useful for debugging. Special hardware support for encryption and decryption can make device computer security much simpler. One of the things that I miss in most IOT prototyping system is software debugging support hardware.
I also include the availability of software libraries for a platform into this category. A ten means very high functionality; low numbers mean limited functionality.
What is an acceptable cost for your IOT device? That depends on the value of the device and the market for your device. A $2.50 price can be great for prototypes, but will be the death of the product in production. You need to size the price to the product and the market. High numbers are low-cost units and low numbers are higher-cost devices.
Anything is more difficult without the right tools. When you make the jump from just doing software to doing a software / hardware mix, here is a list of tools you should have:
30W adjustable temperature soldering iron - heating and connecting wires
Soldering stand - to hold the hot soldering iron
Solder, rosin-core, 0.031” diameter, 1/4 lb (100g) spool - to solder with
Solder sucker -Useful in cleaning up mistakes
Solder wick/braid 5 ft spool - Used along with the solder sucker to clean up soldering messes
Panavise Jr - General purpose 360 degree mini-vise
Digital Multimeter – Good all-around basic multimeter
Diagonal cutters - Trimming of wires and leads
Wire strippers - Tool for taking insulation off wires
Micro needle-nose pliers - for bending and forming components
Solid-core wire, 22AWG, 25 ft spools - black, red, and yellow for bread-boarding and wiring
Adafruit has an excellent beginners kit for $100 [ https://www.adafruit.com/products/136 ]. Figure 1-2 shows the tools that are in it.
Figure 1-2.Adafruit Electronics Toolkit
All the code in this book is in two languages. Specifically, Python is used for the Raspberry Pi and C/C++ (don’t be scared, there are many examples and resources) for the Arduino IDE.
Python is a high-level, general purpose programming language. It is designed to emphasize code readability, and it especially keeps you out of having loose pointers (a curse of all C/C++ programmers) and does the memory management for you. This is the programming language of choice for the Raspberry Pi. Python has the largest set of libraries for IOT and embedded system devices of any language for the Raspberry Pi. All of the examples in this book that use the Raspberry Pi, use Python. I am using Python 2.7 in this book, but it is relatively easy to convert to Python 3.5. However, it is not so trivial to find all the libraries for Python 3.5, so I suggest staying with Python 2.7 .
Why are we using C++ for the majority of the IOT devices? There are four reasons for this:
C programs are compiled into native code for these small devices, giving you much better control over size and timing. Python requires an interpreter, which is a large amount of code that would not fit on small IOT devices, such as the Arduino. On a Raspberry Pi, you may have a Gigabyte (GB) of RAM and 8GB of SD Card storage. On an IOT device, you might only 2,000 bytes (2K) and 32KB of code storage. That is a ratio of 500,000 to 1. That is why you need efficient code on IOT devices. Yes, there is MicroPython, but it is very limited and still uses more memory than most Arduino boards.
When you program in C/C++, you are closer to the hardware and have better control of the timing of operations. This can be very important in some situations. One of the issues of Python is that of the memory garbage collector. Sometimes, your program will run out of memory and Python will invoke the garbage collector to clean up memory and set it up for reuse. This can cause your program to not execute in the time you expected. An interesting note is that the ESP8266 used in several chapters of this book also has a memory garbage collector, which can cause some issues in critical timing sequences. None of are known to exist in the code used in this book. Keeping fingers crossed, however.
Libraries, libraries, libraries. You can find Arduino C/C++ libraries for almost any device and application you can imagine for IOT applications. The Arduino library itself is filled with large amounts of functionality, making it much easier to get your IOT application up and running.
Finally, the Arduino IDE (Integrated Development Environment) is a good (but not great) environment for writing code for small devices. It has its quirks and some disadvantages. The number one disadvantage of the Arduino IDE is that it does not have a built-in debugger. Even with this significant disadvantage, it runs on Linux, Windows, and Mac, and we will use it in this book. The Arduino IDE is widely available, and there are many resources for learning and libraries designed for this. Other alternatives include Visual Micro (runs on Windows, built on Microsoft Visual Studio) and Eclipse (runs on Linux, Windows, and Mac). Eclipse can be a nightmare to set up and update, but they have made improvements in the past few years .
What will we be doing in future chapters? We will be building real IOT projects that actually have a lot of functionality. Yes, it is fun to blink an LED, but it is only the first step to really doing interesting and useful things with all this new technology. Build an IOT weather station. Build an IOT light swarm. Build your own IOT device with your own sensors. It is all accessible and inexpensive and within your ability whether you are an engineer or not.