IoT Motion Controlled Servos
Secure and reliable real time data streaming is essential for IoT. I’ve seen plenty of demonstrations involving applications or “push button here, LED on over there” type hardware, but a friend and I wanted to make something that was more interactive… a way to almost feel the data stream as you manipulate it. So, we decided to build a motion controlled “thing” that mimics your hand movements and displays colors based on finger positioning. Hopefully, with my help you’ll be able to build your own!
A conceptual overview and some more in depth look at some of the source code can be found on the PubNub blog, the complete source code is available in this GitHub Repo, and a separate Instructable detailing the LED matrix driver circuitry has been written.
Step 1: Parts and Tools
There are a lot of pieces to this puzzle, and I will try to list out the major things.
Parts and Pieces
- Raspberry Pi – I am using a V1 Model B+
- Leap Motion Controller
- 5V Power Supply – There are a lot of power hungry parts here, better make it 5A!
- I2C Enabled PWM Driver – Adafruit has a nice one.
- 4 x Micro Servos – Cheap TowerPros will work.
- 4 x Micro Servo Mounts – I modified these from Lynxmotion
- LEDs and Current Limiting Resistors – For visual effect, but not necessary.
- Project Enclosure – This one is laser cut by Ponoko, more on that later.
- Screws, bolts, nuts, zip ties – and other such means of fastening things together
- Cable Wrap – To keep the wires neat and tidy (on amzaon).
- Power Jack, Switch, and Cable – So as to get power from the wall to the circuitry (on amazon)!
- On-Off-On Slide Switch – Used to select Servo output modes
- Small Push Button – Used as an R Pi reboot / shutdown button
- RGB LED + 2 x 470 ohm resistors – Reboot / shutdown indication
- Stand-Offs – To Mount componentes, These cheap ones on amazon are great.
- Hook Up Wires – So as to connect one thing to another
The RGB Matrices sitting on top of the servos are using a custom built driver. This part of the project is fairly advanced, and a separate Instrucable has been written for it.
Software and Services
- PubNub – Data Streaming Service. Free Sandbox mode for developers!
- Java SE – JDK 8 – Be sure to get the correct version for your operating system
- Leap Motion Visualizer and Java SDK – Again Be sure to get the correct package for your operating system
- PubNub Java SE SDK – This needs to go into the project libs directory
- Java IDE – Use your favorite, such as JGrasp, NetBeans, IntelliJ, or Eclipse
- Project Source Code Repository – Hosted on GitHub
Raspberry Pi Setup
- PubNub Python Library – This is an in depth guide for setting up PubNub on the R Pi
- Internet Connectivity – This can be WiFi or Ethernet. If you can ping Google, then you are good to go.
Tools and Such
- Computer – Writing code, viewing of the Instructables, etc
- Electric Drill + Drill Bits/Drivers – Drilling Holes, Driving screws, etc.
- Dremel Rotary Tool – For cutting holes in the enclosure… may not be necessary for you.
- Hot Glue Gun – Is there any other way to stick something to something else?
- Soldering Iron + Solder – Don’t breath the fumes!
- Laser Cutter – Again, I used a service, but you can DIY if you have your own machine laying around.
- Screwdrivers, Pliers – Stuff you should have by now!
Step 2: Project Overview
Like I said, there are lot of pieces to this puzzle! At the top level, there are three main components:
- Computer with Leap Motion Controller – publishing data to the internet
- Raspberry Pi in “the box” – Subscribing to data from the internet
- PubNub – the Communication layer securely connecting these things.
The computer part is simple enough, but in “the box” there is a bit more going on…
- Raspberry Pi – Primary Controller, talks to other parts using I2C Bus
- ATmega328p Matrix Driver Circuit – Receives commands via I2C Bus, controls 2 8×8 RGB matrices
- TLC5916 Based LED Sink Circuit – Controlled by ATmega328p, Sinks LED matrix current
- Adafruit PWM Driver – Receives commands via I2C bus, drives 4 servos
- 5V, 5A power supply – Brings everything to life
Step 3: Java and Leap Motion Controller Setup
We will be using the Java SDK for the Leap Motion controller, so you will need to install the Java Development Kit which includes the Java Runtime Environment. This is pretty straight forward, just choose the correct package for your operating system.
Rather than create an installation package, you will be running the source code directly from a Java IDE. This allows you to play with the code yourself, modify things, and learn! Once you have the JDK installed, you need a good IDE (that is, integrated development environment). Some common ones are Eclipse, IntelliJ, and NetBeans. Use your favorite, or pick one at random. Setting up libraries is a bit different in each, so you will have to refer to the documentation on how to setup a new project!
Java Source Code
There is only one java file needed to run the code, but you will also have to install the Leap Motion and PubNub Java libraries… The GitHub project repository contains the necessary java file. I’ll talk a bit about the code in the next step.
Setting up the Leap can be a bit tricky, and the process is different for different systems. Rather than step you through how I did it, I will direct you to the appropriate Leap Motion articles. You can always ask for help if you get stuck somewhere.
- Download the SDK, Drivers, and Applications from Leap Motion.
- Install the Leap Application, which also installs any necessary drivers and processes.
- Run the Leap Motion Diagnostic Visualizer to make sure Leap is working.
- Read through the Java SDK Documentation, particularly setting up a project.
The last step is to download the PubNub Java SDK. The documentation page talks about how to add the library to your Java Project.
Step 4: Java Source Code
If you are not interested in the java code, you can skip this step; however, you will at least need to put your personal PubNub keys into the code for it to work! The source code can be viewed on GitHub.
The following imports are required. If these result in any errors, you probably don’t have the SDK libraries installed correctly.
It is crucial that you make your project implement Runnable so that we can have all Leap activity operate in its own thread. We begin by setting up the project main, an implementation of the Runnable interface and initializing of global variables we will be using later. The main things to take note of are the global variable “CHANNEL” as well as a pair of keys declared in main.
These strings are the only values that you will have to alter. The channel name can remain the default “leap2pi” or can be some other alpha-numeric value. Everything using PubNub for communication talks over a channel, so you want to use a unique name to prevent cross talk between various projects! The publish and subscribe keys are unique personal identifiers given to you by PubNub when you register for an account. Keep them safe. Keep them private. These keys prevent other people from talking on your channel(s). Take note, there might be a few places in the code that specifically use the string “leap2pi” instead of the constant “CHANNEL,” and all of these instances should be changed.
For more detail: IoT Motion Controlled Servos