Week 12 - Interface and Application Programming
An Interactive Arduino Programming EnvironmentThis week we were supposed to create an interface controlling an input/output device. While writing apps is nice, writing a (micro) framework is nicer!
I wanted to find a way in which I could interactively program a microcontroller just like you do with interpreted languages (rather than compiling and uploading to the device again and again). I also wanted to do it wirelessly - to create a real IoT-like programmable device that could be programmed even if it's inaccessible physically.
I'm also a big fan of data analysis, so naturally I showed an example of programming the board using my favorite toolchain - IPython Notebook, numpy and matplotlib.
Here's a video showing how I wirelessly programmed an Arduino to control a servo and show a graph using the HC-05 Bluetooth module we have in class
In order to allow interactive programming, I had to upload some code to the board that acts as an interpreter. I got started with this module online: http://n.mtng.org/ele/arduino/iarduino.html. After understanding how the code works, I made some significant changes that have made it more useful - like enabling Software Serial communication, allowing to upload the code into the board, etc ...
The next step was to upload the interpreter sketch. Simply put, it works as follows: it piggybacks the Arduino event loop, listening to serial commands that correspond to the native Arduino environment functions. Once such an input is received, it translates it into the actual compiled function.
I then checked via serial that the board responds to programmable commands.
After I got it working, I wrote a python wrapper that mimics as closely as possible the Arduino IDE environment.
The next step was writing an IPython Notebook to program and interface with the board, as well as to generate some cool plots. The static version could be found here: IPython Notebook Example
Once this worked, I decided to connect the HC-05 Bluetooth module to the board.
I then changed the communication port (using the library I developed) to the Bluetooth port. I added a Servo (courtesy of Dan), and wrote a program that randomly changes the position of the servo every 0.5s, and plots a position plot as a function of time using matplotlib. Here's the final output of a sample iteration:
Essentially, this is a pretty useful proof-of-concept that I'll probably use in the future, and will be happy to see others extend.
To use it yourself, check out the tutorial and code here: https://github.com/guyz/interactive-arduino-programming.