Skip to content

Commit

Permalink
Add example and improve README
Browse files Browse the repository at this point in the history
  • Loading branch information
imrehorvath committed Jan 1, 2017
1 parent 027a67b commit 6ccff74
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 15 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,54 @@ Features
* Built on top of the Bridge library
* Uses cURL on the Linux side

Examples
--------

__List the names and Ids of your Adafruit IO Feeds__

```c++
BridgeHttpClient client;

// Add request headers
// REPLACE THE XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX WITH YOUR AIO KEY!!!
client.addHeader("X-AIO-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
client.addHeader("Accept: application/json");

// Using HTTPS and peer cert. will not be able to auth.
client.enableInsecure();

// Adafruit IO REST API call
client.get("https://io.adafruit.com/api/feeds");

// Collect the response body into this string for parsing
String response;

while (client.available() > 0) {
char c = client.read();
response += c;
}

// Parse the list of feeds and print the name and ids, limited to 4 feeds
const int JSON_BUFFER = JSON_ARRAY_SIZE(4) + 4*JSON_OBJECT_SIZE(14);
StaticJsonBuffer<JSON_BUFFER> jsonBuffer;

JsonArray& array = jsonBuffer.parseArray(response);
if (!array.success()) {
SerialUSB.println("parseArray() failed");
while (1) {}
}

// List the feed names and Ids
SerialUSB.println("Your Adafruit IO Feed Name/Id listing:");
for (JsonArray::iterator it=array.begin(); it!=array.end(); ++it) {
JsonObject& feed = it->as<JsonObject&>();
feed["name"].printTo(SerialUSB);
SerialUSB.print("/");
feed["id"].printTo(SerialUSB);
SerialUSB.println();
}
```
Usage
-----
Expand Down Expand Up @@ -116,6 +164,16 @@ if (client.getResponseHeaderValue("Server", server)) {
}
```

__HTTP response body__

```c++
SerialUSB.println("Response Body:");
while (client.available() > 0) {
char c = client.read();
SerialUSB.print(c);
}
```

TODO
----

Expand Down
80 changes: 80 additions & 0 deletions examples/AioFeedNameId/AioFeedNameId.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Adafruit AIO REST API example
What it does: Lists your Adafruit IO Feed Name/Ids using the REST API.
Please note that this sketch is Yun-specific!
Works only with Yun and its clones like the Seeeduino Cloud, but not with
an Uno or other Arduinos.
Dependencies: ArduinoJson library
Usage:
1. Make sure you've installed the ArduinoJson library in the Library Manager
2. Replace the placeholder XXXs below with your actual AIO KEY
3. Upload the sketch using a USB cable.
4. Wait for the RED LED to light up on the board.
5. Open the serial monitor in the Arduino IDE.
Written by Imre Horvath, 2017
*/

#include <Bridge.h>
#include <BridgeHttpClient.h>

#include <ArduinoJson.h>

void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Bridge.begin(); // Initialize Bridge
digitalWrite(13, HIGH);

SerialUSB.begin(9600);
while (!SerialUSB); // wait for the serial connection

BridgeHttpClient client;

// Add request headers
// REPLACE THE XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX WITH YOUR AIO KEY!!!
client.addHeader("X-AIO-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
client.addHeader("Accept: application/json");

// Using HTTPS and peer cert. will not be able to auth.
client.enableInsecure();

// Adafruit IO REST API call
client.get("https://io.adafruit.com/api/feeds");

// Collect the response body into this string for parsing
String response;

while (client.available() > 0) {
char c = client.read();
response += c;
}

// Parse the list of feeds and print the name and ids, limited to 4 feeds
const int JSON_BUFFER = JSON_ARRAY_SIZE(4) + 4*JSON_OBJECT_SIZE(14);
StaticJsonBuffer<JSON_BUFFER> jsonBuffer;

JsonArray& array = jsonBuffer.parseArray(response);
if (!array.success()) {
SerialUSB.println("parseArray() failed");
while (1) {}
}

// List the feed names and Ids
SerialUSB.println("Your Adafruit IO Feed Name/Id listing:");
for (JsonArray::iterator it=array.begin(); it!=array.end(); ++it) {
JsonObject& feed = it->as<JsonObject&>();
feed["name"].printTo(SerialUSB);
SerialUSB.print("/");
feed["id"].printTo(SerialUSB);
SerialUSB.println();
}
}

void loop() {
// Do nothing
}
17 changes: 3 additions & 14 deletions examples/AioFeeds/AioFeeds.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ void setup() {
// Collect the response body into this string for parsing
String response;

// Dump the response body
SerialUSB.println("Response Body:");
// Collect the response body
while (client.available() > 0) {
char c = client.read();
response += c;
SerialUSB.print(c);
}

// Print the HTTP response code
Expand All @@ -71,17 +69,8 @@ void setup() {
while (1) {}
}

for (JsonArray::iterator it = array.begin(); it != array.end(); ++it) {
JsonObject& feed = *it;

SerialUSB.print("Feed name: ");
feed["name"].printTo(SerialUSB);
SerialUSB.println();

SerialUSB.print("Feed id: ");
feed["id"].printTo(SerialUSB);
SerialUSB.println();
}
// Pretty print the JSON to the serial console
array.prettyPrintTo(SerialUSB);
}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=BridgeHttpClient
version=3.0.0
version=3.0.3
author=Imre Horvath <[email protected]>
maintainer=Imre Horvath <[email protected]>
sentence=A practical and easy to use generic HTTP client library for the Yun.
Expand Down

0 comments on commit 6ccff74

Please sign in to comment.