Skip to content
Snippets Groups Projects
Commit c9f13cae authored by Elias Waschin-Scholvin's avatar Elias Waschin-Scholvin
Browse files

Update

parent 6ebea725
No related branches found
No related tags found
No related merge requests found
// 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();
}
// 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();
}
#include <SPI.h> #include <SPI.h>
#include <MFRC522.h> #include <MFRC522.h>
#include <WiFiNINA_Generic.h> #include <WiFiNINA_Generic.h>
#include "NodeRedWebhook.h"
// ----- WIFI ------- // ----- WIFI -------
char ssid[] = "Lab@SmartLab_2G"; char ssid[] = "Lab@SmartLab_2G";
...@@ -22,6 +23,14 @@ MFRC522::MIFARE_Key key; ...@@ -22,6 +23,14 @@ MFRC522::MIFARE_Key key;
byte nuidPICC[4]; byte nuidPICC[4];
unsigned long lastReadTS; 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() { void connectToWifi() {
// set the LED as output // set the LED as output
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
...@@ -34,7 +43,7 @@ void connectToWifi() { ...@@ -34,7 +43,7 @@ void connectToWifi() {
status = WiFi.begin(ssid, pass); status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection: // wait 10 seconds for connection:
delay(10000); delay(3000);
} }
// you're connected now, so print out the data: // you're connected now, so print out the data:
...@@ -123,10 +132,7 @@ void readRFID() { ...@@ -123,10 +132,7 @@ void readRFID() {
return; return;
} }
unsigned long test = millis() - lastReadTS;
Serial.println("Milis ");
Serial.println(test);
if (rfid.uid.uidByte[0] != nuidPICC[0] || if (rfid.uid.uidByte[0] != nuidPICC[0] ||
rfid.uid.uidByte[1] != nuidPICC[1] || rfid.uid.uidByte[1] != nuidPICC[1] ||
rfid.uid.uidByte[2] != nuidPICC[2] || rfid.uid.uidByte[2] != nuidPICC[2] ||
...@@ -135,11 +141,15 @@ void readRFID() { ...@@ -135,11 +141,15 @@ void readRFID() {
) { ) {
Serial.println(F("A new card has been detected.")); Serial.println(F("A new card has been detected."));
String hexstring;
// Store NUID into nuidPICC array // Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) { for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[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.println(F("The NUID tag is:"));
Serial.print(F("In hex: ")); Serial.print(F("In hex: "));
printHex(rfid.uid.uidByte, rfid.uid.size); printHex(rfid.uid.uidByte, rfid.uid.size);
...@@ -147,6 +157,10 @@ void readRFID() { ...@@ -147,6 +157,10 @@ void readRFID() {
Serial.print(F("In dec: ")); Serial.print(F("In dec: "));
printDec(rfid.uid.uidByte, rfid.uid.size); printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println(); Serial.println();
char *hexchars = strdup(hexstring.c_str());
send_webhook(URL, ENDPOINT, hexchars);
} }
else Serial.println(F("Card read previously.")); else Serial.println(F("Card read previously."));
...@@ -185,6 +199,8 @@ void printHex(byte *buffer, byte bufferSize) { ...@@ -185,6 +199,8 @@ void printHex(byte *buffer, byte bufferSize) {
} }
} }
/** /**
* Helper routine to dump a byte array as dec values to Serial. * Helper routine to dump a byte array as dec values to Serial.
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment