Constructing and Programming An IoT Device
Constructing and Programming An IoT Device
2 Programming an IoT
Device
Lesson Time: 4 hours, 5 minutes
Lesson Introduction
Internet of Things (IoT) devices provide the foundation around which an IoT solution is
constructed. Yet for many IT professionals, these devices may be the least familiar piece of
the solution, involving embedded devices and constraints upon size, processing, memory,
and energy consumption unlike those of traditional information technology systems.
Lesson Objectives
In this lesson, you will:
• Select a processing unit that meets requirements for an IoT device.
• Select a power source that meets requirements for an IoT device.
• Select and use a programming toolkit to program an IoT device.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
28 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
TOPIC A
Select and Configure a Processing Unit
At the core of a smart device is its processing component, called a microcontroller or
microprocessor, that enables the device to process data and commands. A wide range of such
components is available, and various attributes must be considered when designing a device.
Microcontrollers
Desktop computers, servers, PLCs, and other large scale computing systems are well suited for
business processes and industrial automation solutions, such as a single assembly line or
manufacturing cells, sales automation, customer relationship management (CRM), and so forth. But
some automation scenarios, such as those involving IoT devices, call for automation on a much
smaller scale. For such situations microcontroller units (MCUs) provide an appropriate
processing capability.
A microcontroller is a single chip (typically no larger than your finger) that provides many of the
functions of a complete computer system, like a PC or a PLC. In fact, the expression system on a
chip (SoC) is often used to refer to microcontrollers with a large number of onboard components.
With its compact size, relatively low cost, and low power requirements, a microcontroller provides a
more appropriate development platform than a larger system like a PLC or a PC for developing
small, portable IoT devices.
The following attributes of microcontrollers make them very useful for a variety of automation
projects.
• Very small, supporting development of wearable devices, personal consumer products, and so
forth
• Consumes relatively little power
• Relatively inexpensive
• Includes numerous I/O pins, enabling it to connect a wide variety of sensors and control devices
• Enables you to incorporate processing capabilities to make a smart device
• Easy to prototype using tools such as an electronics breadboard
Because of these attributes, microcontrollers can provide the basis for consumer IoT devices, smart
products (cars, appliances, thermostats, etc.), remote data sensors for industrial, scientific, or
consumer use, and many other types of embedded systems—objects like IoT devices that have a
complete computer embedded within them.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 29
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
30 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Platform Description
PIC • Pronounced as "pick."
• Programmable Intelligent Computer (originally stood for Peripheral
Interface Controller).
• Originally developed by General Instruments in the mid-1970s.
• Now produced by Microchip Technology.
• Popular with hobbyists and industrial automation developers.
8051 • Formal name is Intel MCS-51.
• CISC microcontroller developed in 1980 for use in embedded
systems.
• No longer manufactured by Intel.
• Remains a popular design through various compatible processors
made by other vendors.
ARM • Acorn RISC Machine/Advanced RISC Machine.
• Developed by ARM Holdings for use in the Acorn Computer BBC
Micro computer.
• 32-bit Reduced Instruction Set Computer (RISC) architecture.
• Low power, but powerful enough to operate in relatively demanding
embedded devices.
• Used in many mobile phones, commercial IoT devices, and netbooks.
• Produced under license by companies such as Apple, Qualcomm, and
Samsung.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 31
Platform Description
AVR • AVR is an 8-bit RISC architecture developed by Atmel and used in
Arduino developer boards.
• Among the first microcontrollers to use on-chip flash memory
(rewritable) instead of EPROM or EEPROM (write once).
• Stands for Advanced Virtual RISC or Alf and Vegard's RISC
processor.
Note: Sources variously state that AVR stands for Advanced
Virtual RISC or Alf and Vegard's RISC processor (after the names
of its Norwegian designers, Alf-Egil Bogen and Vegard Wollan).
FPGAs
When designers at organizations like Intel, AMD, and Qualcomm produce a new CPU design, they
use special software to define a blueprint that describes a layout of millions of microscopic
electronic switches, paths that serve as "wiring," and various other components. The blueprint they
define is reproduced at a very small size in semiconductor materials such as silicon to create a
processor with particular capabilities.
As you can imagine, the process of creating a single chip in this manner requires highly specialized
equipment and skills. Field programmable gate arrays (FPGAs) provide an alternative way to
create custom microchips without requiring all of the special equipment of a chip fabrication facility.
FPGAs are essentially chips that can be "rewired" by software. They provide pre-defined logic
blocks that can be connected and configured through software to remap the circuits inside the chip.
They enable a programmer to create custom chips without having any chip fabrication equipment.
This may sound somewhat like a microprocessor or microcontroller, which can be programmed.
However, FPGAs enable you to essentially redefine the design of the processor itself.
In some situations, FPGAs provide some advantages over microcontrollers and microprocessors,
and can be quite useful for certain applications within IoT projects.
Microcontrollers and microprocessors are typically designed to execute software programs running
in series—one instruction after another. While they may be capable of running multiple operations
in parallel, they are essentially serial processors.
FPGAs, on the other hand, are essentially parallel processors, making them well suited for highly
parallel tasks such as processing signals from multiple sensors, digital signal processing, software-
defined radio, cryptography, industrial process control, and so forth.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
32 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Because they are reprogrammable, they are ideal for prototyping or production of specialized chips
in small numbers. If numerous copies are needed once the design has been thoroughly tested, the
design can be made permanent using application-specific integrated circuits (ASICs).
For the most part, programming an FPGA is very different from writing code in a language such as
C/C++, Java, and so forth. But new development tools make it easier and faster to program
FPGAs. They are often priced similar to microcontrollers, making them a practical alternative to
microcontrollers for custom designs that benefit from parallel processing capabilities.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 33
ACTIVITY 2-1
Selecting Processors for an IoT Project
Data File
C:\095024Data\Constructing and Programming an IoT Device\IoT Processor Worksheet.docx
Scenario
Working with a team of university researchers, you're designing a network of approximately 300
sensor devices to be located throughout a 50 square mile area of remote desert spanning the Texas-
New Mexico border. Each sensor device will detect groundwater levels, chemistry, and seismic
activity. The sensor devices will use a wireless network to periodically transfer the collected data to a
gateway device, which will perform some filtering and processing tasks on the sensor data before
forwarding it on to the university over a satellite connection.
In the following spaces, write your ideas regarding requirements for the processors you would use in
this scenario. (If you'd prefer to type your ideas in a Word document, a worksheet is provided in C:
\095024Data\Constructing and Programming an IoT Device\IoT Processor Worksheet.docx.)
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
34 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Maker/Hobbyist Platforms
Various platforms, including developer boards, shields, programming tools, documentation, and a
support community, have been developed to support makers and other hobbyists interested in
designing and creating their own electronic devices. Because of the widespread support for these
platforms, the relative ease in adding sensors and actuators to them, and relatively easy-to-use
programming tools, these platforms are commonly used by makers, hobbyists, and others to
develop custom IoT devices for prototyping, experimentation, education, and entertainment.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 35
Platform Description
Arduino Arduino is an open source single-board MCU geared toward
development of interactive devices that can sense and control objects in
the physical and digital world. Arduino also refers to a company that
developed the original Arduino board, a programming environment for
programming the boards, the user community that arose around them,
and the project that all of these things originated from.
The Arduino project (named after Arduin of Ivrea, who was King of Italy
from 1002 to 1014) started in 2003 at the Interaction Design Institute in
Ivrea, Italy to provide an inexpensive platform to enable non-engineers to
develop robots, environmental telemetry sensors, and other MCU-based
projects.
The project's open source licensing permits the manufacture of Arduino
boards and software distribution by anyone. As a result, hundreds of
inexpensive clones have been developed, as well as many add-on boards,
called shields.
Raspberry Pi Raspberry Pi is a family of small single-board computers (SBCs) based on
ARM chips. It was developed by the Raspberry Pi Foundation in the
United Kingdom, and was intended to play a role similar to the BBC
Micro—an inexpensive PC for teaching basic computer science concepts
in schools and in developing countries.
It was quickly adopted by hobbyists and experimenters for robotics,
home automation, and IoT. Components such as cases, keyboards, and
mice had to be purchased separately or in a bundle, and many third-party
add-ons have been released for the computer.
Unlike Arduino developer boards, which don't use a full operating
system, Raspberry Pi is typically operated using a compact version of
Linux (Raspbian) or one of the numerous third-party operating systems
that are available for installation on the computer through an SD card.
Examples include Windows 10 IoT Core, Android Things, RISC OS,
FreeBSD, and many others.
Particle The Particle family of development boards is geared toward IoT
applications. Particle offers a complete IoT solution from a single vendor
that includes hardware, an inexpensive cellular data plan, cloud
processing, and IoT apps. This product line is geared toward consumers
and hobbyists who don't want to assemble a solution with components
from separate vendors. Particle provides a web-based programming
environment, smartphone app, and visual programming tools that you
can use to develop a custom IoT solution.
Note: The NodeMCU ESP8266 developer board used in this course is provided in a very small,
inexpensive, low-power package that includes onboard Wi-Fi, making it well suited for IoT
projects. The ESP8266 can be programmed using a variety of programming languages, including
its native AT commands, Lua programming language, and MicroPython. Although the
NodeMCU ESP8266 is not an Arduino, using add-on libraries, you can program it like an
Arduino using the Arduino IDE. (Some developer boards that adhere to the Arduino form
factor, and can therefore use Arduino shields, have been developed.)
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
36 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
On-Chip Peripherals
I/O
Microcontrollers provide various combinations of input and output (I/O) pins used to make
connections to data sensors, displays, actuators, and other components. Some pins work with
analog data values, while others work with digital data. It is important to understand the difference
between analog and digital I/O, as it may help to determine what type of microcontroller, developer
board, sensors, and actuators you should use, as well as how data must be acquired.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 37
Digital processors represent data in digital switches (called bits) that are either on or off. This may be
all you need to represent certain types of information.
For example, you might need to sense when someone has stepped onto a door mat. You only need
to know whether they are on or off the mat. A single bit of data—one on/off switch in memory—
would be adequate to describe the mat state. The state is represented as a 1 or 0, also called HIGH
or LOW.
When someone steps on the mat, the sensor completes a connection with a power source, creating a
HIGH condition on the microcontroller pin connected to the sensor. When the person steps off the
mat, the sensor breaks the connection, changing the power flowing to the pin back to a LOW
condition.
In a situation like this, the sensor controls a simple on or off state, and one pin on the
microcontroller is adequate to read the sensor. Electronic sensors that behave this way are often
called digital sensors because they represent discrete values that can be represented by the digits 0
or 1.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
38 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
One way to electronically represent a range of continuous values is to use the voltage level to
represent the changing values. If the sensor can detect temperatures between 0 and 100 degrees
Fahrenheit and you have an electronic circuit that can safely vary between 0 and 5 volts, you could
use 0 volts to represent 0 degrees and 5 volts to represent 100 degrees. The various voltage levels in
between would represent all other temperatures in the measured range. With this scheme, you still
only have to use one microcontroller pin to read the value from the sensor, but you can obtain a
continuous range of values.
The voltage level provided by the sensor varies as an analog for the varying temperature, so this type
of sensor is called an analog sensor.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 39
Of course, digital computers process digital values. So once an analog signal is provided to the
microcontroller, it must convert the analog signal to a digital value it can work with.
This involves determining the range the analog value represents—for example, a voltage between 0
and 5 might be represented by a digital number between 0 and 255. Once the digital value is
determined, that number must be represented in binary form (a number represented by zeroes and
ones).
Since the previous example (a sensor with a range between zero and 100) uses nice, round numbers,
the conversion would be relatively easy to perform in your head. On the low end of the
measurement scale, zero volts represents a temperature of zero, so no conversion is needed. On the
high end of the scale, five volts represents a temperature of 100. Five goes into 100 20 times, so in
this example you can just multiply the voltage by 20 to get the temperature it represents. A 2.86 volt
sensor reading would equate to a temperature of 57.2 degrees, which is rounded to 57.
The decimal number 57 is represented by the binary value 00111001. An integer in this format can
be processed by a digital computer.
Microcontrollers include an ADC component that enables them to perform these types of
operations quickly. Certain pins on the microcontroller may be dedicated to reading analog sensor
data that is processed through an ADC to read analog sensor data that is converted to digital values.
Signal Conditioning
Signal conditioning is the process of preparing an analog signal so it is in a useful state for the next
stage of processing. For example, analog sensor outputs may need to be adjusted in some way
before they can be passed on to an analog-to-digital converter (and from there on to a
microcontroller).
Various operations may need to be performed to get the signal ready for the ADC, such as:
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
40 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
• Filtering: Removing false signals or "noise" (such as noise induced through close proximity to an
AC power line).
• Amplification: Increasing voltage to make small differences in reading more apparent (greater
precision).
• Attenuation: Decreasing voltage to make a wider range of readings possible (greater range).
Once you have a digital value, you then have the problem of how to communicate it. If you have a
number represented in 8 bits, and you have 8 digital I/O pins to work with, you could
simultaneously set them on or off in the combination that represents the number. In one instant (a
clock cycle), all 8 pins can be read by another device to retrieve the entire 8-bit value. This type of
data communication is called parallel because you are sending 8 bits of data at the same time, in
parallel.
But if you have a limited number of pins and wires at your disposal, using just a single data pin over
8 consecutive clock cycles, you could send a series of pulses one after the other to represent the
data. This approach is called serial communication because you are sending 8 bits of data one after
the other, in series.
For data transmissions that use same signal frequency, serial communication is not as fast as parallel
communication. But it takes fewer wires or conductors to perform the serial transfer of data. And
over long distances, it can be very complicated to keep the signals on parallel data lines in sync with
each other, and to prevent the lines from creating electromagnetic interference (crosstalk) with each
other. Parallel connections are often used for very short, high-speed connections (such as inside a
microcontroller, on a motherboard, or inside a computer case), and most longer distance
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 41
communications are typically performed using simpler, more reliable, and generally less expensive
serial connections.
Differential Signaling
Embedded devices, such as those used in IoT, function with very low power levels. Unfortunately,
low power transmissions are prone to electrical noise. Fortunately there is a way to mitigate the
problem of line noise in low power transmissions, called differential signaling.
The following figure shows two twisted pair data lines within the same cable, one carrying a flat
positive signal, and the other carrying a flat negative signal. When crosstalk from a nearby
electromagnetic source affects the signals, they are both affected the same way, showing an upward
spike and a downward spike. The voltage difference between the two lines essentially remains
constant, even during the spikes.
An electronic circuit called a subtractor can determine the voltage difference between the two lines
and produce it as output. Since the difference remains constant even during noise episodes, this
essentially removes the noise.
Figure 2-8: Using a subtractor to output the difference between two data lines.
Conventional signaling (also called single-ended signaling) uses a single voltage referenced against
a fixed ground value to provide a signal. The value on the data line increases or decreases to
represent the signal, but the value on the ground line remains fixed. Differential signaling splits the
voltage difference across two lines, as shown in the following figure.
If the clean original signal were passed through a subtractor, it would produce the difference
between the two lines as an output.
At the receiving end of a data transmission, noise may have been injected into the two data lines.
When the lines are passed through the subtractor, the noise from both lines will again cancel each
other out, leaving only the voltage difference from the original data signals.
Digital Sensors
Figure 2-10: Digital sensor using serial or parallel connection to the microcontroller.
Digital sensors that measure analog data provide their own analog-to-digital conversion capabilities
so they can send a digital value directly to the microcontroller, rather than requiring the
microcontroller to perform analog to digital conversion. Digital sensors provide multiple data pins
or wires for a parallel data connection or a single data pin or wire for a serial connection. (A ground
pin or wire is also provided to complete the circuit, and other control wires may be used as well.)
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 43
Note: The pin labels printed on your ESP8266 microcontroller board may differ slightly from
those shown here.
MCUs and developer boards include a certain number of general purpose digital I/O pins, and may
include analog I/O pins as well. The number and type of I/O pins may influence your decision on
which product to use as the basis for your IoT projects. The following are various types of data pins
commonly found on microcontrollers and microcontroller development boards.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
44 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 45
Note: Other microcontrollers may include other types of pins, depending on the features they
provide. For example, some microcontrollers include a CAN pin to support controller area
networking, commonly used to enable multiple microcontrollers (in a motor vehicle, for
example) to communicate with each other.
PWM
Another technique related to conversion between digital and analog is pulse width modulation
(PWM). PWM is a digital encoding technique that involves alternating a binary signal between high
and low voltage states over a brief time period to represent more states than just on and off—in
other words, analog values. For example, you might use a signal that is high 25% of the time (a 25%
duty cycle) to represent a value of 25.
If the signal remains constantly high in this case, it would represent a value of 100.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
46 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Quadrature Encoder
In some cases, you may need to measure an analog position or speed (such as the rotation of a
motor or the shaft of a rotating control knob) using digital sensors. A quadrature encoder makes this
possible. With quadrature encoding, the moving object that you need to measure is marked with
evenly spaced tracks of holes or markers that pass by one or more digital sensors. Pulses are
detected as the marks pass by. With some data processing, the pattern of these pulses can be used to
determine speed. With two tracks of slightly offset markers, the direction of rotation can also be
determined. A third track can be used to provide orientation (the "zero" position, for example).
Note: All of the Guidelines for this lesson are available as checklists from the Checklist tile on
the CHOICE Course screen.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 47
• Supports the types of data communication you'll be using, such as Wi-Fi, Bluetooth, cellular, and
Ethernet.
• Supports the number and types (such as analog, digital, PWM) of I/O pins you'll need to
connect with the sensors and actuators you'll be using.
• If an ADC is required, provides a data resolution (number of data bits) sufficient for the
precision your project requires.
• Operates with electrical power (e.g., current and voltage levels) corresponding to the power
sources you intend to use.
• Provides sufficient data processing capabilities to meet your project requirements.
Note: There is generally a tradeoff between performance and efficiency, and many variables
to consider. Consider developing a prototype of your IoT device so you can find a good
balance, perhaps after some experimentation and adjustments to the design. Consider using a
development board or evaluation kit to get something operational quickly. Later, you can
develop your own custom board, if necessary, to optimize the form factor and performance.
• Supports memory and storage adequate to operate the software you'll run on the device.
• Provides power management capabilities in line with the project requirements, such as
supporting low-power modes you intend to use, providing a fast wake-up time, and so forth.
• If encryption or other processing-intensive tasks are required, includes modules to offload these
tasks from the processor, or provides adequate processing power, storage, and memory to
implement in the main processor.
• Provides security options your configuration will require, such as:
• Cryptographic bootloader to ensure you can perform secure over-the-air (OTA) updates
• Cryptographic hardware accelerator that supports the encryption protocols you'll be using
• Built-in tamper protection against botnets, etc.
• Conductive shield layers
• Memory protection
• Can be upgraded remotely (OTA updates).
• To conserve power, does not include components you don't need, or enables unused
components to be shut off.
• Is well documented.
• Is supported with good development tools, frameworks, and code libraries.
• Is supported well by the manufacturer and development community.
• Is priced appropriately and available in the quantities and timeframe required for the project.
Note: The supply of some MCUs is inconsistent. Typically they are much less expensive
when purchased in quantity. Lead times for delivery can be as long as several weeks in some
cases.
• Provide buttons, LEDs, and other components to provide a local user interface on each device.
• Interact with a remote user interface (e.g., web-based) so sensor device can be managed
remotely.
• Run simple programs to obtain sensor data and send it to a local IoT gateway for aggregation
before being forwarded to the cloud for analysis.
For this project, you are evaluating the feasibility of using the NodeMCU ESP8266 microcontroller
board. The following are various attributes you might consider when deciding which microcontroller
and development board to use for a particular IoT project.
Attribute Considerations
Microcontroller • Microcontroller: ESP8266 System on a Chip (SoC).
specifications • Processor: Tensilica L106 32-bit RISC microcontroller unit (MCU)
running at 80 MHz.
• Memory: 32 KB instruction RAM, 32 KB instruction cache RAM, 80
KB user data RAM, 16 KB ETS system data RAM, External Flash: Up
to 16 MB (512 KB to 4 MB typical).
• Interface buses: SPI, I2C, I2S, UART.
Size and form factor •
Small: Some ESP8266 development boards (such as Wemos D1 and
Arducam ESP8266 Uno) are manufactured to use the Arduino form
factor, enabling them to be easily connected with Arduino shields.
• Smaller: Development boards such as the NodeMCU board used in
this course are much more compact, making them well suited for
situations requiring a small form factor, but making them unable to
accept an Arduino plug-in shield.
• Smallest: Boards such as the ESP-01 Module are even smaller, but
they include a very limited number of I/O pins, limiting their use as
multiple-sensor devices. The ESP-01 Module is often used as a serial
to Wi-Fi bridge to provide another microcontroller with Wi-Fi
capabilities.
Communication • Supports Wi-Fi (IEEE 802.11b/g/n at 2.4 GHz) with WEP or WPA/
WPA2 authentication (or open), making it well suited for networks
that do not support other protocols more specific to IoT devices, such
as Zigbee.
• The Wi-Fi radio is limited to 2.4 GHz, which is frequently used by Wi-
Fi devices, and can be subject to a lot of traffic.
I/O pins • Up to 11 usable general purpose I/O pins.
• One analog pin:
• 10-bit analog-to-digital converter is provided, operating in the
range of 0 - 1.0 volts.
• The board includes a voltage divider to enable 3.3 volt analog
signals to be used.
Development tools • Supported by numerous software development kits (SDKs).
• Supported by a wide range of programming languages and software
libraries.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 49
Attribute Considerations
Processing power • Programs uploaded to the processor must share processing cycles and
memory with Wi-Fi and TCP/IP communication and other system
tasks. You must be careful not to overload the CPU with lengthy data
processing tasks.
• Processing power may be too limited to support transport layer
security (TLS) and certificates, but you could use other forms of
encryption within the local network (depending on your
requirements).
Documentation and • Extensive documentation provided by the large hobbyist and
developer support experimenter community that has arisen around it.
• Supported by active developer communities, such as Arduino, Stack
Overflow, others.
Power management • Operates on 3.3 volts, with onboard voltage regulation making it
possible to use 5 volt USB power.
• Supports deep sleep, sleep, and active power modes.
• Can be awakened from sleep mode by an internal timer or external
trigger.
• Consumes about 60 microamperes in deep sleep mode (with the
runtime clock still running), or less than 1.0 milliampere to remain
connected to Wi-Fi.
Note: Although the development board itself can be powered by
5 volt USB power, supplying more than 3.6 volts directly to the
I/O pins might destroy the chip.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
50 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
ACTIVITY 2-2
Preparing a Microcontroller for IoT Prototyping
Before You Begin
Your instructor has provided you with components to develop an IoT sensor device based on the
NodeMCU ESP8266 development board. The development board and several jumper wires have
been pre-assembled onto a prototyping breadboard. Also included in the kit are components needed
to add temperature, humidity, and light sensors and program.
Scenario
You will use the following components to assemble an IoT sensor device.
• NodeMCU ESP8266 CP2102 microcontroller with pre-soldered header pins.
• 400 tie points prototyping breadboard.
• Short micro USB 3.0 data cable, which enables you to connect the device to a development PC
so you can upload programs to the device.
• GL5516 photoresistor and 10K-ohm ¼ watt carbon film resistor, which you will add to the
device to enable it to take light readings.
• DHT11 sensor, which you will add to the device to enable it to take humidity and temperature
readings.
You will start by inspecting the components.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 51
b) Remove the breadboard and ESP8266 microcontroller from the anti-static bag.
• Integrated circuits are typically shipped in an anti-static bag to protect the delicate circuits from
static electricity discharges.
• The microcontroller has already been inserted into a prototyping breadboard to make it easier to
connect electronic components while you are prototyping the design of the circuit.
• Four jumper wires have already been added to the breadboard for you, to simplify the wiring
process and save class time. (Normally, when prototyping, you'd have to cut, bend, and insert
jumper wires yourself.)
• The board provides 30 connecting pins, spaced so the board can be plugged in to a prototyping
breadboard as it is now. Each of these pins provides access to a different function on the
microcontroller.
Note: These boards are supplied by many different vendors. Your board may
appear slightly different from the one shown here, although yours should have
the same dimensions and pin arrangement.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
52 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
b) In this diagram, examine how the sockets are interconnected by metal rails beneath the sockets.
You'll connect this sensor later to provide temperature and humidity readings to the microcontroller.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 53
You'll connect this sensor later to provide light readings to the microcontroller.
c) In your development kit, examine the USB cable.
The ESP8266 microcontroller will run programs you provide, but you won't create those programs
directly on the microcontroller. Instead, you will use a software development toolkit on a desktop (or
notebook) computer to produce an executable application from source code, and you will use the
USB cable to upload the application to the microcontroller.
The USB cable will also enable the development computer to feed a small amount of electrical
current (less than 1 amp at 5 volts) to the microcontroller. This will be adequate to power the
microcontroller itself and some low energy components, like LEDs.
d) In your development kit, examine the carbon film resistor.
Power levels within a circuit do not always match those needed by an electronic component. A
resistor, as its name indicates, resists the flow of electrical current, bringing it down to an acceptable
level for the component. You will use this resistor in conjunction with the light sensor to adjust the
voltage.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic A
54 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
TOPIC B
Select a Microcontroller Power Source
In addition to selecting an appropriate processor for an IoT device, the device designer needs to
select an appropriate source of electrical power. These two tasks often go hand-in-hand.
Power Requirements
Improvements in wireless data communication technologies have enabled the proliferation of IoT
devices. No longer tethered by the need for a wired Ethernet connection, devices are free to be
located in remote locations, outdoors, and in mobile situations. However, power requirements can
still be a limiting factor.
Microcontrollers, sensors, actuators, and other components all require a consistent supply of power
to operate. While it's true that microcontrollers often have the ability to go into a sleep mode to
conserve power, at some point they must be activated. If too little power is provided to the MCU, it
will not be able to operate and may be unstable. Too much power can actually destroy it.
Note: The ESP8266 MCU was designed for 3.3 volts. Higher voltage levels might destroy it.
However, the NodeMCU developer board used in this course includes voltage regulation
circuitry that protect the ESP8266, so you can power it from a computer's 5 volt USB power or
a 9 volt battery. Always double-check the board you are using to ensure you have a proper
power configuration for it.
Power is provided to a microcontroller through its power pins. Developer boards may include their
own power converters and regulation, adequate to power small input and output devices, and they
therefore provide some overvoltage protection to the microcontroller. But it may be necessary to
add external power conversion and regulation for other components you add, such as sensors and
actuators. Actuators, for example, may draw significant amounts of electrical current when they
employ motors, relays, and similar components.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 55
In digital circuits, it is critical to keep voltage levels within the proper range. If the voltage is too
high or too low, the values won't be read properly. In the previous figure, the lower example is out
of range. The voltage levels for the HIGH values are too low to be read as HIGH, so they are not
distinguishable from the low values. Instead of the correct value of 00101110 (46), it is read as 0.
The practice of electronics is fundamentally about manipulating the flow of electrical energy through
a circuit. There are three attributes of electrical energy, which are related:
• Voltage is a measure (volts) of the difference in electrical charge (pressure) between two points.
• Current is a measure (amps) of the rate at which the electrical charge flows.
• Resistance is a measure (ohms) of the forces resisting the flow of the electrical charge.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
56 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Ohm's law describes the relationship between these attributes as Voltage = Current x Resistance.
Changing one value in this equation means the other two values correspondingly change.
Semiconductors
Different types of materials (metals, stone, wood, glass, ceramics, etc.) may be classified as
conductors or insulators. For example, copper and steel are known conductors, whereas glass,
wood, and various ceramic materials are insulators. While this terminology is useful, all materials fall
somewhere within the range of conducting or resisting the flow of electricity.
Some materials, in fact, may be like chameleons, changing their properties depending on the
conditions. They conduct electrical flow in one situation and resist it in another. Such materials are
known as semiconductors. Because of their ability to change their conductance properties under
certain conditions, semiconductor materials are used to create a variety of components used in
electronic circuits.
The following are some of the fundamental types of semiconductor components and examples of
how they can be used in a circuit.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 57
Note: Semiconductors are often sold as standalone (discrete) components that you might add to
a circuit board to meet specific needs when you develop a custom circuit. They are generally
sized to be easily manipulated by a human hand. But when you pick up a component such as a
transistor or diode, most of what you are holding onto is packaging. The "active ingredient"
within the component is typically quite small, like a grain of sand. In fact, one microcontroller
unit, such as the ESP8266 in your development kit, includes millions of semiconductor
components within its very small package.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
58 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
USB Specifications
There are multiple USB specifications, including USB 1.0, 2.0, 3.0, 3.1, 3.2, and USB-C, which
provide different power levels. USB provides a point-to-point network consisting of one host (such
as a development PC) and one device (such as a development board). Power flows from the host to
the device, but data can flow in both directions.
Item Description
1.0 • Pins/wires: 4, in one row
• Power: 5 volts DC
• Current:
• 0.5 amp standard downstream port (PC hosts)
• Maximum data rate: 12 Mbps
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 59
Item Description
2.0 • Pins/wires: 4, in one row
• Power: 5 volts DC
• Current:
• 0.5 amp standard downstream port (PC hosts)
• Maximum data rate: 480 Mbps
3.0 • Pins/wires: 9, in two rows of 5 + 4
• Power: 5 volts DC
• Current types:
• 0.9 amp standard downstream port (PC hosts)
• 1.5 amp charging downstream port (PC hosts)
• 1.5 amp dedicated charging port (Wall chargers)
• Maximum data rate: 5 Gbps
3.1 • Pins/wires: 9, in two rows of 5 + 4
• Power: 5 volts DC
• Current types:
• 1.5 amp standard downstream port (PC hosts)
• 3.0 amp charging downstream port (PC hosts)
• 3.0 amp dedicated charging port (Wall chargers)
• Maximum data rate: 10 Gbps in SuperSpeed+ mode
3.2 • Pins/wires: 9, in two rows of 5 + 4
• Power: 5 volts DC
• Current types:
• 1.5 amp standard downstream port (PC hosts)
• 3.0 amp charging downstream port (PC hosts)
• 3.0 amp dedicated charging port (Wall chargers)
• Maximum data rate: 20 Gbps in SuperSpeed+ mode
Computers sometimes have different types of USB ports, and you may own various cables. If you
need more USB power and speed, you may be able to just use a different cable and USB port on
your computer. Cables and ports are typically color-coded as follows.
• White: USB 1
• Black: USB 2
• Blue: USB 3
• Teal: USB 3.1
• Red, yellow, orange: USB ports that don't lose power when the computer enters sleep or
standby mode
Power Grid
Microcontrollers typically operate on 3.3 volts or 5 volts DC, and wall outlets typically provide AC
power. Worldwide, the most common standards for AC power are 220-240 volts at 50Hz (Europe),
120 volts or 240 volts at 60Hz (North America), and 100 volts at 50 or 60 Hz (Japan). Therefore,
microcontrollers typically require some sort of power adapter to operate off the power grid.
There are several steps involved in converting AC power to DC power. A transformer converts AC
power of one voltage to AC power of another voltage. A rectifier flips half of AC power cycle so it
no longer alternates, but that leaves some gaps in voltage levels that must be filled in. An electronic
filter then smooths out the gaps. Some degree of "voltage ripple" may be left from the conversion.
How much is acceptable depends on the application, and will help to determine the quality and type
of AC/DC converter you need.
AC/DC conversion can be accomplished using the typical small standalone "wall wart" type power
adapter, a small plastic box containing the AC/DC conversion circuitry. Two cords are typically
attached to the box—one that plugs into AC power in a wall outlet and another that delivers the
converted DC power to a jack connector that you attach to the IoT device. This is a typical solution
for commercial products or those produced by hobbyists and makers, but in an industrial
environment, it may be more common to use central power converters with power rails to which
multiple devices can be attached through duplex wires that convey the DC power.
Note: AC/DC power converters are not power sources but rather just an intermediary device
that connects to a power source (such as an AC wall outlet or a power cable leading from a
breaker box) to convert it to DC. However they are often referred to as a power supply.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 61
Backup Generators
While the power grid provides a steady source of power, power outages are not unheard of. Backup
generators are useful for situations where any power outage, even temporary, may cause
unacceptable data loss. However, there are other uses for on-site power generation. For example, a
power generator can be used as a supplement to energy-harvesting sources like wind and solar.
Power generation can also be used strategically to generate electricity during peak hours, when it
may cost more to consume power from the grid.
PoE
In some cases, it may be convenient to provide a single thin cable to provide wired network
connections to IoT devices, but not to run an additional power line. Using Power over Ethernet
(PoE), you can use an Ethernet cable to provide both power and a network connection. Various
techniques for doing this have been developed, and there are now industry standard ways of
accomplishing it.
PoE cables may not be subject to the same code requirements as AC power lines (such as requiring
an enclosing conduit when installed on an exposed wall), so they can help reduce installation costs.
PoE is commonly used to connect IP-based surveillance cameras to network and power, for
example, enabling installers to run just a single Ethernet cable to each camera. It is also used for
building and industrial controls, sensors, and wireless access points.
PoE has been standardized as IEEE 802.3at. Various pairs of wires in the standard RJ-45 Ethernet
jack can be used for power and ground, depending on the types of power needed. One constraint is
the type of cable used. Cat-3 cable can provide 37 volts to 57 volts at 350 mA (PoE 802.at Type 1).
Cat-5 cable can provide up to 600 mA (Type 2).
While many devices only require 9 volts or less, the higher voltage levels supported by PoE enable it
to transmit power over longer distances, such as those supported for data transfer (up to 100 meters
for Cat-5).
In addition to using appropriate cables, you must use routers and switches that support PoE. The
traditional Ethernet specification did not support powered lines, so older devices may not be
compatible with the standards.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
62 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Note: mA is milliamps, a measure of current. For comparison, using IEEE 802.3at Type 2 over
Cat-5, you could power more than 100 continuously running Arduino Uno boards. Or you could
provide about 1/5 of the power needed to run a typical desktop application on a desktop
computer. (This is intended to give you a sense of scale. Of course, power consumption varies
widely depending on many different variables.)
Batteries
Batteries provide a means to take devices off the grid, enabling IoT devices to be mobile or located
away in remote places. Many non-grid power sources, such as solar, wind power, and so forth are
intermittent. Batteries can be recharged by these sources, enabling devices to be off-grid while
maintaining steady power even when the sun is obscured or there is no wind.
When selecting batteries, there are numerous factors to consider.
• Operating temperature: You may need to power devices located in very hot or cold conditions.
Temperature extremes impact all types of batteries, but some types are affected more severely
than others.
• Operating life: A battery might discharge itself faster than the device can use its power. The
initial capacity of the battery may not be high enough for the application.
• Reliability: If a single battery fails, it may hinder the operation of other batteries in the bank.
Batteries should fail less frequently than you will have opportunities to replace them.
• Lifetime cost: The initial cost of the battery, the cost to support and maintain it, the frequency
of replacement, and any risks associated with using it (such as data loss) all contribute to its
lifetime cost.
• Voltage and current rating: Matching power ratings may increase the size or complexity of the
device. For example, you may require multiple batteries of a certain type to produce the voltage
levels your device requires.
Lithium Cells
For rechargeable batteries, lithium continues to be a preferred choice because lithium cells can store
more power in a small package than other battery types. Lithium cells have a normal operating
current voltage between 2.7 and 3.6 volts, putting them in the right range for many microcontrollers.
• Typical consumer-grade batteries (with a lithium iron disulfate or LiFeS2 chemistry) are more
limited than other types, with a temperature range between -4° and 140° Fahrenheit.
• Coin cells such as the CR123A commonly used in cameras and small electronic devices with a
lithium manganese dioxide (LiMNO2 chemistry) provide a low initial voltage, narrow
temperature range, and high self-discharge rate, but they can deliver moderate pulses of energy
from a compact package.
• Lithium thionyl chloride (LiSOCl2) cells provide the highest capacity and energy density of
lithium batteries, and provide long battery life even when exposed to temperature extremes (with
a range between -112° and 257° Fahrenheit), making them well suited for powering remote IoT
devices in extreme environments.
Note: Although the type of chemistry is an important factor, quality of manufacturing and the
design of batteries also has a significant impact on the performance and lifetime of a battery, so
it is important to verify manufacturers' claims.
Solar Panels
In many ways, solar panels provide an ideal energy source for IoT devices. Solar panels can be made
quite small, but can still produce the small amounts of energy needed to operate a microcontroller.
They can be deployed in a wide range of environments—virtually any location exposed to direct
sunlight for at least an hour a day, on average.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 63
Of course, solar panels must be supplemented with rechargeable batteries to maintain a constant
source of power when sunlight is not available to produce energy.
When selecting a solar panel, the best way to confirm your requirements is through testing of an
actual device under real conditions. However, there are some rules of thumb for determining
whether solar panels would work in your situation, and how much energy they would need to
produce.
• Consider using solar panels when your device has rechargeable batteries that can provide
complete power to the IoT device for at least 4 days. This accounts for extended periods of rain
and heavy clouds.
• To estimate the required generation capacity, calculate the amount of power your IoT device will
consume in its most demanding day (Watts per day). Then select a solar panel that will generate
that amount of energy or more in only one hour of good sunlight.
Where additional power is required, solar power can be used in conjunction with other energy
sources, such as wind-generated power.
Wind Power
Wind power is one of the first forms of energy harvesting that many people think of, but it hasn't
been as popular as solar for powering IoT devices. Part of the reason for this is the cost and limited
availability of very small turbines and the widespread availability of inexpensive solar options. Solar
panels have no moving parts, and therefore have lower maintenance than wind turbines.
However, recently, scientists at the Georgia Institute of Technology and the National Center for
Nanoscience and Technology in Beijing developed a combination solar cell and nanogenerator that
can convert solar and wind energy into electricity using a single, compact device. This is a good
combination, since sunny days and windy days tend to occur at different times.
Water Power
Harvesting energy from water power and tidal currents is another option for IoT devices,
particularly in locations near water or at sea. In addition to the traditional water wheel design, energy
can be harvested by relying on the energy produced by waves. Floats attached to arms can power
generators. Tidal gates or fences corral water that drains out as tides, currents, or waves change,
powering turbines that generate electricity.
Energy Harvesting
Most natural and man-made entities are to some degree energy-inefficient. Bodies, engines, light
bulbs, and transistors lose energy to heat. Radio stations transmit megawatts of power that reach
antennas as microwatts, and much of the energy of radio waves leaks out into space. We are literally
surrounded by ambient energy that could be harvested or scavenged to power small autonomous
IoT devices like those used in wearables and wireless sensor networks.
Consider a common self-winding watch, which converts the kinetic energy (motion) of the person
wearing the watch to keep it wound up and supplied with energy to feed the watch's movement. In a
similar way, an IoT device could capture nearby energy in the form of heat, vibration, wind, light,
electromagnetic radiation, and so forth, and store it (using batteries and capacitors) to provide a
sustainable source of power.
Energy sources include such things as solar power, heat generated through friction (such as an
engine), tidal currents, and so forth. Even the kinetic energy of a user pressing keys while using a
portable electronic device may be converted to electrical power. Often very little energy is actually
required to sustain the electronics of an IoT device.
A variety of technologies could be used for energy harvesting, such as:
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
64 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
• Inductive chargers transfer energy between two fields using electromagnetic induction. A
typical use of this is a wireless charging station for a smartphone, but it has potential applications
in energy harvesting. For example, energy harvesting antennas such as rectennas (rectifying
antennas) or nantennas (optical antennas) can collect energy from energy waves such as FM
radio or light. They could be used to harvest stray energy or to collect energy purposely
transmitted over the air when a wired connection is not practical.
• Piezoelectric crystals or fibers generate a small voltage when they are vibrated or mechanically
stressed. A power source might include such things as vibration from an engine, sound waves,
ocean currents, or pedestrian or automobile traffic over a mat.
• Generators and alternators, which use movement of magnets and wire coils (based on
Faraday's Law) to convert kinetic energy into electrical energy. Generators and alternators are the
most developed of these technologies. They have been in common use for more than a century,
and provide the basis for much of the power provided to the electrical grid, backup electrical
systems, and automotive electrical systems. Generators and alternators can easily be adjusted to a
small scale.
Power Management
Managing power for IoT devices is challenging because electronic devices must maintain steady
current and voltage while they operate, and may sometimes require energy in high level bursts (such
as periodic data communication, operating servos to position sensors, and so forth). Devices may
harvest energy from local sources, which may not produce energy in the levels required for such
bursts.
IoT devices may be able to "sleep" during long periods—maintaining just enough capability to
operate timers and maintain data states, occasionally "waking up" to take sensor readings, transfer
data, and so forth—and then go back to sleep. Such an approach to power management can help to
extend battery life, and can enable devices to consume power when it is available (on windy or
sunny days, for example), while conserving power when it is low.
When selecting a microcontroller, ensure that the microcontroller and software libraries you'll use
for development will support the power management features your design requires.
Note: To learn more about the issues you should consider when designing power systems for
IoT, check out the Spotlight on Powering IoT Devices presentation from the Spotlight tile on
the CHOICE Course screen.
• Use efficient power regulation. For example, in situations where very low power consumption
is required, consider using a switching regulator rather than a linear regulator. Although linear
regulators are simple and inexpensive, they tend to lose considerable energy to heat.
• Manage microcontroller low-power modes. Microcontrollers typically support multiple low-
power modes (such as standby, sleep, off). Generally, the deeper the power-saving mode, the
longer it takes to bring the microcontroller back to a full operating mode. Some low-power
modes provide the ability to reawake based on a timer or external trigger. Careful use of these
low-power modes may enable a device to meet data-collection requirements while using very
little power.
Note: Some microcontrollers may support different operating systems, some of which may
not support the low-power modes your scenario requires. Take this into account when
designing a solution.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
66 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
ACTIVITY 2-3
Attaching a Microcontroller to a Power Source
Scenario
You will start the IoT device prototyping process by attaching the microcontroller to your
development computer with a USB cable. This will provide power to the microcontroller, as well as
a communication channel that you will use to upload programs to the microcontroller.
Note: When powering a microcontroller, it is very important to use the correct power, since
overvoltage on certain pins can destroy the microcontroller.
1. a) Attach the small end of the USB cable to the ESP8266 microcontroller development board as
shown.
b) Attach the large end of the USB cable to the USB port on your computer.
• The computer's USB port will provide power to the microcontroller while you develop and test the
IoT device.
• The computer's USB port will also enable communication between the microcontroller and the
development computer so you can write programs on the computer and upload them to the
microcontroller.
2. Identify how you can attach a power source to the microcontroller when you
deploy the IoT device.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 67
• The USB port includes a power connection, which could be used to connect a battery cable
when the device is deployed.
• The board also includes pins for 3.3 volts. Batteries could be attached to this connection as well.
• The microcontroller's native power level is 3.3 volts, but the development board includes a power
regulator, which converts the 5 volt USB power to the 3.3 volts used by the microcontroller.
Note: When designing a device, you should always be careful to adhere to the
power requirements specified by the microcontroller's manufacturer.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic B
68 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
TOPIC C
Use a Software Development Kit to Program an
IoT Device
If you are developing a custom IoT solution, you may need to program IoT devices or other
components used in the overall system. A wide range of toolkits are available for this purpose.
Development Toolkits
The fundamental way to develop programs for microcontrollers is to write machine code to directly
run on the processor, typically using programming software such as an assembler. However,
programming in an assembler is a complex task that requires very specialized knowledge of the
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 69
microcontroller you're programming. To simplify the task, generalized tools called compilers are
often provided to enable programmers to use common high-level languages such as C, C++,
Python, or Java to write programs.
Programmers write code using a code editor, or a more comprehensive program called an
integrated development environment (IDE) , which typically includes a code editor and other
features used to manage the software development process, such as debugging tools, tools for
editing graphics and other assets used in the program, and so forth. An IDE is typically configured
to enable the developer to compile, run, and debug a program within a unified user interface, instead
of having to perform these functions separately in a command-line console.
There are many common tasks such as communicating over the Internet (using TCP/IP, HTTP,
etc.) or reading data values from a sensor, which a developer might need to perform on a variety of
different microcontrollers. Software libraries provide developers with a relatively simple way to do
this, providing a layer of software between the programmer and the microcontroller. The library
provides the programmer with an application programming interface (API) the programmer
uses when writing code. The API is intended to be simpler to program than programming the
hardware directly, and it may support many different types and combinations of hardware. Much of
the complexity is handled by the library, making it much easier for the application developer to
implement the software features they need to develop, and also making it easier to create software
that works seamlessly on many different microcontrollers.
When creating a program, an application developer imports a particular library to perform a
particular kind of task—such as an Ethernet library to perform network communication tasks, a Wi-
Fi library to connect to a Wi-Fi network, or an Encryption library to perform data encryption tasks.
Once the library is imported into the programming project, the developer can use the APIs it
provides to perform various tasks.
While software libraries simplify implementation of a particular task (such as connecting to a
network), frameworks operate on a larger scale, providing a foundation or template for an entire
programming deliverable, such as an application.
Programming Languages
Many different programming languages are used in the development of IoT software, including IoT
devices, client applications, analytics tools, and other software that enables the IoT to function.
Traditionally, developers of embedded devices have created programs that run on bare metal or at
least close-to-the-metal, meaning they run directly (or almost directly) on the microcontroller,
rather than having an operating system or some other runtime environment separating the program
from the complexity of (and some of the power of) the microcontroller. This type of programming
requires a programming language such as assembly language (bare metal), or C (close to the metal)
that can directly access memory and hardware registers. These languages are typically described as
low-level languages.
By contrast, other languages are called high-level languages. High-level languages were designed
to protect the developer from some of the dangers of such direct access, to promote programming
methods such as object-oriented programming, and streamline development tasks—at the cost of
less direct control over hardware. Languages such as C#, Java, Python, and others are in this
category.
Note: Depending on the context, C is also sometimes called a high-level language, particularly
when compared to assembly language. But C's ability to directly address memory and hardware
registers place it at a much lower level (closer to the metal) than languages like Java and Python.
For microcontroller programming, low-level languages are typically used, although high-level
languages are also sometimes used. Conversely, for programming IoT client applications (for
desktop computers, mobile devices, and web) high-level programming languages are typically used,
although low-level languages can also be used.
The following are some programming languages commonly used for IoT development.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
70 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Language Description
Assembly Language Assembly language closely represents the machine language that runs
on a processor.
C C was originally developed for programming close to the metal
(telephone switching systems) and is still one of the most popular
languages for programming embedded devices. It generally compiles to
machine language quite efficiently, producing programs that run quickly
and make good use of the limited storage and memory typically provided
by an embedded device.
Unlike assembly language, C is machine independent. A wide range of C
compilers are available for many different platforms, and a program
written for one microcontroller can often be compiled for another
microcontroller with relatively few modifications.
C++ C++ (pronounced see plus plus) was developed as an extension of C, and
has likewise been optimized for development of embedded systems. C++
extends C to provide features that support object-oriented programming,
such as the ability to define classes and subclasses. The C++ extensions
help to support reusability of code, and may make it better suited for
large, complex applications than C.
Java Java is a high-level programming language, also based on C, which was
designed for portability by running within a runtime environment (a Java
virtual machine) that can be adapted to run on a wide range of different
computing systems while requiring minimal or no changes to the source
code.
Java is widely used for programming web and cloud applications, as well
as client applications such as those running on Android mobile devices.
Embedded Java, available in various products from a variety of vendors,
is geared toward development of embedded systems, with some products
optimized for IoT development. Like C++, Java is well suited for large,
complex applications.
C# C# (pronounced see sharp) is another object-oriented programming
language based on C. C# was developed by Microsoft, and is commonly
used on web servers, though it can also be used to develop desktop
applications and other types of software. C# is commonly used for IoT
development when using Microsoft platforms such as the Azure IoT
Suite.
Swift Swift was developed by Apple Inc. as a general purpose programming
language for various Apple systems including iOS, macOS, watchOS, and
tvOS. This language is commonly used to develop IoT client applications
for Apple products.
Rust Rust is an open source systems programming language, similar to C++,
whose development was sponsored by Mozilla Research. Rust was
designed for uses similar to C and C++, but with improved safety
features for memory handling and concurrent operations.
Go Go (or Golang) is a free, open source, high-level programming language
developed at Google. It is used internally at Google, and is a supported
language on the Google Cloud Platform.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 71
Language Description
Python Another high-level language that is commonly used for web applications,
but is also used to develop cloud applications, desktop applications, and
so forth. MicroPython enables Python to run directly on a
microcontroller.
Python may be involved in IoT development even when not used to
directly develop programs for IoT devices or client applications—as a
tool for automated testing, debugging, and data analysis.
JavaScript This high-level programming language is most commonly deployed
within web browsers, and may therefore be used within web-based client
applications for IoT. However, JavaScript is also used in a variety of other
tools, such as NodeJS, which can be used to program IoT devices, as well
as other programs used in IoT gateways and in the cloud to process IoT
data.
Real-Time Operations
Because real-time operating systems must function on constrained devices, they must typically be
very conservative in their use of resources such as processing cycles, memory, storage, and electrical
energy, and must be optimized for the sorts of tasks IoT devices must perform. In many cases—
particularly in mission-critical situations such as healthcare, aviation, power plants, and public safety
—devices must be able to process data without buffering delays, share limited resources among
multiple tasks, schedule and prioritize tasks, and be able to switch seamlessly between various power
management modes to save energy.
A system is said to be deterministic when the timing of specific tasks can be guaranteed within a
certain margin of error.
• Arm Mbed, developed by Arm, operates on 32-bit ARM Cortex-M microcontrollers, which are
used in a wide range of IoT devices and development boards. The operating system is supported
by a software development kit that includes tools for developing C/C++ programs that run on
the operating system.
• Contiki is an open source operating system that has been used to develop many different kinds
of IoT devices, including sensors and controllers used in smart cities, radiation monitoring, and
alarms. The system has been ported to run on microcontrollers from various manufacturers,
including Atmel (AVR, ARM), Texas Instruments, Microchip Technology, NXP
Semiconductors, and STMicroelectronics. Even providing a graphical user interface and TCP/IP
networking capabilities, it functions in a very small footprint—about 30 KB of RAM.
Numerous other systems are used for IoT devices, including:
• TinyOS
• RIOT OS
• QNX
• Windriver VxWorks
• Android Things
• Apache Mynewt
• Embedded Apple iOS and OS X
• Nucleus RTOS
• Green Hills Integrity
• Huawei LiteOS
• OpenWrt/LEDE/Linino/DD
• Tizen
• uClinux
• Yocto
• Fuchsia
• Nuttx
Note: Many IoT devices run their programs on bare metal—using no operating system.
Microcontroller Firmware
Firmware is the base code that enables a microcontroller to start running when the device is
powered up. Firmware is stored in a microcontroller's ROM, EPROM, or flash memory. This
memory persists even after the device is powered down, so the code it contains can be used to start
the device running.
In complex devices like personal computers, firmware (such as the ROM BIOS) starts the process
of loading the operating system, but the operating system itself loads from storage such as a hard
disk drive or solid-state drive. Once the operating system is loaded, a user then can run programs,
also typically stored on a hard disk or solid state drive.
But in the case of an embedded device (such as an IoT device), the entire operating system (if there
is one) and the program that runs on the microcontroller (typically only one program, which
autostarts when the device is powered on) may all be loaded from firmware.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 73
The process of making an executable varies somewhat among different development systems but
typically involves compiling, linking, and building.
• Building is the process of performing all of the tasks necessary to create an executable program
from source code. It includes compiling and linking, and possibly other tasks needed to create
the finished program.
• Compiling is the process of translating programming source code (such as code written in C,
Java, or C++) into machine code that can run on the microcontroller.
• Linking is necessary when multiple files have been compiled and need to be combined into a
single executable file.
Once your build has completed, you load the program on the microcontroller, where you can run it
and test it.
How you load the program depends on the microcontroller and development tools you are using. In
the case of a PIC microcontroller, for example, you would typically need an extra piece of hardware,
called a microcontroller programmer, to upload program code to a microcontroller. A PIC
programmer, for example, might attach to your development computer through a USB cable. The
PIC programmer includes a socket that you would plug the microcontroller into, and then upload
the program.
Other development systems use a bootloader, a small program that always remains stored on the
microcontroller to enable the microcontroller to download code directly from another system,
without requiring use of a separate microcontroller programmer device. The microcontroller must
be installed on a development board with USB, Wi-Fi, or some other data connection to the
development computer.
Once a device is deployed (and far away from the development computer), it may be necessary to
update its firmware. In many cases, this can be accomplished "over the air" (OTA) using the device's
Wi-Fi or other network connection. Again, this is typically accomplished through a bootloader.
Debugging
With any software development environment, the ability to gain insight into a program's state while
it is running is important. Software development environments typically provide tools that enable a
programmer to suspend execution at any point, step through code as it executes, and view what data
values are held in the program.
Debugging a desktop application running directly on the development computer is clearly a bit more
straightforward than debugging a program running on a microcontroller separate from the
development computer. Because microcontrollers are constrained by processing power, memory,
and other limitations, in some cases, they may be limited in their ability to support interactive
debugging.
Following are some common techniques programmers use to debug microcontrollers.
• Simulator: The microcontroller is simulated in software running on the development computer,
enabling many different operations to be debugged directly on the development computer.
However, additional debugging on the actual device is also required for tasks that can't be
simulated.
• In-circuit emulator: The microcontroller is temporarily replaced with a special processor that
behaves much like the actual microcontroller, but includes extra capabilities to support real time
debugging. However, additional debugging on the actual device is also required for tasks that
can't be simulated.
• In-circuit debugger: This is a separate device located between the development computer and
the microcontroller, that facilitates real time debugging. It enables an actual microcontroller to be
used, but reduces the burden on the microcontroller.
• Serial monitor: Microcontrollers often include a serial interface that enables the microcontroller
to output text messages, which the programmer can view in a console on the development
computer. Although it takes a fairly long time (by real-time processing standards, perhaps tens of
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
74 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
milliseconds) to output a text message, this approach is very useful for seeing what a
microcontroller is actually doing when it runs.
• Onboard display: Even if the completed device won't have its own display, an onboard liquid
crystal display can provide useful real time feedback while debugging.
• Pin state: The programmer can set an unused I/O pin to a high or low state to represent the
state of various data values during debugging. A voltmeter or another tool called a logic probe
can be used to reveal the pin state. Or an LED light can be temporarily attached to the pin to
illuminate when the pin state is high.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 75
Item Description
Arduino IDE C++ based firmware that enables you to program the ESP8266 like an
Arduino, with very minor differences. You must install a board driver so
the Arduino IDE can communicate with and compile programs for the
ESP8266.
ESP8266 BASIC Open source BASIC interpreter optimized for IoT development and
geared toward hobbyists. Once installed on an ESP8266 device, you
access the device from a web browser, where you can write and run code
directly.
ESP-Open-SDK Free and (mostly) open source integrated SDK, based on tools developed
by Tenselica, the manufacturer of the ESP8266 chip.
ESP-Open-RTOS Open source software development framework based on the FreeRTOS
real-time operating system.
Espruino Firmware and SDK, which enables the ESP8266 to be programmed using
JavaScript and a framework very similar to the popular Node.js
development environment. Code is interpreted, rather than compiled,
meaning it is translated to machine language just before it is executed.
This provides faster turnaround during development, although runtime
performance may be a bit slower than compiled programs. Programming
can be done through a web-based IDE or a desktop application. A
graphical editor provides a useful tool for learning.
MicroPython Firmware and compiler for a subset of the Python 3 programming
language, including libraries to support various components of the
ESP8266. Includes a console for immediately executing Python code on
the microcontroller interactively.
Mongoose OS Open source operating system that supports programming in C and
JavaScript.
NodeMCU Firmware provided with NodeMCU's ESP8266 development board,
which uses the Embedded Lua programming language.
PlatformIO A cross-platform IDE that enables you to program using Arduino C/C+
+ code and libraries, while providing features found in professional
programming tools, such as code completion and integrated debugging
tools. You can use other programming editors, such as Microsoft Visual
Studio Code.
ZBasic for ESP8266 Free compiler produced by Elba Corporation based on Microsoft Visual
Basic (not to be confused with the ZBasic compilers produced by
Simutek). Originally produced for the Basic Stamp development board,
the compiler also supports the ESP8266.
Zerynth Real-time operating system, development environment, device
management system, and related mobile applications that support IoT
development in Python and C for many different microcontrollers,
including the ESP8266.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
76 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
ACTIVITY 2-4
Uploading a Program to an IoT Device
Before You Begin
The Arduino IDE has been installed on your computer as part of the setup for this course. The
microcontroller is connected to the development computer by a USB cable.
Scenario
The Arduino IDE is included in the data files for the course. You will use it to test the connection
between your development computer and the microcontroller, and upload your first program to the
device.
Note: Your instructor will let you know if your course data files have been
installed in a different location.
b) Right-click on arduino.exe and select Pin to Start.
This will enable you to run the program directly from the Start menu.
c) Close the arduino File Explorer window.
d) Select the Windows Start button in the lower-left corner of the screen.
The Windows Start menu is displayed.
e) Select the arduino tile.
2. Examine the type of board you are using and test the connection.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 77
a) Select the Tools menu, and examine the options shown on the menu.
Note: The menu is very long, and has been abbreviated in this image to save
space.
b) Select Tools→Port, and select the COM port option that is shown.
c) Select Tools→Get Board Info.
• If a board is found, information about the board will be shown. Non-Arduino devices such as the
NodeMCU ESP8266 may not show a board name, as this image shows.
• If a board is not found, the Board Info dialog box will not be shown. If this happens, make sure
that both ends of the USB cable are connected properly, and select the port from the Tools→Port
menu.
Note: If a COM port is not shown, you may have to install a UART to USB
bridge for the ESP8266 device to be recognized. If this happens, you might try
running C:\095024Data\Tools\usb_driver\CP210xVCPInstaller_x64.exe from
the course data files and restarting the computer.
d) Select OK to dismiss the Board Info dialog box.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
78 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
a) Select File→Examples→ESP8266→Blink.
Note: If line numbers are not showing, select File→Preferences, check Display
line numbers, and select OK.
• A second window is opened, containing a very simple program written in the C programming
language.
• The program includes two parts (functions) named setup and loop.
• The instructions in setup run when the device starts. The setup function is used in Arduino
projects to create the initial set of data and configuration values the program will use.
• In line 13, the setup function identifies that the program will communicate with the LED light that
is soldered onto the development board.
b) Examine the loop function, which begins at line 17.
• The instructions in the loop function run repeatedly once setup has completed. This is where
the main work of the program is performed. For example, an IoT device might read sensor data
here and forward it to an IoT gateway.
• In this example program, the loop function sets the state of the LED in lines 18 and 22,
alternating between LOW and HIGH.
• Delays in lines 21 and 23 create a pause between the two LED state changes, so the light
flashes. It will stay on for one second, and off for two seconds. (The delay is specified in
milliseconds, so 1000 is one second.)
• This program automatically runs when the device is powered up. Embedded devices typically
have one program that autostarts in this way.
c) Select Sketch→Upload (or press Control+U).
• The program (called a sketch in the Arduino tool) is compiled.
• Compiling may take a few minutes the first time you do it.
• After the compiling process has completed, the program is uploaded to the device.
• When the new program is being written to microcontroller memory, a blue LED will flash.
• Once the upload process has completed, the program runs on the device, blinking the onboard
LED as it was programmed to do.
• When the program runs, the light is on for a second, and off for two seconds.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 79
a) Revise the delay values in lines 21 and 23 so both delays last only one half second (500
milliseconds).
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
80 | Certified Internet of Things (IoT) Practitioner (Exam ITP-110)
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device | Topic C
Certified Internet of Things (IoT) Practitioner (Exam ITP-110) | 81
Summary
In this lesson, you selected a processing unit that meets the requirements for an IoT device, and you
started assembling it. You attached it to a suitable power source, and you configured and used a
programming toolkit to program it.
What tools will your organization use to program IoT devices and integrate them
into the IoT infrastructure?
Note: Check your CHOICE Course screen for opportunities to interact with your classmates,
peers, and the larger CHOICE online community about the topics covered in this course or
other topics you are interested in. From the Course screen you can also access available
resources for a more continuous learning experience.
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM
Lesson 2: Constructing and Programming an IoT Device |
Licensed For Use Only By: Multimatics . [email protected] Nov 22 2019 3:26AM