Advertisement
pleasedontcode

"RFID SMS" rev_02

Jun 13th, 2024
389
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: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-13 11:30:54
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Enhance the RFID system to assign a unique random */
  21.     /* identifier to each new RFID tag detected using the */
  22.     /* MFRC522 module. Every new identifier will be send */
  23.     /* via gsm to number +41 123456 */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SPI.h>
  28. #include <SoftwareSerial.h> //https://github.com/plerup/espsoftwareserial
  29. #include <MFRC522.h> //https://github.com/miguelbalboa/rfid
  30. #include <Sim800L.h> //https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36. void generateAndSendUniqueID();
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t gsm_SIM800L_RING_PIN_D17 = 17;
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t gsm_SIM800L_RST_PIN_D16 = 16;
  43. const uint8_t gsm_SIM800L_DTR_PIN_D22 = 22;
  44.  
  45. /***** DEFINITION OF Software Serial *****/
  46. const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_TX_D14 = 14;
  47. const uint8_t gsm_SIM800L_Serial_PIN_SERIAL_RX_D21 = 21;
  48. SoftwareSerial gsm_SIM800L_Serial(gsm_SIM800L_Serial_PIN_SERIAL_RX_D21, gsm_SIM800L_Serial_PIN_SERIAL_TX_D14); // RX, TX
  49.  
  50. /***** DEFINITION OF SPI PINS *****/
  51. const uint8_t rfid_MFRC522_SPI_PIN_MOSI_D23 = 23;
  52. const uint8_t rfid_MFRC522_SPI_PIN_MISO_D19 = 19;
  53. const uint8_t rfid_MFRC522_SPI_PIN_SCLK_D18 = 18;
  54. const uint8_t rfid_MFRC522_SPI_PIN_CS_D5 = 5;
  55.  
  56. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. /***** used to store raw data *****/
  58. bool gsm_SIM800L_RST_PIN_D16_rawData = 0;
  59. bool gsm_SIM800L_DTR_PIN_D22_rawData = 0;
  60.  
  61. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  62. /***** used to store data after characteristic curve transformation *****/
  63. float gsm_SIM800L_RST_PIN_D16_phyData = 0.0;
  64. float gsm_SIM800L_DTR_PIN_D22_phyData = 0.0;
  65.  
  66. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  67. MFRC522 rfid(rfid_MFRC522_SPI_PIN_CS_D5, rfid_MFRC522_SPI_PIN_SCLK_D18); // Instance of the class
  68. Sim800L GSM(gsm_SIM800L_Serial, gsm_SIM800L_RST_PIN_D16, gsm_SIM800L_DTR_PIN_D22, gsm_SIM800L_RING_PIN_D17); // Instance of the class
  69.  
  70. // Array to store new NUID
  71. byte nuidPICC[4];
  72.  
  73. void setup(void)
  74. {
  75.   // put your setup code here, to run once:
  76.  
  77.   pinMode(gsm_SIM800L_RING_PIN_D17, INPUT_PULLUP);
  78.  
  79.   pinMode(gsm_SIM800L_RST_PIN_D16, OUTPUT);
  80.   pinMode(gsm_SIM800L_DTR_PIN_D22, OUTPUT);
  81.  
  82.   pinMode(rfid_MFRC522_SPI_PIN_CS_D5, OUTPUT);
  83.   // start the SPI library:
  84.   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);
  85.  
  86.   gsm_SIM800L_Serial.begin(9600);
  87.  
  88.   rfid.PCD_Init(); // Init MFRC522
  89.  
  90.   // Initialize GSM module
  91.   GSM.begin(4800);
  92. }
  93.  
  94. void loop(void)
  95. {
  96.   // put your main code here, to run repeatedly:
  97.  
  98.   updateOutputs(); // Refresh output data
  99.  
  100.   // Check for new RFID card
  101.   if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
  102.     // If a new card is detected, generate and send a unique ID
  103.     generateAndSendUniqueID();
  104.     rfid.PICC_HaltA(); // Halt PICC
  105.     rfid.PCD_StopCrypto1(); // Stop encryption on PCD
  106.   }
  107. }
  108.  
  109. void updateOutputs()
  110. {
  111.   digitalWrite(gsm_SIM800L_RST_PIN_D16, gsm_SIM800L_RST_PIN_D16_rawData);
  112.   digitalWrite(gsm_SIM800L_DTR_PIN_D22, gsm_SIM800L_DTR_PIN_D22_rawData);
  113. }
  114.  
  115. void generateAndSendUniqueID() {
  116.   // Generate a unique random identifier
  117.   String uniqueID = "";
  118.   for (int i = 0; i < 8; i++) {
  119.     uniqueID += String(random(0, 9)); // Generate a random digit
  120.   }
  121.  
  122.   // Send the unique identifier via GSM
  123.   char* number = "+41123456";
  124.   bool error = GSM.sendSms(number, uniqueID.c_str());
  125.  
  126.   if (!error) {
  127.     Serial.println("SMS sent successfully: " + uniqueID);
  128.   } else {
  129.     Serial.println("Failed to send SMS");
  130.   }
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement