Looking at your code, I don't see how two events can be used. The RFID code executes every pass through the loop() routine. Thus, the 'failed' event would be triggered continuously, except in the 'success' case where an RFID tag is actually read. Immediately thereafter, the 'failed' case would start being called continuously again. The hub really isn't designed to handle a flood of network packets coming in at a rate of 5 times per second, which is what I believe would happen. Thus, I commented out the code I added for the 'failed' case. Good luck with your project!
//******************************************************************************************
// File: ST_Anything_Buttons_ESP8266WiFi.ino
// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
// Summary: This Arduino Sketch, along with the ST_Anything library and the revised SmartThings
// library, demonstrates the ability of one NodeMCU ESP8266 to
// implement a multi input/output custom device for integration with Hubitat.
// The ST_Anything library takes care of all of the work to schedule device updates
// as well as all communications with the NodeMCU ESP8266's WiFi.
//
// ST_Anything_Buttons implements the following Hubitat Capabilities as a demo of what
// is possible with a single NodeMCU ESP8266
// - 7 x Button devices (sends "pushed" if held for less than 0.750 second, else sends "held")
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2020-09-30 Dan & Daniel Original Creation
//
//******************************************************************************************
//******************************************************************************************
// RC522 RFID Reader Settings
//******************************************************************************************
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
// Define RGB LED Control Pins
#define LED_R D0
#define LED_G D3
#define LED_B D4
#define RST_PIN D1 // Configurable, see typical pin layout above
#define SS_PIN D2 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
// Valid RFID UID
unsigned int RFID_OK[4] = {0x81,0x99,0x83,0x20};
//******************************************************************************************
// SmartThings Library for ESP8266WiFi
//******************************************************************************************
#include <SmartThingsESP8266WiFi.h>
//******************************************************************************************
// ST_Anything Library
//******************************************************************************************
#include <Constants.h> //Constants.h is designed to be modified by the end user to adjust behavior of the ST_Anything library
#include <Device.h> //Generic Device Class, inherited by Sensor and Executor classes
#include <Sensor.h> //Generic Sensor Class, typically provides data to ST Cloud (e.g. Temperature, Motion, etc...)
#include <Executor.h> //Generic Executor Class, typically receives data from ST Cloud (e.g. Switch)
#include <InterruptSensor.h> //Generic Interrupt "Sensor" Class, waits for change of state on digital input
#include <PollingSensor.h> //Generic Polling "Sensor" Class, polls Arduino pins periodically
#include <Everything.h> //Master Brain of ST_Anything library that ties everything together and performs ST Shield communications
#include <IS_Button.h> //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin for button presses
//*************************************************************************************************
// NodeMCU v1.0 ESP8266-12e Pin Definitions (makes it much easier as these match the board markings)
// DO NOT UNCOMMENT THESE LINES - for reference only
//*************************************************************************************************
//#define LED_BUILTIN 16
//#define BUILTIN_LED 16
//
//#define D0 16 //no internal pullup resistor
//#define D1 5
//#define D2 4
//#define D3 0 //must not be pulled low during power on/reset, toggles value during boot
//#define D4 2 //must not be pulled low during power on/reset, toggles value during boot
//#define D5 14
//#define D6 12
//#define D7 13
//#define D8 15 //must not be pulled high during power on/reset
//******************************************************************************************
//Define which Arduino Pins will be used for each device
//******************************************************************************************
//#define PIN_BUTTON_1 D1 //Hubitat Capabilities - Pushable Button/Holdable Button/Releasable Button (Normally Open!)
//******************************************************************************************
//ESP8266 WiFi Information
//******************************************************************************************
String str_ssid = "SBFL_24_20"; // <---You must edit this line!
String str_password = "It is a BIG secret!"; // <---You must edit this line!
IPAddress ip(192, 168, 20, 98); //Device IP Address // <---You must edit this line!
IPAddress gateway(192, 168, 20, 1); //Router gateway // <---You must edit this line!
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <---You must edit this line!
IPAddress dnsserver(192, 168, 20, 1); //DNS server // <---You must edit this line!
const unsigned int serverPort = 8090; // port to run the http server on
// Hubitat Hub Information
IPAddress hubIp(192, 168, 20, 90); // Hubitat hub ip // <---You must edit this line!
const unsigned int hubPort = 39501; // Hubitat hub port
//******************************************************************************************
//st::Everything::callOnMsgSend() optional callback routine. This is a sniffer to monitor
// data being sent to ST. This allows a user to act on data changes locally within the
// Arduino sktech.
//******************************************************************************************
void callback(const String &msg)
{
// Serial.print(F("ST_Anything Callback: Sniffed data = "));
// Serial.println(msg);
//TODO: Add local logic here to take action when a device's value/state is changed
//Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)
//st::receiveSmartString("Put your command here!"); //use same strings that the Device Handler would send
}
//******************************************************************************************
// Arduino Setup() routine
//******************************************************************************************
void setup()
{
//******************************************************************************************
// RC522 RFID Reader Settings
//******************************************************************************************
// set LED Control Pins as output:
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
// Turn Off all LEDs
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, LOW);
//Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
//mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
//Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
//******************************************************************************************
// HubDuino Settings
//******************************************************************************************
// Declare each Device that is attached to the Arduino
// Notes: - For each device, there is typically a corresponding "tile" defined in your
// SmartThings Device Hanlder Groovy code, except when using new COMPOSITE Device Handler
// - For details on each device's constructor arguments below, please refer to the
// corresponding header (.h) and program (.cpp) files.
// - The name assigned to each device (1st argument below) must match the Groovy
// Device Handler names. (Note: "temphumid" below is the exception to this rule
// as the DHT sensors produce both "temperature" and "humidity". Data from that
// particular sensor is sent to the ST Hub in two separate updates, one for
// "temperature" and one for "humidity")
// - The new Composite Device Handler is comprised of a Parent DH and various Child
// DH's. The names used below MUST not be changed for the Automatic Creation of
// child devices to work properly. Simply increment the number by +1 for each duplicate
// device (e.g. contact1, contact2, contact3, etc...) You can rename the Child Devices
// to match your specific use case in the ST Phone Application.
//******************************************************************************************
//Polling Sensors
//Interrupt Sensors
//static st::IS_Button sensor1(F("button1"), PIN_BUTTON_1, 750, LOW, true, 500);
//Special sensors/executors (uses portions of both polling and executor classes)
//Executors
//*****************************************************************************
// Configure debug print output from each main class
// -Note: Set these to "false" if using Hardware Serial on pins 0 & 1
// to prevent communication conflicts with the ST Shield communications
//*****************************************************************************
st::Everything::debug=true;
st::Executor::debug=true;
st::Device::debug=true;
st::PollingSensor::debug=true;
st::InterruptSensor::debug=true;
//*****************************************************************************
//Initialize the "Everything" Class
//*****************************************************************************
//Initialize the optional local callback routine (safe to comment out if not desired)
st::Everything::callOnMsgSend = callback;
//Create the SmartThings ESP8266WiFi Communications Object
//STATIC IP Assignment - Recommended
st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, st::receiveSmartString, "OfficeESP");
//DHCP IP Assigment - Must set your router's DHCP server to provice a static IP address for this device's MAC address
//st::Everything::SmartThing = new st::SmartThingsESP8266WiFi(str_ssid, str_password, serverPort, hubIp, hubPort, st::receiveSmartString);
//Run the Everything class' init() routine which establishes WiFi communications with SmartThings Hub
st::Everything::init();
//*****************************************************************************
//Add each sensor to the "Everything" Class
//*****************************************************************************
// st::Everything::addSensor(&sensor1);
//*****************************************************************************
//Add each executor to the "Everything" Class
//*****************************************************************************
//*****************************************************************************
//Initialize each of the devices which were added to the Everything Class
//*****************************************************************************
st::Everything::initDevices();
}
//******************************************************************************************
//Arduino Loop() routine
//******************************************************************************************
void loop()
{
//*****************************************************************************
// CR522 Reader Logic
//*****************************************************************************
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.print(F(": Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
// Compare Card UID with Valid ID
compare_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
delay(200);
digitalWrite(LED_G, LOW);
digitalWrite(LED_R, LOW);
//*****************************************************************************
//Execute the Everything run method which takes care of "Everything"
//*****************************************************************************
st::Everything::run();
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
* Helper routine to compare Card IDs.
*/
void compare_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
if (buffer[i] != RFID_OK[i]) {
digitalWrite(LED_G, LOW);
digitalWrite(LED_R, HIGH);
// Send Failed Attempt Trigger to Hubitat
// Commented out as it would otherwise flood the Hubitat Hub with useless events
// in rapid succession every time through the loop() routine
//String msg = "button1 held";
//st::Everything::sendSmartString(msg);
return;
}
digitalWrite(LED_G, HIGH);
digitalWrite(LED_R, LOW);
// Send Successful Attempt Trigger to Hubitat
String msg = "button1 pushed";
st::Everything::sendSmartString(msg);
}
}