Read the step-by-step getting started tutorial below to begin developing with BeagleBone · BeagleBone Black · BeagleBone Black Wireless. Please download the . You will get an output similar to the one shown belowname. Grant Root BeagleBone Black has faster processor, more memory Getting Started with.

Getting Started With Beaglebone Black Pdf

Language:English, German, Japanese
Genre:Health & Fitness
Published (Last):15.04.2016
ePub File Size:28.61 MB
PDF File Size:17.82 MB
Distribution:Free* [*Registration Required]
Uploaded by: WILEY

Getting. Started with. BeagleBone. Updated for the latest. BeagleBones running .. When Gerald Coley, the hardware designer of BeagleBone Black and all of. BeagleBone Black is his newest, strongest— and most affordable—tool for . For example: “Getting Started With BeagleBone by Matt Richardson (Maker Media). The Beagle Bone is a great step up from microcontrollers (such as AVR, PIC, Windows and its tougher to install the drives on Win than other OS's we'll start . and Ethernet works, wouldn't it be nice to get rid of that Ethernet cable? This tutorial may not work on beaglebone black or the latest versions of.

We need to allow traffic from the BBB to be sent through our computer's connection. To do this, right click on the internet adapter that has a connection, for me it is the Wi-Fi adapter, and click Properties. You may need Admin rights.

Go to the Sharing tab, and check the allow network sharing box. In the drop down menu, select your BBB connection, for me Ethernet 3.

Click Ok. Next, we have to change some of the BBB's connection settings. Right click your BBB's connection, and click Properties. Click Ok, then Ok. Simply close it, then re-open another SSH connection. We can't access the internet quite yet, as there is still some changes needed in the BBB.

You should now be able to ping 8. But if you try ping google. Getting Around with Linux 23 www. If you want to append text to the end of a file, the command you type will look like this: To see the results, use cat again: What a beautiful day! In other words, it will take a series of files and appends one after the other. To try that out, first make a few file: See you real soon! All these command line tools can make it fairly easy to work with files, but sometimes you might just want to get inside a file to view and edit its 24 Getting Started With BeagleBone www.

For that, I recommend the text editor nano.

To open your file in nano, simply type nano followed by the file you want to open. To save, type Control-O and to exit, type Control-X. Nano can do a lot of stuff including searching, clipboard operations, and spell check.

To view the help information within nano, type Control-G. Of course, you can also create new files with nano. Just type the name of the file you want to create after the nano command and that file will be written when you save within nano.

Copying, Moving, and Renaming Files Copying and moving files around from the command line is fairly straight- forward. Date and Time Most computers have a real time clock, a piece of hardware that keeps track of the date and time. Unfortu- nately, the BeagleBone does not have a real time clock.

This means that when you boot up the BeagleBone, it will have no idea what the date and time are. To prove it, try the date command: The time just needs to be set every time it boots up. First, backup the old localtime file by renaming it localtime. A symbolic link is like a forwarding notice for a file.

You can place one in a directory and most software that wants to read or write the file will be redirected to the file in another location. The shell prompt in the following example is truncated slightly to fit on the screen without wrapping the text. Edit the ntpdate configuration file with nano: Feel free to pick your own NTP server if you wish. Save the file by typing Control-O and exit nano with Control-X. Now run the ntpdate-sync command to have the BeagleBone fetch the latest time.

After a few moments, try executing the date command again: The BeagleBone is also pre-configured by default to synchronize with the NTP server once an hour, so you may have a period of time after booting when the time is incorrect.

You can always run ntpdate-sync to force it to synchronize immediately. See Appendix B for how to set up ntpdate-sync to happen automatically on start up. Software Installation, Updates If your BeagleBone has a connection to the Internet, you can easily install or upgrade software and code libraries. The software comes in packages and Getting Around with Linux 27 www. To update the list of available packages, simply execute: If you want to update the software, the opkg command upgrade will upgrade all of your installed packages if there are new versions available.

To install the version control system git, for instance, you would run: For instance if you only wanted to upgrade Python, you could run: To edit the hostname, execute the following, keeping in mind that the host- name can only contain letters, numbers, or a hyphen.

After you save the file and quit nano, you need to reboot your BeagleBone. After you log in again, you should see your custom hostname in the prompt. To set or change the password, use the command passwd: To shut down the BeagleBone properly, issue the following command: The board will turn back on when you reconnect it to power. If you just want to reboot the board, use the reboot command. Each header has 46 pins and if you look closely you can see that pins 1, 2, 45, and 46 are labeled on each header Figure Header P8 Figure Most pins can even be switched between modes to accommodate dif- ferent possible functions.

GPIO pins have two states: We say such a pin is floating. Other platforms, like the Arduino, may use 5 volts. Use only 3. Otherwise you can permanently damage the board. Once you get to know how the Linux kernel uses a virtual filesystem to read and write pins, it makes programming the BeagleBone much easier. As long as you can read and write files, you can work with GPIO.

You might also like: BLACKROOTS SCIENCE PDF

First Steps with Digital Electronics 33 www. Be very careful not to accidentally connect the rail to the 5 volt source on pins 5 and 6. The GPIO pins can only han- dle 3. Place an LED in the breadboard so that the cathode side the shorter wire is connected to the negative rail and the anode side is in one of the rows of the breadboard. Using a ohm resistor, connect the anode side of the LED to another row on the breadboard.

Connect the other side of the ohm resistor to pin 12 on header P8. On a ohm resistor, the color bands printed on it will be brown, black, brown, and then gold or silver. Your circuit should look like Figure Even if you look at BeagleBoard. Many of the pins default to other modes. First Steps with Digital Electronics 35 www. On the command line, change to the gpio directory: Start typing the first few letters of the directory or file and then hit tab key.

If the system has a single file or directory that matches that, it will fill in the rest of the name for you. To do that, use the echo command to write the number 44 to the export file: For example, if you attach a BeagleBone expansion board, or cape, it may request access to a few of these pins for itself. Change to that directory: Write 1 to the value file: We can then set the pin low and turn off the LED by writing a 0 to the value file: Feel free to experiment with the other pins in Figure Input If you can control an output pin by writing to the value file, it stands to reason that you can read an input pin by reading the value file.

By doing this, you can check the state of physical buttons and switches. Place a push button into the breadboard so that it straddles the center channel. Connect one lead of the button to the positive rail. Connect the other lead of the button to the input pin 11 on header P8.

See Figure of BeagleBone wired up. Connect a 10K pulldown resistor from the ground rail to the button lead that connects to the input pin. That is to say, connected to either 3. The 10K pull- down resistor in the step above ensures that when the button is not pressed and the connection between 3. Using a resistor ensures that when the switch is closed, the 3. Instead, it goes to the input pin.

First, export the pin to the user- space and change to its directory. Set the pin direction as an input: This should return 0 for low. This means that the pin is connected to ground. Now press and hold the button while you execute the cat value command again.

If you have the button wired up correctly, you should now see a 1, indicating the pin is high connected to 3. An easy way to execute a command again is to hit the up arrow key on your keyboard and then hit enter. You can keep hitting up to scroll back through your history of com- mands.

Just hit enter when you get to the command you want. It may seem trivial, but these examples represent the very basic foundation of digital electronics with the BeagleBone.

With the right circui- try, these could easily become tilt sensors, blenders, door security sensors, buzzers, motors, and much more. And since the BeagleBone is already network-enabled, it lets you change the settings of your outlet timer from the comfort of our computer, or even from the other side of the globe.

This project will also demonstrate the basics of using shell scripting as one method to programmatically execute commands. Using another strand of hookup wire, connect pin 2 from the Power- Switch Tail to one of the ground pins on the BeagleBone.

Be sure the lamp is switched on. The PowerSwitch Tail is a high voltage relay circuit that has been packaged up for easy use. It lets you use the 3. First Steps with Digital Electronics 41 www. Execute the following commands to actuate the relay and turn the lamp on. Make sure you run the unexport commands shown there before you try this example. Now unexport the pin. While shell scripts have the potential to be powerful and complex, they can also be very basic and as easy to write as entering the commands onto the command line.

This will launch the nano text editor. Type in the following code: Use nano to create another file called lightOff. To make both of those scripts executable, execute the following com- mands: Now when you type.

When you type. You will need to be in the home directory to do this. Add the following lines to the end of the file: The cron scheduler will allow you to execute com- mands as frequently as once a minute or you can even set a command to execute many years in the future. Each crontab entry is on its own line with 5 space-delimited settings sometimes 6, in the case of specifying years followed by another space and then the command that should be executed as you can see in Table See Table for what the entry would look like.

If you want to adjust the timing of the lamp, all you have to do is log into your BeagleBone and edit your crontab. First Steps with Digital Electronics 45 www. The downside to the flexibility that the BeagleBone affords is that there are many different ways to do the same thing. This chapter will walk you through one way to use code to interact with the pins on the board.

However, on an embedded Linux system like the BeagleBone, the compiler or interpreter exists on the board itself. This chapter will show you how to use the Python interpreter on the Beagle- Bone to program the pins to behave the way you want. The code is relatively easy to understand and there are many libraries that help with complex tasks.

However, this would take a long time. Luckily for us, the fine engineers at Adafruit have created an open source Python library for accessing the pins. I especially like this library because it follows a lot of the same conventions as the popular RPi. From the command line, update the package listing. Python 2. Using Python, you can create an infinite loop to turn the LED on and off until you terminate your script.

See Figure Python Pin Control 49 www. This will allow you to easily down- load files to your computer, edit them, and upload them back to the Beagle- Bone. My favorite FTP client, Transmit, even lets me edit the files in a text editor and automatically uploads them to the board whenever I hit save. See Chapter 8. Use the code from Example for the contents of your file. Source code for blink. OUT while True: HIGH time. LOW time. Unlike many other languages, the indentation you use for each line of code is important to get right.

You can uses spaces or tabs, just as long as you keep it consistent throughout your code. If not, check the errors that are displayed and check your code against the code above. Python evaluates the code one line at a time, starting at the top of the script. When it gets to the line that says while True: The while statement can be used to test for conditions as well. Any time the condition evaluates as true, it runs the code beneath it again, which is why while True: This line tells the system the location of the Python interpreter to process your code.

From the command line, use the following command to do that: When executing files, you need to be explicit about their location unless you move the script to a directory that is included in your path. Read on for more on that. To view directories that are in your path, run the following command sample output is shown: However, the actual directory may not already exist.

Put the code from Example in it.

Example Source code for button. IN while True: Python Pin Control 53 www. Print a message to the terminal. Try it out before we dig into the details of the code: Why is that?

Getting Started with BeagleBone Black

For one, you can hold up the Python script until the button is released, like in the code in Example Holding the process until the button is released! You can have your code look for a pin going high rising , going low falling , or just changing from one state to the other. These are called interrupts. Using interrupts to indicate when the button is pushed or released!

Wait here until the button changes from high to low. That is to say, only things that are on or off. Sometimes, however, you want an input that delivers a range of values, such as the temperature, the light levels, or the state of a dial. The BeagleBone has seven pins that can be used as analog inputs to get values from these types of sensors.

Like all computers, the BeagleBone does its work in the digital realm, so val- ues from analog sensors must be converted to digital.

See Figure for an analogy of analog and digital input. Python Pin Control 55 www. The switch on the left is like digital: The dimmer on the right is like analog: Essentially, it will let us turn a dial to control the amount of voltage going into the analog input pin.

Chapter 1: The Beagle Hardware Platform

Since it can only take up to 1. Starting with a blank breadboard, use a jumper wire to connect pin 34 from header P9 to the ground rail on your breadboard. With another jumper wire, connect pin 32 from header P9 to the positive rail in your breadboard. This is the 1. Insert the 2K potentiometer into the breadboard as shown in Figure Connecting a potentiometer to pin 32 on header P9 4. Out of the three pins of the potentiometer, connect the middle one to pin 33 on header P9.

This is one of the seven analog input pins. Connect one of the outside pins to the positive rail choose either one for now. Connect the remaining pin to the ground rail. Python Pin Control 57 www. Source code for potentiometer. Sets the pins to be used as analog inputs. Prints out a reading from the analog input pin 33 on header P9. Execute the script and watch the terminal as you turn the pot from one side to the other: So if you want to get the actual voltage, you can multiply that number by 1.

In Example , the reading from pin 33 is multiplied by 1.

Python Pin Control 59 www. You can also find touch surfaces or arcade-style control sticks that act as analog sensors.

There are 8 PWM channels that can be used on the BeagleBone and each channel can be connected to a few different pins as seen in Table PWM Channels and their respective pins. The amount of time the pin is high is called the duty cycle. It can be set anywhere from 0 totally off to totally on.

Chapter 1: The Beagle Hardware Platform

Python Pin Control 61 www. Source code for pwm. Start using PWM on pin 13 of header P8 and set its duty cycle to 0. Loop the code indented beneath, counting up from 0 to and storing that value in i. Set the duty cycle of pin 13 on header P8 to be value of i, which increments with each iteration of this loop. Stops pulsing pin 13 of header P8.

Disable PWM on any remaining pins. Save the file and execute it from the command line: You should see it fade up from totally off to totally on! If you want to take this further, try modifying the code so that it fades up and down like a breathing LED.

Of course, a good project usually goes beyond a simple button and LED. Hopefully, you can apply the basics in this chapter to all kinds of creative projects! If you want to learn more about the ins and outs of Python, I recommend Learn Python The Hard Way, a free online book that walks you through all the Python fundamentals.

Python Pin Control 63 www. That is, physical projects that interact with the Internet in some way. It could be as basic as sending email alerts when a physical thing happens, sending sensor data to the cloud, or even controlling a robot via a web-based interface. In the project at the end of Chapter 2, I mention that you can change your router settings so that you can use SSH to switch the light or change its timing settings from wherever you are in the world. What if your less tech-savvy house mates want to make 65 www.

Before I jump into how to connect these two resources together with code, I want to explain a Python fundamental, the function. A function is a way of encapsulating a block of code meant to accomplish a single task. For instance, there are a few lines of code involved in reading a temperature sensor and printing its value in degrees Fahrenheit. You can write all that code in a function called printTemperatureInF so that you can execute that code in just one command.

You can also create a function that takes an input value in Celsius and outputs the converted value in Fahrenheit.

Inputs to a function are called parameters and outputs are called return values. Since there will be a few lines of code needed in order to connect to our mail server and send the email, we may want to encapsulate that code into its own function. For instance, the line of code GPIO. OUT calls a function written by Adafruit to make it easy to set pin 12 on header P8 as an output pin. There are a few steps involved in setting the mode of a GPIO pin and therefore a few lines of code.

Writing your own function is actually pretty easy as seen in Example Source code for function. Welcome to my Python script! Print a greeting message to the console with the value of name. When you put the code from Example into a file and execute it, you should see the greeting printed. Try calling greeting multiple times with different names in the same script. All function definitions start with the keyword def and then the name of the function that you want to make.

Inside the parentheses, you can name each of the parameters, or inputs, that you want the function to take. In the ex- ample above, the function greet takes a single parameter, name. We can test this function by itself, before getting into working on the physical side of the project. Be careful to not send dozens of email messages in quick suc- cession, or you may find that your email provider slows down your access to email, or worse, suspends it temporarily.

Since Gmail is a popular email service, I included their server name in Example Create a file called emailer. Putting Python Projects Online 67 www. Source code for emailer. SMTP 'smtp. Import the functions needed to create an email. Create a function called alertMe that takes two inputs: Create an object called myAddress and store your address as a string replace myAddress gmail. Create an object called msg and store the body in it in the MIME format the body is passed as an input to this function.

Set the subject of the message from the input of the function. Connect to SMTP server smtp. Start encrypting all commands to the SMTP server you may need to remove this line for some email providers. Send login credentials to the server replace these with your login and password. Send the email.


Close the connection to the SMTP server. The alertMe function in Example takes a subject and a body as inputs and connects to the email server to send the message.

Hardcoding to Make Things Easy In Example we hardcoded the SMTP server, login, email address, and password into the alertMe function, which means we put these details di- rectly into the code as opposed to taking them as inputs from other sources.

If you execute emailer. Now this function definition can be im- ported into other Python files as long as emailer. Detecting whether or not a door is open is pretty simple. A door security sensor will act like a digital switch and can connect directly to the BeagleBone. These sensors can be found in a hardware store or in your local Radio Shack. Putting Python Projects Online 69 www. A normally open sensor will disconnect the two terminals when the door is opened and the sensor is separated from the magnet.

Conversely, a normally closed sensor will connect the two terminals when the door is opened. Some door sensors will work both ways and will have a common terminal, a normally open terminal, and a normally closed terminal, like the one in Figure Any version will work in this project be- cause we can use code to check for either a high or low signal.

This door sensor has a normally open and a normally closed terminal. In the code, I will indicate where the logic should be changed in case you have a normally closed sensor. Connect one terminal of the door sensor to pin 11 on header P8. Connect the other terminal of the door sensor to one of the 3.

Connect a 10K pull-down resistor between pin 11 on header P8 and ground. Wiring the door sensor to pin 11 on header P8 with a 10K pull- down resistor. Putting Python Projects Online 71 www. The main loop constantly checks if the door has been opened. When it senses that the door has gone from being closed to being opened, it saves a time value into alertTime.

That time is 60 seconds in the future. For every subse- quent check, if the door is still opened, it checks if 60 seconds has passed. If so, it sends an email alert. If the door has been closed, it clears the value of alertTime, ensuring that the next time the door is open, it will start tracking a full 60 seconds again. Example shows how that logic is converted into Python.

Source code for doorAlert. Alerting in 60 seconds if left open. Sent email alert! Set the value of alertTime to 0 initially. If the door is opened, execute the indented code below If using a normally open sensor, this line should read if not GPIO. If the time to send the alert has not yet been set as the value of alertTime… …then take the time now, add 60 seconds and store that in alertTime. If the time to send the alert has been set, execute the code indented below. If the current time is past alertTime… Call the alertMe function from emailer.

Wait here until the door is closed again to prevent multiple emails being sent If using a normally open sensor, this line should read while not GPIO.

If the door is closed, set alertTime back to 0. The doorAlert. You must remove the line from emailer. If you want to do that now, jump ahead to Ap- pendix B. When you sent a request for a particular URL, the server found that file in its directory structure and transmitted it to you.

One of the things I absolutely love about embedded Linux platforms like the BeagleBone is that they can run many modern web application frameworks. You can use these frameworks along with the GPIO libraries to make web interfaces for physical projects. Start by creating a new file called hello-flask. Source code for hello-flask. If this file itself is being executed and not imported by another file , execute the code indented beneath it.

Have the flask server run on port 81 and report and log requests and errors to the console. After saving hello-flask. The server responded with a code , which means OK. Browsers request a small graphic icon when visiting a site to represent it in the location bar and in the bookmarks menu.

Your site does not have a favicon. To terminate the server and get back to the command line, type Control-C. You could write the code yourself to send this formatting along with the return statement in that example, but including all that code in the Python script would get unwieldy very quickly. Luckily for us, Flask uses a template engine called Jinja2, which takes care of combining HTML formatting with the data that our application will provide.

With the capabilities of Jinja2, you can create HTML files that have place- holders for data that comes from our web application. This means that the code that controls the look of the page is kept separate from the actual logic of the application.

To try it out yourself, create a new file called flask-template. Then create a subdirectory called templates and create a file in there called main. Use Example as the contents for that file. Source code for flask-template.

Store the current time into now. Create a human-readable string of the date and time. Create a dictionary of values to pass to the template in key: Take the value of the key time and replace it here.

It will look in the folder templates and find the specified file, in this case, main. Flask also gives Jinja2 the values stored in the dictionary templateData, which was created a few lines above. These values can be placed in the template by name in between two curly braces, as you see in Example After saving flask-template.

Source code for flask-door. IN app. Store the doorStatus string in a dictionary with the key doorStatus. Be sure that this references the correct template HTML file, in this case main-door. You can also use Flask to control digital outputs, pulse the PWM pins, and get readings from analog sensors connected to the analog-to-digital converter. Another powerful feature of Flask is that it can process variables within URLs.

You can set up Flask so that the address http: All this can be done with just a little bit of code as shown in Example After adding the route above to your web application, your server will respond to requests made to http: Data Logging with Xively Since it has a traditional filesystem built right in, using the BeagleBone makes a lot of sense at the core of of data logging projects. The data it collects can be saved to a file and then accessed via SFTP, viewed via the Web, or emailed to you directly.

You can also have the BeagleBone push data to a cloud service like Xively, which makes it easy to view current and historical data from sen- sors via the web see Figure You can also use a TMP Pin 1 of the TMP35 connects to 3. The TMP35 will output 0. In fact, the formula to determine the temperature is: Source code for tmp Convert from the milivolts measurement to Celsius.

Convert from Celsius to Fahrenheit. Print the Fahrenheit value, rounded to a single decimal place. When you execute this Python script, you should see a Fahrenheit value printed every second. Try warming the sensor with your fingers. The value should go up! Let go and watch the value drop again. Wiring the TMP35 temperature sensor to the analog pin at pin 40 on header P9. Connecting to Xively Xively is an Internet of Things platform that takes in data from sensors all over the world and stores that in a database.

It can then serve the data via their web interface or via an API for your own applications.

Other currently available software images

It was previously known as Cosm, and before that, Pachube. Additionally, their developer libraries make it very easy to connect to their services in many different programming languages. Create an account at the Xively signup page.

A free developer account will be sufficient for personal use. Give your device a name and choose whether you want the data to be public or private. You can change these settings later, if you wish. Putting Python Projects Online 81 www. On your BeagleBone, use pip to install the Xively Python library: Create a new file called xively-temp.

When you execute xively-temp. Source code for xively-temp. Enter your API key within quotation marks here. Enter your feed ID within parenthesis here. Enter your channel ID here. Push the data to the Xively server. Under a single device within Xively, you can also add more channels to send multiple sensor values. After storing a second value from the analog-to- digital converter, add the value to the feed.

To set up this project so that it launches automatically, flip ahead to Appendix B. Taking it Further It would be difficult to cover all of the possible ways your BeagleBone could communicate over the Internet. Here are a few ad- ditional resources for using Python online: Temboo Temboo makes it easy to connect to hundreds of different APIs. Putting Python Projects Online 83 www. For instance, you can create a web page that always shows a live-updating temperature reading from the BeagleBone.

Chances are, most of the sites you visit use JavaScript in some way or another. With a framework like Node.The cover photo was created by Marc de Vinck. Hello Derek, on page 29 chapter one of the paperback version, you suggested a wireless access point with integrated multiport router. Connecting to Xively Xively is an Internet of Things platform that takes in data from sensors all over the world and stores that in a database. After saving hello-flask. See Appendix B for how to set up ntpdate-sync to happen automatically on start up.