a digital scan of a 35mm film image of a processing sketch running on an LCD
Skip to Content

Real-time Sky

For the com lab final, I decided to revisit the blog-improvement assignment — might have been low hanging fruit given my prior experience on these fronts, but I wanted to try something new: tapping real-time sensor data to determine the aesthetic properties of my website.

I spend such an inordinate amount of time in front of screens, that bringing in a bit of the outside world onto said screens seemed like a nice thing to do (if just a bit sad).

So I decided to set up a camera pointed at the sky, 24 / 7, and use the average sky color to set the background on this website.

First, I needed a camera. I scrounged up an old webcam, and put it in a small tupperware container since it was going to have to brave the elements on my windowsill.

Camera in tupperware enclosureCamera in tupperware enclosure with lid

Next up was how to get data from the camera to the web. I just found out about a website called Pachube (confoundingly pronounced "patch bay") which provides a platform for the hosting and exchange of small bits of sensor data — ideal for this application. So I set up an input feed on Pachube to receive and store the sky color values.

I whipped up a Processing application to read from an old webcam, average the pixel values, and then send the value up to Pachube every minute. (The EEML Library for Processing made interfacing with Pachube completely painless.)

Getting the data out of Pachube and into my Drupal Blog needed another piece of glue code: the Pachube PHP Library. I created a Drupal module to wrap up the library, and another small module to query their servers, grab the current color value, pass it on to a bit of JavaScript, which then set the background color via jQuery.

Right now the background only updates on refresh (since the data changes quite slowly) but it wouldn't be much more work to set up a timer and some AJAX to fetch the latest value at a regular interval.

With the software working...

Sky camera monitoring software

And the camera situated...

Sky camera in windowsill

I was ready for the data to start coming in. My webcam had other ideas, though. It turns out that even with auto-exposure and auto-gain turned on, the sensor doesn't have enough dynamic range to capture the sky with any kind of accuracy. The sky is just too bright during the day, and too dark at night. There are a few interesting color values at dawn and dusk, but otherwise it's either solid black or solid white.

Here's a look at tonight's sunet (the top of the gradient is about 6:00 PM ET, the bottom is 2:00 PM ET (the sun officially set at 4:30). There's about an hour of meaningful color data between the blown-out day and solid-black night. I'm investigating more sensitive camera options, in hopes of getting meaningful values for more of the day.

4 Hours of sky cam data

Here's the Processing code. It's a bit verbose, since I entertained a few what-if scenarios and drew out some portions for legibility's sake. The low frame rate helps keep CPU usage (and therefore CPU temperature and fan noise) within the realm of rationality.

  1. // Sky color logger.
  2.  
  3. // Takes a photograph of the sky every second, then finds and stores the average color value.
  4. // Each minute, average the one-second averages to find the average sky color over the last minute.
  5. // Upload the result to Pachube (http://www.pachube.com/) as a hexadecimal color value.
  6. // Built with the EEML Library for Processing, available at http://www.eeml.org/library/
  7. import processing.video.*;
  8. import eeml.*;
  9.  
  10. DataOut dataOut;
  11. Capture cam;
  12.  
  13. int pixelCount;
  14. color averageColor;
  15. color minuteAverageColor;
  16. int lastMinute;
  17.  
  18. int minuteAverageRed = 0;
  19. int minuteAverageGreen = 0;
  20. int minuteAverageBlue = 0;
  21. int averageRed = 0;
  22. int averageGreen = 0;
  23. int averageBlue = 0;
  24. int sampleCount = 0;
  25.  
  26. int[] averages; // Stores recent averages.
  27.  
  28. void setup() {
  29.  size(640, 480);
  30.  noStroke();
  31.  frameRate(2);
  32.  
  33.  // Set up communication with Pachube.
  34.  String feedURL = "http://www.pachube.com/api/feeds/3929.xml";
  35.  String apiKey = "YOUR PACHUBE API KEY HERE";
  36.  dataOut = new DataOut(this, feedURL, apiKey);
  37.  dataOut.addData(0, "sky,color");
  38.  
  39.  // Set up the camera.
  40.  String[] devices = Capture.list();
  41.  println(devices);
  42.  cam = new Capture(this, 320, 240, devices[1]);
  43.  cam.frameRate(1); // Read 1 frame per second.
  44.  pixelCount = cam.width * cam.height;
  45.  
  46.  // Take note of the time. We will upload once per minute.
  47.  lastMinute = minute();
  48.  
  49.  // Store recent averages in an array.
  50.  averages = new int[240];
  51.  
  52.  // Fill the averages array with white to start
  53.  for(int i = 0; i < averages.length; i++) averages[i] = 255;
  54. }
  55.  
  56. void draw() {
  57.  background(0);
  58.  
  59.  // Show the live camera feed in the top left.
  60.  fill(0);
  61.  image(cam, 0, 0);
  62.  
  63.  // Show the latest average color in the top right.
  64.  fill(averageColor);
  65.  rect(320, 0, 320, 240);
  66.  
  67.  // Show the last minute average in the bottom left.
  68.  // This is what was actually sent to Pachube.
  69.  fill(minuteAverageColor);
  70.  rect(0, 240, 320, 240);
  71.  
  72.  // Draw the recent averages as horizontal lines in the bottom right.
  73.  // The latest values are pushed onto the top of the stack.
  74.  for(int i = 0; i < averages.length; i++) {
  75.    stroke(averages[i]);
  76.    strokeWeight(1);
  77.    line(320, 240 + i, 640, 240 + i);
  78.  }
  79.  noStroke();
  80.  
  81.  // Aggregate the averages and send to Pachube every minute.
  82.  if((minute() != lastMinute) && (sampleCount > 0)) {
  83.  
  84.    // Find the aggregated average for each minute.
  85.    minuteAverageRed = minuteAverageRed / sampleCount;
  86.    minuteAverageGreen = minuteAverageGreen / sampleCount;
  87.    minuteAverageBlue = minuteAverageBlue / sampleCount;
  88.  
  89.    minuteAverageColor = color(minuteAverageRed, minuteAverageGreen, minuteAverageBlue);
  90.  
  91.    // Convert to hex and send to Pachube.
  92.    println("Sending sky value " + hex(minuteAverageColor, 6) + " to Pachube.");
  93.    dataOut.update(0, hex(minuteAverageColor, 6));
  94.    int response = dataOut.updatePachube();
  95.    println("Pachube response: " + response); // Should be 200 if successful; 401 if unauthorized; 404 if feed doesn't exist.
  96.  
  97.    // Shift values in the averages array over one, and then add the latest to the beginning.
  98.    for(int i = averages.length - 1; i > 0; i--) averages[i] = averages[i - 1];
  99.    averages[0] = minuteAverageColor;
  100.  
  101.    // Reset the minute averages.
  102.    minuteAverageRed = 0;
  103.    minuteAverageGreen = 0;
  104.    minuteAverageBlue = 0;
  105.  
  106.    // Reset the sample count.
  107.    sampleCount = 0;
  108.  
  109.    // Reset the minute mark.
  110.    lastMinute = minute();
  111.  }
  112. }
  113.  
  114. void captureEvent(Capture cam) {
  115.  // Get the latest camera data.
  116.  cam.read();
  117.  cam.loadPixels();
  118.  
  119.  // Reset the averages for this frame.
  120.  averageRed = 0;
  121.  averageGreen = 0;
  122.  averageBlue = 0;
  123.  
  124.  // Average the pixels.
  125.  int pixelColor;
  126.  
  127.  for (int i = 0; i < pixelCount; i++) {
  128.    pixelColor = cam.pixels[i];
  129.  
  130.    // Extract the RGB values.
  131.    averageRed += (pixelColor >> 16) & 0xff;
  132.    averageGreen += (pixelColor >> 8) & 0xff;
  133.    averageBlue += pixelColor & 0xff;
  134.  }
  135.  
  136.  // Take the average.
  137.  averageRed = averageRed / pixelCount;
  138.  averageGreen = averageGreen / pixelCount;
  139.  averageBlue = averageBlue / pixelCount;
  140.  
  141.  // Note this average for the aggregated average taken at the minute mark.
  142.  // It would be simpler not to store every single frame's average, and instead to average
  143.  // a minute's worth of pixel data (e.g. average = averageChannel / (pixelCount * sampleCount))
  144.  // but for the sake of the display it's useful to have average data from the latest frame as well.
  145.  minuteAverageRed += averageRed;
  146.  minuteAverageGreen += averageGreen;
  147.  minuteAverageBlue += averageBlue;
  148.  
  149.  averageColor = color(averageRed, averageGreen, averageBlue);
  150.  
  151.  // Iterate the sample count... this is what we use to take the average
  152.  // at the minute mark. With the camera at 1 FPS, it should always be 60, but
  153.  // in case of timing issues it's worth keeping track.
  154.  sampleCount++;
  155. }

December 14 2009 at 2 AM

Its a great idea. if you ever revisit this, try adding a neutral density filter in front of the webcam. Maybe one that stops down the exposure value several values, like 6 or 8.
Another idea is to point the camera north-facing to avoid the sunny side of the sky most of the year.

February 18 2011 at 3 PM

Add Your Comment