From c9f13cae7e118ffdcef652e0855e81a64f00c64b Mon Sep 17 00:00:00 2001 From: Elias Waschin-Scholvin <> Date: Tue, 29 Jun 2021 09:33:19 +0200 Subject: [PATCH] Update --- arduino/sketch_main/AnotherIFTTTWebhook.h | 101 ++++++++++++++++++++++ arduino/sketch_main/NodeRedWebhook.h | 100 +++++++++++++++++++++ arduino/sketch_main/sketch_main.ino | 28 ++++-- 3 files changed, 223 insertions(+), 6 deletions(-) create mode 100644 arduino/sketch_main/AnotherIFTTTWebhook.h create mode 100644 arduino/sketch_main/NodeRedWebhook.h diff --git a/arduino/sketch_main/AnotherIFTTTWebhook.h b/arduino/sketch_main/AnotherIFTTTWebhook.h new file mode 100644 index 0000000..d94520c --- /dev/null +++ b/arduino/sketch_main/AnotherIFTTTWebhook.h @@ -0,0 +1,101 @@ +// The MIT License +// +// Copyright (c) 2015 Neil Webber +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +// ==================================================================== +// ==================================================================== +// An adapted version of IFTTT Trigger by Neil Webber +// Original: https://gist.github.com/outofmbufs/d6ced37b49a484c495f0 +// +// Adaptation from: https://www.siytek.com +// +// How to use: +// 1. Place AnotherIFTTTWebhook.h in the same directory as your project +// 2. Add #include "AnotherIFTTTWebhook.h" to your main project file +// 3. Send webhook using function in main file: +// send_webhook(EVENT, KEY, Value1, Value2, Value3); +// ==================================================================== +// ==================================================================== + +WiFiClient client; + +char *append_str(char *here, char *s) { + while (*here++ = *s++) + ; + return here-1; +} + +char *append_ul(char *here, unsigned long u) { + char buf[20]; // we "just know" this is big enough + + return append_str(here, ultoa(u, buf, 10)); +} + +void send_webhook(char *MakerIFTTT_Event, char *MakerIFTTT_Key, char *value1, char *value2, char *value3) { + + // connect to the Maker event server + client.connect("maker.ifttt.com", 80); + + // construct the POST request + char post_rqst[256]; // hand-calculated to be big enough + + char *p = post_rqst; + p = append_str(p, "POST /trigger/"); + p = append_str(p, MakerIFTTT_Event); + p = append_str(p, "/with/key/"); + p = append_str(p, MakerIFTTT_Key); + p = append_str(p, " HTTP/1.1\r\n"); + p = append_str(p, "Host: maker.ifttt.com\r\n"); + p = append_str(p, "Content-Type: application/json\r\n"); + p = append_str(p, "Content-Length: "); + + // we need to remember where the content length will go, which is: + char *content_length_here = p; + + // it's always two digits, so reserve space for them (the NN) + p = append_str(p, "NN\r\n"); + + // end of headers + p = append_str(p, "\r\n"); + + // construct the JSON; remember where we started so we will know len + char *json_start = p; + + // As described - this example reports a pin, uptime, and "hello world" + p = append_str(p, "{\"value1\":\""); + p = append_str(p, value1); + p = append_str(p, "\",\"value2\":\""); + p = append_str(p, value2); + p = append_str(p, "\",\"value3\":\""); + p = append_str(p, value3); + p = append_str(p, "\"}"); + + // go back and fill in the JSON length + // we just know this is at most 2 digits (and need to fill in both) + int i = strlen(json_start); + content_length_here[0] = '0' + (i/10); + content_length_here[1] = '0' + (i%10); + + // finally we are ready to send the POST to the server! + client.print(post_rqst); + client.stop(); + +} diff --git a/arduino/sketch_main/NodeRedWebhook.h b/arduino/sketch_main/NodeRedWebhook.h new file mode 100644 index 0000000..16d83ac --- /dev/null +++ b/arduino/sketch_main/NodeRedWebhook.h @@ -0,0 +1,100 @@ +// The MIT License +// +// Copyright (c) 2015 Neil Webber +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +// ==================================================================== +// ==================================================================== +// An adapted version of IFTTT Trigger by Neil Webber +// Original: https://gist.github.com/outofmbufs/d6ced37b49a484c495f0 +// +// Adaptation from: https://www.siytek.com +// +// How to use: +// 1. Place AnotherIFTTTWebhook.h in the same directory as your project +// 2. Add #include "AnotherIFTTTWebhook.h" to your main project file +// 3. Send webhook using function in main file: +// send_webhook(EVENT, KEY, Value1, Value2, Value3); +// ==================================================================== +// ==================================================================== + +WiFiClient client; + +char *append_str(char *here, char *s) { + while (*here++ = *s++) + ; + return here-1; +} + +char *append_ul(char *here, unsigned long u) { + char buf[20]; // we "just know" this is big enough + + return append_str(here, ultoa(u, buf, 10)); +} + +void send_webhook(char *url, char *endpoint, char *id) { + + client.connect(url, 80); + + // construct the POST request + char post_rqst[256]; // hand-calculated to be big enough + + char *p = post_rqst; + p = append_str(p, "POST /api/"); + p = append_str(p, endpoint); + p = append_str(p, " HTTP/1.1\r\n"); + p = append_str(p, "Host: "); + p = append_str(p, url); + p = append_str(p, "\r\n"); + p = append_str(p, "Content-Type: application/json\r\n"); + p = append_str(p, "Content-Length: "); + + // we need to remember where the content length will go, which is: + char *content_length_here = p; + + // it's always two digits, so reserve space for them (the NN) + p = append_str(p, "NN\r\n"); + + // end of headers + p = append_str(p, "\r\n"); + + // construct the JSON; remember where we started so we will know len + char *json_start = p; + + // As described - this example reports a pin, uptime, and "hello world" + p = append_str(p, "{\"id\":\""); + p = append_str(p, id); + p = append_str(p, "\"}"); + + // go back and fill in the JSON length + // we just know this is at most 2 digits (and need to fill in both) + int i = strlen(json_start); + content_length_here[0] = '0' + (i/10); + content_length_here[1] = '0' + (i%10); + + Serial.println("Send event"); + Serial.println(url); + Serial.println(post_rqst); + + // finally we are ready to send the POST to the server! + client.print(post_rqst); + client.stop(); + +} diff --git a/arduino/sketch_main/sketch_main.ino b/arduino/sketch_main/sketch_main.ino index e6cb2e7..28bf2fe 100644 --- a/arduino/sketch_main/sketch_main.ino +++ b/arduino/sketch_main/sketch_main.ino @@ -1,6 +1,7 @@ #include <SPI.h> #include <MFRC522.h> #include <WiFiNINA_Generic.h> +#include "NodeRedWebhook.h" // ----- WIFI ------- char ssid[] = "Lab@SmartLab_2G"; @@ -22,6 +23,14 @@ MFRC522::MIFARE_Key key; byte nuidPICC[4]; unsigned long lastReadTS; +// ------- IFTT ------- +char YOUR_IFTTT_API_KEY[] = "ba8Pa_lMmzkmMi5YC0PAiM"; +char YOUR_IFTTT_EVENT_NAME[] = "zoom"; + +// -------- Node-Red ---- +char URL[] = "elwa.eu.ngrok.io"; +char ENDPOINT[] = "event"; + void connectToWifi() { // set the LED as output pinMode(LED_BUILTIN, OUTPUT); @@ -34,7 +43,7 @@ void connectToWifi() { status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: - delay(10000); + delay(3000); } // you're connected now, so print out the data: @@ -123,10 +132,7 @@ void readRFID() { return; } - unsigned long test = millis() - lastReadTS; - Serial.println("Milis "); - Serial.println(test); - + if (rfid.uid.uidByte[0] != nuidPICC[0] || rfid.uid.uidByte[1] != nuidPICC[1] || rfid.uid.uidByte[2] != nuidPICC[2] || @@ -135,11 +141,15 @@ void readRFID() { ) { Serial.println(F("A new card has been detected.")); + String hexstring; + // Store NUID into nuidPICC array for (byte i = 0; i < 4; i++) { nuidPICC[i] = rfid.uid.uidByte[i]; + hexstring += String(rfid.uid.uidByte[i], HEX); } - + + Serial.println(hexstring); Serial.println(F("The NUID tag is:")); Serial.print(F("In hex: ")); printHex(rfid.uid.uidByte, rfid.uid.size); @@ -147,6 +157,10 @@ void readRFID() { Serial.print(F("In dec: ")); printDec(rfid.uid.uidByte, rfid.uid.size); Serial.println(); + + + char *hexchars = strdup(hexstring.c_str()); + send_webhook(URL, ENDPOINT, hexchars); } else Serial.println(F("Card read previously.")); @@ -185,6 +199,8 @@ void printHex(byte *buffer, byte bufferSize) { } } + + /** * Helper routine to dump a byte array as dec values to Serial. */ -- GitLab