Skip to content

Instantly share code, notes, and snippets.

@abachman
Last active January 9, 2018 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abachman/4cf13359f6a9cd0ff4340edb03b48948 to your computer and use it in GitHub Desktop.
Save abachman/4cf13359f6a9cd0ff4340edb03b48948 to your computer and use it in GitHub Desktop.
Arduino -> Adafruit IO HTTP request
// using the Adafruit Feather M0 ATWINC1500 board
#if !defined(ARDUINO_SAMD_MKR1000) && defined(ARDUINO_ARCH_SAMD)
#define FEATHER_ATWINC1500
#include <SPI.h>
#include <WiFi101.h>
WiFiSSLClient *http_client = new WiFiSSLClient;
#endif // ATWINC1500
// using the Feather Huzzah (ESP8266) board
#ifdef ESP8266
#define FEATHER_HUZZAH
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
WiFiClientSecure *http_client = new WiFiClientSecure;
#endif // ESP8266
// Everything else
#include "ArduinoHttpClient.h"
#define WIFI_SSID "xxx"
#define WIFI_PASS "xxx"
#define IO_USERNAME "xxx"
#define IO_KEY "xxx"
#define IO_FEED "xxx"
HttpClient *http;
String path;
void setup() {
#ifdef FEATHER_ATWINC1500
WiFi.setPins(8,7,4,2);
#endif
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.begin(115200);
http = new HttpClient(*http_client, "io.adafruit.com", 443);
path = "/api/v2/" IO_USERNAME "/feeds/" IO_FEED "/data/retain";
}
void loop() {
// make request
http->beginRequest();
http->get(path.c_str());
http->sendHeader("X-AIO-Key", IO_KEY);
http->endRequest();
int status = http->responseStatusCode();
String body = http->responseBody();
if (status >= 200 && status <= 299) {
if (body.length() > 0) {
Serial.println(body);
} else {
Serial.println("NO VALUE");
}
} else {
Serial.print("ERROR, status code ");
Serial.println(status);
}
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment