Skip to content

Commit

Permalink
Added support for IFTTT Webhook integration (#708)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KingBain authored Jan 15, 2022
1 parent c1733a8 commit f80b247
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
37 changes: 37 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 6 additions & 2 deletions esp3d/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1719,7 +1721,7 @@ bool COMMAND::execute_command (int cmd, String cmd_params, tpipe output, level_a
}
break;
//Set/Get Notification settings
//[ESP610]type=<NONE/PUSHOVER/EMAIL/LINE> T1=<token1> T2=<token2> TS=<Settings> [pwd=<admin password>]
//[ESP610]type=<NONE/PUSHOVER/EMAIL/LINE/IFTTT> T1=<token1> T2=<token2> TS=<Settings> [pwd=<admin password>]
//Get will give type and settings only not the protected T1/T2
case 610:
#ifdef AUTHENTICATION_FEATURE
Expand All @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions esp3d/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 63 additions & 1 deletion esp3d/notifications_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,6 +136,8 @@ const char * NotificationsService::getTypeString()
return "Email";
case ESP_LINE_NOTIFICATION:
return "Line";
case ESP_IFTTT_NOTIFICATION:
return "IFTTT";
default:
break;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -386,7 +437,6 @@ bool NotificationsService::getEmailFromSettings()
return true;
}


bool NotificationsService::begin()
{
bool res = true;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -462,4 +523,5 @@ void NotificationsService::handle()
}
}


#endif //NOTIFICATION_FEATURE
1 change: 1 addition & 0 deletions esp3d/notifications_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class NotificationsService
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();
Expand Down

0 comments on commit f80b247

Please sign in to comment.