first commit
This commit is contained in:
@@ -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
|
||||
}
|
||||
78
libraries/BridgeHttpClient/examples/AioFeeds/AioFeeds.ino
Normal file
78
libraries/BridgeHttpClient/examples/AioFeeds/AioFeeds.ino
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
Adafruit AIO REST API example
|
||||
|
||||
What it does: Get your feeds on the Adafruit IO 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, 2016
|
||||
*/
|
||||
|
||||
#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;
|
||||
|
||||
// Collect the response body
|
||||
while (client.available() > 0) {
|
||||
char c = client.read();
|
||||
response += c;
|
||||
}
|
||||
|
||||
// Print the HTTP response code
|
||||
SerialUSB.print("Response code: ");
|
||||
SerialUSB.println(client.getResponseCode());
|
||||
|
||||
// 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) {}
|
||||
}
|
||||
|
||||
// Pretty print the JSON to the serial console
|
||||
array.prettyPrintTo(SerialUSB);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Do nothing
|
||||
}
|
||||
66
libraries/BridgeHttpClient/examples/PostJSON/PostJSON.ino
Normal file
66
libraries/BridgeHttpClient/examples/PostJSON/PostJSON.ino
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Asynchronous POST example
|
||||
|
||||
What it does: Issues a POST request to httpbin using the async 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.
|
||||
|
||||
Usage:
|
||||
1. Upload the sketch using a USB cable.
|
||||
2. Wait for the RED LED to light up on the board.
|
||||
3. Open the serial monitor in the Arduino IDE.
|
||||
|
||||
Written by Imre Horvath, 2016
|
||||
*/
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <BridgeHttpClient.h>
|
||||
|
||||
BridgeHttpClient client;
|
||||
|
||||
void setup() {
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW);
|
||||
Bridge.begin(); // Initialize Bridge
|
||||
digitalWrite(13, HIGH);
|
||||
|
||||
SerialUSB.begin(9600);
|
||||
while (!SerialUSB); // wait for a serial connection
|
||||
|
||||
client.addHeader("X-Api-Key: 12345");
|
||||
client.addHeader("Accept: application/json");
|
||||
client.addHeader("Content-Type: application/json");
|
||||
|
||||
client.enableInsecure(); // Using HTTPS and peer cert. will not be able to auth.
|
||||
|
||||
String data = "{\"sensorData\":\"";
|
||||
data += 123;
|
||||
data += "\"}";
|
||||
client.postAsync("https://httpbin.org/post", data);
|
||||
SerialUSB.print("Sending request");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
if (client.finished()) {
|
||||
|
||||
SerialUSB.println();
|
||||
SerialUSB.println("Response Body:");
|
||||
while (client.available() > 0) {
|
||||
char c = client.read();
|
||||
SerialUSB.print(c);
|
||||
}
|
||||
|
||||
SerialUSB.print("Response Code: ");
|
||||
SerialUSB.println(client.getResponseCode());
|
||||
|
||||
while (1) {} // stop
|
||||
|
||||
} else {
|
||||
// not finished yet, wait and retry
|
||||
SerialUSB.print(".");
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user