Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h> // Include the Wi-Fi library
- #include <SPI.h>
- #include <MFRC522.h>
- //--------------------------------------------------------------------------------------------------------------
- #define RST_PIN D3 // Configurable, see typical pin layout above
- #define SS_PIN D8 // Configurable, see typical pin layout above
- MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
- const char* ssid = "REDACTED"; // The SSID (name) of the Wi-Fi network you want to connect to
- const char* password = "REDACTED"; // The password of the Wi-Fi network
- const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
- char devid[] = "REDACTED"; //device ID from Pushingbox
- //--------------------------------------------------------------------------------------------------------------
- void setup() {
- Serial.begin(9600); // Start the Serial communication to send messages to the computer
- SPI.begin(); // Init SPI bus
- mfrc522.PCD_Init(); // Init MFRC522 card
- delay(10);
- Serial.println('\n');
- WiFi.begin(ssid, password); // Connect to the network
- Serial.print("Connecting to ");
- Serial.print(ssid); Serial.println(" ...");
- int i = 0;
- while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
- delay(1000);
- Serial.print(++i); Serial.print(' ');
- }
- Serial.println('\n');
- Serial.println("Connection established!");
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
- delay(10);
- Serial.println('\n');
- Serial.println(F("Please scan a card.")); //shows in serial that it is ready to read
- }
- //--------------------------------------------------------------------------------------------------------------
- void loop()
- {
- // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
- MFRC522::MIFARE_Key key;
- for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
- //some variables we need
- byte block;
- byte len;
- MFRC522::StatusCode status;
- //--------------------------------------------------------------------------------------------------------------
- // 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.println(F("**Card Detected:**"));
- //--------------------------------------------------------------------------------------------------------------
- mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
- //mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
- //--------------------------------------------------------------------------------------------------------------
- Serial.println(F("Name: "));
- byte buffer1[18];
- block = 4;
- len = 18;
- //-------------------------------------------------------------------------------------------------GET FIRST NAME
- status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Authentication failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- status = mfrc522.MIFARE_Read(block, buffer1, &len);
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Reading failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- //PRINT FIRST NAME
- for (uint8_t i = 0; i < 16; i++)
- {
- if (buffer1[i] != 32)
- {
- Serial.write(buffer1[i]);
- }
- }
- //----------------------------------------------------------------------------------------------------GET LAST NAME
- byte buffer2[18];
- block = 1;
- status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Authentication failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- status = mfrc522.MIFARE_Read(block, buffer2, &len);
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Reading failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- //PRINT LAST NAME
- for (uint8_t i = 0; i < 16; i++) {
- Serial.write(buffer2[i] );
- }
- Serial.println(" ");
- //-----------------------------------------------------------------------------------------------------------GET ID
- Serial.print(F("ID: "));
- byte buffer3[18];
- block = 6;
- status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 6, &key, &(mfrc522.uid)); //line 834
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Authentication failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- status = mfrc522.MIFARE_Read(block, buffer3, &len);
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Reading failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- //PRINT ID
- for (uint8_t i = 0; i < 16; i++) {
- Serial.write(buffer3[i] );
- }
- Serial.println(" ");
- //--------------------------------------------------------------------------------------------------------GET GRADE
- Serial.print(F("Grade Level: "));
- byte buffer4[18];
- block = 8;
- status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 8, &key, &(mfrc522.uid)); //line 834
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Authentication failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- status = mfrc522.MIFARE_Read(block, buffer4, &len);
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Reading failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- //PRINT GRADE
- for (uint8_t i = 0; i < 16; i++) {
- Serial.write(buffer4[i] );
- }
- Serial.println(" ");
- //-------------------------------------------------------------------------------------------------GET ROOM
- Serial.print(F("Homeroom: "));
- byte buffer5[18];
- block = 10;
- status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 10, &key, &(mfrc522.uid)); //line 834
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Authentication failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- status = mfrc522.MIFARE_Read(block, buffer5, &len);
- if (status != MFRC522::STATUS_OK) {
- Serial.print(F("Reading failed: "));
- Serial.println(mfrc522.GetStatusCodeName(status));
- return;
- }
- //PRINT MANAGER
- for (uint8_t i = 0; i < 16; i++) {
- Serial.write(buffer5[i] );
- }
- Serial.println(" ");
- Serial.println("Card read successfully. Uploading to Google Sheet...");
- //-------------------------------------------------------------------------------------------------
- WiFiClient client;
- //Send RFID card data to Pushingbox API, then Google Sheets
- if (client.connect(WEBSITE, 80))
- {
- client.print("GET /pushingbox?devid="); //Upload data to Google Sheet.
- client.print(devid);
- client.print("&LastName=");
- for (uint8_t i = 0; i < 16; i++)
- {
- client.print(buffer2[i] );
- }
- client.print("&FirstName=");
- for (uint8_t i = 0; i < 16; i++)
- {
- client.print(buffer1[i] );
- }
- client.print("&IdNumber=");
- for (uint8_t i = 0; i < 16; i++)
- {
- client.print(buffer3[i] );
- }
- client.print("&Grade=");
- for (uint8_t i = 0; i < 16; i++)
- {
- client.print(buffer4[i] );
- }
- client.print("&Manager=");
- for (uint8_t i = 0; i < 16; i++)
- {
- client.print(buffer5[i] );
- }
- Serial.println(F("\n**Card data successfully sent to Google Sheet.**\n"));
- //Necessary Info for Upload
- client.println(" HTTP/1.1");
- client.print("Host: ");
- client.println(WEBSITE);
- client.println("User-Agent: ESP8266/1.0");
- client.println("Connection: close");
- client.println();
- delay(1000); //change value if you want to read cards faster
- mfrc522.PICC_HaltA();
- mfrc522.PCD_StopCrypto1();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement