Hello there friend! SparkFun will be closed on 8/17/2018 @2pm for a company event. We will not be offering our normal Friday tour or Local Pickups during this time. We will resume normal operations on Monday 8/20/2018. Any orders placed after 2pm MT on 8/17/2018 will be processed when we resume normal operations. Have a great day!

Enginursday: Fun with Sockets

These Raspberry Pi tips and Python tricks let you take your sensing projects off the desk and into the wild with wireless transmission and processing of data.

Favorited Favorite 1

Does Moose, my beast of a chocolate lab, roll around in the throes of boredom when I’m not around? Or does he just sleep? I wanted to see an account of the shenanigans afoot in my home—or lack thereof—on the days Moose doesn’t make it to work with me. I wanted to be able to graphically track Moose’s activities using a low-level network interface by sending data from the Raspberry Pi to my workstation through both a server and client socket, respectively.

alt text

First the data needed to be serialized for transmission. In this case, I’m using Google’s Protobuf, a protocol buffer that is an automated solution to serializing the data. It encodes and parses the data on one end and reads and decodes it back to its original form on the client side. A socket makes communication between the Raspberry Pi and my computer easy, fast and convenient. I can now unload data straight to my computer for processing without storing any of it on the Raspberry Pi itself. And with a few calls to matplotlib in the script the data is automatically graphed very similarly to MATLAB. All the sensing is done on Raspberry Pi, with all the data processing done on my home computer. The data processing takes seconds on my computer, where it would have taken much longer with less data on the Pi. Before leaving for work I run the program on the Raspberry Pi, and when I get home I “ctrl c” to stop, which also starts the graphing. Then I can look to see what Moose has been up to.

Ryan “Mort” Mortenson, a SparkFun customer and “nerd from the wild,” volunteered to create an I2C library for the SparkFun 6 Degrees of Freedom IMU Digital Combo to use with Raspberry Pi and Arduino-compatible products. Since I’m currently enrolled in a Python course, I figured I’d take on the challenge of testing his work-in-progress on the Raspberry Pi. The following project uses a Raspberry Pi 3, the SparkFun 6DoF IMU Digital Combo, an Intocircuit Power Castle and a Windows 7 machine with Python 2.7. If you don’t know what version of Python you are running on either your workstation or Raspberry Pi, then run the pi_requirements in the project files link below, and all required packages will automatically be installed. You will need to download this twice—once on the server, the Raspberry Pi, and once on the client, your personal workstation.

If you are new to Raspberry Pi, I recommend starting with the Getting Started Guide for Raspberry Pi and connecting to your Pi through an SSH connection. On both machines, make sure Python is in your path. Windows users may have to edit their environment variables to use pip to install. If you are using pip, the command is: “pip install -r pi_requirements”.

Getting Started

Download the Project Files

SSH into your Raspberry Pi and have it powered portably. I needed to get a range on how far Moose could get away from the router before the socket failed, since it is over the same network. To do this, I moved the Raspberry Pi around my house and pinged its IP address until it failed. I found out I have pretty good coverage throughout my house and backyard as well.

Open a terminal window and type the command: “ping IP address”. You’ll need to know your Raspberry Pi’s IP address. (Knowing your Pi’s IP address is also necessary for using another machine to SSH or VNC into your Pi, so you should already have it.)

alt text

OK. He’s good to roam around the house and yard.


alt text

Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.

Now I have this 6DoF board attached to the Raspberry Pi’s GPIO, and it’s powered through a portable power bank and all tucked in to a doggie backpack. With Mort’s protobuf_cleint.py running on my home workstation and protobuf_server.py running on the Raspberry Pi, the data is read from the 6DoF IMU, serialized to a string and sent over the socket network interface. Killing the program pulls up the graphs of the X, Y and Z data for both the Accelerometer and Gyroscope readings. Here is what it looks like:

alt text

The server, the Raspberry Pi, serves up the data collected to the client.

alt text

The client, my home computer, is expecting the data as outlined in the socket setup.

Once the socket communication is severed, the graphing begins. This is a graphical representation of the raw data; it hasn’t been converted into any meaningful or useful data yet.

alt text

alt text

alt text

alt text

alt text

alt text

From here, a more playful project may be to characterize the flight of a homemade RC plane with a Pi Zero and smaller battery pack instead. Look for a new project in the tutorial and 6DoF IMU Combo Hookup Guide coming out soon.

In the meantime, Moose will keep his jet pack on for further testing while I figure out the data conversion in matplotlib.

Comments 7 comments

  • I think my kitty would object to the “doggie backpack” as being a bit much for her to wear all day! ;-)

  • Even though I’ve got 45+ years of dealing with computers (over 20 of them writing compilers), it took me a few minutes (when reading the “Socket Programming HOWTO”) to figure out that “IPC” stood for “Inter Process Communication”.

    I built a “gadget” running on a Beaglebone Black to make a temperature reading about once an hour, and send it off to an on-line database. FWIW, I use i2cdump to get the data, redirecting it to a file on the ramdisk. A little program translates it to a “useable” value, and creates a script to use wget to transfer the data to the database, then there’s code to run the script. It checks every few minutes for these scripts that have not completed, and retrying them and once they’ve succeeded transferring to an “archive” directory. (This assures that the data eventually gets to the database, even if it doesn’t work the first time or six.) The reason for this exercise is a friend who lives where it freezes, and sometimes goes away for several weeks, and wants to know that the furnace is still working and the power is on. We can check a few times a day to get “warm fuzzies” that the house is OK, and if not, we’ll call a trusted neighbor to go check on it (or find out that there’s a wide spread power outage). The one wrinkle we ran into is that the cable modem/router locks up every few days, so we put a mechanical timer (usually used to turn a lamp on & off to make it appear that someone is home) to turn it off briefly (the minimum the timer will do for “OFF”) every night in the “wee hours”, and we haven’t had that problem since. (I’m working on adding a wireless sensor to measure the outdoor temperature and report that, so that we don’t have to put up with the time-to-download all the extra stuff from weather.com – but that’s not “high priority”.) Note that although I used a BBB, the same approach would also work on a RPi. Oh, yes: since the BBB doesn’t have a built-in battery backed Real Time Clock Calender, I used an RTCC that also features a temperature measurement (it also has automagic temperature compensation for its crystal, as well as recording the time stamp for external power failure & restoration). Only problem is that the temperature is reported in Kelvin, with a resolution of 0.5K, and with the translation to F that gets a resolution of 0.9F, but that’s plenty to see that the furnace is still working.

  • Note for MacOS users: pip install -r python_requirements.txt does quite work due to the way the the package six (six-1.4.1 on my machine) was installed. To get past this issue which breaks the install of protobuf I used the following command. Not sure if this is the best fix, but it gets rid of the error and lets google.protobuf.internal be installed. sudo pip install protobuf --upgrade --ignore-installed six

    • does quite work

      Do you mean “doesn’t quite work”?

      • It doesn’t install protobuf. I didn’t dig deeper than that. Here is the error I saw:

        Collecting six==1.10.0 (from -r python_requirements.txt (line 9)) Downloading six-1.10.0-py2.py3-none-any.whl Requirement already satisfied (use --upgrade to upgrade): setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from protobuf==3.1.0.post1->-r python_requirements.txt (line 4)) Installing collected packages: six, cycler, numpy, python-dateutil, pytz, pyparsing, matplotlib, protobuf, pyserial Found existing installation: six 1.4.1 DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project. Uninstalling six-1.4.1:

        Exception: Traceback (most recent call last): File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/basecommand.py", line 246, in main status = self.run(options, args) File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/commands/install.py", line 352, in run root=options.root_path,

        • I have run into a lot of issues with installing python modules on Unix machines because of permissions on the directory where the modules are installed. One suggestion is to run the command with the -H option (works for me most of the time):

          sudo -H pip install -r python_requirements.txt

          Another suggestion would be to setup a virtual environment so you can install python modules an any directory you specify.


          Hope this helps.

          • Thanks for the tip. I believe I did actually use sudo with the -H flag to set the HOME environment variable correctly. I left that out following the common convention of doing that for whatever reason people do that. To be honest, mostly because I forgot if it was necessary by the time I documented it. Another fix I could have used would have, similar to what you suggested, been to install another copy of Python. I’d have done that with the Homebrew package manager, but I didn’t have the desire to have more redundant tools on my system.

Related Posts

Enginursday: COSMOS

Recent Posts

Liquidation Sale


All Tags