I wanted to see if it would be possible to create a makeshift remote sensing device using Wolfram on the Raspberry Pi. Toss in VNC and some earlier code that I’ve written and here’s what I came up with.
It seems like the Internet of Things and remote sensing are all the rage right now, and there exists a healthy number of products to make web-based remote sensing devices. Since the new version of the Raspberry Pi makes it actually possible to use Mathematica’s GUI interface, I thought this would be an interesting little project to test how Mathematica could be used in a remote sensing IOT type environment.
First, the sensor is a simple photoresistor connected to an MCP3008. As is true with most of my projects, I rely heavily on the instructional pages at learning.adafruit.com . Additionally, I am using my wiringPiLink code, which is available at Github, to call the WiringPi functions from within Mathematica.
Wolfram introduced gauges in version 10 of Mathematica, so I thought I’d start there with the interface.
AngularGauge accepts a value and a range. Since the MCP3008 is a 10-bit ADC, the range is 0 to 1023. We can make the gauge dynamic by applying
Dynamic to the value inside the AngularGauge function:
Now, to update the gauge value
goutput I use a scheduled task, which reads the value of the MCP3008 every 50 ms.
gtask = CreateScheduledTask[goutput = wiringPiAnalogRead,0.05];];
In the interface, I’ve attached these operations to buttons, but I’ll ignore that point in this post for the sake of simplicity.
Next, we have the source tab. There’s nothing overly special here. The on/off buttons turn the LED on and off (I have the LED connected to wiringPi pin 1) and the sliders set the parameters that can be sent to wiringPiFlicker to make a candle-light-like LED response.
I had to spend a little bit of time on the Collect tab in order to deal with some responsivity issues. Attempting to put a dynamic plot within a
Manipulate resulted in some serious lag, so I created my own interface with a
Slider and appropriate dynamic symbols. I also decided not to update the plot in “real time” since plot updating was too slow for my taste. It is nice to see that the
ProgressBar feature works relatively smoothly. In a future version of this project I’d like to fix this bar so that it becomes greyed out if the user changes the slider. I also have some x-axis tick-mark overlap that needs tweaking.
The last part of the interface is the Analyze tab. This part of the interace is inspired by another project (currently on the back burner) dealing with simulation of candle light flicker. A histogram of the light intensities read by the sensor is plotted and these data are fit to a normal distribtuion. The p-value of the result is displayed along with a visualization of the result.
The various parts of the interface are tied together using
I decided to make the sensor interface remote by using a VNC connection. This wouldn’t be an ideal solution if privacy and security were of concern, but all of this work is done behind my firewall so I’m not going to worry about it. I didn’t do anything special beyond setting up the VNC server on the RPi and getting the VNC app for my iPod touch. Making the geometry 660×440 (1.5:1) made the display fit nicely on the iPod’s screen.
here’s a video of the remote sensor in action.
I’m pleasantly surprised that this project did not require any serious modifications to solutions that can be readily found on the web already.
If you’d like,
here is the notebook (Looking for it – I lost the original in my website crash) that creates the interface. There are two bugs that I am aware of and haven’t messed with yet. The first is on the Collect tab. The first time a collection is run, no data is actually collected. I am unsure as to the source of this bug; however I have noted that I cannot reproduce this error when I use the Windows version of Mathematica, so I suspect there is something subtle with the RPi implementation. Just collect data a second time and move on. The second bug is in the Analyze tab. I don’t do a good job initializing the statistics values, and therefore the first time the interface shows up, the plot shows an error. Simply click the update button and you’ll get the expected behavior from here on out.