Skip to content

Instantly share code, notes, and snippets.

@joshhodgson
Created September 5, 2016 15:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhodgson/87a811bed624eebede9b08a884db5f1b to your computer and use it in GitHub Desktop.
Save joshhodgson/87a811bed624eebede9b08a884db5f1b to your computer and use it in GitHub Desktop.
/*
* HTTP over TLS (HTTPS) example sketch
*
* This example demonstrates how to use
* WiFiClientSecure class to access HTTPS API.
* We fetch and display the status of
* esp8266/Arduino project continuous integration
* build.
*
* Created by Ivan Grokhotkov, 2015.
* This example is in public domain.
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#define BUTTON 4
#define LEDRED 15
const char* ssid = "xxxx";
const char* password = "xxxx";
const char* host = "api.lifx.com";
const int httpsPort = 443;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
bool buttonPressed;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LEDRED, OUTPUT);
digitalWrite(LEDRED, 1);
buttonPressed = false;
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
digitalWrite(LEDRED, 0);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
attachInterrupt(digitalPinToInterrupt(BUTTON), button, FALLING);
}
void button(){
buttonPressed=true;
Serial.println("Button has been pressed");
}
void loop() {
if(buttonPressed){
digitalWrite(LEDRED, 1);
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "/v1/lights/group:Living Room/toggle";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + "?duration=2 HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
buttonPressed=false;
digitalWrite(LEDRED, 0);
}else{
Serial.println("Button not pressed");
}
delay(100);
Serial.println(digitalRead(BUTTON));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment