|
#include <ESP8266WiFi.h> |
|
|
|
// Replace with your SSID and Password |
|
const char* ssid = ""; |
|
const char* password = ""; |
|
|
|
// Replace with your unique IFTTT URL resource |
|
const char* resource = ""; |
|
|
|
|
|
// Maker Webhooks IFTTT |
|
const char* server = "maker.ifttt.com"; |
|
|
|
void setup() { |
|
Serial.begin(115200); |
|
|
|
initWifi(); |
|
webIFTTTRequest(); |
|
|
|
// Deep sleep mode until RESET pin is connected to a LOW signal (pushbutton is pressed) |
|
ESP.deepSleep(0); |
|
} |
|
|
|
void loop() { |
|
// sleeping so wont get here |
|
} |
|
|
|
|
|
|
|
//============================================== |
|
// Establish a Wi-Fi connection with your router |
|
//============================================== |
|
void initWifi() { |
|
Serial.print("\nConnecting to: "); |
|
Serial.print(ssid); |
|
WiFi.begin(ssid, password); |
|
|
|
while(WiFi.status() != WL_CONNECTED) { |
|
delay(250); |
|
Serial.print("."); |
|
} |
|
|
|
Serial.print("\nIP address: "); |
|
Serial.println(WiFi.localIP()); |
|
} |
|
|
|
|
|
//============================================== |
|
// HTTP request to the IFTTT web service |
|
//============================================== |
|
void webIFTTTRequest() { |
|
Serial.print("Connecting to "); |
|
Serial.print(server); |
|
|
|
WiFiClient client; |
|
if (!client.connect(server, 80)) { |
|
Serial.println("connection failed"); |
|
} |
|
|
|
Serial.print("Request resource: "); |
|
Serial.println(resource); |
|
client.print(String("GET ") + resource + " HTTP/1.1\r\n" + |
|
"Host: " + server + "\r\n" + |
|
"Connection: close\r\n\r\n"); |
|
|
|
|
|
unsigned long timeout = millis(); |
|
// Read all the lines of the reply from server and print them to Serial |
|
while (client.available() == 0) { |
|
if (millis() - timeout > 5000){ |
|
Serial.println(">>> Client Timeout !"); |
|
client.stop(); return; |
|
} |
|
} |
|
|
|
while(client.available()){ |
|
Serial.write(client.read()); |
|
} |
|
Serial.println("\nclosing connection"); |
|
client.stop(); |
|
} |