cloudfridge2

The Internet of Things (IoT) is essentially the internet we already have, but now it’s not just humans using it — all kinds of devices are using it too. Today you can buy a washing machine that tweets when it’s done or an irrigation controller that checks the weather before watering.

Free data hosting services, and Arduino libraries for accessing them, make it easy and inexpensive to connect virtually any sensor to the cloud and read data back. These services store historical data, provide analytics tools, and reformat data streams so they’re easy for other devices to read.

The infrastructure is here for experimenting with your own IoT devices. So, what do we want to connect to the internet, and why?

Your fridge is an electricity hog, and every time someone opens the door, all the cold air slides out and the fridge has to cool it again. Opening the door is responsible for 7%–10% of the cost of running a fridge, burning about 28 watt-hours (Wh) every time, or 500 kilowatthours a year —$100 or more annually. And if you leave it open too long, the compressor runs continuously, using about 1kW per hour. Sure, you have to open the door to use the fridge, but perhaps if you know how much those midnight-snacks surveys cost, they won’t seem as appetizing.

This project connects your fridge door to the Xively data service using an Ethernet-connected Arduino and a magnetic sensor. It monitors how often the door is opened and for how long, and creates a persistent data archive you can use to detect usage trends. We chose Xively (formerly called Cosm, formerly Pachube) because it’s straightforward to use, it accurately time-stamps and archives the data, it’s free for small projects like this, and it was started by friends of ours. However, there are many other similar services such as Nimbits, Paraimpu, ThingSpeak, 2lemetry, sen.se, and ioBridge.

NOTE: Of course the circuit we describe in this project consumes energy too. The implementation shown were consumes about 200mA at 9V = 1.8W, or 16kWh per year. This power usage could be reduced 4x by updating less frequently and using CPU sleep modes. Using WiFi instead of Ethernet would also use less power.

Check out the Make Lab’s Cloudfridge feed at: https://xively.com/feeds/640418878

Project Steps

Set up your data feed.

Create an account at xively.com. Click on the Develop tab, then select Add Device. Enter the name and description for your CloudFridge device, choose the Private Device setting, and click Add Device.

Now you’re in the device development page. Take note of the Feed ID number at the top. Click on Add Channel to create the 2 data streams for your feed: “openCount” for the number of fridge openings and “openDuration” for the duration of openings. Click on Save Channel. Your feed is ready to accept data.

You also need an API key to authorize your device to update the feed. Click on the Add Key button. Give the key a name, check all 4 permissions checkboxes, and click Save Key. You’ll be shown a 48-character long string of gibberish. Make a note of it. This key and the feed ID are what you’ll need later.

Build the hardware.

The hardware for this project requires very little soldering. The wiring diagram is shown in the figures.

The door sensor is a magnetic reed switch, cannibalized from a $2 door/window alarm gadget. Pop open the case and solder a pair of wires across the glass tube that is the reed switch. Feed the wires through the power switch hole and snap the case back together.

Normally for switch inputs, you need a resistor to set the voltage when the switch isn’t pressed. Instead, we’ll use the Arduino’s internal pull-up resistor on the switch pin. You’ll see it enabled in the sketch with commands like:

pinMode(doorPin, INPUT);

digitalWrite(doorPin, HIGH); // turn on internal pullup resistor

Plug the Ethernet Shield into the Arduino and plug the reed switch wires into the Gnd and Pin 7 positions on the shield. For a visual indicator, plug a BlinkM or BlinkM MinM directly into analog pins A2–A5. BlinkMs speak the I2C serial protocol, which is normally shared with Arduino analog pins A4 and A5. Pins A2 and A3 form a “virtual power supply” for the BlinkM by setting A3 high and A2 low.

Load the Arduino sketch.

Download the Arduino sketch, and libraries for Xively settings and BlinkM communication, from http://cdn.makezine.com/make/36/cloudfridge_code.zip.

Open the sketch CloudFridge0.ino and click the XivelyDetails.h tab. Copy and paste in your Xively Feed ID and API key from Step 1 and save the file.

The sketch is based on the PachubeClientString example sketch in the Arduino Ethernet library, but uses DNS to connect to api.xively.com and uses DHCP to automatically get on your home network, instead of requiring you to pick a static IP. It also uses a rollover-proof way to handling the millis() function which wraps around to zero after 49 days.

Every 100 milliseconds (ms), it reads the door sensor and updates the data. Every 30 seconds, it sends accumulated door data to Xively. To alter these intervals, change the values of the doorUpdateMillis and postingInterval variables, respectively.

The 2 data values being sent are the variables doorOpenings and doorOpenMillis. To send these 2 values to Xively, they must first be turned into a string and that string’s length determined. (This is because Xively uses HTTP PUT requests, which require a Content-length header.) I used the sprintf() function:

char datastr[80];

doorOpenings=2;

doorUpdateMillis=13700;

sprintf(datastr, “%ld,%ld”, doorOpenings, doorOpenMillis);

Serial.println(datastr); // prints “2,13700”

to turn the variables into a list of comma-separated values (CSV). Xively accepts CSV, XML, or JSON, but CSV is easiest for this application.

Test it out.

Place a magnet on the reed switch to trigger the “door closed” state. Connect an Ethernet cable to your Arduino and your home’s router. Plug a USB cable between your Arduino and your computer and press Upload in the Arduino IDE. Open up the Arduino Serial Monitor and you’ll see a status message about getting on the Ethernet and a test-send of data to Xively.

If you now remove the magnet, the Arduino will print doorOpen! every 100ms until you put the magnet back. And after few seconds more, it will make a successful HTTP PUT request of the door data to Xively. Now the data is in Xively, ready to be examined and processed.

View Xively data.

Xively’s graphs give you a basic idea of what’s going on, but they’re geared towards sampling instead of aggregation. Sampling is OK for atmospheric sensors like temperature, but for accumulative data like our door openings, aggregation is better.

So it’s more instructive when debugging to look at the data in XML format using query URLs. Xively has a great API and the “Historical Queries” documentation is very useful here. For every feed you create, there’s an XML data URL available from your feed’s homepage. For the CloudFridge in the MAKE Labs, that URL is https://api.xively.com/v2/feeds/640418878.xml. By adding query arguments to this, we can choose what time periods and data samplings we want to see. The most important query argument is interval=0, which says to show all data. To list data from the recent past, take the current time as shown by your XML feed, subtract a few minutes from it, and make that your start= query argument. For example: the query URL https://api.xively.com/v2/feeds/640418878.xml?interval=0&start=2013-07-22T23:30:00Z produces the XML output in.

In that XML there’s a section for each datastream, and an ISO 8601 time-stamp for every datapoint at 30-second intervals. In this particular time-slice of the data for our fridge, you can see there were 2 openings, one for 7.5 seconds and another for 7.6 seconds.

Install it.

In the kitchen, a bare circuit board could get messy. Put it in any box you like. I drew up an enclosure in SketchUp that fits the Arduino with Ethernet shield and BlinkM and shared it at thingiverse.com/thing:21939. It has an optional lid that when snapped on, acts as a nice diffuser for the BlinkM. So now you have a glanceable indication of your CloudFridge’s state. Off means it’s just hanging out, blue means the door is open, and red means there was a network error.

If you don’t have an Ethernet hookup close to your fridge (Really, you don’t?), then you can use any old Ethernet-to-WiFi bridge you might have laying around, such as an Airport Express or Linksys WET11.

Test-fit the reed switch and magnet to your fridge door gap, with the door closed. The blue light should go out. Open your fridge and the blue light should go on. Affix the reed switch with double-stick foam tape or hot glue. The magnet should stick securely to your fridge door, but a bit of foam tape will hold it if not. Try your new CloudFridge out a few times, making sure the BlinkM lights up blue each time the door is opened.

TIP: If the magnet seems weak, replace it with a much stronger rare-earth magnet that works up to 1″ away.

Send alerts.

Xively has an alert mechanism that will send an HTTP POST to the URL of your choice when a specific condition is met. For instance, let’s declare that any time the fridge is open for more than 20 seconds, I’m obviously just standing there searching for a snack. Let’s send a tweet to publicly chastise me. An easy way is by using Zapier, a service that lets you automate tasks between web applications. Connecting a Xively trigger to Zapier, say when openDuration exceeds 20000, lets your Xively feeds send a tweet via Twitter (mine says, “Close the fridge, Tod!”), activate a call or SMS with Twilio, or influence any number of web apps.

This is just the beginning. Apply this same setup to your front door, pet door, air conditioner, or any other device that wants to be on the Internet of Things.

Conclusion

This is just the beginning of what you can do with the Internet of Things. Add a thermistor to monitor temperatures inside your fridge. Apply this same setup to your front door, pet door, or secret underground lair door. If your back door is left open too long for a nice summer breeze, use your net-connected Nest thermostat to command the air conditioner to switch off. If your CurrentCost electricity meter shows electricity prices going up, lower the alert thresholds on your triggers to chastise you sooner. Many of our devices — phones, laptops, DVRs — are already full members of the Internet of Things, and others will soon join them. Perhaps these new members will be things that you make.