Advertisement
CircadianRebel

Untitled

Dec 13th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h>
  3.  
  4. #define STASSID "MyWiFi"
  5. #define STAPSK  "PASSWORD"
  6.  
  7. #define API_KEY "aBCDef1234ghiJKL"
  8. #define EVENT_NAME "EVENT-NAME"
  9.  
  10. bool doorST;
  11.  
  12. const char* ssid = STASSID;
  13. const char* password = STAPSK;
  14.  
  15. const String host = "maker.ifttt.com";
  16. const int httpsPort = 443;
  17. int doorPin = 4;
  18.  
  19.  
  20. void message(){
  21.     Serial.println();
  22.     Serial.println(digitalRead(doorPin));
  23.     Serial.print("connecting to ");
  24.     Serial.println(ssid);
  25.     WiFi.mode(WIFI_STA);
  26.     WiFi.begin(ssid, password);
  27.     while (WiFi.status() != WL_CONNECTED) {
  28.       delay(500);
  29.       Serial.print(".");
  30.     }
  31.     Serial.println("");
  32.     Serial.println("WiFi connected");
  33.     Serial.println("IP address: ");
  34.     Serial.println(WiFi.localIP());
  35.  
  36.  
  37.     // Use WiFiClientSecure class to create TLS connection
  38.     WiFiClientSecure client;
  39.     Serial.print("connecting to ");
  40.     Serial.println(host);
  41.  
  42.     //Set client in non-secure mode
  43.     client.setInsecure();
  44.  
  45.     //Reserved memory for BearSSL rx/tx
  46.     client.setBufferSizes(512, 512);
  47.  
  48.     if (!client.connect(host.c_str(), httpsPort)) {
  49.       Serial.println("connection failed");
  50.       return;
  51.     }
  52.  
  53.     // String value1, value2, value3;
  54.  
  55.     //values can't contain space due to it will be used as part of url
  56.     //value1 = "test1";
  57.     // value2 = "test2";
  58.     // value3 = "test3";
  59.  
  60.     String url = "/trigger/" + String(EVENT_NAME) + "/with/key/" + String(API_KEY);
  61.  
  62.     //url += "?value1=" +  value1;
  63.     //url += "&value2=" +  value2;
  64.     //url += "&value3=" +  value3;
  65.  
  66.     Serial.print("requesting URL: ");
  67.     Serial.println(url);
  68.  
  69.     client.print("GET " +  url + " HTTP/1.1\r\n");
  70.     client.print("Host: " + host + "\r\n");
  71.     client.print("User-Agent: ESP8266\r\n");
  72.     client.print("Connection: Close\r\n\r\n");
  73.  
  74.     Serial.println("request sent");
  75.  
  76.     unsigned long dataTime = millis();
  77.  
  78.     //Wait for data ready within 5 seconds
  79.     while (client.connected() && !client.available() && millis() - dataTime < 5000) {
  80.       delay(0);
  81.     }
  82.  
  83.     char buf;
  84.     int recv = 0;
  85.     String res;
  86.     int httpCode = 0;
  87.  
  88.     //Read and handle response
  89.     while (client.connected() && client.available()) {
  90.       recv = client.read();
  91.       if (recv < 0)
  92.         continue;
  93.       buf = (char)recv;
  94.       res += buf;
  95.       if (buf == '\r')
  96.       {
  97.         if (res.indexOf("HTTP/1.1 ") > -1) {
  98.           int pos1 = res.indexOf("HTTP/1.1 ");
  99.           int pos2 = res.indexOf(" ", 9);
  100.           res = res.substring(9, pos2);
  101.           httpCode = atoi(res.c_str());
  102.         }
  103.         res = "";
  104.       }
  105.     }
  106.  
  107.     Serial.print("response HTTP code: ");
  108.     Serial.println(httpCode);
  109.  
  110.     Serial.print("response Payload: ");
  111.     Serial.println(res);
  112.  
  113.     // 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
  114.     Serial.println("I'm going into deep sleep mode for 30 seconds");
  115.     ESP.deepSleep(30e6);
  116. }
  117.  
  118.  
  119. void setup() {
  120.   Serial.begin(115200);
  121.   pinMode(doorPin, INPUT);
  122.   doorST = (digitalRead(doorPin));
  123.  
  124.   if (doorST == 1) {
  125.     Serial.println("I'm awake, but nothing happened So i will go back to sleep");
  126.     Serial.println(digitalRead(doorPin));  
  127.   }
  128.   else if (doorST == 0) {
  129.    message();
  130.   }
  131.  
  132.   ESP.deepSleep(30e6);
  133. }
  134.  
  135. void loop() {
  136.  
  137.   Serial.println("This shouldnt print if code is correct");
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement