CCCPaste

"RFID SMS" rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "RFID SMS"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-06-13 11:30:54

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Enhance the RFID system to assign a unique random */
	/* identifier to each new RFID tag detected using the */
	/* MFRC522 module. Every new identifier will be send */
	/* via gsm to number +41 123456 */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include  //https://github.com/plerup/espsoftwareserial
#include  //https://github.com/miguelbalboa/rfid
#include  //https://github.com/vittorioexp/Sim800L-Arduino-Library-revised

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void generateAndSendUniqueID();

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t gsm_SIM800L_RING_PIN_D17 = 17;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t gsm_SIM800L_RST_PIN_D16 = 16;
const uint8_t gsm_SIM800L_DTR_PIN_D22 = 22;

/***** DEFINITION OF Software Serial *****/
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_TX_D14 = 14;
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_RX_D21 = 21;
SoftwareSerial gsm_SIM800L_Serial(gsm_SIM800L_Serial_PIN_SERIAL_RX_D21, gsm_SIM800L_Serial_PIN_SERIAL_TX_D14); // RX, TX

/***** DEFINITION OF SPI PINS *****/
const uint8_t rfid_MFRC522_SPI_PIN_MOSI_D23 = 23;
const uint8_t rfid_MFRC522_SPI_PIN_MISO_D19 = 19;
const uint8_t rfid_MFRC522_SPI_PIN_SCLK_D18 = 18;
const uint8_t rfid_MFRC522_SPI_PIN_CS_D5 = 5;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool gsm_SIM800L_RST_PIN_D16_rawData = 0;
bool gsm_SIM800L_DTR_PIN_D22_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float gsm_SIM800L_RST_PIN_D16_phyData = 0.0;
float gsm_SIM800L_DTR_PIN_D22_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
MFRC522 rfid(rfid_MFRC522_SPI_PIN_CS_D5, rfid_MFRC522_SPI_PIN_SCLK_D18); // Instance of the class
Sim800L GSM(gsm_SIM800L_Serial, gsm_SIM800L_RST_PIN_D16, gsm_SIM800L_DTR_PIN_D22, gsm_SIM800L_RING_PIN_D17); // Instance of the class

// Array to store new NUID
byte nuidPICC[4];

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(gsm_SIM800L_RING_PIN_D17, INPUT_PULLUP);

  pinMode(gsm_SIM800L_RST_PIN_D16, OUTPUT);
  pinMode(gsm_SIM800L_DTR_PIN_D22, OUTPUT);

  pinMode(rfid_MFRC522_SPI_PIN_CS_D5, OUTPUT);
  // start the SPI library:
  SPI.begin(rfid_MFRC522_SPI_PIN_SCLK_D18, rfid_MFRC522_SPI_PIN_MISO_D19, rfid_MFRC522_SPI_PIN_MOSI_D23, rfid_MFRC522_SPI_PIN_CS_D5);

  gsm_SIM800L_Serial.begin(9600);

  rfid.PCD_Init(); // Init MFRC522

  // Initialize GSM module
  GSM.begin(4800);
}

void loop(void)
{
  // put your main code here, to run repeatedly:

  updateOutputs(); // Refresh output data

  // Check for new RFID card
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    // If a new card is detected, generate and send a unique ID
    generateAndSendUniqueID();
    rfid.PICC_HaltA(); // Halt PICC
    rfid.PCD_StopCrypto1(); // Stop encryption on PCD
  }
}

void updateOutputs()
{
  digitalWrite(gsm_SIM800L_RST_PIN_D16, gsm_SIM800L_RST_PIN_D16_rawData);
  digitalWrite(gsm_SIM800L_DTR_PIN_D22, gsm_SIM800L_DTR_PIN_D22_rawData);
}

void generateAndSendUniqueID() {
  // Generate a unique random identifier
  String uniqueID = "";
  for (int i = 0; i < 8; i++) {
    uniqueID += String(random(0, 9)); // Generate a random digit
  }

  // Send the unique identifier via GSM
  char* number = "+41123456";
  bool error = GSM.sendSms(number, uniqueID.c_str());

  if (!error) {
    Serial.println("SMS sent successfully: " + uniqueID);
  } else {
    Serial.println("Failed to send SMS");
  }
}

/* END CODE */