Introduction xix
Part I Raspberry Pi Basics Chapter 1
Chapter 1 Raspberry Pi Hardware 3
Introduction to the Platform 3
Who Should Use the RPi 5
When to Use the RPi 5
When to Not Use the RPi 6
RPi Documentation 7
The RPi Hardware 8
Raspberry Pi Versions 9
The Raspberry Pi Hardware 10
Raspberry Pi Accessories 12
Important Accessories 13
Recommended Accessories 14
Optional Accessories 16
HATs 19
How to Destroy Your RPi! 20
Summary 21
Support 21
Chapter 2 Raspberry Pi Software 23
Linux on the Raspberry Pi 24
Linux Distributions for the RPi 24
Create a Linux SD Card Image for the RPi 25
Connecting to a Network 26
Regular Ethernet 27
Ethernet Crossover Cable 29
Communicating with the RPi 31
Serial Connection with the USB-to-TTL 3.3V Cable 31
Connecting through Secure Shell (SSH) 33
Transferring Files Using PuTTY/psftp over SSH 35
Controlling the Raspberry Pi 36
Basic Linux Commands 36
Basic File Editing 41
What Time Is It? 43
Package Management 44
Configuring the Raspberry Pi 46
The Raspberry Pi Configuration Tool 46
Updating the RPi Software 48
Video Output 49
Interacting with the Onboard LEDs 50
Shutdown and Reboot 53
Summary 54
Chapter 3 Exploring Embedded Linux Systems 55
Introducing Embedded Linux 56
Advantages and Disadvantages of Embedded Linux 57
Is Linux Open Source and Free? 58
Booting the Raspberry Pi 58
Managing Linux Systems 69
The Super User 69
System Administration 70
Linux Commands 89
Linux Processes 96
Other Linux Topics 99
Using Git for Version Control 99
A Practice-Based Introduction 101
Git Branching 105
Common Git Commands 107
Using Desktop Virtualization 108
Code for This Book 110
Summary 111
Further Reading 111
Bibliography 111
Chapter 4 Interfacing Electronics 113
Analyzing Your Circuits 114
Digital Multimeter 114
Oscilloscopes 115
Basic Circuit Principles 117
Voltage, Current, Resistance, and Ohms Law 117
Voltage Division 119
Current Division 120
Implementing RPi Circuits on a Breadboard 121
Digital Multimeters (DMMs) and Breadboards 123
Example Circuit: Voltage Regulation 124
Discrete Components 126
Diodes 126
Light-Emitting Diodes (LEDs) 128
Smoothing and Decoupling Capacitors 130
Transistors 132
Optocouplers/Opto-isolators 138
Switches and Buttons 140
Logic Gates 143
Analog-to-Digital Conversion 150
Sampling Rate 151
Quantization 151
Operational Amplifiers 152
Concluding Advice 155
Summary 156
Further Reading 157
Chapter 5 Programming on the Raspberry Pi 159
Introduction 160
Performance of Languages on the RPi 160
Setting the RPi CPU Frequency 165
A First Circuit for Physical Computing 166
Scripting Languages 168
Scripting Language Options 168
Bash 169
Lua 171
Perl 173
Python 173
Dynamically Compiled Languages 176
JavaScript and Node.js on the RPi 176
Java on the RPi 178
C and C++ on the RPi 180
C and C++ Language Overview 182
LED control in c 194
The C of C++ 196
Overview of Object-Oriented Programming 199
Object-Oriented LED Control in C++ 203
Interfacing to the Linux OS 206
Glibc and Syscall 206
Improving the Performance of Python 208
Cython 208
Extending Python with C/C++ 211
Summary 215
Further Reading 216
Bibliography 216
Part II Interfacing, Controlling, and Communicating 217
Chapter 6 Interfacing to the Raspberry Pi Input/Outputs 219
Introduction 220
General-Purpose Input/Outputs 221
GPIO Digital Output 222
GPIO Digital Input 225
Internal Pull-Up and Pull-Down Resistors 226
Interfacing to Powered DC Circuits 227
C++ Control of GPIOs Using sysfs 229
More C++ Programming 237
An Enhanced GPIO Class 242
Memory-Based GPIO Control 245
GPIO Control Using devmem 2 246
GPIO Control Using C and /dev/mem 248
Changing the Internal Resistor Configuration 250
WiringPi 252
Installing wiringPi 252
The gpio Command 253
Programming with wiringPi 254
Toggling an LED Using wiringPi 255
Button PressLED Response 257
Communicating to One-Wire Sensors 260
PWM and General-Purpose Clocks 263
GPIOs and Permissions 270
Writing udev Rules 270
Permissions and wiringPi 272
Summary 273
Chapter 7 Cross-Compilation and the Eclipse IDE 275
Setting Up a Cross-Compilation Toolchain 276
The Linaro Toolchain for Raspbian 277
Debian Cross-Toolchains 279
Cross-Compilation Using Eclipse 282
Installing Eclipse on Desktop Linux 282
Configuring Eclipse for Cross-Compilation 283
Remote System Explorer 286
Integrating GitHub into Eclipse 289
Remote Debugging 289
Automatic Documentation (Doxygen) 294
Building Linux 297
Downloading the Kernel Source 298
Building the Linux Kernel 299
Deploying the Linux Kernel 303
Building a Linux Distribution (Advanced) 305
Summary 307
Further Reading 308
Chapter 8 Interfacing to the Raspberry Pi Buses 309
Introduction to Bus Communication 310
I2C 310
I2C Hardware 311
An I2C Test Circuit 315
Using Linux I2C-Tools 318
I2C communication in C 325
Wrapping I2C Devices with C++ Classes 328
Spi 330
SPI Hardware 330
SPI on the RPi 332
A First SPI Application (74HC595) 334
Bidirectional SPI Communication in C/C++ 339
Multiple SPI Slave Devices on the RPi 346
UART 347
The RPi UART 348
UART Examples in c 352
UART Applications - GPS 357
Logic-Level Translation 359
Summary 361
Further Reading 361
Chapter 9 Enhancing the Input/Output Interfaces on the RPi 363
Introduction 364
Analog-to-Digital Conversion 364
SPI Analog-to-Digital Converters (ADCs) 365
ADC Application: An Analog Light Meter 368
Testing the SPI ADC Performance 370
The C Library for BCM2835 (Advanced) 373
Digital-to-Analog Conversion 376
An I2C Digital-to-Analog Converter 376
An SPI Digital-to-Analog Converter 379
Adding PWM Outputs to the RPi 381
Extending the RPi GPIOs 387
The MCP23017 and the I 2 C Bus 389
The MCP23S17 and the SPI Bus 393
A C++ Class for the MCP23x17 Devices 394
Adding UARTs to the RPi 397
Summary 403
Chapter 10 Interacting with the Physical Environment 405
Interfacing to Actuators 406
DC Motors 407
Stepper Motors 414
Relays 418
Interfacing to Analog Sensors 420
Linear Analog Sensors 422
Nonlinear Analog Sensors 423
Analog Sensor Signal Conditioning 428
Interfacing to an Analog Accelerometer 431
Interfacing to Local Displays 433
MAX7219 Display Modules 433
Character LCD Modules 436
OLED Dot-Matrix Display 440
Building C/C++ Libraries 444
Makefiles 444
CMake 446
Summary 452
Chapter 11 Real-Time Interfacing Using the Arduino 453
The Arduino 454
An Arduino Serial Slave 457
A UART Echo Test Example 457
UART Command Control of an Arduino 461
An Arduino I2C Slave 464
An I2C Test Circuit 464
I2C Register Echo Example 465
I2C Temperature Sensor Example 467
I2C Temperature Sensor with a Warning LED 469
Arduino Slave Communication Using C/C++ 471
An I2C Ultrasonic Sensor Application 473
An Arduino SPI Slave 476
Programming the Arduino from the RPi Command Line 478
Summary 480
Part III Advanced Interfacing and Interaction 481
Chapter 12 The Internet of Things 483
The Internet of Things (IoT) 484
The RPi as an IoT Sensor 485
The RPi as a Sensor Web Server 487
Nginx 488
GNU Cgicc Applications (Advanced) 494
A C/C++ Web Client 498
Network Communications Primer 499
A C/C++ Web Client 500
Secure Communication Using OpenSSL 502
The RPi as a Thing 503
ThingSpeak 504
The Linux Cron Scheduler 506
Sending E-mail from the RPi 510
If This Then That (IFTTT) 512
Large-Scale IoT Frameworks 513
MQ Telemetry Transport (MQTT) 514
IBM Bluemix Internet of Things 515
An IBM IoT MQTT Node.js Publish Example 518
An IBM IoT MQTT C++ Publish Example 520
Visualize Data Using IBM Quickstart 521
The C++ Client/Server 523
IoT Device Management 526
Remote Monitoring of the RPi 527
RPi Watchdog Timers 528
Static IP Addresses 529
Power over Ethernet (PoE) 530
Summary 533
Chapter 13 Wireless Communication and Control 535
Introduction to Wireless Communications 536
Bluetooth Communications 537
Installing a Bluetooth Adapter 537
Android App Development with Bluetooth 543
Wi-Fi Communications 544
Installing a Wi-Fi Adapter 544
The NodeMCU Wi-Fi Slave Processor 547
ZigBee Communications 559
Introduction to XBee Devices 559
XBee Configuration 561
An XBee AT Mode Example 563
An XBee API Mode Example 568
Near Field Communication 572
Summary 575
Chapter 14 Raspberry Pi with a Rich User Interface 577
Rich UI RPi Architectures 578
The RPi as a General-Purpose Computer 579
RPi with an LCD Touchscreen 582
Virtual Network Computing (VNC) 583
Fat-Client Applications 585
Rich UI Application Development 586
Introduction to GTK+ on the RPi 586
Introduction to Qt on the RPi 590
Qt Primer 592
Qt Concepts 592
Qt Development Tools 596
A First Qt Creator Example 597
A Qt Weather GUI Application 598
Remote UI Application Development 602
Fat-Client Qt GUI Application 603
Multithreaded Server Applications 606
The Multithreaded Weather Server 609
Summary 612
Further Reading 613
Chapter 15 Images, Video, and Audio 615
Capturing Images and Video 616
The RPi Camera 616
USB Webcams 619
Video4Linux2 (V4L2) 621
Streaming Video 627
Image Processing and Computer Vision 628
Image Processing with OpenCV 628
Computer Vision with OpenCV 631
Boost 633
Raspberry Pi Audio 634
Core Audio Software Tools 635
Audio Devices for the RPi 635
Text-to-Speech 643
Summary 644
Further Reading 645
Chapter 16 Kernel Programming 647
Introduction 648
Why Write Kernel Modules? 648
Loadable Kernel Module (LKM) Basics 649
A First LKM Example 650
The LKM Makefile 652
Building the LKM on a Linux Desktop Machine 653
Building the LKM on the RPi 654
Testing the First LKM Example 657
An Embedded LKM Example 659
Interrupt Service Routines (ISRs) 661
Performance 665
Enhanced Button GPIO Driver LKM 665
The kobject Interface 666
Enhanced LED GPIO Driver LKM 673
Kernel Threads 674
Conclusions 675
Summary 676
Index 677