Skip to content
Rachel edited this page Jun 22, 2017 · 7 revisions

Picture from Arduino.cc

Ethernet shields are useful because an ethernet connection is not as fickle as a WiFi one can be. Establishing an internet connection inside of a sketch is a bit different, but streaming to Initial State is basically the same.

The sketches arduino-ethernet-example.ino and arduinoethernetshieldstream.ino are almost identical, but the former uses HTTP GET while the latter uses HTTP POST. Since I already went over the HTTP POST method for the ESP8266 here, I'll be going over the arduino-ethernet-example.ino sketch.

This time we'll be using the HTTP GET method:

char server[] = "insecure-groker.initialstate.com";    // name address for Initial State (using DNS)

We must use the insecure Initial State endpoint because of HTTP limitations. This comes with a security warning, but can also be solved by turning a more powerful board (like a Pi) into a hub that handles the encryption the board couldn't.

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /api/events?accessKey=YOUR_ACCESS_KEY&bucketKey=SOME_BUCKET_KEY&streamKey=1 HTTP/1.1");
    client.println("Host: insecure-groker.initialstate.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

We connect to the Initial State server on port 80 then build our GET request. Your access key tells the API which account the data belongs to and the bucket key tells the API which data bucket the data should go to.

The GET request is in this format:

GET /api/events?accessKey=YOUR_ACCESS_KEY&bucketKey=SOME_BUCKET_KEY&streamKey=1 HTTP/1.1
Host: insecure-groker.initialstate.com
Connection: close

"streamKey=1" is the key-value pair we are streaming. You can add as many as you like by appending "&key=value" (substituting your own key name and value) to the end of the string (but before the HTTP/1.1).

And that's it! Easy right?

Now for using Ciao.

<< ESP8266 - Ciao Library >>

Clone this wiki locally