This summer several colleagues and I had some seed money to brainstorm ways to put more sustainability ideas into our curriculum (physics, chemistry, and math was represented). One of the projects we decided to flesh out is having students develop arduino hardware and software to collect data about our campus that would help with energy management. We followed some online tutorials though we decided to have the arduino simply send its data to a web page every 5 minutes instead of having it act as a server that you could go to with your browser. The idea is that the arduino will record its probe data every 5 minutes (say), hook up to the internet, and send the data to a simple url like cooldatabasesite/probe1=X&probe2=Y. We did the initial testing with a Wolfram Data Drop but later decided to just make our own database to collect the information. I wanted to write this to get the gist of the project down and to possibly see if our database could be of use to others.
With some more internal funds we now have enough Arduino Uno’s, CC3000 wifi boards, and DHT 22 temperature/humidity probes for my whole class this fall (13 students). The total cost was something like $1200.
I based the code on two examples that come once you download the cc3000 library from Adafruit. The “build test” example checks the functionality of the board, and, importantly for how we wanted use it, sends the MAC address of the board back along the serial line to the computer. Then the “web client” example is what I made slight adjustments to in order to get it to work with our database. Really you’re just having it connect to a simple web page (with a GET request) and then having it read the result and then shut down. There’s a lot of code to help with connecting to the wifi but it’s pretty straightforward. At my institution the main wifi signal uses WPA2-EAP (I think) for security (the kind where you put in a username and password in the wifi configuration on your computer as opposed to the kind where you put your username and password into a browser page) and the cc3000 can’t handle that. I thought we were out of luck but one forum I read suggested seeing if my ITS department would be willing to just store the MAC address of the boards and allow them on the network without all the certification stuff. It turns out they were willing to do that! It’s on the “guest” wifi signal as opposed to the main one but it seems to work great. (Note the comment above about being able to have the arduino print out the MAC address with the initial test).
So you load the cc3000 library, run the “build test,” grab the MAC address, send it to ITS, then run a doctored version of “web client” with the appropriate wifi settings and the correct url for the database (and collecting the appropriate probe data).
One major headache that happened was the arduino would hang every few hours or so. It gets stuck in a loop and won’t do anything. Hitting the reset button fixes it, but that’s a pain for a machine placed somewhere on campus. Luckily there’s a very cool “watchdog” library for arduino that allows you to program the machine to reset if it goes into an infinite loop. Unfortunately the longest you can have it do something before it’s decided that it’s stuck is 8 seconds using the default watchdog approach. That sucks because the whole “connect to the wifi, send the info, parse the result” takes something like 24 seconds. Luckily, I found a very cool work around that allows you to multiply that 8 seconds by any integer you’d like. It uses interrupts on the watchdog and is very slick. Check it out. So now if it hangs in the wifi hookup process, it just resets itself and continues right along. If I look at my data I see usually one or two gaps of 6 minutes instead of 5 minutes between my data points every day. Not too bad.
I wanted to build a pretty flexible database for all this data. I wanted authorized uses to be able to add new machines (register the MAC address), attach probes to it (so the system could make sure that the type of data submitted was of the right sort), and describe the location it’s in. I also wanted to have a place to store the arduino code on the machine so that people could check it to understand any calibration issues. I’ve got nearly everything done:
- Admin section
- Approve uses
- Edit machines (location, attached probes)
- Add/edit probes (what’s available to be attached to machines)
- Add machine
- submit MAC address (right now the format is just decimal without colons but possibly changeable)
- attach probes
- put in location
- short name
- GPS coordinate (I haven’t standardized these yet but I think it should be possible)
- Accept measurements
- where Y and Z are ids of the probes in the system
- Display data
- You see a list of locations
- Then see a list of available measurements
- Then see a plotly.js-driven interactive plot of the data
- below which is a list of downloadable formats
- html table
This was the first time I programmed an application using Test Driven Development and I really liked it. I now have a suite of tests (over 50) that test every aspect of the application. That way if I make a change I’ll know if I’ve screwed something else up. Basically it’s a way to test all your web pages and user-clickable events to make sure they’re all still working. In the old days I’d just make a change, make sure the new thing I was working on was working and only find out days later that I screwed something else up. Very cool.
You can see the working database here.
I’m pretty excited about what we can do with that. We can have machines all over campus submitting data that we can really put to use. I’m also excited because I think I could easily handle other people’s data as well. If you’ve got a wifi-enabled arduino, just register on the site (and maybe send me a message), I can approve you and decide if you should be an admin or what and then you’d have access to your data. Alternatively people could feel free to fork my application and just host it on their own server.
By the way, there’s also a Plotly approach having arduino stream data to a graph if that’s all you’re interested in.
In the old days I’d just make up some dumb thing for my students to work on when learning how to work with an Arduino (make a working stoplight circuit, move a motor at a speed determined by the temperature, etc). What I’m excited about with this project is that they’ll be learning many of the same skills while adding to a larger university-wide project that other people care about. I hope that helps motivate them.
Working with my math colleague has been fun because he has his students deal with data sets in his statistics courses. Now he’s excited to use this data instead with really very little change to the learning outcomes. Again motivating the students to come up with hypotheses that could be tested using our data should be easier with this.
The other thing I’m excited to do in this upcoming class is to encourage the students to engage with community members. My hope is to identify several potential “clients” that they can go talk to and figure out what kind of data they want logged and why. It’s a math methods class for physics majors so maybe they can do some modeling of the system being measured. Not sure about that (though simple heat flow should be pretty easy) but it’s certainly possible.
So I’m pretty excited. Your thoughts? Here are some starters for you:
- This is great! Something else you could do is . . .
- This is dumb. Haven’t you seen . . .
- Why didn’t you post your Arduino code here?! It’s almost as if you’re typing this on a chromebook at home because you left your development computer at work.
- Wait, let me get this straight: you abandoned a solution based on a Wolfram product? Who are you and what happened to SuperFly?
- It seems like you’re doing application testing, not unit testing. Did you do any unit testing?
- What code do you use for the database? (It’s a Laravel/PHP/MySQL stack – I really like coding in the Laravel framework)
- Why don’t you use a Raspberry Pi for this?
- Wait, 15 machines every 5 minutes is 3 million data points per year. Can your system handle that and expand beyond it?
- Could you have a machine take rapid data over a short period and then repeat that daily?
- Why are you so inconsistent with your capitalization of aRdUiNo?
- Why are you doing all of this in a math methods class (the actual name is Mathematical and Computational Methods for Physicists)