Internet of Things

Page 1

INTERNET OF THINGS Dogan Ibrahim

In addition, he is the author of over 250 technical papers, published in journals, and presented in seminars and conferences.

ISBN 978-1-907920-44-8

DESIGN

www.elektor.com

LEARN

Elektor International Media BV

This book is written for students, for practising engineers and for hobbyists who want to learn more about the building blocks of an IoT system and also learn how to setup an IoT system using these blocks. This book has been written with the assumption that the reader has taken a course on digital logic design and has been exposed to writing programs using at least one high-level programming language. Knowledge of the C programming language will be very useful. Also, familiarity with at least one member of the PIC series of microcontrollers (e.g. PIC16 or PIC18) will be an advantage. The knowledge of assembly language programming is not required because all the projects in the book are based on using the C language. If you are a total beginner in programming you can still access the book, but first you are advised to study introductory books on microcontrollers.

INTERNET OF THINGS

● DOGAN IBRAHIM

He is the author of over 60 technical books, published by international famous publishers, such as Wiley, Butterworth, and Newnes.

AN INTRODUCTION WITH PIC MICROCONTROLLERS

AN INTRODUCTION WITH PIC MICROCONTROLLERS

Prof Dr Dogan Ibrahim is a Fellow of the Institution of Electrical Engineers.

The Internet of Things (IoT) is a new concept in intelligent automation and intelligent monitoring using the Internet as the communications medium. The “Things” in IoT usually refer to devices that have unique identifiers and are connected to the Internet to exchange information with each other. Such devices usually have sensors and/or actuators that can be used to collect data about their environments and to monitor and control their environments. The collected data can be processed locally or it can be sent to centralized servers or to the cloud for remote storage and processing. For example, a small device at the size of a matchbox can be used to collect data about the temperature, relative humidity and the atmospheric pressure. This data can be sent and stored in the cloud. Anyone with a mobile device can then access and monitor this data at any time and from anywhere on Earth provided there is Internet connectivity. In addition, users can for example, adjust the central heating remotely using their mobile devices and accessing the cloud.

INTENET OF THINGS

AN INTRODUCTION WITH PIC MICROCONTROLLERS

Dogan Ibrahim LEARN

DESIGN

SHARE

SHARE

LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR


Table of Contents

Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 1   Introduction to the Internet of Things . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1  Why IoT? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2  Cloud Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.2.1  Advantages of the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.3  Machine to Machine (M2M) and IoT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4  IoT Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.5  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Chapter 2   Basic concepts and possible IoT architectures . . . . . . . . . . . . . . . . . . . . . 17 2.1  The IoT Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2  IoT Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2.1  Using Distributed Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.2.2  Using Common Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.2.3  Using Shared Distributed Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Chapter 3   Sensors and actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.1  Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.1.1  Examples of Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.2  Actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3.2.1  Examples of Actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 3.3  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Chapter 4   IoT communication technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.1  Low Power Communication Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.1.1  Bluetooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.1.2  Wi-Fi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.1.3  ZigBee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.1.4  Basic RF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.1.5  RFID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.1.6  Direct Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 4.2  Which Communication Technology to Use in IoT Systems ? . . . . . . . . . . . . . . . . . 44

●5


INTERNET OF THINGS: An Introduction with PIC Microcontrollers 4.3  IoT Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4.4  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Chapter 5   Internet of Things development kits . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.1  TinkerForge Starter Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.2  ARM mbedIoT Starter Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.3  Pinoccio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 5.4  WunderBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.5  Espruino Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 5.6  Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 5.7  BeagleBone Black . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.8  FlyportPro Starter Kit Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.9  Parallella . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 5.10  Spark Electron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 5.11  Intel Galileo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 5.12  Flutter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 5.13  RasWIK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5.14  Onion Omega . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 5.15  Clicker 2 for PIC18FJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 5.16  BL600-eBoB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 5.17  panStamp AVR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5.18  panStamp NRG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 5.19  Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 5.19.1  Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 5.19.2  Arduino Fio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 5.20  IntrinsycIoT Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5.21  EFM32 Gecko Starter Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.22  Congatec Qseven IoT Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 5.23  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Chapter 6   Using the Clicker 2 for PIC18FJ board . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6.1  The Clicker 2 Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 6.2  The PIC18F87J50 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.2.1  Internal Structure of the PIC18F87J50 Microcontroller . . . . . . . . . . . . . . . . . 68

●6


Table of Contents 6.2.2  The Program Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.2.3  The Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.2.4  The Clock Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 6.2.5  Watchdog Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 6.2.6  Parallel Input-Output (I/O) Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 6.2.7  Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 6.2.8  Analog-to-Digital Converter (ADC) Module . . . . . . . . . . . . . . . . . . . . . . . . . 80 6.2.9  Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 6.3  Programming the PIC18F87J50 Microcontroller on the Clicker 2 Board . . . . . . . . . 87 6.4  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Chapter 7   mikroC PRO for PIC programming language . . . . . . . . . . . . . . . . . . . . . . . 91 7.1  Structure of a mikroC Pro for PIC Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 7.1.1  Beginning and Ending of a Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . 91 7.1.2  Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 7.1.3  Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 7.1.4  Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 7.1.5  Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 7.1.6  Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 7.1.7  Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 7.1.8  Operators in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 7.1.9  Modifying The Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 7.1.10  Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 7.2  PIC Microcontroller Input-Output Port Programming . . . . . . . . . . . . . . . . . . . . . . 95 7.3  mikroC Pro For PIC Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 7.3.1  Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 7.4  Passing Arrays to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 7.5  mikroC Pro For PIC Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 7.6  mikroC Pro for PIC Integrated Development Environment (IDE) . . . . . . . . . . . . . 101 7.7  Using the Clicker 2 for PIC18FJ Development Board . . . . . . . . . . . . . . . . . . . . . 101 7.7.1  Creating and Compiling the Source File . . . . . . . . . . . . . . . . . . . . . . . . . . 101 7.7.2  Programming the Clicker 2 board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 7.7.3  Using the Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

●7


INTERNET OF THINGS: An Introduction with PIC Microcontrollers 7.7.4  Other Useful Windows of the mikroC Pro for PIC Compiler . . . . . . . . . . . . . 108 7.8  Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Chapter 8   Using a Click board with the Clicker 2 for PIC18FJ board . . . . . . . . . . . . . 111 8.1  The 7-Seg Click Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 8.2  Plugging in the 7-Seg Click Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 8.3  The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 8.3.1  Writing the 7-Seg Click Display Program as a Function . . . . . . . . . . . . . . . 116 Chapter 9   Using a sensor with the Clicker 2 for PIC18FJ board . . . . . . . . . . . . . . . . 119 9.1  Connecting the HTU21D Click Board to the Clicker 2 Board . . . . . . . . . . . . . . . . 119 9.2  The HTU21D Click Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 9.3  Operation of the HTU21D Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 9.4  The Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 9.5  Displaying the Temperature and Humidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Chapter 10   Using an Actuator with the Clicker 2 for PIC18FJ board . . . . . . . . . . . . . 125 10.1  Connecting the Relay Click Board to the Clicker 2 Board . . . . . . . . . . . . . . . . . 125 10.2  The Relay Click Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 10.3  Operation of the Relay Click Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 10.4  The Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Chapter 11   Using Bluetooth connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 11.1  About the Bluetooth Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 11.2  Adding Bluetooth Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 11.3  The Bluetooth Click Board and RN-41 Bluetooth Controller Module . . . . . . . . . . 130 11.3.1  Configuring the Bluetooth Module via the UART Port . . . . . . . . . . . . . . . . 131 11.4  Connecting the Bluetooth Module to the Clicker 2 Board . . . . . . . . . . . . . . . . . 140 11.4.1  Bluetooth Module in Slave Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 11.4.2  Bluetooth Module in Master Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Chapter 12   Using WIFI connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 12.1  Adding WiFi Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 12.2  Connecting the WiFi Plus Click Board to the Clicker 2 Board . . . . . . . . . . . . . . . 150 12.3  The WiFi Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 12.3.1  WiFi Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 12.3.2  Making a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

●8


Table of Contents 12.4  The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 12.5  Testing the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Chapter 13   INTERNET of THINGS example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 13.1  Block Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 13.2  SENSORS Node Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 13.3  ACTUATORS Node Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 13.4  The SENSORS Node Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 13.5  The ACTUATORS Node Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 13.6  The PC Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 13.7  The Raspberry Pi Computer Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Chapter 14   Storing the data on the CLOUD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 14.1  The Hardware Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 14.2  The Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 14.3  Using the Beebotte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 14.3.1  Installing the Beebotte API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 14.3.2  The Beebotte Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 14.3.3  Beebotte Raspberry Pi Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 14.4  The PC Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Chapter 15   Communicating using an Android mobile phone . . . . . . . . . . . . . . . . . . 175 15.1  Android Mobile Phone Application Development . . . . . . . . . . . . . . . . . . . . . . . 175 15.2  Steps in Application Development Using the B4Android Software . . . . . . . . . . . 176 15.3  Example Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Appendix A  Introduction to the RASPBERRY PI computer . . . . . . . . . . . . . . . . . . . . 181 Appendix B  Using the Linux Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Appendix C  Programming listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

●9


Preface

Preface Internet of Things (IoT) is a new concept in intelligent automation and intelligent monitoring using the Internet as the communications medium. The “Things” in IoT usually refer to devices that have unique identifiers and are connected to the Internet to exchange information with each other. Such devices usually have sensors and/or actuators that can be used to collect data about their environments and to monitor and control their environments. The collected data can be processed locally or it can be sent to centralized servers or to the cloud for remote storage and processing. For example, a small device at the size of a matchbox can be used to collect data about the temperature, relative humidity and the atmospheric pressure. This data can be sent and stored in the cloud. Anyone with a mobile device (e.g. a mobile phone) can then access and monitor this data at any time and from anywhere on Earth provided there is Internet connectivity. Users in addition can for example adjust the central heating remotely using their mobile devices and accessing the cloud. Some devices that can act as members of an IoT system are: •

Sensors are required to sense the environment

Processors (usually microcontrollers) are required to collect and either to process this data locally or to send it to a central processor or to the cloud

Actuators are required to change the state of other devices, for example to turn on a switch

Communication devices are required to send and/or receive data remotely from the processors, e.g. using the Internet.

Power sources are required to power the electronic components

This book is written for students, for practising engineers, and for hobbyists who want to learn more about the building blocks of an IoT system, and also learn how to setup an IoT system using these blocks. This book has been written with the assumption that the reader has taken a course on digital logic design, and has been exposed to writing programs using at least one high-level programming language. Knowledge of the C programming language will be very useful. Also, familiarity with at least one member of the PIC series of microcontrollers (e.g. PIC16 or PIC18) will be an advantage. The knowledge of assembly language programming is not required because all the projects in the book are based on using the C language. If you are a total beginner in programming you can still use the book, but first you are advised to study introductory books on microcontrollers. Chapter 1 is an introduction to the IoT systems. In Chapter 2, the basic concepts and possible IoT architectures are discussed. The important parts of any IoT system are the sensors and actuators and they are described briefly in Chapter 3. The devices in an IoT system usually communicate with each other and the important topic of IoT communications technologies is the topic of Chapter 4. Perhaps the easiest way to learn how to create an IoT system is by using a development kit. There are many IoT development kits available in the market place and Chapter 5 describes the features of some of the commonly used kits. The Clicker 2 for PIC18FJ board is a development kit manufactured by mikroElektronika and can be used as processors in IoT systems. The features of this development kit are described in detail in Chapter 6. mikroC Pro for PIC is a popular microcontroller C language and Chapter 7 is a brief introduction to this language, giving its main features and also describing how to use this language. Click boards are sensor and actuator boards that can be used in IoT systems. Chapter 8 is about the use of a Click board with the Clicker 2 for PIC18FJ development kit. Similarly, the use of a sensor click board is described as a project in Chapter 9. The use of a click board actuator is the topic of Chapter 10. Here, a buzzer click board is used

● 11


INTERNET OF THINGS: An Introduction with PIC Microcontrollers as an example in a simple project. The Bluetooth and WiFi connectivity are important parts of any IoT system. Bluetooth can be used to establish communication between the devices in an IoT network. The WiFi can be used to send and receive data from the cloud using the Internet, as well as for communication between the various devices in an IoT network. Chapter 11 and Chapter 12 describe the use of Bluetooth and WiFi technologies in microcontroller based systems. The remaining chapters of the book give examples of creating a simple WiFi based IoT system where the data is stored on the cloud and thus can be accessed from any device provided the device has Internet access. Dogan Ibrahim London, 2015

â—? 12


Chapter 1 • Introduction to the Internet of Things

Chapter 1 •  Introduction to the Internet of Things 1.1 • Why IoT? Internet of Things (IoT) is a new revolution in remote monitoring and automation using the Internet to connect devices. It is expected that by the year 2020 over 50 billion devices (or things) will be connected by the Internet. The use of the IoT spans a wide range of applications, including homes, offices, factories, cities, industry, environment, agriculture, health, retail, transportation, and many others. Some applications of the IoT at homes include wireless enabled and Internet connected smart appliances which can be turned ON and OFF remotely using for example a mobile phone. Smart refrigerators can keep record of items stored and can place orders automatically through the Internet. Smart televisions can learn the watching habits of their owners and inform them when a show of possible interest to them is due. Smart heating control systems enable the central heating to be controlled automatically and remotely using the Internet. Wireless enabled burglar alarm systems can be activated automatically and warn the owners of any intrusion by sending messages to their mobile devices. Smart home lighting systems can turn ON and OFF automatically and can change their intensities to adapt to the environment. For example, the light intensity can be reduced automatically at day time to save energy. The lights can also be controlled automatically by sensing human movements. Smart smoke detectors can raise alarms in human voice describing where the problem is and what actions to take. There are many applications of the IoT in cities. For example, smart car parks can recognise that the drivers are looking for a space for parking and then inform them the availability of nearest car parks. Smart roads can inform bad weather conditions to drivers for example by sending them SMS messages. Traffic congestion, accident details, and road works can be sent to drivers to make the roads safer. Weather alerts can be sensed and the data can be analysed, informing the authorities to avoid any possible hazards. Smart environmental IoT systems can measure the pollution levels. The data collected by these systems can be analysed and any life threatening conditions can be broadcast to residents via the Internet so that the affected areas can be avoided. IoT can be used by the emergency services for a better environment. For example, gas leakage details can be sent automatically to gas service departments to avoid any explosions or poisoning. Similarly, water leakage can be detected by sensors and the relevant authorities can be informed to rectify the problems. Emergency services can be given priority at the traffic lights to speed up their attendance to problems. Forest fires can be detected early and appropriate actions can be taken to avoid any large scale damage to the environment. Similarly, river floods can be detected early and IoT based river flood monitoring systems can be used to raise alerts when the water level in rivers rises sharply. There are many applications of the IoT in the retail industry. For example, contactless cards can be used to purchase goods. The purchasing habits of the users can be stored by the system and the system can inform the users when special offers and discounts are available for any item that may interest them. IoT systems can also be used in vehicles. For example, the state of a vehicle engine can be sent to a maintenance centre by the help of an IoT based remote vehicle diagnostics application. Owners can then be informed of any faults in their vehicles. Appointments could also be made automatically with the maintenance centres. IoT systems can be used at the airports to inform passengers of any delays in departures

● 13


INTERNET OF THINGS: An Introduction with PIC Microcontrollers or arrivals. In addition, tickets can easily be purchased to any required destination for example by simple voice commands. IoT systems can also be used in healthcare to save lives. For example, wearable IoT devices allow continuous monitoring of health parameters such as the blood pressure, ECG, body temperature etc. Health centres can automatically be informed if a serious condition such as heart attack is detected.

1.2 • Cloud Computing Cloud computing has very strong links with the IoT applications as it provides storage and access features with great flexibility and interoperability through direct interface for communication and data exchange. Cloud computing is delivered over the Internet and it provides various hardware and software services including data access, storage, data processing, software, and hardware. The sensor data in IoT applications is normally stored on the Cloud. This has many benefits as we shall see in the next section. Cloud computing is elastic, which means that users can have as little or as much of a service as they want, and this service is fully managed by the provider. Based on the usage and availability of the services, there are several cloud models: Public Cloud is available to the general public where anyone with Internet access can use these services. These services are generally available free of charge on a self-service basis. Community Cloud shares the cloud services between several organizations or groups. Users of community cloud share the costs of the services. Private Cloud is managed by a single organization or a number of people for their use. Hybrid Cloud is a combination of two or more clouds. There are basically three types of services provided by a Cloud: Infrastructure as a service (IaaS): Here, the service providers are responsible for all the cost of the hardware, such as the servers, storage, networking, backup etc. The computers are provided to the users as virtual machines that can be started, stopped, and configured by the users. Users pay only for the computing services that they use. The applications software are built and managed on the Cloud machines by the users. Platform as a Service (PaaS): In this type of service, the providers in addition to the hardware they also provide development tools, applications programming interface, software libraries, compilers, and other software tools. The applications software are built and managed on the Cloud by the users. Software as a Service (SaaS): Here, the service providers offer users with specific applications, such as email, online storage, applications software and so on. SaaS applications are platform independent and can be accessed from anywhere and at any time using laptops, desktops, tablets, and mobile phones.

1.2.1 • Advantages of the Cloud Some of the most important benefits of the Cloud are as follows:

● 14

There is no need to purchase expensive computer and network hardware. All these are provided by the Cloud service providers.

The system can be setup easily and quickly as you do not have to know how to configure the computers, servers, backup etc.


Chapter 2 • Basic concepts and possible IoT architectures

Chapter 2 •  Basic concepts and possible IoT architectures In this chapter we shall be looking at the basic concepts and essential components of the world of the IoT. Possible IoT architectures will also be discussed and proposed.

2.1 • The IoT Concepts The basic concepts of the IoT are that devices (or things) interact with each other using some form of communications technology. Users can then control or monitor these devices over the Internet in order to change or see their operational states. It is this ability to communicate directly or indirectly with each other that makes IoT devices different from other types of sensor networks. In a typical IoT system, sensors collect information about the equipment (or the environment) they are connected to. This information is transmitted to a digital processor either by direct connection or by using Wi-Fi. The processor stores the received data in an Internet cloud so that it is accessible from anywhere in the world and at any time. Users can monitor or change the state of the equipment via the Internet using mobile devices such as mobile phones, tablets, PCs, or laptops. This change is usually under control of the user through the cloud and the digital processor which triggers the required actuator to achieve the required state change. In summary, the main components of all IoT systems are: •

Equipment (or the environment) to be monitored or controlled

Sensors (receiving the status of the equipment or the environment)

Actuators (controlling the environment or the equipment)

Communication links (usually Wi-Fi)

Digital processor(s)

Internet cloud

User devices (e.g. mobile phone, tablet, PC, or laptop)

2.2 • IoT Architectures There is no single IoT architecture. The architecture to use in a particular application depends on the type of application and on the availability of sensors, Wi-Fi link, processors etc. Some possible IoT architectures are described in this section.

2.2.1 • Using Distributed Processors In this architecture, each equipment has a local processor (e.g. a microcontroller) allocated to it. The processor receives information about the state of the equipment using sensors. The equipment is controlled by its local processor via the actuators. Local processors send information to a central processor which in turn stores this information on an Internet cloud. Users can interact with the cloud and monitor or change the state of any equipment in the system. The central processor gets commands from the cloud and monitors or controls each equipment as required. Figure  2-1 on page 18 shows a schematic of an IoT system using distributed processors. In this figure, the state of appliances fridge, dish washer, and cooker are received by the sensors and sent to the central processor via individual Wi-Fi links. The central processor stores this information on the cloud. Users can monitor or change the state of the appliances (e.g. turn an appliance ON) by sending commands to the central processor via the cloud using

● 17


INTERNET OF THINGS: An Introduction with PIC Microcontrollers the Wi-Fi. The central processor activates the required actuator by sending commands to local processor using the Wi-Fi links to achieve the required operation.

Figure 2-1  IoT using distributed processors There are several variations of the basic distributed processor architecture. For example, the equipment may be allowed to communicate with each other to exchange data, or they can communicate directly with the users. In addition, the local processors may be allowed to send and receive data directly from the cloud. Another alternative to Figure  2-1 is that each local processor can communicate with each other and/or with the central using other communication technologies, such as Bluetooth, ZigBee, RF, RFID, and so on. Distributed processor architecture has the disadvantage that the cost is relatively high, and each processor requires a power supply which may not be easy or convenient to obtain. This configuration is suitable if the equipment is placed with a distance from each other.

2.2.2 • Using Common Processor In this architecture, the sensors and actuators are all connected to a common processor through its input and output ports. There is no direct communication between the equipment. The common processor stores the states of various equipment on the cloud using the Wi-Fi link. Users normally send commands to the cloud when they wish to monitor or control an equipment. The common processor receives the commands from the cloud and activates the required actuator connected to its output port. Figure  2-2 on page 19 shows a schematic of an IoT system using common processor. This architecture has the advantage that the cost is relatively low and the power supply requirement is minimal.

● 18


Chapter 3 • Sensors and actuators

Chapter 3 •  Sensors and actuators Sensors and actuators are the building blocks of IoT systems. A sensor is a device that can measure a physical phenomenon such as temperature, humidity, wind speed, solar radiation, pressure, speed, acceleration etc. and then provide a measurable representation of the phenomenon usually in the form of an electrical signal or change in the electrical characteristic of an electrical component (e.g. voltage or current, or change of resistance, inductance, etc). Sensors are attached to the input ports of processors in IoT systems. Actuators are devices that create actions based on some form of stimulation or external trigger. For example, an electric motor is an actuator which rotates when triggered with an electric current. Actuators are attached to the output ports of processors in IoT systems. In this chapter we shall be looking at the basic principles of sensors and actuators used in IoT systems and give some examples of such components available in the market place.

3.1 • Sensors In general, sensors fall into two categories: analog and digital. Most modern sensors nowadays are digital and can easily be interfaced to a digital processor. Analog sensors give analog outputs (e.g. voltage, current, resistance, etc) where the output is related to the measured phenomenon either linearly or non-linearly. For example, a thermocouple given an analog output voltage related to the measured temperature. Similarly, the resistance of a thermistor device changes with the measured temperature. Analog sensor signals are continuous in time and usually vary in the range 0 to +5 volts. Analog sensors are connected to digital processors through an analog-todigital converter (ADC) chip. These chips convert the analog input voltage into digital form. Depending upon the required accuracy and the cost of implementation, an ADC can be 8, 10, 16, or even 24 bits wide. In most real-time applications 8 or 10-bit ADC converters are used. In a 10-bit ADC converter there are 1024 quantization levels (0 to 1023) and the reference voltage of the ADC is usually +5V. Thus, an input voltage of +5V produces a digital value of 1023. The resolution of such a converter is 4.88 mV/bit, or 0.2048 value/mV. For example, a 2V input (2000 mV) voltage produces the digital value of 2000x0.2048 = 409 in decimal, or “01 1001 1001” in binary. Digital sensors produce digital (or discrete) binary output signals which are related to the measured phenomenon. The accuracy of a digital sensor depends upon the number of bits used to represent the measured quantity. In general, digital sensors are more accurate than analog sensors. In addition, digital sensors have the advantages that they can be connected directly to digital processors, with or without using a known bus structure. Some commonly used bus structures with digital sensors are I2C, SPI, and UART.

3.1.1 • Examples of Sensors This section presents some of the commonly used sensors from different manufacturers. 1. Click Boards (mikroElektronika) These are small sensor (and some actuator) boards manufactured by mikroElektronika (www.mikroe.com). These boards are designed using company’s own protocol called the mikroBUS. Although the click boards are designed to be plug-in compatible with mikroelektronika’s microcontroller development boards (e.g. EasyPIC V7), they can also be used with other microcontroller systems and development boards. As shown

● 21


INTERNET OF THINGS: An Introduction with PIC Microcontrollers in Figure  3-1, a click boards consists of 2x8 pins and are designed with the PIC microcontrollers in mind.

Figure 3-1  Click board structure The pin configuration of a click board is shown in Figure  3-2. There are a large number of click boards available from mikroElektronika. Some example click boards are given in this section.

Figure 3-2  Click board pin configuration Light Click This board (Figure  3-3) incorporates a PD15-22CTR8 PIN photodiode to measure the ambient light intensity. The board also includes an MCP3201 ADC converter with SPI interface. The board operates with either 3.3V or 5V. Measured ambient light intensity is sent as an analog or digital signal to the external interface.

Figure 3-3  Light Click board

● 22


Chapter 3 • Sensors and actuators Pressure Click This board (Figure  3-4) measures the atmospheric pressure using an LPS331AP type digital pressure sensor chip. The pressure can be measured in the range 260-1260 mbar. The board is designed to operate with 3.3V power supply and measured pressure is sent as a digital signal to the external interface.

Figure 3-4  Pressure Click board DHT22 Click DHT22 is a temperature and humidity measurement board (Figure  3-5) using the DHT22 sensor chip. This sensor can detect the temperature between -40 and 80 degrees centigrade with half a degree precision. The relative humidity can be measured between 0 to 100%, and is accurate to 2%. The board is designed to operate with 3.3V or 5V and a serial digital signal is sent to the external interface.

Figure 3-5  DHT22 Click board GPS3 Click GPS3 click board (Figure  3-6 on page 24) is a high sensitivity GPS module using the L80 GPS chip set. The board operates with 3.3V and gives GPS information to the external interface. On-board patch antenna is provided. An external antenna interface is also available on the board.

● 23


Chapter 4 • IoT communication technologies

Chapter 4 •  IoT communication technologies Communication between the IoT devices, between the processors and the cloud, and between the users and the cloud is an important part of any IoT system. The range of transmission is not an important factor when selecting a communication technology. What is more important is reliable communication at low-cost and with low-power. Low Power Radio (LPR) or also known as Short Distance Radio (SDR) products can be used to establish communication between the IoT devices and the processors. Alternatively, Wi-Fi can be used in most parts of an IoT system. In this chapter we shall be looking at various communication technologies that can be used in IoT systems.

4.1 • Low Power Communication Systems The developer has several choices when it is required to design an IoT system to use an LPR device. This choice depends mainly upon the required range of operation, cost, power consumption, ease of use, physical size, weight, physical location of the system, and so on. Basically, the developer can choose between the following LPR devices: Bluetooth, Wi-Fi, ZigBee, Basic RF, and RFID. Here, the first three are based on well-known and established protocols, employing high levels of encryption algorithms. Basic RF is based on communication using well-known RF techniques without employing any of the above protocols. RFID is commonly used in product identification. A brief description and comparison of each of the LPR communication methods is given below. Brief specifications of LPR communication technologies are summarized in Table  4-1. Table 4-1 LPR communication technologies Bluetooth

Wi-Fi

ZigBee

Basic RF (UHF)

Basic RF (VHF)

Range

50-100m

100m

75m

500m

5-10km

Data Rate

24Mbps

54Mbps

250kbps

various

Various

Security

128-bit encryption

various

128-bit encryption

various

Various

Band

2.4GHz

2.4GHz

2.4GHz (+868MHz UK)

433, 866, 915MHz and others

150, 169, 173MHz and others

Power Consumption

High

High

Low

Low - medium

Low-medium

4.1.1 • Bluetooth Bluetooth is a wireless technology standard originally developed to replace the wired RS232 standard used in serial communication for many years. Invented by Ericsson in 1994, Bluetooth is used for short range data communication over the 2.4GHz (24002480MHz) unlicensed Industrial, Scientific and Medical (ISM) band. Bluetooth is based on the IEEE standard 802.15.1. Today, mobile devices, such as mobile phones, tablets, and laptop computers are equipped with Bluetooth capabilities. Bluetooth is commonly used to easily transfer image, audio, and video files between mobile devices. The protocol uses the spread spectrum frequency hopping technology where the data to be sent is divided into small packets and each packet is transmitted on one of the 79 designated Bluetooth channels with a bandwidth of 1MHz.

● 39


Chapter 5 • Internet of Things development kits

Chapter 5 •  Internet of Things development kits Perhaps the easiest and the cheapest way to learn the technical details of IoT systems and to have hands on experience using such systems is to purchase IoT development kits, or sometimes called IoT starter kits. In this chapter we shall be looking briefly at the features of some of the commercially available IoT kits.

5.1 • TinkerForge Starter Kit This kit (Figure  5-1) allows devices to be controlled over the internet. For example, mains switches, dimmers, and other home automation components and actuators can be controlled remotely by this kit. An external antenna is used to extend the range of the kit. The kit basically consists of a Master Brick and a Remote Switch Bricklet with a 433MHz transceiver. The Master Brick operates over the USB connection to control the remote equipment. Additional modules are available for example to measure the temperature or to react on movements. The kit is distributed with a Master Brick, Remote Switch Bricklet, and all the necessary cables, antenna, and the mounting kit.

Figure 5-1  TinkerForge Starter Kit (www.tinkerforge.com)

5.2 • ARM mbedIoT Starter Kit This IoT starter kit (Figure  5-2 on page 48) has been developed jointly by ARM and IBM. The kit consists of two parts: •

A microcontroller development board based on ARM’s Cortex-M4 processor that is pre-configured

A sensor board containing sensors to measure temperature, acceleration, potentiometers, rotating dimmer, a buzzer, a small joystick, an LED that can show different colours, and an LCD

The two components can be linked together over a network and a USB cable. A web site link is given where users can see the data collected by the sensors in real-time. One application using this kit may be to send the collected data to a remote sensor for processing. Based on this data the centre can then send commands to activate various actuators.

● 47


INTERNET OF THINGS: An Introduction with PIC Microcontrollers

Figure 5-2  ARM mbedIoT Starter Kit (http://developer.mbed.org/platforms/IBMEthernetKit)

5.3 • Pinoccio This is a microcontroller based IoT development kit having three models: Starter Kit, Lead Scout, and Field Scout. The Starter Kit (Figure  5-3) includes a Field Scout and a Lead Scout. The Field Scout is the basic module unit, based on the Arduino microcontroller. Every Field Scout can talk to other Field Scouts over the RF radio within a range of 100m and in a mesh type network, called TROOP (Figure  5-4). This network is said to be 14 times more efficient than a Wi-Fi. The Field Scout units contain a built-in temperature sensor and an RGB LED. A Wi-Fi Backpack module can be added to the basic unit to enable it to be connected to the Internet. A Lead Scout is a Field Scout with a Wi-Fi backpack and is included in the Starter kit. A rechargeable high capacity Li-Po battery is included with the unit which allows power levels to be monitored. A free API is provided so that users can develop their own software using the Pinoccio kits. In addition, an easy to use compiler, called ScoutScript, is provided for users who want to develop their own applications.

Figure 5-3  Pinioccio Starter Kit (https://pinocc.io)

Figure 5-4  Pinoccio mesh network (https://pinocc.io)

● 48


Chapter 6 • Using the Clicker 2 for PIC18FJ board

Chapter 6 •  Using the Clicker 2 for PIC18FJ board In this chapter we shall be looking at the features of the Clicker 2 for PIC18FJ board (called the Clicker 2 board for short from now on) and see how it can be programmed using the mikroC Pro for PIC programming language. The Clicker 2 board has some interesting features which makes it an ideal board for use in IoT based applications: •

The board has a small footprint (81mm x 60.5mm)

The board has low power consumption

The processor is the powerful PIC18F87J50 microcontroller (12MIPS) with 128K flash program memory and 3904 bytes of RAM

The board has a battery connector where an external 2000mAh Li-Polymer battery can be connected

On-board Li-Polymer battery charger

There are two mikroBUS sockets on the board, enabling up to two click boards to be connected by just plugging them into these sockets (additional Click boards can be connected using shield boards). There are over eighty click boards, most of them developed as sensors that can be used in IoT based applications. In addition, Bluetooth, Wi-Fi or RF Click boards can be plugged in to the board to communicate with other Clicker 2 boards or to communicate directly with the internet or with mobile devices in IoT based applications.

The board can be programmed via its USB connector

Push-button switches and LEDs are provided on the board that can be useful during program development

ON/OFF switch and Reset buttons are provided

In the remainder of this chapter we shall be looking at how to use and program the Clicker 2 board. In addition, a brief introduction will be made to the mikroC Pro programming language which is the language developed by mikroElektronika(www.mikroe. com) for use in their PIC microcontroller family of development boards.

6.1 • The Clicker 2 Board The Clicker 2 is a small (81mm x 60.5mm) microcontroller development board (Figure  6-1 on page 66) based on the mid-range 8-bit PIC18F87J50 microcontroller, providing up to 12MIPS throughput. The board is sold ready assembled and is manufactured by mikroElektronika. A functional block diagram of the Clicker 2 board is shown in Figure  6-2 on page 66. At the heart of the circuit is the PIC18F87J50 microcontroller. The two Click board sockets are connected to the microcontroller input/output ports and they provide UART, SPI, and I2C bus interfaces as well as the capability to plug in two Click boards. The two headers at either side of the board (HDR1 and HDR2) provide general purpose input/output ports to and from the microcontroller. Header mProg is used to program the microcontroller using the mikroProg programmer device (www.mikroe.com). Two push-button switches are connected to port pins RD7 and RH3 of the microcontroller. The switch output is normally at logic HIGH and goes LOW when a switch is pressed. Two LEDs are connected to port pins RD4 and RE4 of the microcontroller through 2.2K current limiting resistors.

● 65


INTERNET OF THINGS: An Introduction with PIC Microcontrollers

Figure 6-1  Clicker 2 board (www.mikroe.com) Thus, sending a logic HIGH signal to either of these port pins will turn ON the corresponding LED. LTC3586 power management IC is used in the design. The board can be powered through the mini USB port or using an external Li Polymer rechargeable battery (Figure  6-3 on page 67), which can be charged through the on-board charger.

Figure 6-2  Functional block diagram of the Clicker 2 board

● 66


Chapter 7 • mikroC PRO for PIC programming language

Chapter 7 •  mikroC PRO for PIC programming language The Clicker 2 board is normally programmed using the mikroC Pro for PIC programming language and the compiler. This compiler has been developed by mikroElektronika (www. microe.com) and is one of the easy to learn compilers for microcontrollers with rich resources, such as a large number of library functions and an integrated development environment with built-in simulator, and an in-circuit-debugger. A demo version of the compiler with a 2K program limit is available from mikroElektronika. mikroC Pro for PIC is a C language developed specifically for the PIC family of microcontrollers. In this chapter we shall be looking at some of the basic features of this language and discuss its main differences from the standard C languages. The chapter is not an introduction to the C language and it is expected that the readers have a working knowledge of a version of C.

7.1 • Structure of a mikroC Pro for PIC Program Figure 7.1 shows the simplest structure of a mikroC Pro for PIC program. This program flashes an LED connected to port RB0 (bit 0 of PORT B) of a PIC microcontroller with one second intervals. Do not worry if you do not understand the operation of the program at this stage as all will be clear as we progress through the chapter. Some of the programming statements used in Figure  7-1 are described in the following sections.

Figure 7-1  Structure of a simple mikroC Pro for PIC program

7.1.1 • Beginning and Ending of a Main Program In mikroC Pro for PIC language a main program begins with the keywords:

void main()

After this, a curly opening bracket is used to indicate the beginning of the program body. The main program is terminated with a closing curly bracket. Thus, as shown in

● 91


Chapter 8 • Using a Click board with the Clicker 2 for PIC18FJ board

Chapter 8 •  Using a Click board with the Clicker 2 for PIC18FJ board In this chapter we shall see how to plug in and use a Click board on the Clicker 2 for PIC18FJ board (it will be called the Clicker 2 board for simplicity). Here, a 2-digit 7-segment Click board will be used as a display. A display could be useful in IoT based applications to display the output of a sensor, or the state of the system at any time.

8.1 • The 7-Seg Click Board The 7-Seg click board is a microBUS compatible board for adding 7-segment display to a system. The board features two 74HC595 8-bit serial-in, parallel-out shift register modules as well as two 7-segment displays. The board communicates with target board via SPI interface using signals SDI, SDO, and SCK. It can operate from either +5V or +3.3V (by default it is configured to operate from +3.3V). Figure  8-1 shows the functional block diagram of the board. The serial output of one of the 74HC595 converters is connected to the serial input of the other converter and the parallel outputs of both converters drive the two displays at the same time. Both displays are enabled from a common signal at the same time. The circuit diagram of the board is shown in Figure  8-2.

Figure 8-1  Functional block diagram of the 7-Seg Click board

Figure 8-2  Circuit diagram of the 7-Seg Clicker board (www.mikroe.com)

● 111


INTERNET OF THINGS: An Introduction with PIC Microcontrollers

8.2 • Plugging in the 7-Seg Click Board The 7-Seg Click board can be plugged in to either the mikroBUS socket 1 or 2 of the Clicker 2 board. As shown in Figure  8-3, in this project it is connected to mikroBUS socket 1. The Clicker 2 board should then be connected to the PC via its mini USB connector for programming and providing power. No hardware modifications or setup are necessary on either of the boards. The Clicker 2 board can now be programmed using the mikroC Pro for PIC programming language.

Figure 8-3  7-Seg Click board plugged in to the Clicker 2 board

8.3 • The Project This is a simple project aimed to show how the 7-Seg Click board can be used in IoT based projects. In this project the Clicker 2 board will be programmed to count up and display from 0 to 99 on the 7-Seg Click board with one second delay between each count. 7-segment displays are made up of 7 segments of LEDs and an optional decimal point. As shown in Figure  8-4 on page 113, the segments are identified using letters a – g. Thus, for example, number 1 is displayed by turning ON segments b and c only. Number 2 is displayed by turning ON segments a,b,g,e,and d. Similarly, number 8 is displayed by turning ON all the segments.

● 112


Chapter 9 • Using a sensor with the Clicker 2 for PIC18FJ board

Chapter 9 •  Using a sensor with the Clicker 2 for PIC18FJ board Sensors are important parts of all IoT systems. In this Chapter we shall see how a sensor can be connected to our Clicker 2 for PIC18FJ board (or simply the Clicker 2 board). The sensor used in this chapter is the HTU21D humidity and temperature sensor which is available as a Clicker board.

9.1 • Connecting the HTU21D Click Board to the Clicker 2 Board The HTU21D Click board (see Figure  9-1) can be plugged in to either of the two mikroBUS sockets on the Clicker 2 board. In this project we shall be plugging in to socket 2 (see Figure  9-2). The project hardware is now ready and there are no other hardware components required.

Figure 9-1  HTU21D Click board

Figure 9-2  HTU21D Click board and Clicker 2 board

● 119


INTERNET OF THINGS: An Introduction with PIC Microcontrollers

9.2 • The HTU21D Click Board As shown in Figure  9-1 on page 119, the HTU21D is a mikroBUS compatible click board that can be used to measure the relative humidity and the temperature. The sensor is calibrated and tested during the manufacturing process. Some application areas of this sensor are: medical equipment, humidifiers, printers, home appliances, automotive applications, and so on. The HTU21D sensor has the following features: •

No calibration required

Temperature measurement range: -40ºC to +125ºC

Relative humidity measurement range: 0% to 100%

Operating voltage: 3.6V

Current consumption 450µA (0.02µA in sleep mode)

Resolution: 12 or 8 bits

Measurement time: 14ms (12 bits) or 2ms (8 bits)

I2C bus compatible

9.3 • Operation of the HTU21D Sensor Figure  9-3 on page 121 shows the circuit diagram of the HTU21D Click board. Sensor pins SCK and SDA are pulled-up to VCC using 10K resistors (as required by the I2C standard). mikroBUS socket 2 has the following pin assignments: RA1 RG0 RD0 RB2 RD1 RC7 RC3 RC6 RC4

RD6 (SCL)

RC5

RD5 (SDA)

3.3V 5V GND GND Sensor pins SCK and SDA are thus connected to I2C pins SCL (RD6) and SDA (RD5) of the Clicker 2 board through the mikroBUS 2 socket. The SCK is the clock (maximum 0.4MHz) and used to synchronize the communication between the microcontroller and the HTU21D sensor chip. SDA is the serial data pin used to transfer data in and out of the device. RD6 and RD5 pins are shared with the second I2C bus (i.e. I2C2) of the PIC18F87J50 microcontroller.

● 120


Chapter 10 • Using an Actuator with the Clicker 2 for PIC18FJ board

Chapter 10 •  Using an Actuator with the Clicker 2 for PIC18FJ board Actuators are important parts of all IoT systems as they can be used to change the state of external objects requiring large currents or voltages for their operation. e.g. a high voltage motor can be turned ON and OFF, a mains pump can be turned ON and OFF, a cooker, a microwave oven, a television, a fridge, or a similar mains operated appliance can be turned ON and OFF and so on. In this Chapter we shall see how a simple actuator can be connected to our Clicker 2 for PIC18FJ board (or simply the Clicker 2 board). The actuator used in this chapter is a simple relay which is available as a Clicker board, called the Relay Click, available from the manufacturer mikroElektronika (www.mikroe.com).

10.1 • Connecting the Relay Click Board to the Clicker 2 Board The Relay Click board (see Figure  10-1) can be plugged in to either of the two mikroBUS sockets on the Clicker 2 board. In this project we shall be plugging in to socket 1 (see Figure  10-2). The project hardware is now ready and there are no other hardware components required.

Figure 10-1  Relay Click board

Figure 10-2  Relay Clicker board and Clicker 2 board

● 125


INTERNET OF THINGS: An Introduction with PIC Microcontrollers

10.2 • The Relay Click Board As shown in Figure  10-1 on page 125, the Relay Click is a mikroBUS compatible clicker board that can be used in applications where relays are needed. e.g. for turning high power devices ON and OFF. The basic features of the Relay Clicker board are: •

Two independent G6DAAS1-5DC relays on the board

Relay outputs up to 5A, 250V AC/30V DC loads

Relays driven by current sinking through transistors

+5V operation

LED to indicate power to the board

LEDs to indicate the relay status

Relay outputs terminated at screw connectors

10.3 • Operation of the Relay Click Board Figure  10-3 shows the circuit diagram of the Relay Click board. mikroBUS socket 1 has the following pin assignments: RA0 RG3 RD2 RB3 RD3 RG2 RC3 RG1 RC4 RD6 RC5 RD5 3.3V 5V GND GND In reference to Figure  10-3, we can see that the relay 1 and relay 2 control pins are connected to input/output ports RG3 and RD3 of the Clicker 2 board respectively.

Figure 10-3  Circuit diagram of the Relay Click board (www.mikroe.com)

● 126


Chapter 11 • Using Bluetooth connectivity

Chapter 11 •  Using Bluetooth connectivity Bluetooth connectivity is very important in IoT based applications as it enables simple and low-cost communication between various nodes in the system. In a typical application processors may receive sensor data and send this data to a central processor or to users’ mobile devices using the Bluetooth connectivity. The central processor can send the received data to the cloud using a WiFi connectivity. Similarly, messages can be received from the cloud, or from a central processor, or directly from users’ mobile devices using WiFi connectivity, and then appropriate actions can be taken such as activating actuators and so on using either WiFi or Bluetooth connectivity. The main disadvantage of Bluetooth is that it is not Internet compatible and therefore it is not possible to send data directly to the cloud using Bluetooth connectivity. All smart mobile devices are equipped with the Bluetooth connectivity and thus can be used to receive or send data to either the sensor processors or to a central processor. In this Chapter we shall see how to add Bluetooth connectivity to our Clicker 2 for PIC18FJ board both in Master and in Slave modes.

11.1 • About the Bluetooth Connectivity Bluetooth is a form of digital communication standard for exchanging data over short distances using short-wavelength radio waves in the ISM (Industrial, Scientific and Medical) band from 2.402 – 2.489GHz. Bluetooth was originally conceived in 1994 as an alternative to the RS232 serial communications. Bluetooth communication is in the form of packets where the transmitted data is divided into packets and each packet is transmitted using one of the designated Bluetooth channels. There are 79 channels, each with 1MHz bandwidth, starting from 2.402GHz. The channels are hopped 1600 times per second using an adaptive frequency hopping algorithm. Because the communication is based on RF, the devices do not have to be in line of sight of each other in order to communicate. Each Bluetooth device has a Media Access Control (MAC) address where communicating devices can recognize and establish link if required. Bluetooth communication operates in a master-slave structure, where one master can communicate with up to 7 slaves. At any time data can be transferred between a master and a slave device. The master can choose which slave to communicate to. In the case of multiple slaves the master switches from one slave to the next. Bluetooth is a secure way to connect and exchange data between various devices such as mobile phones, laptops, PCs, printers, faxes, GPS receivers, digital cameras and so on. The effective communication range in a Bluetooth application depends on many factors, such as the antenna size and configuration, battery condition, attenuation from walls, and so on. Further information about the Bluetooth communication standards can be obtained from many books, from the Internet, and from the Bluetooth Special Interest Group.

11.2 • Adding Bluetooth Connectivity In this Chapter we shall be using the Bluetooth Click board to add connectivity to our processor board Clicker 2 for PIC18FJ (or simply the Clicker 2 board). Bluetooth Click (Figure  11-1 on page 130) is a small mikroBUS compatible board manufactured by mikroElektronika (www.mikroe.com) that provides Bluetooth connectivity. The board is based on the RN-41 Bluetooth controller chip which operates

● 129


Chapter 12 • Using WIFI connectivity

Chapter 12 •  Using WIFI connectivity WiFi connectivity is very important in IoT based applications as it enables various nodes in the system to communicate with each other, and also the nodes to communicate directly with the cloud or with a mobile device. In a typical application processors receive sensor data and send this data directly to the cloud or to a central processor or to users’ mobile devices using the WiFi connectivity. Similarly, messages can be received from the cloud, or from a central processor, or directly from users’ mobile devices using WiFi connectivity and then appropriate actions can be taken such as activating actuators and so on. WiFi connectivity can also be used to provide Internet access to the nodes or to a central processor participating in an IoT project. Most homes, offices, public places, and factories have WiFi devices (or routers) operating at all times of the day. Using the existing WiFi devices, the connectivity between various nodes in an IoT based system becomes a lowcost and an easy process. In this Chapter we shall see how easy it is to add WiFi connectivity to our Clicker 2 for PIC18FJ board.

12.1 • Adding WiFi Connectivity In this Chapter we shall be using the WiFi Plus Click board to add connectivity to our processor board Clicker 2 for PIC18FJ (or simply the Clicker 2 board). WiFi Plus Click (Figure  12-1) is a small mikroBUS compatible board manufactured by mikroElektronika (www.mikroe.com) that provides WiFi connectivity to an embedded system. The board is based on the MRF24WB0MA 2.4GHz, IEEE standard 802.11 compliant module as well as MCW1001 companion controller with on board TCP/IP stack and 802.11 connection manager. The board operates from 3.3V and communicates with the target microcontroller via UART interface. Figure  12-2 shows the circuit diagram of the WiFi Plus clicker board. In this chapter we shall see how to use the WiFi Plus Click board in a project.

Figure 12-1  WiFi Plus Click board

Figure 12-2  Circuit diagram of the WiFi Plus Click board

● 149


Chapter 13 • INTERNET of THINGS example

Chapter 13 •  INTERNET of THINGS example In this Chapter we shall look at the design of a simple IoT system using two sensors and an actuator. This example shows how the data collected by the sensors can be sent to a central processor using WiFi. The central processor also communicates with a PC again using WiFi. Although this is a simple example, it demonstrates the techniques that can be used during the development of a real IoT project. The sensors in this design are based on the Click boards mounted on the Clicker 2 for PIC18FJ boards (or simply the Clicker 2 boards). The central processor is a Raspberry Pi computer. The reason for using a Raspberry Pi computer is because of its fast and powerful hardware architecture, very popular operating system, and easy to use and flexible Python programming language. It will be helpful if the readers have some experience of socket based network programming using the UDP protocol. Also, some knowledge of the Raspberry Pi computer and the Python programming language will be an advantage. The details of the Python programming language are beyond the scope of this book and interested readers should refer to many books and articles available on the Internet. Some information on the Raspberry Pi computer is given in Appendix A on page 181 for those readers who are not familiar with this computer. In this book the Linux operating system is used on the Raspberry Pi. Appendix B on page 191 gives an introduction to the commands of the Linux operating system that could be useful to those readers who are not familiar with this operating system.

13.1 • Block Diagram The block diagram of the example system is shown in Figure  13-1 on page 158. Four Click boards are used in this system, plugged-in onto two Clicker 2 boards, which are used as processors. The Clicker 2 boards are named as SENSORS node and ACTUATORS node: •

SENSORS: Two Click boards: HDU21D and Wi-Fi Plus are plugged-in onto the SENSORS Clicker 2 board. HDU21D measures the temperature and the humidity. The WiFi Click board is plugged-in to mikroBUS socket 1 of the Clicker 2 board, and the HDU21D Click board is plugged-in to socket 2.

ACTUATORS: A Buzz click board and a WiFi Plus Click board are pluggedin onto the ACTUATORS Clicker 2 board. The WiFi Click board is plugged-in to mikroBUS socket 2 of the Clicker 2 board, and the Buzz Click board is pluggedin to socket 1.

The communication between the Clicker 2 boards and the Raspberry Pi computer is based on WiFi via the wireless access point. The operation of the system is as follows: Temperature and humidity data will be collected continuously by the SENSORS node every minute. This data will be sent to the Raspberry Pi computer where it will be displayed on the Raspberry Pi computer monitor in text form. Users can turn ON the buzzer on the ACTUATORS node by sending a command using the PC.

● 157


Chapter 14 • Storing the data on the CLOUD

Chapter 14 •  Storing the data on the CLOUD In IoT applications we may want to access the data in real-time from anywhere in the world and using any kind of device that has access to the Internet. In this Chapter we shall see how the data received by our Raspberry Pi computer can be stored on an Internet cloud so that it can be accessed by any kind of device provided the device has access to the Internet.

14.1 • The Hardware Setup The hardware setup in this Chapter is shown in Figure  14-1. This figure is basically same as Figure 13.4, but here additionally the Internet cloud is shown with the Raspberry Pi computer sending and receiving data from the cloud.

Figure 14-1  The hardware setup

14.2 • The Software In order to store the data on an Internet cloud, a program is developed on the Raspberry Pi that receives the transmitted temperature and humidity data from the SENSORS node and then sends this data to the Internet cloud. Anyone with a device that can browse the Internet can access and see the data in text or in graphical form. In this application, the Raspberry Pi is the central processor as in the example in Chapter 13 on page 159. The SENSORS and ACTUATORS programs used in this chapter are same as the ones given in Appendix C.16 on page 236 and Appendix C.17 on page 242 respectively. The Raspberry Pi program is modified to send the temperature and humidity data to the cloud. The PC program is modified to set the upper limit of the temperature such that the buzzer will sound if the temperature goes above this set point. In summary, the operation of the system can be described as follows: SENSORS node: Read and send the temperature and humidity data to the Raspberry Pi computer every minute.

● 165


INTERNET OF THINGS: An Introduction with PIC Microcontrollers ACTUATORS node: Turn the buzzer ON if a “1” command is received from the Raspberry Pi computer. Raspberry Pi computer: Read the temperature and humidity data and send them to the Internet cloud. Receive command from the PC and control the buzzer accordingly. PC: The PC receives temperature and humidity data from the cloud and displays in text or in graphical form. In addition, the program receives the upper limit of the temperature from the user and sends commands to the ACTUATORS node to control the buzzer as required. There are several software packages available that help to store and manipulate data on an Internet cloud. Some popular ones are Cosm, Nimbits, SensorCloud, Exosite, Leylan, Manybots, Xively, Beebotte and so on. In this chapter we shall see how to develop a Python program on the Raspberry Pi using the Beebotte software package.

14.3 • Using the Beebotte Beebotte is a cloud platform for storing and then providing access to real-time data. Beebotte software package offers the following features: •

Providing connection and real-time display of data from anywhere and anytime

Easy programming by the help of the API and tutorials provided by Beebotte

Availability of various widgets

Automatic display of stored data in real-time and in graphical form

Beebotte is available in several versions depending upon the cost, message capacity, and message history: XS This is the free version of the software and it offers the following features: •

Unlimited channels

50,000 messages a day

10,000 persistent messages a day

3 months history

SSL Encryption

Small This version is available at $10 a month and it offers the following features: •

Unlimited channels

200,000 messages a day

20,000 persistent messages a day

12 months history

SSL Encryption

Medium This version is available at $30 a month and it offers the following features:

● 166


Chapter 15 • Communicating using an Android mobile phone

Chapter 15 •  Communicating using an Android mobile phone In IoT applications we may want to access the system using a mobile device such as a mobile phone, tablet, or a PC. For example, we may want to send a message to a node in the system, or display the status of a node or display the collected data on our mobile phone. In fact, the phone can be an excellent remote control for the system. The Android Operating System (OS) has been developed by Google for mobile phones, tablets, notebooks and so on. Android OS is very popular and is used by many mobile device manufacturers. Some of the advantages for using the Android OS are: •

The Android development tools can easily be downloaded from the Internet free of charge.

Android application developers can share (or even sell) their applications through various distribution channels.

There are many hardware platforms that are compatible with the Android OS.

There are millions of users of the Android OS all around the world.

In this Chapter we shall see how an Android based mobile phone can be used to access the system we have developed in Chapter 14 on page 165. Here, the PC will be replaced with an Android mobile phone (model Samsung Note 4) and a GUI based application will be developed for the mobile phone to send a message to the Raspberry Pi computer to activate the buzzer, as was done with the PC.

15.1 • Android Mobile Phone Application Development There are several ways that an application can be developed for an Android based mobile device. Some of the required tools are: •

Java Development Kit (JDK)

Android Software Developer’s kit (SDK)

Android Developer Tool (ADT)

Eclipse Integrated Development Environment

Basic 4 Android Programming Language (B4Android)

In this chapter we shall be using the Basic 4 Android (B4Android) programming language to develop our application. B4Android is an interactive programming language similar to the popular Visual Basic. Programmers can develop their applications on a PC and then download them to their Android mobile devices. There are several ways that an application can be developed using the B4Android: •

Using the Android emulator

Connect to a real device through the USB (in debug mode)

Connect to a real device with B4A-Bridge software

The Android emulator, although very useful, is very slow compared to a real device. The emulator can be useful during the development of small programs. Perhaps the best and the most reliable way to develop a complex application is by using a real device with the B4A-Bridge software. In this Chapter we shall be using a Samsung Note 4 type mobile phone with the Android OS. The B4Android application development

● 175


Appendix A • Introduction to the RASPBERRY PI computer

Appendix A • Introduction to the RASPBERRY PI computer The Raspberry Pi computer (see Figure  A-1) is a credit card sized board that is a powerful computer. This computer runs under the popular Linux operating system. As we shall see in later sections, the Raspberry Pi has three models: Raspberry Pi Model A, Raspberry Pi Model B, and Raspberry Pi 2 Model B, all being extremely affordable, costing approximately $25, $35, and $45 respectively. In this book we shall be using the basic Raspberry Pi Model B. The Raspberry Pi is a fully featured computer and you can do most things with it that you do with a desktop computer or a laptop. Part of the reason for its low cost is that it requires some external parts for its operation, such as a power-supply, keyboard, monitor, a case to protect it and so on. The computer boots and runs from an SD card which can also be used to store data. Additionally, external USB based hard disks and flash memory devices can be connected to the computer to increase its data capacity. The Raspberry Pi computer has ports that you can plug mouse and keyboard, monitor (or TV), hard disk, flash memory device, audio output (speakers), Ethernet connector, and SD card.

Figure A-1  The Raspberry Pi computer (Model B)

A.1 • What Can You Do With a Raspberry Pi? The Raspberry Pi is a very powerful computer. Its performance is comparable with a PC using a Pentium 2 processor, running at 300MHz. In general, you can do almost anything that you can on any other Linux desktop computer. Some example application areas are: •

General purpose computer that can be used to learn the Linux operating system

General purpose computer that can be used to learn programming languages

For learning how computers work

For browsing the internet

For building your own web site

● 181


Appendix B • Using the Linux Command Line

Appendix B • Using the Linux Command Line Linux is one of the most popular operating systems in use today. Linux is very similar to other operating systems, such as Windows and UNIX. Linux is an Open operating system based on UNIX, and has been developed collaboratively by many companies since 1991. In general, Linux is harder to manage than some other operating systems like Windows, but offers more flexibility and configuration options. There are several popular versions of the Linux operating system such as Debian, Ubuntu, Red Hat, Fedora and so on. Linux instructions are text-based. In this chapter we shall be looking at some of the useful Linux commands and see how you can manage your Raspberry Pi. When you apply power to your Raspberry Pi, the Linux command line (or the Linux shell) is the first thing you see and it is where you can enter operating system commands.

B.1  The Command Prompt After you login to Raspberry Pi, you see the following prompt displayed where the system waits for you to enter a command:

pi@raspberrypi ~$

Here, pi is the name of the user who is logged in. raspberrypi is the name of the computer, used to identify it when connecting over the network. ~ character indicates that you are currently in your default directory. $ character indicates that you are a normal user (not a privileged super-user) Useful Linux Commands In this section we shall be looking at some of the useful Linux commands where examples will be given for each command. A summary of the commands to be covered in this chapter are given below. In this chapter, commands entered by the user are shown in bold for clarity. Also, it is important to remind you that all the commands must be terminated by the Enter key: Directory related commands pwd

Show current working directory

ls

List directory contents

cd

Change current directory

File related commands cp

Copy a file

mv

Move (or rename) a file

rm

Remove (or delete) a file or a directory

mkdir

Create a directory

rmdir

Remove (or delete) a directory

cat

Display contents of a file

less

Display contents of file (not all)

more

Display contents of a file (not all)

vi

Edit a file using vi editor

● 191


Appendix C • Programming listings

Appendix C • Programming listings C.1  Program Description Language (PDL) 7-Seg Clicker project BEGIN/MAIN

Clear count to 0

Configure I/O port directions

Initialize SPI bus

Clear 74HC595 registers

Enable displays

DO FOREVER

Extract MSB digit of count

Extract LSB digit of count

CALL WriteDigit(MSB digit, LSB digit)

Increment count

Wait 1 second

ENDDO

END/MAIN BEGIN/WriteDigit(a, b)

Write digit b via SPI bus

Write digit a via SPI bus

Latch the signal

END/WriteDigit

● 205


INTERNET OF THINGS: An Introduction with PIC Microcontrollers

C.2 • Progam listing for 7-Seg Clicker project (SEVEN.c) /*----------------------------------------------------------------------------7-SEG CLICKER PROJECT ===================== In this project a 7-Seg Clicker board is used. The board consists of a 2 digit 7-segment display. In this project it is assumed that the 7-Seg Clicker board is plugged onto a Clicker 2 for PIC18FJ 2board. The 7-Seg Clicker board operates using the SPI bus. Two 74HC595 type serial to parallel converter chips are used on the board. The data to be displayed for both digits is sent in serial form from the Clicker 2 board. This data is converted into parallel form and is presented to the two displays. The displays are turned ON when the display_enable (port pin RG3) is set HIGH. In this project the display counts up from 00 to 99 with one second delay between each output. Program: SEVEN.c Author:

D. Ibrahim

Date:

May 2015

------------------------------------------------------------------------------*/ // // Define the 7-segment bit pattern for each number // const unsigned short CharTable[10]= { 0x7E, // '0' 0x0A, // '1' 0xB6, // '2' 0x9E, // '3' 0xCA, // '4' 0xDC, // '5'

_a_ f|

|b

|_g_| e|

|c

|_d_|.dp

0xFC, // '6' 0x0E, // '7' 0xFE, // '8' 0xDE

// '9'

}; sbit HC595_stcp at RD3_bit; sbit HC595_stcp_direction at TRISD3_bit; sbit HC595_mr at RD2_bit; sbit HC595_mr_direction at TRISD2_bit; sbit display_enable at RG3_bit; sbit display_enable_direction at TRISG3_bit;

â—? 206


Index

Index Symbols 7-Seg Click Board 111 8-Bit Mode 79 16-Bit Mode 80 74HC595 bit 113 74HC595 converters 111 A actuator 125 actuators 15, 21, 36, 47 Actuators 17 ACTUATORS node 159 ADC 100 A/D conversion 81 A/D converters 80 ADXL345 34 AES 128-bit encryption 40 Alcohol Click 25 analog 21 analog sensors 21 Android mobile phone 175 Android Software Developer’s kit (SDK) 175 ANSI C Ctype 100 ANSI C Math 100 ANSI C Stdlib 100 ANSI C String 101 architecture 17 Arduino 59 Arduino Fio 59 Arduino Uno 59 ARM mbedIoT 47 Arrays 93 Atmel SAM 3S ARM CPU 53 B Basic 4 Android Programming Language (B4Android) 175 basic concepts 17 Basic RF 39, 42 BeagleBone Black 51 Beebotte 15, 166 Beebotte API 167 Beebotte Environment 169 BL600-eBoB 56 Bluetooth 15, 18, 39 Bluetooth Click 129 Bluetooth Connectivity 129 Bluetooth module 40 board structure 22 Bridge/grove module 49

Brown-out reset 69 C C,<address> 136 CAN 100 CANSPI 100 Changing File Permissions 195 chmod 195 click boards 21 Clicker 2 55, 90 Clicker 2 board 65 Clock Sources 72 Cloud computing 14 Cloud service 15 Command Prompt 191 common processor 18 Communication links 17 Compact Flash 100 CongatecQsevenIoT Development Kit 62 Constants 93 Control form 156 Conversion 101 converter 21 Copying a File 198 Cosm 15 Creating a Subdirectory 193 Cryptography 45 Current Directory 193 D Deleting a File 199 DHT22 Click 23 digital 21 Digital processor 17 digital processors 38 Digital sensors 21 Directory related commands 191 Directory Structure 192 Disk Usage 203 Displaying File Permissions 194 distributed processor 18 Dock board 55 DS1820 32 DVI socket 184 E EasyPIC V7 21 EEPROM 100 EFM32 Gecko Starter Kit 61 Electric Motors 36 Equipment 17 Espruino Pico 49 Ethernet 100 Ethernet modules 44

● 255


INTERNET OF THINGS: An Introduction with PIC Microcontrollers External Reset 72 external trigger 21 F Fail-safe clock monitor 69 File related commands 191 Flash Memory 100 Flutter 53 FlyportPro Starter Kit 51 Function Prototypes 98 G gateways 16 G<char> 135 GetDigit() 115 GK 135 GPS3 Click 23 Graphics LCD 100 Gyroscope/accelerometer module 49 H HDMI cable 184 HDMI to DVI converter 185 HDU21D 157 Header Files 95 HTU21D Click 119 HTU21D Sensor 120 Humidity 123 Hydrogen Click 24 HYT-271 33 I I2C 100 IaaS 14 iDigi 15 IEEE standard 802.11 149 IEEE standard 802.15.1 39 In-circuit debugger 69 Infrastructure as a service 14 Intel Galileo 53 Internal oscillator 69 Internal Oscillator 74 Internet 17 Internet cloud 17 Interrupts 83 IntrinsycIoT Development Kit 60 IoT 13 IoT devices 39 IoT Security 45 IoT Systems 44 IR transmitter module 49 ISM band frequency 43 Iteration Statements 95 I, <value1>,<value2> 136 â—? 256

K K 136 Keypad 100 Killing a Process 202 L Large 167 LCD 100 LED segment 113 Lelylan 15 Light Click 22 Light/colour/proximity module 49 Linux Command Line 191 LM35DZ 32 Low Power Radio 39 ls command arguments 195 M M2M 15 Manchester Code 100 Manual Connection 137 Manybots 15 Master Brick 47 Master Mode 144 MCP3201 22 Media Access Control (MAC) 129 Medium 166 mesh network 48 Methane Click 25 microBUS 111 microcontroller 17, 47 mikroBUS 119 mikroC Pro 91 mikroProg 87 ML8511 34 mobile devices 17 ModBus 15 MPX4115 33 MQ-7 35 multi-tasking applications 84 N Nimbits 15 numbers and bit patterns 114 O One Wire 100 Onion Omega 54 Operators Arithmetic 94 Assignment 94 Bitwise 94 Conditional 94


INTERNET OF THINGS Dogan Ibrahim

In addition, he is the author of over 250 technical papers, published in journals, and presented in seminars and conferences.

ISBN 978-1-907920-44-8

DESIGN

www.elektor.com

LEARN

Elektor International Media BV

This book is written for students, for practising engineers and for hobbyists who want to learn more about the building blocks of an IoT system and also learn how to setup an IoT system using these blocks. This book has been written with the assumption that the reader has taken a course on digital logic design and has been exposed to writing programs using at least one high-level programming language. Knowledge of the C programming language will be very useful. Also, familiarity with at least one member of the PIC series of microcontrollers (e.g. PIC16 or PIC18) will be an advantage. The knowledge of assembly language programming is not required because all the projects in the book are based on using the C language. If you are a total beginner in programming you can still access the book, but first you are advised to study introductory books on microcontrollers.

INTERNET OF THINGS

● DOGAN IBRAHIM

He is the author of over 60 technical books, published by international famous publishers, such as Wiley, Butterworth, and Newnes.

AN INTRODUCTION WITH PIC MICROCONTROLLERS

AN INTRODUCTION WITH PIC MICROCONTROLLERS

Prof Dr Dogan Ibrahim is a Fellow of the Institution of Electrical Engineers.

The Internet of Things (IoT) is a new concept in intelligent automation and intelligent monitoring using the Internet as the communications medium. The “Things” in IoT usually refer to devices that have unique identifiers and are connected to the Internet to exchange information with each other. Such devices usually have sensors and/or actuators that can be used to collect data about their environments and to monitor and control their environments. The collected data can be processed locally or it can be sent to centralized servers or to the cloud for remote storage and processing. For example, a small device at the size of a matchbox can be used to collect data about the temperature, relative humidity and the atmospheric pressure. This data can be sent and stored in the cloud. Anyone with a mobile device can then access and monitor this data at any time and from anywhere on Earth provided there is Internet connectivity. In addition, users can for example, adjust the central heating remotely using their mobile devices and accessing the cloud.

INTENET OF THINGS

AN INTRODUCTION WITH PIC MICROCONTROLLERS

Dogan Ibrahim LEARN

DESIGN

SHARE

SHARE

LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.