Introduction: Turn the Rapiro Into an IoT Device With Eclipse Orion and MQTT
I finally found time to play with my Rapiro robot (thanks again Element14 for accepting me for this RoadTest!).
You can have a look at the review I wrote, but in a nutshell, after having figured out a few hardware issues, I was essentially ready to start hacking and code what I had in mind: I wanted my Rapiro to become an IoT (Internet of Things) device, that I could control for virtually anywhere in the world!
Meet Rapiro. Rapiro is a nice little toy, that comes in a kit. It is, as you've guessed, a robot!
The robot itself consists of 12 servos (3 in each arm, 2 in each leg, 1 for the head and 1 for the waist), enabling a wide range of movements. Not-so-wide movements actually, the robots' limb are very short!
A Raspberry Pi, in the head of the robot, allows
A PiCam, a 5Mpix camera extension to the Raspberry Pi, allows to take pictures, records videos, ...
A USB WiFi dongle allows to control the robot completely wirelessly
Fusion PCB service from Seeedstudio( www.seeedstudio.com/fusion.html )
I won't detail how to assemble the Rapiro, neither will I explain how to get your Raspberry Pi setup with a Linux distro since there are plenty of resources on the Internet. The fun part actually starts after you have finished assembling the Rapiro, and when the Raspberry Pi is properly running, allowing you to open a remote SSH connection to start hacking the beast! The goal is very simple: my Rapiro is able to get Internet connectivity, thanks to its Raspberry Pi and WiFi dongle, so I want to make it controllable remotely. To that effect, I am going to use MQTT, a very lightweight protocol well-suited for Internet of Things communications, to send commands to the robot. Thanks to the PiCam, the robot should also, when asked to do so, take a picture and transmit it. I want to be able to open a browser from wherever in the world, and start controlling my Rapiro (because why not, eh? ), so I will develop a simple web UI to send the actual MQTT commands.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Install Eclipse Orion
Eclipse Orion is an IDE that you can run from your favorite web browser. Once you have Orion running on a device (here it's going to be on the Raspberry Pi attached to my Rapiro, obviously), all you really need is a browser for writing.
We are going to run Orion on top of Node.js so we are going to need Node.js first. + expand sourceview plain
cd wget http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-arm-pi.tar.gz tar -xvzf node-v0.10.26-linux-arm-pi.tar.gz sudo mv node-v0.10.26-linux-arm-pi /opt/node/ In order to have the node and npm executables in your PATH, you can edit the /etc/profile file and add the following lines: + expand sourceview plain
NODE_JS_HOME=/opt/node" PATH="$PATH:$NODE_JS_HOME/bin" export PATH Great! Node is now correctly installed, and we can use the standard package manager (npm) to install and start Orion. + expand sourceview plain
npm install orion npm start orion You should see an output similar to this: + expand sourceview plain
> firstname.lastname@example.org start /home/pi/node_modules/orion > node server.js Using workspace: /home/pi/node_modules/orion/.workspace Listening on port 8081... And if you open your favorite web browser (http://ip-address-of-your-raspberrypi:8081), you'll see that Orion is actually running – congrats!
So what's next? Orion is going to help us develop a very simple application that will expose Rapiro's Arduino-based controller, to the Internet.
Step 2: Allow the Rapiro to Be Controlled From Anywhere With MQTT
Rapiro built-in serial commands
By default, the Rapiro is meant to be controlled over a serial connection. Basically, the servo-motors are all attached to an Arduino board, onto which runs a program that listens to commands on the serial interface. These commands can be either built-in, like #M1 to start walking or #M5 to wave hands, or more complex sequences, like #PR000G255B000T010 to set the eyes LEDs to Green (R=0, G=255, B=0), in 10 units of time (T=10). The Rapiro's head is home for its second brain, in addition to the Arduino controller: a RaspberryPi. It is powered by the same battery pack than the robot, and connected to the Arduino's serial line meaning that I can actually send serial commands from virtually any program that I'd run on the Raspberry Pi. As a very first test, you can ask the Rapiro to start walking from the command line of your Raspberry Pi: + expand sourceview plain
echo "#M1" | sudo minicom -b 57600 -o -D /dev/ttyAMA0 So now the goal is to use Orion to write a simple app that will use MQTT to listen to commands that a web UI is going to send, and forward these commands to the Arduino controller over the serial port. In Orion, we're going to create a new project (File > New > Project > Basic).
And in this project is going to be our main application, so let's create a file called e.g. main.js.
+ expand sourceview plain
Step 3: Web UI
Here the idea is pretty straightforward. I want a dead simple web UI that will allow me to publish MQTT messages, and receive base64-encoded images that I can display in my page.
When actually using the Web UI, granted that the NodeJS app is still running on the Raspberry Pi, the Rapiro will start doing what you're asking it too, and obviously you can use the Orion shell output to troubleshoot what's happening.