Hardware Reference
In-Depth Information
In our case, we need to format the data in the JSON format by including all measurements
in a field called datastreams . This is the result inside the Arduino sketch, where we
put everything in a string variable called data :
String data = "";
data = data + "\n" +
"{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" :
\"Temperature\",\"current_value\" : \"" +
String((int)temperature) + "\"}," + "{\"id\" :
\"Light\",\"current_value\" : \"" + String(lightLevel) +
"\"},"+ "{\"id\" : \"Humidity\",\"current_value\" : \"" +
String((int)humidity) + "\"}]}";
length = data.length();
From the preceding code, we also get the length of the data string, which will be used
when we transmit data to the Xively servers. Then, we can connect to the Xively server:
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
When the sketch is connected to Xively, we can send the data. This will be done by ex-
ecuting several print() functions using the client instance. To do so, we will use a
PUT request on the feed ID that we defined before, so the server knows we want to send
the JSON data. This is the result in terms of the code inside the Arduino sketch:
client.println("PUT /v2/feeds/" + String(feedID) + ".json
HTTP/1.1");
client.println("Host: api.xively.com");
client.println("X-ApiKey: " + String(API_key));
client.println("Content-Length: " + String(length));
client.print("Connection: close");
client.println();
client.print(data);
client.println();
After the data is sent, we need to read the answer coming from the Xively server before
we can continue:
while (client.available()) {
char c = client.read();
Search WWH ::




Custom Search