You go to work, and your dog is napping. You come home, and your dog is napping. Did she ever move? How many steps did she take during the day? A concerned dog owner might wonder. To answer this vital question, we’ve created the following simple Pedometer project using the Pedometer 3 click and OLED C click additions to the PocketBeagle®.
The goal of this project is to demonstrate how easy it is to add new hardware to PocketBeagle®, write or find any necessary drivers, and get the project working in a useful application while having a little fun. The PocketBeagle® is a tiny Linux computer, completely open source, developed by BeagleBoard.org®. It features the OSD3358-SM System-in-Package (SiP) from Octavo Systems.
Whether you are doing a stand alone project, or rapidly prototyping components of a new design, the eco-system available for the OSD335x is robust. For more back story on the motivation for this project, visit the Octavo Systems blog page.BeagleBoard.org®PocketBeagle® Headers
The following figure shows the pin-out of the 2 headers of PocketBeagle®.
The default function of each pin is highlighted in dashed line. Click boards™ have a standardized MikroBUS™ pin-out (shown below).
BeagleBoard.org® PocketBeagle® (pictured below) headers conform to the MikroBUS™ standard and allow for two click boards™ to be interfaced with PocketBeagle® at a time (shown in the following figure). Although no guarantees are made with respect to compatibility, most click boards™ work with PocketBeagle® with a little bit of effort. There are actually hundreds of click boards to choose from, and since we wanted to get the pedometer up and running quickly, we chose the OLED click already used in this project: ExpandPocketBeagle® Easily with Mikro click boards: OLED C and followed the instructions on the Hackster project. For the Pedometer input, Mikro makes a Pedometer Click: https://www.mikroe.com/pedometer-3-click. This was a new click for a PocketBeagle®community, so we had to write the driver software for it. See Section 11 below on the basics of how to do this.
BeagleBoard.org® PocketBeagle® header pins with click board positions.
Follow the steps below to make your very own pedometer.
Step1: Install Click boards on BeagleBoard.org® PocketBeagle®
Install the 2 click boards into the 2 available positions on the PocketBeagle®. The positions are important as we need to select the device tree overlays according to them. For this project, install the OLED C click board in position 1 and Pedometer 3 click on position 2. By installing the click boards this way, we select the interface with which the click boards interact with the PocketBeagle®. The OLED C click uses the SPI0 interface and Pedometer 3 click uses the I2C2 interface. The 2 click boards installed on the PocketBeagle® are shown below:
Note that the microUSB port will be underneath the OLED C Click.
Step2: Download and Install the latest Linux image
Go to Beagleboard.org to download the latest Linux image: https://beagleboard.org/latest-images. Look for the Stretch IoT Debian Image for PocketBeagle® and click the link to download it (see below).
Download and install an SD card programming utility onto your computer if you do not already have one. We recommend https://etcher.io/. Insert a new microSD card into a card reader/writer and attach it to your computer. Open the balenaEtcher application and follow the instructions for selecting the.img file and burning it from your computer to the microSD card (see below). This may take several minutes.
Eject the SD card when prompted and remove the card. Insert it into the PocketBeagle® SD card slot – you should hear a click when it seats properly. Make sure that it is properly inserted before powering the system.
Step3: Connect PocketBeagle® via Cloud9 IDE
With the latest images, you shouldn’t need to install any USB drivers, but just case, they can be found on the BeagleBoard Getting Started Page.
Use a microUSB to USB cable to connect PocketBeagle® to a computer/laptop. You should be able to bring up theCloud9 IDE by entering the URL: http://192.168.7.2:3000/ide.html into the web browser. (use http://192.168.6.2:3000/ide.html for MAC o/s) Make sure you are using Chrome or Firefox. The IDE should look like the following figure:
Step4: Connect PocketBeagle® to the Internet
In order to download some additional software to the PocketBeagle®, we need to share our computer internet connection with the device. Open up the control panel in Windows and navigate to the Network & Sharing Center (see below).
Select the network connection where you connect to the Internet (in the case above, Wi-Fi). A window like the one shown below will pop up.
Select “Properties” and then click on the “Sharing” tab at the top of the new window that pops up (shown below).
Click to “Allow other network users to connect through this computer’s Internet connection, ” and select the connection (in the case above, Ethernet 6) to the PocketBeagle®. Click OK.
Now, you have to manually change the IP address and its settings on the PocketBeagle® network device. Click on the appropriate network (Ethernet 6 in the example) and go to Properties (see below).
Click on “Internet Protocol Version 4 (TCP/IPv4)” from the list and then click Properties. In the pop-up window, add the IP address of the gateway (192.168.7.1) and the other settings shown below.
Click OK. At this point, you should have noticed that the connection has closed between the PocketBeagle®and your computer due to the network connection resetting. Just reload the Cloud9 page.
Now, we need to tell the PocketBeagle® how to connect to the internet connection via the computer gateway (IP 192.168.7.1). Since we will have to reboot the device multiple times during this process, we should save time by setting it up to connect automatically. First, type the following command into the Cloud9 terminal to test the USB connection:
Ping is a debugging tool to test TCP/IP connections between devices. You should see an output on the screen, which is a response from your computer to say everything is ok (“Reply from 192.168.7.1: bytes = X time = X ms TTL = X”). Press Ctrl+C to stop it.
Change to the “bin” directory below home and open a new script using the nano editor:
Add the following lines to this file and press Ctrl+X to leave it. The editor will prompt you to save the changes you have made, so make sure to select Y for yes:
/sbin/route add default gw 192.168.7.1
echo "nameserver 184.108.40.206" >> /etc/resolv.conf
This adds the computer gateway address to the PocketBeagle® IP address routing table and adds a name server to translate the IP addresses into names and vice versa. We also need to update the permissions so that this script is executable:
chmod 755 network.sh
Next, we will create a directory to store log files:
Now that we have this basic infrastructure, we can create our cron entry. First, we need to edit the"crontab" for root:
sudo crontab -e
(remember that the default password for PocketBeagle® is "tmppwd").
This will prompt you to select an editor to add the cron entry; you can type 1 ENTER to open the nano editor. You should add the following line:
@reboot sleep 30 && sh/home/debian/bin/network.sh > /home/debian/bin/logs/cronlog 2>&1
This instructs cron that right after a reboot, it should first sleep for 30 seconds, and then execute our script. The output of the script and any error messages should be put into the file: "/bin/logs/cronlog". Now, reboot and check the connection:
(Don’t forget Ctrl+C to stop the output.)
Step5: Device Tree Overlay
Download PB-SPI0-OLEDC-CLICK.dtbo from the attachments. This is the device tree overlay for the OLED display. Drag and drop the file into Cloud9 and copy it into the /lib/firmware directory:
sudo cp PB-SPI0-OLEDC-CLICK.dtbo /lib/firmware/
Don't forget to enter the password for debian (tmppwd) when you execute a command as root.Step6: Edit /boot/uEnv.txt to invoke overlays
Edit /boot/uEnv.txt to invoke the overlay on boot with this command:
sudo nano /boot/uEnv.txt
Enter the names of overlay binary file (with total path) into uEnv.txt and un-comment the uboot_overlay_addr# variable that the overlay binary was assigned to:
Save /boot/uEnv.txt (use Ctrl+O) then exit (use Ctrl+X) and then reboot:
After boot up, you should see some gibberish text displayed on OLED C Click screen. If not, then there might be an issue with the device tree overlay.
Step7: Install necessary software
This Python code for this project relies on the Linux Frame Buffer Image-viewer package (fbi), the Pillow Image Editor package, and the Adafruit-GPIO package. Run the following commands to install these packages:
sudo apt-get update
sudo apt-get install fbi
sudo apt-get install python3.5-dev
sudo apt-get install python3-pip
sudo pip3 install Adafruit-GPIO
sudo apt-get install libjpeg-dev
sudo apt-get install zlib1g-dev
sudo apt-get install libpng-dev
sudo pip3 install --upgrade setuptools
sudo apt-get install libfreetype6-dev
sudo pip3 --no-cache-dir install pillow
Note that many of these installations require quite some time to complete.
Step8: Upload Code to Cloud9
Make sure the OLED screen does not time out (approx. 3 mins). You should still see the letters on the screen. If the screen is blank, the screen has timed out. If this occurs, reboot. Download the pedometer.zip file (attached to this hackster) to your PC. Unzip the contents and drop the entire folder into the Cloud9.Step9: Execute code (autorun)
Change current directory to pedometer. If you followed step 8, you should see the directory in Cloud9.
Edit the "crontab" again to execute our code on reboot (remember that the default password for PocketBeagle® is "tmppwd"):
sudo crontab -e
You should add the following line:
@reboot sleep 15 && sh /var/lib/cloud9/pedometer/run.sh >/var/lib/cloud9/pedometer/logs/cronlog 2>&1
Now, reboot again:
After the startup, the screen should show a sleeping “z…z…z” animation.
Step10: Take it for a walk!
The pedometer will display the sleep animation while still:
After you walk for a 10-15 steps (make sure the device is in a pocket or held firmly against your center of mass), you should see an animation of a person walking:
When you stop to rest, the pedometer will display your step total:
The count will time-out after 30 seconds unless you start walking again.
Here it is on Goldie the dog. She was very confused at first by the strange device on her harness, but got used to it pretty quickly.
This tutorial was fun to make, and we hope you enjoyed walking through it. With a bit of hardware, a bit of original code, and a lot of help from the BeagleBoard.org® community, it’s actually pretty easy to add a new click board to the PocketBeagle®. We hope you can use this project as an example to start your own unique and exciting designs!
Step11: (Aside) Tips fromJenniferfor writing a new driver
1) Don’t unless you absolutely have to. Writing a driver for a new piece of hardware can be extremely time-intensive and frustrating, so it is worth spending a few hours searching for any existing open source drivers available.
2) Look for application notes from the device designer on how to use it. We found an app note on the Kionix Product Page on Getting Started with the Pedometer. This formed the backbone of our driver code.
3) Download the datasheet and understand what interface is necessary to communicate with the device.
4) (From the datasheet) Understand what basic configuration is needed for the device i.e. many require that you hold it in reset, configure some basic stuff like modes and data rates, and then release it from reset. This usually takes the form of reading/writing registers.
5) (From the datasheet) Understand the necessary configuration for your specific application. We had to set a lot of configurations for step amplitude thresholds and timings in order to get the pedometer engine to detect steps well.
6) Write testable code. Set up the interface first and make sure that you are reading/writing correctly. Verify that the configurations are being written the way you expect.
7) While debugging, look for forums online to see what issues other designers ran into (and fixed). And when in doubt, read more of the datasheet.