From f80b247f388ba99db0add4bde2d212c00619a6e5 Mon Sep 17 00:00:00 2001 From: John Bain Date: Sat, 15 Jan 2022 01:05:56 -0500 Subject: [PATCH] Added support for IFTTT Webhook integration (#708) * first push * modified post to work with IFTTT * working but returning wrong error code * command successfully being sent to, IFTTT * resolved issue with ESP600 returing "Wrong Command --- .vscode/settings.json | 37 +++++++++++++++++++ esp3d/command.cpp | 8 +++-- esp3d/config.h | 1 + esp3d/notifications_service.cpp | 64 ++++++++++++++++++++++++++++++++- esp3d/notifications_service.h | 1 + 5 files changed, 108 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fed5a7bd..db798a72 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,42 @@ "terminal.integrated.env.windows": { "PATH": "C:\\Users\\user\\.platformio\\penv\\Scripts;C:\\Users\\user\\.platformio\\penv;C:\\ImageMagick\\ImageMagick-7.0.8-Q16;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Program Files\\PuTTY\\;C:\\Program Files\\Git\\cmd;C:\\Program Files\\nodejs\\;C:\\Program Files\\TortoiseGit\\bin;C:\\Program Files\\dotnet\\;C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\110\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\120\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\120\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\120\\DTS\\Binn\\;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;C:\\Program Files\\LLVM\\bin;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Users\\user\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Users\\user\\AppData\\Local\\GitHubDesktop\\bin;C:\\Eclipse\\GNUTools\\2.6-201507152002\\bin;C:\\Users\\user\\AppData\\Roaming\\npm;C:\\Users\\user\\AppData\\Local\\atom\\bin;C:\\Unxtools;;%USERPROFILE%\\AppData\\Local\\Microsoft\\WindowsApps;C:\\ImageMagick\\ImageMagick-7.0.8-Q16;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Program Files\\PuTTY\\;C:\\Program Files\\Git\\cmd;C:\\Program Files\\nodejs\\;C:\\Program Files\\TortoiseGit\\bin;C:\\Program Files\\dotnet\\;C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\110\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\120\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\120\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\120\\DTS\\Binn\\;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;C:\\Program Files\\LLVM\\bin;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Users\\user\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Users\\user\\AppData\\Local\\GitHubDesktop\\bin;C:\\Eclipse\\GNUTools\\2.6-201507152002\\bin;C:\\Users\\user\\AppData\\Roaming\\npm;C:\\Users\\user\\AppData\\Local\\atom\\bin;C:\\Unxtools;;%USERPROFILE%\\AppData\\Local\\Microsoft\\WindowsApps", "PLATFORMIO_CALLER": "vscode" + }, + "files.associations": { + "string": "cpp", + "*.tcc": "cpp", + "unordered_map": "cpp", + "array": "cpp", + "atomic": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "vector": "cpp", + "exception": "cpp", + "fstream": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "numeric": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "typeinfo": "cpp" } } \ No newline at end of file diff --git a/esp3d/command.cpp b/esp3d/command.cpp index 7e2b8776..00883efb 100644 --- a/esp3d/command.cpp +++ b/esp3d/command.cpp @@ -1244,6 +1244,8 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a ESPCOM::print ( (const char *) CONFIG::intTostr (ESP_EMAIL_NOTIFICATION), output, espresponse); ESPCOM::print (F("\"},{\"Line\":\""), output, espresponse); ESPCOM::print ( (const char *) CONFIG::intTostr (ESP_LINE_NOTIFICATION), output, espresponse); + ESPCOM::print (F("\"},{\"IFTTT\":\""), output, espresponse); + ESPCOM::print ( (const char *) CONFIG::intTostr (ESP_IFTTT_NOTIFICATION), output, espresponse); ESPCOM::print (F("\"}]}"), output, espresponse); ESPCOM::println (F (","), output, espresponse); //Token 1 @@ -1719,7 +1721,7 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a } break; //Set/Get Notification settings - //[ESP610]type= T1= T2= TS= [pwd=] + //[ESP610]type= T1= T2= TS= [pwd=] //Get will give type and settings only not the protected T1/T2 case 610: #ifdef AUTHENTICATION_FEATURE @@ -1737,7 +1739,7 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a } char sbuf[MAX_DATA_LENGTH + 1]; static String tmp; - tmp = (Ntype == ESP_PUSHOVER_NOTIFICATION)?"PUSHOVER":(Ntype == ESP_EMAIL_NOTIFICATION)?"EMAIL":(Ntype == ESP_LINE_NOTIFICATION)?"LINE":"NONE"; + tmp = (Ntype == ESP_PUSHOVER_NOTIFICATION)?"PUSHOVER":(Ntype == ESP_EMAIL_NOTIFICATION)?"EMAIL":(Ntype == ESP_LINE_NOTIFICATION)?"LINE":(Ntype == ESP_IFTTT_NOTIFICATION)?"IFTTT":"NONE"; if (CONFIG::read_string (ESP_NOTIFICATION_SETTINGS, sbuf, MAX_NOTIFICATION_SETTINGS_LENGTH) ) { tmp+= " "; tmp += sbuf; @@ -1758,6 +1760,8 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a Ntype = ESP_EMAIL_NOTIFICATION; } else if (parameter == "LINE") { Ntype = ESP_LINE_NOTIFICATION; + } else if (parameter == "IFTTT") { + Ntype = ESP_IFTTT_NOTIFICATION; } else { ESPCOM::println (INCORRECT_CMD_MSG, output, espresponse); return false; diff --git a/esp3d/config.h b/esp3d/config.h index 2cd7e43a..e458df4a 100644 --- a/esp3d/config.h +++ b/esp3d/config.h @@ -420,6 +420,7 @@ const int DEFAULT_DHT_INTERVAL = 30; #define ESP_PUSHOVER_NOTIFICATION 1 #define ESP_EMAIL_NOTIFICATION 2 #define ESP_LINE_NOTIFICATION 3 +#define ESP_IFTTT_NOTIFICATION 4 #ifdef SDCARD_FEATURE #define DEFAULT_IS_DIRECT_SD 1 diff --git a/esp3d/notifications_service.cpp b/esp3d/notifications_service.cpp index 4b7a40ee..0d733be9 100644 --- a/esp3d/notifications_service.cpp +++ b/esp3d/notifications_service.cpp @@ -59,6 +59,10 @@ typedef WiFiClientSecure TSecureClient; #define LINESERVER "notify-api.line.me" #define LINEPORT 443 +#define IFTTTTIMEOUT 5000 +#define IFTTTSERVER "maker.ifttt.com" +#define IFTTTPORT 443 + #define EMAILTIMEOUT 5000 NotificationsService notificationsservice; @@ -132,6 +136,8 @@ const char * NotificationsService::getTypeString() return "Email"; case ESP_LINE_NOTIFICATION: return "Line"; + case ESP_IFTTT_NOTIFICATION: + return "IFTTT"; default: break; } @@ -154,6 +160,9 @@ bool NotificationsService::sendMSG(const char * title, const char * message) case ESP_LINE_NOTIFICATION : return sendLineMSG(title,message); break; + case ESP_IFTTT_NOTIFICATION : + return sendIFTTTMSG(title,message); + break; default: break; } @@ -189,6 +198,7 @@ bool NotificationsService::sendPushoverMSG(const char * title, const char * mess data += message; data += "&device="; data += wifi_config.get_hostname(); + //build post query postcmd = "POST /1/messages.json HTTP/1.1\r\nHost: api.pushover.net\r\nConnection: close\r\nCache-Control: no-cache\r\nUser-Agent: ESP3D\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nContent-Length: "; postcmd += data.length(); @@ -329,6 +339,47 @@ bool NotificationsService::sendLineMSG(const char * title, const char * message) Notificationclient.stop(); return res; } +//IFTTT +bool NotificationsService::sendIFTTTMSG(const char * title, const char * message) +{ + String data; + String postcmd; + bool res; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + TSecureClient Notificationclient; +#pragma GCC diagnostic pop +#if defined(ARDUINO_ARCH_ESP8266) && !defined(USING_AXTLS) + Notificationclient.setInsecure(); +#endif //ARDUINO_ARCH_ESP8266 && !USING_AXTLS + if (!Notificationclient.connect(_serveraddress.c_str(), _port)) { + //log_esp3d("Error connecting server %s:%d", _serveraddress.c_str(), _port); + return false; + } + + //build data for post + + data ="value1="; + data += title; + data += "&value2="; + data += message; + data += "&value3="; + data += wifi_config.get_hostname(); + + //build post query + postcmd = "POST /trigger/" + _token1 + "/with/key/" + _token2 + " HTTP/1.1\r\nHost: maker.ifttt.com\r\nConnection: close\r\nCache-Control: no-cache\r\nUser-Agent: ESP3D\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: "; + postcmd += data.length(); + postcmd +="\r\n\r\n"; + postcmd +=data; + + //log_esp3d("Query: %s", postcmd.c_str()); + //send query + Notificationclient.print(postcmd); + res = Wait4Answer(Notificationclient, "Congratulations", "Congratulations", IFTTTTIMEOUT); + Notificationclient.stop(); + return res; +} + //Email#serveraddress:port bool NotificationsService::getPortFromSettings() { @@ -386,7 +437,6 @@ bool NotificationsService::getEmailFromSettings() return true; } - bool NotificationsService::begin() { bool res = true; @@ -416,6 +466,17 @@ bool NotificationsService::begin() _port = LINEPORT; _serveraddress = LINESERVER; break; + case ESP_IFTTT_NOTIFICATION: + if (CONFIG::read_string (ESP_NOTIFICATION_TOKEN1, sbuf, MAX_NOTIFICATION_TOKEN_LENGTH) ) { + _token1 = sbuf; + } + if (CONFIG::read_string (ESP_NOTIFICATION_TOKEN2, sbuf, MAX_NOTIFICATION_TOKEN_LENGTH) ) { + _token2 = sbuf; + } + _port = IFTTTPORT; + _serveraddress = IFTTTSERVER; + break; + case ESP_EMAIL_NOTIFICATION: if (CONFIG::read_string (ESP_NOTIFICATION_TOKEN1, sbuf, MAX_NOTIFICATION_TOKEN_LENGTH) ) { _token1 = base64::encode(sbuf); @@ -462,4 +523,5 @@ void NotificationsService::handle() } } + #endif //NOTIFICATION_FEATURE diff --git a/esp3d/notifications_service.h b/esp3d/notifications_service.h index 54909b1e..b69ef35c 100644 --- a/esp3d/notifications_service.h +++ b/esp3d/notifications_service.h @@ -50,6 +50,7 @@ private: bool sendPushoverMSG(const char * title, const char * message); bool sendEmailMSG(const char * title, const char * message); bool sendLineMSG(const char * title, const char * message); + bool sendIFTTTMSG(const char * title, const char * message); bool getPortFromSettings(); bool getServerAddressFromSettings(); bool getEmailFromSettings();