This week we do step 1 of our on-air light.  We make the arduino check the status of the live Ustream channel.

To do this we use an ethernet shield to poll the Ustream API to check the status of our strem.  We learn how to setup the ethernet shield and then poll a web server.

We also introduce you to JSON and a new JSON Library in this episode of Let’s Make It.

Downloads

HD Apple HD Apple SD Audio MP3 Android

Checking the Ustream Stream

/*############################ CHECKING THE USTREAM FEED ################################
       This sketch will read a stream status from ustream to determine if the
       stream is up "live" or down.
      
       It controls pin 13 to light an LED if the stream is live.

 
   From: Mike Myers (http://mikemyers.me)  @netnutmike
       Let's Make It Episode 6 (http://tech-zen.tv/index.php/shows/let-s-make-it/episodes/59-sensor-fun-with-arduino-1-massive-failure-but-4-successes-let-s-make-it-episode-6)
      
       http://tech-zen.tv
      
       For the sample code, show notes, contact information and many more
       videos, visit the show page at http://tech-zen.tv/letsmakeit

       Please subscribe to our YouTube channel or our netcasts at any of
       your favorite netcast / podcast outlets.

       We normally record Let's Make It live on Tuesday evenings around
       7pm eastern. You can watch it live by going to tech-zen.tv and clicking
       the live link at the top of the page.

       We also have a community setup for our viewers and listeners. You can
       join the community by going to community.tech-zen.tv.

       We love input on what you would like to know or if you have an idea for
       a new Let's Make it episode, you can contact us via email or phone at
       the show information page.
################################################################################*/

#include <SPI.h>
#include <Ethernet.h>
#include <aJSON.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(199,66,238,68); // Ustream
int controlPin = 13;

int reading=false;
int chpos = 0;
char rtnchar[500];
int islive;


// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  int weconnected;
 
  // Setup Control Pin
  pinMode(controlPin, OUTPUT);
 
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  do {
  // start the Ethernet connection:
    Serial.println("Trying to get IP Address");
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP, Trying Again in 1 minute");
      weconnected = false;
      delay(60000);
  
    } else {
      Serial.println(Ethernet.localIP());
      weconnected = true;
    }
  } while (!weconnected);
 
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

 
}

void loop()
{
  checkStream();
  delay(60000);
}

void checkStream()
{
  reading=false;
  chpos=0;
  aJsonObject* root;
 
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /json/channel/tech-zen-tv/getValueOf/status HTTP/1.0");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
    digitalWrite(controlPin, LOW);
  }
 
  while (client.connected()) {
    // if there are incoming bytes available
    // from the server, read them and print them:
    if (client.available()) {
      char c = client.read();
      if (c == '{') {
        reading=true;
        }
     
      if (reading) {
        rtnchar[chpos] = c;
        ++chpos;
        //Serial.print(chpos);
        Serial.print(c);
        }
      }
    }
   
  rtnchar[chpos] = '\0';
  islive = false;
  Serial.println();

  root = aJson.parse(rtnchar);

  if (root != NULL) {
      //Serial.println("Parsed successfully 1 " );
      aJsonObject* query = aJson.getObjectItem(root, "results");

       if (strcmp(query->valuestring, "live") == 0) {
         islive = true;
         }
       
        if (islive==true) {
          Serial.println("It's Live");
          digitalWrite(controlPin, HIGH);
        } else {
          Serial.println("No Stream");
          digitalWrite(controlPin, LOW);
        }
  }
 
  root=NULL;
  Serial.println("disconnecting.");
  Serial.println();
 
  client.stop();
   
   
}