Preface
The whole world is moving from desktop/computers to smartphones/embedded systems. We are at the door to Internet of Things (IoT). The number of Internet connected users crossed 3.2 billion in 2015, which is almost half of the total population. The number of connected devices has exceeded the number of humans already. IoT is going to bring a combination of solutions, mixing physical world and digital world. We will interact with the digital world in newer ways than keyboard and mouse. Everyday objects will have logic in them to do their work in a better way. Multiple things will work collectively in order to achieve better results. IoT will bring lot many possibilities and opportunities along.
IoT implementation needs power-efficient embedded system solutions. There has been an exponential rise in the demand of the embedded systems in the last few years. Therefore, there is a demand for embedded programming as well. Embedded development boards such as BeagleBone play a key role here. These boards provide opportunities to new programmers to get their hands on ARM-based embedded systems. BeagleBone comes as an ultra-small, cost-effective solution with a powerful hardware that runs Debian Linux. This book tries to explore the hardware and software capabilities of BeagleBone to create real-life electronics and IoT applications quickly.
Being an open source hardware board, BeagleBone is the perfect choice to study embedded systems. Board design files and in-depth datasheets are open for being studied. It comes with an Ethernet port that allows deploying the IoT projects without making any addition to the board. It provides plenty of GPIO, ADC, UART, I2C, and SPI pins that make it the right choice for electronic projects. One can install Debian, Ubuntu, Android, and many other Liunx distributions. By default, it comes with a full-fledged Debian Linux OS running on it. This gives you all the benefits of Linux kernel such as multitasking, multiuser, and extensive device drivers support. It also allows you to do programming in many languages, including C, C++, JavaScript, Python, Ruby, Perl, and so on. This book uses BeagleBone as a tool to write useful applications on the embedded systems. Starting with the basics to set up BeagleBone and Cloud9 IDE, this book covers interfacing with various electronic components via simple programs. The electronics theory related to these components is explained in detail before using them in a program. Then, the book covers some real-life IoT applications.
This book is divided in two parts: the first part is covers programs in JavaScript and the second part of this book provides electronics projects and IoT applications in Python. Most of the physicial computing theories and concepts are covered in the JavaScript part. Programs are explained in the explanation section, immediately after it's source code. Troubleshooting steps are given wherever needed. Some programs have an execution section, which explains how a program works internally. Programming language conventions and error handling are loosely followed to make programs short and easy to understand. The language that is to be covered first is a tough choice. I choose JavaScript as it is energy-efficient, event-driven architecture. It is more suited as an IoT solution. Only sending the important information reduces the processing that is to be done on the Thing connected to the Internet. Javascript is a natural language of web. It comes preinstalled in the BeagleBone along with Node and Cloud9. You can start Javascript programming immediately after connecting BeagleBone. Let's start the journey of programming the BeagleBone.
What this book covers
Chapter 1, Cloud9 IDE, gives the essential information you need before you start programming on BeagleBone. It gives hardware and software-related information about the boards in the BeagleBone series. It provides you different ways to connect to BeagleBone and start Cloud9 IDE to program.
Chapter 2, Blinking Onboard LEDs, covers how to use Cloud9 IDE to write the first physical computing program—blinking onboard LED on the BeagleBone. It covers another program that creates a display pattern among the LEDs.
Chapter 3, Blinking External LEDs, explains the General Purpose Input/Output (GPIO) theory and how to attach the external LEDs to the GPIO pins. Then, it covers blinking LED and display pattern programs.
Chapter 4, Controlling LED Using a Push Button, teaches how to read from input components using polling. The interrupt method programs for each of these methods are covered.
Chapter 5, Reading from Analog Sensors, covers the theory about Analog I/O and how BeagleBone supports it. Then, it has programs to read from the TMP36 temperature sensor and light sensor.
Chapter 6, PWM – Writing Analog Information, explains how the Pulse Width Modulation (PWM) technique is used to write the analog information and how BeagleBone supports it. It has a program to fade-in LED and control the servo motor using PWM.
Chapter 7, Internet of Things with BeagleBone, explains how to implement IoT in JavaScript using BeagleBone. It covers important information about IoT. Then, there are three real-life examples. Two of them are programs to remotely control LED and servo motor. Another program is to shoot an e-mail alert when overtemperature is detected.
Chapter 8, Physical Computing in Python, explains rewriting all the programs that are covered from Chapter 3 to Chapter 6 in the Python language.
Chapter 9, UART, I2C, SPI Programming, covers popular buses in the embedded systems—UART, I2C, and SPI. All these protocols are explained here in detail. This chapter covers programs that communicate over each of these buses.
Chapter 10, Internet of Things Using Python, teaches IoT programs in Python. It has programs that were similar to the programs covered in Chapter 7. It has an additional program that uploads the temperature data over the Cloud website and we will receive the temperature graph over time for analysis.
Appendix A, GPIO Control in Bash, teaches how to set the direction and turn GPIO on/off directly by writing sysfs files.
Appendix B, BeagleBone Capes, provides information about BeagleBone add-on boards called capes.
Appendix C, Pinmux and the Device Tree, gives details about the new hardware description files that help the kernel to initialize BeagleBone. They are called device tree. It covers how to use the device tree files to select the role of the BeagleBone pin among other possibilities.
This book does not cover BeagleBone Programmable Realtime Units (PRUs) and building/customising installable image.
What you need for this book
The required hardware is as follows:
BeagleBone Black or BeagleBone Green or BeagleBone White
Micro SD card (4 GB+)
One breadboard
Ten Male-to-Male jumper wires (different colors)
Ten LEDs
Ten 470 Ohm resistor
Single push button
TMP36 sensor
LDR
Single 10k Ohm resistor
Micro servo motor
ADXL345 accelerometer module
Nokia 5110 LCD module
The optional hardware is as follows:
5V 2A power adapter with 5.5 mm DC jack for BeagleBone Black and BeagleBone White (https://www.sparkfun.com/products/12889)
5V 2A micro USB power adapter for BeagleBone Green (https://www.sparkfun.com/products/12890)
FTDI USB serial cable (https://www.adafruit.com/products/70)
Who this book is for
This book is for anyone who wants to learn programming on the embedded systems and understand key concepts such as GPIO, PWM, and bus. It is intended for a programming beginner who is willing to explore the embedded systems programming by doing electronics projects. This book will be helpful for a BeagleBone owner who wants to quickly implement small-scale home automation solutions. Some hands-on experience is expected on C or Python. Some familiarity with electronics is helpful. However, it is not essential.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "It can be turned on by doing shell access to BeagleBone and executing command sudo ifconfig usb0 up
."
A block of code is set as follows:
var b = require('bonescript'); var state = b.HIGH; b.pinMode("USR3", b.OUTPUT); b.digitalWrite("USR3", state); setInterval(blink,1000); function blink() { if(state == b.LOW) state = b.HIGH; else state = b.LOW; b.digitalWrite("USR3", state); }
Any command-line input or output is written as follows:
sudo dd if=<image_file_path> of=/dev/sdx bs=1M ; sync
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You can stop the loop by clicking on the red Stop button in Cloud9."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <feedback@packtpub.com>
, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Downloading the color images of this book
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/ProgrammingTheBeagleBone_Graphics.pdf.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at <questions@packtpub.com>
, and we will do our best to address the problem.