Skip to content

Instantly share code, notes, and snippets.

@abachman
Created January 15, 2018 20:57
Show Gist options
  • Save abachman/c1732ba1ab7b9b33e256449543b5c4ab to your computer and use it in GitHub Desktop.
Save abachman/c1732ba1ab7b9b33e256449543b5c4ab to your computer and use it in GitHub Desktop.
esp8266 HTTP and MQTT
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "ArduinoHttpClient.h"
WiFiClientSecure *http_client = new WiFiClientSecure;
// LED connected to pin 4 (labelled SDA on the Feather Huzzah)
#define LED 4
// Everything else
#define WIFI_SSID "xxx"
#define WIFI_PASS "xxx"
#define IO_USERNAME "xxx"
#define IO_KEY "xxx"
#define IO_FEED "xxx"
#define IO_SERVER "io.adafruit.com"
#define IO_SERVERPORT 8883
Adafruit_MQTT_Client mqtt(http_client, IO_SERVER, IO_SERVERPORT, IO_USERNAME, IO_USERNAME, IO_KEY);
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, IO_USERNAME "/feeds/onoff");
/// HTTP setup
HttpClient *http;
String path;
void setup() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.begin(115200);
pinMode(LED, OUTPUT);
delay(1000);
http = new HttpClient(*http_client, "io.adafruit.com", 443);
path = "/api/v2/" IO_USERNAME "/feeds/" IO_FEED "/data/retain";
// 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.print("GOT INITIAL VALUE: ");
Serial.println(body);
if (body.startsWith("ON")) {
Serial.println("LED On");
digitalWrite(LED, HIGH);
} else {
Serial.println("LED Off");
digitalWrite(LED, LOW);
}
} else {
Serial.println("NO VALUE");
}
} else {
Serial.print("ERROR, status code ");
Serial.println(status);
}
delay(5000);
mqtt.subscribe(&onoffbutton);
}
void loop() {
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
Serial.println((char *)subscription->lastread);
// Check if it's the onoff button feed
if (subscription == &onoffbutton) {
Serial.print(F("On-Off button: "));
Serial.println((char *)onoffbutton.lastread);
if (strcmp((char *)onoffbutton.lastread, "ON") == 0) {
digitalWrite(LED, HIGH);
}
if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) {
digitalWrite(LED, LOW);
}
}
}
// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
mqtt.disconnect();
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 30;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment