Advertisement
pleasedontcode

"RFID SMS" rev_12

Jun 12th, 2024
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "RFID SMS"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-12 14:52:04
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read info about tag which is closed to rfid and */
  21.     /* send information to gsm to number +39 1234567 */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <SPI.h>
  26. #include <SoftwareSerial.h>
  27. #include <MFRC522.h>    // https://github.com/miguelbalboa/rfid
  28. #include <Sim800L.h>    // https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34. void sendTagInfoToGSM();
  35.  
  36. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  37. const uint8_t rfid_MFRC522_RST_PIN_D4 = 4;
  38. const uint8_t gsm_SIM800L_RST_PIN_D2 = 2;
  39.  
  40. /***** DEFINITION OF Software Serial *****/
  41. const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_TX_A0 = A0;
  42. const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_RX_A1 = A1;
  43. SoftwareSerial gsm_SIM800L_Serial(gsm_SIM800L_Serial_PIN_SERIAL_RX_A1, gsm_SIM800L_Serial_PIN_SERIAL_TX_A0);
  44.  
  45. /***** DEFINITION OF SPI PINS *****/
  46. const uint8_t rfid_MFRC522_SPI_PIN_MOSI_D51 = 51;
  47. const uint8_t rfid_MFRC522_SPI_PIN_MISO_D50 = 50;
  48. const uint8_t rfid_MFRC522_SPI_PIN_SCLK_D52 = 52;
  49. const uint8_t rfid_MFRC522_SPI_PIN_CS_D53 = 53;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. /***** used to store raw data *****/
  53. bool rfid_MFRC522_RST_PIN_D4_rawData = 0;
  54. bool gsm_SIM800L_RST_PIN_D2_rawData = 0;
  55.  
  56. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  57. /***** used to store data after characteristic curve transformation *****/
  58. float rfid_MFRC522_RST_PIN_D4_phyData = 0.0;
  59. float gsm_SIM800L_RST_PIN_D2_phyData = 0.0;
  60.  
  61. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  62. MFRC522 rfid(rfid_MFRC522_SPI_PIN_CS_D53, rfid_MFRC522_RST_PIN_D4); // Instance of the MFRC522 class
  63. Sim800L GSM(gsm_SIM800L_Serial, gsm_SIM800L_RST_PIN_D2); // Instance of the Sim800L class
  64.  
  65. void setup(void) {
  66.     // put your setup code here, to run once:
  67.  
  68.     pinMode(rfid_MFRC522_RST_PIN_D4, OUTPUT);
  69.     pinMode(gsm_SIM800L_RST_PIN_D2, OUTPUT);
  70.  
  71.     pinMode(rfid_MFRC522_SPI_PIN_CS_D53, OUTPUT);
  72.     // start the SPI library:
  73.     SPI.begin();
  74.  
  75.     gsm_SIM800L_Serial.begin(9600);
  76.  
  77.     // Initialize the MFRC522
  78.     rfid.PCD_Init();
  79.  
  80.     // Initialize the Sim800L
  81.     GSM.begin();
  82.     GSM.reset();
  83.     GSM.delAllSms(); // Clean SMS memory
  84.  
  85.     Serial.begin(9600); // Debugging
  86. }
  87.  
  88. void loop(void) {
  89.     // put your main code here, to run repeatedly:
  90.  
  91.     updateOutputs(); // Refresh output data
  92.  
  93.     // Check for new RFID card
  94.     if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
  95.         sendTagInfoToGSM();
  96.         rfid.PICC_HaltA(); // Halt PICC
  97.         rfid.PCD_StopCrypto1(); // Stop encryption on PCD
  98.     }
  99. }
  100.  
  101. void updateOutputs() {
  102.     digitalWrite(rfid_MFRC522_RST_PIN_D4, rfid_MFRC522_RST_PIN_D4_rawData);
  103.     digitalWrite(gsm_SIM800L_RST_PIN_D2, gsm_SIM800L_RST_PIN_D2_rawData);
  104. }
  105.  
  106. void sendTagInfoToGSM() {
  107.     // Read the RFID tag's UID
  108.     String uid = "";
  109.     for (byte i = 0; i < rfid.uid.size; i++) {
  110.         uid += String(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
  111.         uid += String(rfid.uid.uidByte[i], HEX);
  112.     }
  113.     uid.toUpperCase();
  114.  
  115.     // Send the UID via SMS
  116.     String message = "RFID Tag UID: " + uid;
  117.     GSM.sendSms("+391234567", message); // Replace with the actual phone number
  118.     Serial.println("Sent SMS: " + message); // Debugging
  119. }
  120.  
  121. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement