The Chicken Cam

Chicken

@jmhobbs

I make a website for dogs.

Pack

packdog.com / velvetcache.org / whatcheer.com

I'm also starting a tiny farm.

Buttercup Farm

/buttercupfarmomaha    @buttercupfarmomaha

I have no idea what I'm doing.

The Goal

Watch chickens online.

Because, chickens.

The Supplies

Raspberry Pi Webcam 21 Chickens

The problem with true livestreaming.

Speed Test (Slow)

Let's Roll Our Own!

Flowchart

Node.js

  • Single Threaded
  • Easy HTTP
  • Why not?

Fetch JPEG

var http_callback = function(response) {
  var img = '';
 
  response.setEncoding('binary');
 
  response.on('data', function (chunk) { img += chunk; });
 
  response.on('end', function () {
    latest_image = img;
    latest_image_timestamp = +(new Date());
    setTimeout(updateImage, REFRESH_INTERVAL);
  });
};
 
function updateImage () {
  if(( +(new Date()) - last_request ) > PAUSE_TIMEOUT) {
    setTimeout(updateImage, PAUSE_INTERVAL);
  }
  else {
    http
      .request(http_options, http_callback)
      .on('error', function (e) {
        setTimeout(updateImage, BACKOFF_INTERVAL);
      })
      .end();
  }
}

Serve Image


var server = http.createServer(function (request, response) {
  if( DEBUG ) { console.log('Incoming Request:', request.url); }

  if(request.url === '/') {
    // ...
  }
  else if (request.url.substr(0, 6) === '/image') {
    last_request = +(new Date());
    frames_served++;
    response.writeHead(200, {"Content-Type": "image/jpeg"});
    response.end(latest_image, 'binary');
  }
  else {
    response.writeHead(404, {'Content-Type': 'text/plain'});
    response.end('404 - Not Found');
  }
});

Front End


$chicken_cam.on('load', function () {
  $('#error-message, #sad').hide();
  $chicken_cam.show();
  setTimeout(update, {{UPDATE_INTERVAL}});
});

$chicken_cam.on('error', function () {
  $('#error-message, #sad').show();
  $chicken_cam.hide();
  setTimeout(update, {{ERROR_RETRY_TIMEOUT}});
});

function update () {
  $chicken_cam.attr('src', '/image?t=' + (+new Date()));
}

update();

DEMO

http://chicken-cam.herokuapp.com/

Improvements

socket.io

Error Handling

https://github.com/jmhobbs/chicken-cam

Questions?

Chicken Madness