IFTTTMode.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "IFTTTMode.h"
  2. #include "Global.h"
  3. //insert trigger name in "triggername" field and your Webhoook Key ID in "keyID" field
  4. const char* resource = "https://maker.ifttt.com/trigger/triggername/json/with/key/keyID";
  5. const char* serverIFTTT = "maker.ifttt.com";
  6. //insert SSID and Password in field
  7. const char *ssidIFTTT = "SSID";
  8. const char *passwordIFTTT = "password";
  9. IFTTTMode::IFTTTMode()
  10. {
  11. }
  12. void IFTTTMode::RunSetup(){
  13. Serial.print("\nConnecting to: ");
  14. Serial.print(ssidIFTTT);
  15. WiFi.begin(ssidIFTTT, passwordIFTTT);
  16. while(WiFi.status() != WL_CONNECTED) {
  17. delay(250);
  18. Serial.print(".");
  19. }
  20. Serial.print("\nIP address: ");
  21. Serial.println(WiFi.localIP());
  22. }
  23. void IFTTTMode::IFTTTCommand(){
  24. Serial.print("Connecting to ");
  25. Serial.print(serverIFTTT);
  26. WiFiClient client;
  27. if (!client.connect(serverIFTTT, 80)) {
  28. Serial.println("connection failed");
  29. }
  30. Serial.print("Request resource: ");
  31. Serial.println(resource);
  32. client.print(String("GET ") + resource + " HTTP/1.1\r\n" +
  33. "Host: " + serverIFTTT + "\r\n" +
  34. "Connection: close\r\n\r\n");
  35. unsigned long timeout = millis();
  36. // Read all the lines of the reply from server and print them to Serial
  37. while (client.available() == 0) {
  38. if (millis() - timeout > 5000){
  39. Serial.println(">>> Client Timeout !");
  40. client.stop(); return;
  41. }
  42. }
  43. while(client.available()){
  44. Serial.write(client.read());
  45. }
  46. Serial.println("\nclosing connection");
  47. client.stop();
  48. }