Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
$9.99 | ALL EBOOKS & VIDEOS
Over 7,000 tech titles at $9.99 each with AI-powered learning assistants on new releases
BeagleBone By Example
BeagleBone By Example

BeagleBone By Example: Learn how to build physical computing systems using BeagleBone Black and Python

$35.99 $9.99
Book Aug 2016 262 pages 1st Edition
eBook
$35.99 $9.99
Print
$43.99
Subscription
$15.99 Monthly
eBook
$35.99 $9.99
Print
$43.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Aug 31, 2016
Length 262 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785285059
Category :
Table of content icon View table of contents Preview book icon Preview Book

BeagleBone By Example

Chapter 1. Getting Started with BeagleBone

If you are reading this book right now, it means that you have taken your first step to get started with your BeagleBone board to build real-time physical computing systems using your BeagleBone board and Python programming language. This chapter will teach you how to set up your BeagleBone board for the first time and write your first few Python codes on it.

By end of this book, you would have learned the basics of interfacing electronics to BeagleBone boards and coding it using Python which will allow you to build almost anything from a home automation system to a robot through examples given in this book.

Firstly, in this chapter, you will learn how to set up your BeagleBone board for the first time with a new operating system, followed by usage of some basic Linux Shell commands that will help you out while we work on the Shell Terminal to write and execute python codes and do much more like installing different libraries and software on your BeagleBone board. Once you get familiar with usage of the Linux terminal, you will write your first code on python that will run on your BeagleBone board. Once you are comfortable with that, we will modify the code to make it do something more in the next chapters. Most of the time, we will be using the freely available open-source codes and libraries available on the Internet to write programs on top of it and using it to make the program work for our requirement instead of entirely writing a code from scratch to build our embedded systems using BeagleBone board. The contents of this chapter are divided into the following sections:

  • Prerequisites

  • About the single board computer - BeagleBone board

  • Know your BeagleBone board

  • Setting up your BeagleBone board

  • Working on Linux Shell

  • Coding on Python in BeagleBone board

Prerequisites


This topic will cover what parts you need to get started with BeagleBone Black. You can buy them online or pick them up from any electronics store that is available in your locality.

The following is the list of materials needed to get started:

  • 1x BeagleBone Black

  • 1x miniUSB type B to type A cable

  • 1x microSD Card (4 GB or More)

  • 1x microSD Card Reader

  • 1x 5V DC, 2A Power Supply

  • 1x Ethernet Cable

Note

There are different variants of BeagleBone boards like BeagleBone, BeagleBone Black, BeagleBone Green and some more old variants. This book will mostly have the BeagleBone Black shown in the pictures. Note that BeagleBone Black can replace any of the other BeagleBone boards such as the BeagleBone or BeagleBone Green for most of the projects. These boards have their own extra features so to say. For example, the BeagleBone Black has more RAM, it has almost double the size of RAM available in BeagleBone and an in-built eMMC to store operating system instead of booting it up only through operating system installed on microSD card in BeagleBone White. Keeping in mind that this book should be able to guide people with most of the BeagleBone board variants, the tutorials in this book will be based on operating system booted from microSD card inserted on the BeagleBone board. We will discuss about this in detail in the Setting up your BeagleBone board and installing operating system's topics of this chapter.

BeagleBone Black – a single board computer


This topic will give you brief information about single board computers to make you understand what they are and where BeagleBone boards fit inside this category.

Have you ever wondered how your smartphones, smart TVs, and set-top boxes work?

All these devices run custom firmware developed for specific applications based on the different Linux and Unix kernels. When you hear the word Linux and if you are familiar with Linux, you will get in your mind that it's nothing but an operating system, just like Windows or Mac OS X that runs on desktops and server computers. But in the recent years the Linux kernel is being used in most of the embedded systems including consumer electronics such as your smartphones, smart TVs, set-top boxes, and much more. Most people know Android and iOS as an operating system on their smart phones. But only a few know that, both these operating systems are based on Linux and Unix kernels.

Did you ever question how they would develop such devices? There should be a development board right? What are they?

This is where Linux Development boards like our BeagleBone boards are used.

By adding peripherals such as touch screens, GSM modules, microphones, and speakers to these single board computers and writing the software that is the operating system with graphical user interface to make them interact with the physical world, we have so many smart devices now that people use every day.

Nowadays you have proximity sensors, accelerometers, gyroscopes, cameras, IR blasters, and much more on your smartphones. These sensors and transmitters are connected to the CPU on your phone through the Input Output ports on the CPU, and there is a small piece of software that is running to communicate with these electronics when the whole operating system is running in the smartphone to get the readings from these sensors in real-time. Like the autorotation of screen on the latest smartphones. There is a small piece of software that is reading the data from accelerometer and gyroscope sensors on the phone and based on the orientation of the phone it turns the graphical display.

So, all these Linux development boards are tools and base boards using which you can build awesome real world smart devices or we can call them physical computing systems as they interact with the physical world and respond with an output.

Getting to know your board – BeagleBone Black


BeagleBone Black can be described as low cost single board computer that can be plugged into a computer monitor or TV via a HDMI cable for output and uses standard keyboard and mouse for input. It's capable of doing everything you'd expect a desktop computer to do, like playing videos, word processing, browsing the Internet, and so on. You can even setup a web server on your BeagleBone Black just like you would do if you want to set up a webserver on a Linux or Windows PC.

But, differing from your desktop computer, the BeagleBone boards has the ability to interact with the physical world using the GPIO pins available on the board, and has been used in various physical computing applications. Starting from Internet of Things, Home Automation projects, to Robotics, or tweeting shark intrusion systems. The BeagleBone boards are being used by hardware makers around the world to build awesome physical computing systems which are turning into commercial products also in the market. OpenROV, an underwater robot being one good example of what someone can build using a BeagleBone Black that can turn into a successful commercial product.

Hardware specification of BeagleBone Black


A picture is worth a thousand words. The following picture describes about the hardware specifications of the BeagleBone Black. But you will get some more details about every part of the board as you read the content in the following picture.

If you are familiar with the basic setup of a computer. You will know that it has a CPU with RAM and Hard Disk. To the CPU you can connect your Keyboard, Mouse, and Monitor which are powered up using a power system.

The same setup is here in BeagleBone Black also. There is a 1GHz Processor with 512MB of DDR3 RAM and 4GB on board eMMC storage, which replaces the Hard Disk to store the operating system. Just in case you want more storage to boot up using a different operating system, you can use an external microSD card that can have the operating system that you can insert into the microSD card slot for extra storage.

As in every computer, the board consists of a power button to turn on and turn off the board and a reset button to reset the board. In addition, there is a boot button which is used to boot the board when the operating system is loaded on the microSD card instead of the eMMC. We will be learning about usage of this button in detail in the installing operating systems topic of this chapter.

There is a type A USB Host port to which you can connect peripherals such as USB Keyboard, USB Mouse, USB Camera, and much more, provided that the Linux drivers are available for the peripherals you connect to the BeagleBone Black.

Tip

It is to be noted that the BeagleBone Black has only one USB Host Port, so you need to get an USB Hub to get multiple USB ports for connecting more number of USB devices at a time. I would recommend using a wireless Keyboard and Mouse to eliminate an extra USB Hub when you connect your BeagleBone Black to monitor using the HDMI port available.

The microHDMI port available on the BeagleBone Black gives the board the ability to give output to HDMI monitors and HDMI TVs just like any computer will give.

You can power up the BeagleBone Black using the DC Barrel jack available on the left hand side corner of the board using a 5V DC, 2A adapter. There is an option to power the board using USB, although it is not recommended due to the current limit on USB ports. We will see about this in detail in the upcoming chapters when we connect USB Wi-Fi dongle and USB camera to the BeagleBone Black.

There are 4 LEDs on board to indicate the status of the board and help us for identifications to boot up the BeagleBone Black from microSD card. The LEDs are linked with the GPIO pins on the BeagleBone Black which can be used whenever needed.

You can connect the BeagleBone Black to the LAN or Internet using the Ethernet port available on the board using an Ethernet cable. You can even use a USB Wi-Fi module to give Internet access to your BeagleBone Black. In Chapter 5, Connecting Physical Computing Systems to the Internet, you will learn how to do this.

The expansion headers which are in general called the General Purpose Input Output (GPIO) pins include 65 digital pins. These pins can be used as digital input or output pins to which you can connect switches, LEDs and many more digital input output components, 7 analog inputs to which you can connect analog sensors like a potentiometer or an analog temperature sensor, 4 Serial Ports to which you can connect Serial Bluetooth or Xbee Modules for wireless communication or anything else, 2 SPI and 2 I2C Ports to connect different modules such as sensors or any other modules using SPI or I2C communication. It also has 8 PWM output pins that can be used for applications like fading and LED or in robotic applications for varying the speed of a motor which we will be discussing later in the upcoming chapters.

We also have the serial debugging port to view the low-level firmware pre-boot and post-shutdown/reboot messages via a serial monitor using an external serial to USB converter while the system is loading up and running. After booting up the operating system, this also acts as a fully interactive Linux console.

Setting up your BeagleBone board


Your first step to get started with BeagleBone boards with your hands on will be to set it up and test it as suggested by the BeagleBone Community with the Debian distribution of Linux running on BeagleBone Black that comes preloaded on the eMMC on board. This section will walk you through that process followed by installing different operating system on your BeagleBone board and log in into it. And then get into start working with files and executing Linux Shell commands via SSH.

  1. Connect your BeagleBone Black using the USB cable to your Laptop or PC.

    This is the simplest method to get your BeagleBone Black up and running. Once you connect your BeagleBone Black, it will start to boot using the operating system on the eMMC storage. To log in into the operating system and start working on it, the BeagleBone Black has to connect to a network and the drivers that are provided by the BeagleBoard manufacturers allow us to create a local network between your BeagleBone Black and your computer when you connect it via the USB cable. For this, you need to download and install the device drivers provided by BeagleBone board makers on your PC as explained in step 2.

  2. Download and install device drivers.

    Once the installation is complete, click on Finish. It is shown in the following screenshot:

    Once the installation is done, restart your PC. Make sure that the Wi-Fi on your laptop is off and also there is no Ethernet connected to your Laptop. Because now the BeagleBone Black device drivers will try to create a LAN connection between you laptop and BeagleBone Black so that you can access the webserver running by default on the BeagleBone Black to test it's all good, up, and running. Once you reboot your PC, get to step 3.

  3. Connect to the Web Server Running on BeagleBone Black.

    Open your favorite web browser and enter the IP address 192.168.7.2 on the URL bar, which is the default static IP assigned to BeagleBone Black.

    This should open up the webpage as shown in the following screenshot:

    If you get a green check mark with the message your board is connected. You can make sure that you got the previous steps correct and you have successfully connected to your board.

    Tip

    If you don't get this message, try removing the USB cable connected to the BeagleBone Black, reconnect it and check again. If you still don't get it. Then check whether you did the first two steps correctly.

  4. Play with on board LEDs via the web server.

    If you Scroll down on the web page to which we got connected, you will find the section as shown in the following screenshot:

This is a sample setup made by BeagleBone makers as the first time interaction interface to make you understand what is possible using BeagleBone Black. In this section of the webpage, you can run a small script. When you click on Run, the On board status LEDs that are flashing depending on the status of the operating system will stop its function and start working based on the script that you see on the page. The code is running based on a JavaScript library built by BeagleBone makers called the BoneScript. We will not look into this in detail as we will be concentrating more on writing our own programs using python to work with GPIOs on the board. But to make you understand, here is a simple explanation on what is there on the script and what happens when you click on the run button on the webpage.

The pinMode function defines the on board LED pins as outputs and the digitalWrite function sets the state of the output either as HIGH or LOW. And the setTimeout function will restore the LEDs back to its normal function after the set timeout, that is, the program will stop running after the time that was set in the setTimeout function.

Say I modify the code to what is shown in the following screenshot:

Note

You can notice that, I have changed the states of two LEDs to LOW and other two are HIGH and the timeout is set to 10,000 milliseconds.

So when you click on the Run button. The LEDs will switch to these states and stay like that for 10 seconds and then restore back to its normal status indication routine, that is, blinking.

You can play around with different combinations of HIGH and LOW states and setTimeout values so that you can see and understand what is happening.

You can see the LED output state of BeagleBone Black in the following screenshot for the program we executed earlier:

You can see that the two LEDs in the middle are in LOW state. It stays like this for 10 seconds when you run the script and then it will restore back to its usual routine. You can try with different timeout values and states of LEDs on the script given in the webpage and try clicking on the Run button to see how it works.

Like this we will be writing our own python programs and setting up servers to use the GPIOs available on the BeagleBone Black to make them work the way we desire to build different applications in each project that is available in this book.

Installing operating systems


We can make the BeagleBone Black boot up and run using different operating systems just like any computer can do. Mostly Linux is used on these boards which is free and open source, but it is to be noted that specific distributions of Linux, Android, and Windows CE have been made available for these boards as well which you can try out.

The stable versions of these operating systems are made available at http://beagleboard.org/latest-images.

By default, the BeagleBone Black comes preloaded with a Debian distribution of Linux on the eMMC of the board. However, if you want, you can flash this eMMC just like you do to your Hard Drive on your computer and install different operating systems on it.

Note

As mentioned in the note at the beginning of this chapter, considering all the tutorials in this book should be useful to people who own BeagleBone as well as the BeagleBone Black. We will choose the recommended Debian package by www.BeagleBoard.org Foundation and we will boot the board every time using the operating system on microSD card.

Perform the following steps to prepare the microSD card and boot BeagleBone using that:

  1. Goto: http://beagleboard.org/latest-images.

  2. Download the latest Debian Image.

    The following screenshot highlights the latest Debian Image available for flashing on microSD card:

  3. Extract the image file inside the RAR file that was downloaded:

    Tip

    You might have to install WinRAR or any .rar file extracting software if it is not available in your computer already.

  4. Install Win32 Disk Imager software.

    To write the image file to a microSD card, we need this software. You can go to Google or any other search engine and type win32 disk imager as keyword and search to get the web link to download this software as shown in the following screenshot:

    The web link, where you can find this software is http://sourceforge.net/projects/win32diskimager/. But this keeps changing often that's why I suggest you can search it via any search engine with the keyword.

  5. Once you download the software and install it. You should be able to see the window as shown in the following screenshot when you open the Win32 Disk Imager:

    Now that you are all set with the software, using which you can flash the operating system image that we downloaded. Let's move to the next step where you can use Win32 Disk Imager software to flash the microSD card.

  6. Flashing the microSD card.

    Insert the microSD into a microSD card reader and plug it onto your computer. It might take some time for the card reader to show up your microSD card. Once it shows up, you should be able to select the USB drive as shown in the following screenshot on the Win32 Disk Imager software.

  7. Now, click on the icon highlighted in the following screenshot to open the file explorer and select the image file that we extracted in step 3:

  8. Go to the folder where you extracted the latest Debian image file and select it.

  9. Now you can write the image file to microSD card by clicking on the Write button on the Win32 Disk Imager. If you get a prompt as shown in the following screenshot, click on Yes and continue:

  10. Once you click on Yes, the flashing process will start and the image file will be written on to the microSD card. The following screenshot shows the flashing process progressing:

    Once the flashing is completed, you will get a message as shown in the following screenshot:

  11. Now you can click on OK, exit the Win32 Disk Imager software and safely remove the microSD card from your computer.

Now you have successfully prepared your microSD card with the latest Debian operating system available for BeagleBone Black. This process is same for all other operating systems that are available for BeagleBone boards. You can try out different operating systems such as the Angstrom Linux, Android, or Windows CE others, once you get familiar with your BeagleBone board by end of this book.

For Mac users, you can refer to either https://learn.adafruit.com/ssh-to-beaglebone-black-over-usb/installing-drivers-mac or https://learn.adafruit.com/beaglebone-black-installing-operating-systems/mac-os-x.

Booting your BeagleBone board from a SD card


Since you have the operating system on your microSD card now, let us go ahead and boot your BeagleBone board from that microSD card and see how to login and access the filesystem via Linux Shell.

You will need your computer connected to your Router either via Ethernet or Wi-Fi and an Ethernet cable which you should connect between your Router and the BeagleBone board. The last but most important thing is an External Power Supply using which you will power up your BeagleBone board because power supply via a USB will be not be enough to run the BeagleBone board when it is booted from a microSD card.

  1. Insert the microSD card into BeagleBone board.

    Now you should insert the microSD card that you have prepared into the microSD card slot available on your BeagleBone board.

  2. Connect your BeagleBone to your LAN.

    Now connect your BeagleBone board to your Internet router using an Ethernet cable.

    You need to make sure that your BeagleBone board and your computer are connected to the same router to follow the next steps.

  3. Connect external power supply to your BeagleBone board.

  4. Boot your BeagleBone board from microSD card.

    On BeagleBone Black and BeagleBone Green, you have a Boot Button which you need to hold on while turning on your BeagleBone board so that it starts booting from the microSD card instead of the default mode where it starts to boot from the onboard eMMC storage which holds the operating system. In case of BeagleBone White, you don't have this button, it starts to boot from the microSD card itself as it doesn't have onboard eMMC storage.

Depending on the board that you have, you can decide whether to boot the board from microSD card or eMMC. Consider you have a BeagleBone Black just like the one I have shown in the preceding picture. You hold down the User Boot button that is highlighted on the image and turn on the power supply. Once you turn on the board while holding the button down, the four on-board LEDs will light up and stay HIGH as shown in the following picture for 1 or 2 seconds, then they will start to blink randomly.

Once they start blinking, you can leave the button.

Now your BeagleBone board must have started Booting from the microSD card, so our next step will be to log in to the system and start working on it. The next topic will walk you through the steps on how to do this.

Logging into the board via SSH over Ethernet


If you are familiar with Linux operations, then you might have guessed what this section is about. But for those people who are not daily Linux users or have never heard the term SSH, Secure Shell (SSH) is a network protocol that allows network services and remote login to be able to operate over an unsecured network in a secure manner. In basic terms, it's a protocol through which you can log in to a computer and assess its filesystem and also work on that system using specific commands to create and work with files on the system.

In the steps ahead, you will work with some Linux commands that will make you understand this method of logging into a system and working on it.

  1. Setup SSH Software.

    To get started, log in to your BeagleBone board now, from a Windows PC, you need to install any SSH terminal software for Windows.

    My favorite is PuTTY, so I will be using that in the steps ahead. If you are new to using SSH, I would suggest you also get PuTTY.

    The software interface of PuTTY will be as shown in the following screenshot:

    You need to know the IP address or the Host Name of your BeagleBone Black to log in to it via SSH. The default Host Name is beaglebone but in some routers, depending on their security settings, this method of login doesn't work with Host Name. So, I would suggest you try to login entering the hostname first. If you are not able to login, follow step 2. If you successfully connect and get the login prompt with Host Name, you can skip step 2 and go to Step 3.

    But if you get an error as shown in the following screenshot, perform Step 2.

  2. Find an IP address assigned to BeagleBone board.

    Whenever you connect a device to your Router, say your computer, printer, or mobile phone. The router assigns a unique IP to these devices. The same way, the router must have assigned an IP to your BeagleBone board also. We can get this detail on the router's configuration page of your router from any browser of a computer that is connected to that router.

    In most cases, the router can be assessed by entering the IP 192.168.1.1 but some router manufacturers have a different IP in very rare cases. If you are not able to assess your router using this IP 192.168.1.1, refer your router manual for getting access to this page.

    Note

    The images that are shown in this section are to give you an idea about how to log in to your router and get the IP address details assigned to your BeagleBone board from your router. The configuration pages and how the devices are shown on the router will look different depending on the router that you own.

    Enter the 192.168.1.1 address in you browser.

    When it asks for User Name and Password, enter admin as User Name and password as Password

    These are the mostly used credentials by default in most of the routers. Just in case you fail in this step, check your router's user manual.

    Considering you logged into your router configuration page successfully, you will see the screen with details as shown in the following screenshot:

    If you click on the highlighted part, Attached Devices, you will be able to see the list of devices with their IP as shown in the following screenshot, where you can find the details of the IP address that is assigned to your BeagleBone board.

    So now you can note down the IP that has been assigned to your BeagleBone board. It can be seen that it's 192.168.1.14 in the preceding screenshot for my beaglebone board. We will be using this IP address to connect to the BeagleBone board via SSH in the next step.

  3. Connect via SSH using IP Address.

    Once you click on Open you might get a security prompt as shown in the following screenshot. Click on Yes and continue.

    Now you will get the login prompt on the terminal screen as shown in the following screenshot:

    If you got this far successfully, then it is time to log in to your BeagleBone board and start working on it via Linux Shell.

  4. Log in to BeagleBone board.

    When you get the login prompt as shown in the preceding screenshot, you need to enter the default username which is debian and default password which is temppwd. Now you should have logged into Linux Shell of your BeagleBone board as user with username debian.

Now that you have successfully logged into your BeagleBone board's Linux Shell, you can start working on it using Linux Shell commands like anyone does on any computer that is running Linux.

The next section will walk you through some basic Linux Shell commands that will come in handy for you to work with any Linux system.

Working on Linux Shell


Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. Since we will be working on BeagleBone board as a development board to build electronics projects, plugging it to a Monitor, Keyboard, and Mouse every time to work on it like a computer might be unwanted most of the times and you might need more resources also which is unnecessary all the time. So we will be using the shell command-line interface to work on the BeagleBone boards. If you want to learn more about the Linux command-line interfaces, I would suggest you visit to http://linuxcommand.org/.

Now let's go ahead and try some basic shell commands and do something on your BeagleBone board.

You can see the kernel version using the command uname -r. Just type the command and hit enter on your keyboard, the command will get executed and you will see the output as shown here:

Next, let us check the date on your BeagleBone board:

Like this shell will execute your commands and you can work on your BeagleBone boards via the shell.

Getting kernel version and date was just for a sample test. Now let's move ahead and start working with the filesystem.

  • ls: This stands for list command. This command will list out and display the names of folders and files available in the current working directory on which you are working.

  • pwd: This stands for print working directory command. This command prints the current working directory in which you are present.

  • mkdir: This stands for make directory command. This command will create a directory in other words a folder, but you need to mention the name of the directory you want to create in the command followed by it.

Say I want to create a folder with the name WorkSpace, I should enter the command as follows:

When you execute this command, it will create a folder named WorkSpace inside the current working directory you are in, to check whether the directory was created. You can try the ls command again and see that the directory named WorkSpace has been created.

To change the working directory and go inside the WorkSpace directory, you can use the next command that we will be seeing.

  • cd: This stands for change directory command. This command will help you switch between directories depending on the path you provide along with this command.

Now to switch and get inside the WorkSpace directory that you created, you can type the command as follows:

cd WorkSpace

You can note that whenever you type a command, it executes in the current working that you are in. So the execution of cd WorkSpace now will be equivalent to cd /home/debian/WorkSpace as your current working directory is /home/debian.

Now you can see that you have got inside the WorkSpace folder, which is empty right now, so if you type the ls command now, it will just go to the next line on the shell terminal, it will not output anything as the folder is empty.

Now if you execute the pwd command, you will see that your current working directory has changed.

  • cat: This stands for the cat command. This is one of the most basic commands that is used to read, write, and append data to files in shell.

To create a text file and add some content to it, you just need to type the cat command cat > filename.txt

Say I want to create a sample.txt file, I would type the command as shown next:

Once you type, the cursor will be waiting for the text you want to type inside the text file you created. Now you can type whatever text you want to type and when you are done press Ctrl + D. It will save the file and get back to the command-line interface.

Say I typed This is a test and then pressed Ctrl + D. The shell will look as shown next.

Now if you type ls command, you can see the text file inside the WorkSpace directory.

If you want to read the contents of the sample.txt file, again you can use the cat command as follows:

Alternatively, you can even use the more command which we will be using mostly:

Now that we saw how we can create a file, let's see how to delete what we created.

  • rm: This stands for remove command. This will let you delete any file by typing the filename or filename along with path followed by the command.

Say now we want to delete the sample.txt file we created, the command can be either rm sample.txt which will be equivalent to rm /home/debian/WorkSpace/sample.txt as your current working directory is /home/debian/Workspace.

After you execute this command, if you try to list the contents of the directory, you will notice that the file has been deleted and now the directory is empty.

Like this, you can make use of the shell commands work on your BeagleBone board via SSH over Ethernet or Wi-Fi. We will be seeing how to connect your BeagleBone board to Wi-Fi in Chapter 5, Connecting Physical Computing Systems to the Internet where you can setup your BeagleBone board to connect to your router via Wi-Fi instead of Ethernet to give wireless access to your BeagleBone board.

Now that you have got a clear idea and hands-on experience on using the Linux Shell, let's go ahead and start working with python and write a sample program on a text editor on Linux and test it in the next and last topic of this chapter.

Writing your own Python program on BeagleBone board


In this section, we will write our first few Python codes on your BeagleBone board. That will take an input and make a decision based on the input and print out an output depending on the code that we write. There are three sample codes in this topic as examples, which will help you cover some fundamentals of any programming language, including defining variables, using arithmetic operators, taking input and printing output, loops and decision making algorithm.

Before we write and execute a python program, let us get into python's interactive shell interface via the Linux shell and try some basic things like creating a variable and performing math operations on those variables.

To open the python shell interface, you just have the type python on the Linux shell like you did for any Linux shell command in the previous section of this chapter.

Once you type python and hit Enter, you should be able to see the terminal as shown in the following screenshot:

Now you are into python's interactive shell interface where every line that you type is the code that you are writing and executing simultaneously in every step. To learn more about this, visit https://www.python.org/shell/ or to get started and learn python programming language you can get our Python By Example book in our publication.

Let's execute a series of syntax in python's interactive shell interface to see whether it's working.

Let's create a variable A and assign value 20 to it:

Now let's print A to check what value it is assigned:

You can see that it prints out the value that we stored on it.

Now let's create another variable named B and store value 30 to it:

Let's try adding these two variables and store the result in another variable named C. Then print C where you can see the result of A+B, that is, 50.

That is the very basic calculation we performed on a programming interface. We created two variables with different values and then performed an arithmetic operation of adding two values on those variables and printed out the result.

Now, let's get a little more ahead and store string of characters in a variable and print them.

Wasn't that simple. Like this you can play around with python's interactive shell interface to learn coding. But any programmer would like to write a code and execute the program to get the output on a single command right.

Let's see how that can be done now.

To get out of the Python's Interactive Shell and get back to the current working directory on Linux Shell, just hold the Ctrl button and press D, that is, Ctrl + D on the keyboard. You will be back on the Linux Shell interface as shown next:

Now let's go ahead and write the program to perform the same action that we tried executing on python's interactive shell. That is to store two values on different variables and print out the result when both of them are added. Let's add some spice to it by doing multiple arithmetic operations on the variables that we create and print out the values of addition and subtraction.

You will need a text editor to write programs and save them. You can do it using the cat command also. But in future when you use indentation and more editing on the programs, the basic cat command usage will be difficult. So, let's start using the available text editor on Linux named nano, which is one of my favorite text editors in Linux. If you have a different choice and you are familiar with other text editors on Linux, such as vi or anything else, you can go ahead and continue the next steps using them to write programs.

To create a python file and start writing the code on it using nano, you need to use the nano command followed by the filename with extension .py.

Let's create a file named ArithmeticOperations.py.

Once you type this command, the text editor will open up.

Here you can type your code and save it using the keyboard command Ctrl + X.

Let's go ahead and write the code which is shown in the following screenshot and let's save the file using Ctrl + X:

Then type Y when it prompts to save the modified file.

Then if you want to change the file with a different name, you can change it in the next step before you save it. Since we created the file now only, we don't need to do it. In case if you want to save the modified copy of the file in future with a different name, you can change the filename in this step:

For now we will just hit Enter that will take us back to the Linux Shell and the file AritmeticOperations.py will be created inside the current working directory, which you can see by typing the ls command. You can also see the contents of the file by typing the more command that we learned in the previous section of this chapter.

Now let's execute the python script and see the output. To do this, you just have to type the command python followed by the python program file that we created, that is, the ArithmeticOperations.py.

Once you run the python code that you wrote, you will see the output as shown earlier with the results as output.

Now that you have written and executed your first code on python and tested it working on your BeagleBone board, let's write another python code, which is shown in the following screenshot where the code will ask you to enter an input and whatever text you type as input will be printed on the next line and the program will run continuously.

Let's save this python code as InputPrinter.py:

In this code, we will use a while loop so that the program runs continuously until you break it using the Ctrl + D command where it will break the program with an error and get back to Linux Shell.

Now let's try out our third and last program of this section and chapter, where when we run the code, the program asks the user to type the user's name as input and if they type a specific name that we compare, it prints and says Hello message or if a different name was given as input, it prints Go Away; let's call this code Say_Hello_To_Boss.py.

Instead of my name Jayakarthigeyan, you can replace it with your name or any string of characters on comparing which, the output decision varies.

When you execute the code, the output will look as shown in the following screenshot:

Like we did in the previous program, you can hit Ctrl + D to stop the program and get back to Linux Shell.

In this way, you can work with python programming language to create codes that can run on the BeagleBone boards in the way you desire.

Since we have come to the end of this chapter, let's give a break to our BeagleBone board.

Let's power off our BeagleBone board using the command sudo poweroff, which will shut down the operating system.

After you execute this command, if you get the error message shown in the following screenshot, it means the BeagleBoard has powered off.

You can turn off the power supply that was connected to your BeagleBone board now.

Summary


So here we are at the end of this chapter. In this chapter, you have learned how to boot your BeagleBone board from a different operating system on microSD card and then log in to it and start coding in Python to run a routine and make decisions. Using this knowledge, we will see how to make an LED blink which is connected to any GPIO pin on the GPIO headers of the BeagleBone board and how to read the switch button status using python code in the next few chapters. But before that, we will look at fundamentals of Basic Electronics in the next chapter.

On an extra note, you can take this chapter to another level by trying out a little more by connecting your BeagleBone board to an HDMI monitor using a microHDMI cable and connecting a USB Keyboard and Mouse to the USB Host of the BeagleBone board and power the monitor and BeagleBone board using external power supply and boot it from microSD Card and you should be able to see some GUI and you will be able to use the BeagleBone board like a normal Linux computer. You will be able access the files, manage them, and use Shell Terminal on the GUI also. If you own BeagleBone Black or BeagleBone Green, you can try out to flash the onboard eMMC using the latest Debian operating system and try out the same thing that we did using the operating system booted from microSD card.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Get to grips with the fundamentals of BeagleBone
  • Leverage Python scripts to program BeagleBone for your requirements
  • Build four exciting projects, from home automation to a tele-controlled robot

Description

BeagleBone is a low cost, community-supported development platform to develop a variety of electronic projects. This book will introduce you to BeagleBone and get you building fun, cool, and innovative projects with it. Start with the specifications of BeagleBone Black and its operating systems, then get to grips with the GPIOs available in BeagleBone Black. Work through four types of exciting projects: building real-time physical computing systems, home automation, image processing for a security system, and building your own tele-controlled robot and learn the fundamentals of a variety of projects in a single book. By the end of this book, you will be able to write code for BeagleBone in order to operate hardware and impart decision-making capabilities with the help of efficient coding in Python.

What you will learn

[*] Program on BeagleBone Black using Python [*] Interface sensors and actuators to BeagleBone Black [*] Build your own real-time physical computing systems [*] Connect physical computing systems to cloud servers [*] Build your own home automation and home security system [*] Build your own tele-controlled robot with real-time video streaming

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Aug 31, 2016
Length 262 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781785285059
Category :

Table of Contents

17 Chapters
BeagleBone By Example Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewer Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
1. Getting Started with BeagleBone Chevron down icon Chevron up icon
2. Circuit Fundamentals and GPIO Chevron down icon Chevron up icon
3. Introduction to Physical Computing Systems Chevron down icon Chevron up icon
4. Real-time Physical Computing Systems Using BeagleBone Chevron down icon Chevron up icon
5. Connecting Physical Computing Systems to the Internet Chevron down icon Chevron up icon
6. Home Automation Using BeagleBone Chevron down icon Chevron up icon
7. Working with Images Using Computer Vision Chevron down icon Chevron up icon
8. Home Security Systems Using BeagleBone Black Chevron down icon Chevron up icon
9. Exploring Robotics Chevron down icon Chevron up icon
10. Building Your Own Robot Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.