Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #define STASSID "MyWiFi"
- #define STAPSK "PASSWORD"
- #define API_KEY "aBCDef1234ghiJKL"
- #define EVENT_NAME "EVENT-NAME"
- bool doorST;
- const char* ssid = STASSID;
- const char* password = STAPSK;
- const String host = "maker.ifttt.com";
- const int httpsPort = 443;
- int doorPin = 4;
- void message(){
- Serial.println();
- Serial.println(digitalRead(doorPin));
- Serial.print("connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- 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());
- // Use WiFiClientSecure class to create TLS connection
- WiFiClientSecure client;
- Serial.print("connecting to ");
- Serial.println(host);
- //Set client in non-secure mode
- client.setInsecure();
- //Reserved memory for BearSSL rx/tx
- client.setBufferSizes(512, 512);
- if (!client.connect(host.c_str(), httpsPort)) {
- Serial.println("connection failed");
- return;
- }
- // String value1, value2, value3;
- //values can't contain space due to it will be used as part of url
- //value1 = "test1";
- // value2 = "test2";
- // value3 = "test3";
- String url = "/trigger/" + String(EVENT_NAME) + "/with/key/" + String(API_KEY);
- //url += "?value1=" + value1;
- //url += "&value2=" + value2;
- //url += "&value3=" + value3;
- Serial.print("requesting URL: ");
- Serial.println(url);
- client.print("GET " + url + " HTTP/1.1\r\n");
- client.print("Host: " + host + "\r\n");
- client.print("User-Agent: ESP8266\r\n");
- client.print("Connection: Close\r\n\r\n");
- Serial.println("request sent");
- unsigned long dataTime = millis();
- //Wait for data ready within 5 seconds
- while (client.connected() && !client.available() && millis() - dataTime < 5000) {
- delay(0);
- }
- char buf;
- int recv = 0;
- String res;
- int httpCode = 0;
- //Read and handle response
- while (client.connected() && client.available()) {
- recv = client.read();
- if (recv < 0)
- continue;
- buf = (char)recv;
- res += buf;
- if (buf == '\r')
- {
- if (res.indexOf("HTTP/1.1 ") > -1) {
- int pos1 = res.indexOf("HTTP/1.1 ");
- int pos2 = res.indexOf(" ", 9);
- res = res.substring(9, pos2);
- httpCode = atoi(res.c_str());
- }
- res = "";
- }
- }
- Serial.print("response HTTP code: ");
- Serial.println(httpCode);
- Serial.print("response Payload: ");
- Serial.println(res);
- // Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
- Serial.println("I'm going into deep sleep mode for 30 seconds");
- ESP.deepSleep(30e6);
- }
- void setup() {
- Serial.begin(115200);
- pinMode(doorPin, INPUT);
- doorST = (digitalRead(doorPin));
- if (doorST == 1) {
- Serial.println("I'm awake, but nothing happened So i will go back to sleep");
- Serial.println(digitalRead(doorPin));
- }
- else if (doorST == 0) {
- message();
- }
- ESP.deepSleep(30e6);
- }
- void loop() {
- Serial.println("This shouldnt print if code is correct");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement