CCCPaste

"RFID SMS" rev_12

/********* 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: Arduino Mega
	- Source Code created on: 2024-06-12 14:52:04

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* read info about tag which is closed to rfid and */
	/* send information to gsm to number +39 1234567 */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#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 sendTagInfoToGSM();

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t rfid_MFRC522_RST_PIN_D4 = 4;
const uint8_t gsm_SIM800L_RST_PIN_D2 = 2;

/***** DEFINITION OF Software Serial *****/
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_TX_A0 = A0;
const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_RX_A1 = A1;
SoftwareSerial gsm_SIM800L_Serial(gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF SPI PINS *****/
const uint8_t rfid_MFRC522_SPI_PIN_MOSI_D51 = 51;
const uint8_t rfid_MFRC522_SPI_PIN_MISO_D50 = 50;
const uint8_t rfid_MFRC522_SPI_PIN_SCLK_D52 = 52;
const uint8_t rfid_MFRC522_SPI_PIN_CS_D53 = 53;

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

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
MFRC522 rfid(rfid_MFRC522_SPI_PIN_CS_D53, rfid_MFRC522_RST_PIN_D4); // Instance of the MFRC522 class
Sim800L GSM(gsm_SIM800L_Serial, gsm_SIM800L_RST_PIN_D2); // Instance of the Sim800L class

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

    pinMode(rfid_MFRC522_RST_PIN_D4, OUTPUT);
    pinMode(gsm_SIM800L_RST_PIN_D2, OUTPUT);

    pinMode(rfid_MFRC522_SPI_PIN_CS_D53, OUTPUT);
    // start the SPI library:
    SPI.begin();

    gsm_SIM800L_Serial.begin(9600);

    // Initialize the MFRC522
    rfid.PCD_Init();

    // Initialize the Sim800L
    GSM.begin();
    GSM.reset();
    GSM.delAllSms(); // Clean SMS memory

    Serial.begin(9600); // Debugging
}

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()) {
        sendTagInfoToGSM();
        rfid.PICC_HaltA(); // Halt PICC
        rfid.PCD_StopCrypto1(); // Stop encryption on PCD
    }
}

void updateOutputs() {
    digitalWrite(rfid_MFRC522_RST_PIN_D4, rfid_MFRC522_RST_PIN_D4_rawData);
    digitalWrite(gsm_SIM800L_RST_PIN_D2, gsm_SIM800L_RST_PIN_D2_rawData);
}

void sendTagInfoToGSM() {
    // Read the RFID tag's UID
    String uid = "";
    for (byte i = 0; i < rfid.uid.size; i++) {
        uid += String(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
        uid += String(rfid.uid.uidByte[i], HEX);
    }
    uid.toUpperCase();

    // Send the UID via SMS
    String message = "RFID Tag UID: " + uid;
    GSM.sendSms("+391234567", message); // Replace with the actual phone number
    Serial.println("Sent SMS: " + message); // Debugging
}

/* END CODE */