Created
March 7, 2017 22:34
-
-
Save jctosta/4a4e8136b074f09da6335e7c6d1b3f72 to your computer and use it in GitHub Desktop.
Wireless Doorbell with NodeMCU and IFTTT Maker Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
#define BUTTON 4 | |
int buttonState = 0; | |
const char* ssid = "<wifi_ssid>"; | |
const char* password = "<wifi_password>"; | |
const char* host = "maker.ifttt.com"; | |
const int httpsPort = 443; | |
// Use web browser to view and copy | |
// SHA1 fingerprint of the certificate | |
const char* fingerprint = "C0 5D 08 5E E1 3E E0 66 F3 79 27 1A CA 1F FC 09 24 11 61 62"; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
pinMode(BUTTON, INPUT_PULLUP); | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
buttonState = digitalRead(BUTTON); | |
if (buttonState == LOW) { | |
Serial.println("pressed"); | |
// 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; | |
} | |
if (client.verify(fingerprint, host)) { | |
Serial.println("certificate matches"); | |
} else { | |
Serial.println("certificate doesn't match"); | |
} | |
String url = "/trigger/{eventName}/with/key/{privateKey}"; | |
Serial.print("requesting URL: "); | |
Serial.println(url); | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"User-Agent: NodeMCU Doorbell\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"); | |
delay(300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment