Guest User

Untitled

a guest
Nov 14th, 2019
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h> // Include the Wi-Fi library
  2. #include <SPI.h>
  3. #include <MFRC522.h>
  4.  
  5. //--------------------------------------------------------------------------------------------------------------
  6.  
  7. #define RST_PIN D3 // Configurable, see typical pin layout above
  8. #define SS_PIN D8 // Configurable, see typical pin layout above
  9.  
  10. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
  11.  
  12. const char* ssid = "REDACTED"; // The SSID (name) of the Wi-Fi network you want to connect to
  13. const char* password = "REDACTED"; // The password of the Wi-Fi network
  14. const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
  15. char devid[] = "REDACTED"; //device ID from Pushingbox
  16.  
  17. //--------------------------------------------------------------------------------------------------------------
  18.  
  19. void setup() {
  20. Serial.begin(9600); // Start the Serial communication to send messages to the computer
  21. SPI.begin(); // Init SPI bus
  22. mfrc522.PCD_Init(); // Init MFRC522 card
  23. delay(10);
  24. Serial.println('\n');
  25.  
  26. WiFi.begin(ssid, password); // Connect to the network
  27. Serial.print("Connecting to ");
  28. Serial.print(ssid); Serial.println(" ...");
  29.  
  30. int i = 0;
  31. while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
  32. delay(1000);
  33. Serial.print(++i); Serial.print(' ');
  34. }
  35.  
  36. Serial.println('\n');
  37. Serial.println("Connection established!");
  38. Serial.print("IP address:\t");
  39. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  40. delay(10);
  41. Serial.println('\n');
  42. Serial.println(F("Please scan a card.")); //shows in serial that it is ready to read
  43. }
  44.  
  45. //--------------------------------------------------------------------------------------------------------------
  46.  
  47. void loop()
  48. {
  49.  
  50. // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  51. MFRC522::MIFARE_Key key;
  52. for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  53.  
  54. //some variables we need
  55. byte block;
  56. byte len;
  57. MFRC522::StatusCode status;
  58.  
  59. //--------------------------------------------------------------------------------------------------------------
  60.  
  61. // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  62. if ( ! mfrc522.PICC_IsNewCardPresent()) {
  63. return;
  64. }
  65.  
  66. // Select one of the cards
  67. if ( ! mfrc522.PICC_ReadCardSerial()) {
  68. return;
  69. }
  70.  
  71. Serial.println(F("**Card Detected:**"));
  72.  
  73. //--------------------------------------------------------------------------------------------------------------
  74.  
  75. mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
  76.  
  77. //mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
  78.  
  79. //--------------------------------------------------------------------------------------------------------------
  80.  
  81. Serial.println(F("Name: "));
  82.  
  83. byte buffer1[18];
  84.  
  85. block = 4;
  86. len = 18;
  87.  
  88. //-------------------------------------------------------------------------------------------------GET FIRST NAME
  89.  
  90. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  91. if (status != MFRC522::STATUS_OK) {
  92. Serial.print(F("Authentication failed: "));
  93. Serial.println(mfrc522.GetStatusCodeName(status));
  94. return;
  95. }
  96.  
  97. status = mfrc522.MIFARE_Read(block, buffer1, &len);
  98. if (status != MFRC522::STATUS_OK) {
  99. Serial.print(F("Reading failed: "));
  100. Serial.println(mfrc522.GetStatusCodeName(status));
  101. return;
  102. }
  103.  
  104. //PRINT FIRST NAME
  105. for (uint8_t i = 0; i < 16; i++)
  106. {
  107. if (buffer1[i] != 32)
  108. {
  109. Serial.write(buffer1[i]);
  110. }
  111. }
  112.  
  113. //----------------------------------------------------------------------------------------------------GET LAST NAME
  114.  
  115. byte buffer2[18];
  116. block = 1;
  117.  
  118. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
  119. if (status != MFRC522::STATUS_OK) {
  120. Serial.print(F("Authentication failed: "));
  121. Serial.println(mfrc522.GetStatusCodeName(status));
  122. return;
  123. }
  124.  
  125. status = mfrc522.MIFARE_Read(block, buffer2, &len);
  126. if (status != MFRC522::STATUS_OK) {
  127. Serial.print(F("Reading failed: "));
  128. Serial.println(mfrc522.GetStatusCodeName(status));
  129. return;
  130. }
  131.  
  132. //PRINT LAST NAME
  133. for (uint8_t i = 0; i < 16; i++) {
  134. Serial.write(buffer2[i] );
  135. }
  136. Serial.println(" ");
  137.  
  138.  
  139. //-----------------------------------------------------------------------------------------------------------GET ID
  140. Serial.print(F("ID: "));
  141. byte buffer3[18];
  142. block = 6;
  143.  
  144. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 6, &key, &(mfrc522.uid)); //line 834
  145. if (status != MFRC522::STATUS_OK) {
  146. Serial.print(F("Authentication failed: "));
  147. Serial.println(mfrc522.GetStatusCodeName(status));
  148. return;
  149. }
  150.  
  151. status = mfrc522.MIFARE_Read(block, buffer3, &len);
  152. if (status != MFRC522::STATUS_OK) {
  153. Serial.print(F("Reading failed: "));
  154. Serial.println(mfrc522.GetStatusCodeName(status));
  155. return;
  156. }
  157.  
  158. //PRINT ID
  159. for (uint8_t i = 0; i < 16; i++) {
  160. Serial.write(buffer3[i] );
  161. }
  162. Serial.println(" ");
  163.  
  164. //--------------------------------------------------------------------------------------------------------GET GRADE
  165. Serial.print(F("Grade Level: "));
  166. byte buffer4[18];
  167. block = 8;
  168.  
  169. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 8, &key, &(mfrc522.uid)); //line 834
  170. if (status != MFRC522::STATUS_OK) {
  171. Serial.print(F("Authentication failed: "));
  172. Serial.println(mfrc522.GetStatusCodeName(status));
  173. return;
  174. }
  175.  
  176. status = mfrc522.MIFARE_Read(block, buffer4, &len);
  177. if (status != MFRC522::STATUS_OK) {
  178. Serial.print(F("Reading failed: "));
  179. Serial.println(mfrc522.GetStatusCodeName(status));
  180. return;
  181. }
  182.  
  183. //PRINT GRADE
  184. for (uint8_t i = 0; i < 16; i++) {
  185. Serial.write(buffer4[i] );
  186. }
  187. Serial.println(" ");
  188.  
  189. //-------------------------------------------------------------------------------------------------GET ROOM
  190.  
  191. Serial.print(F("Homeroom: "));
  192. byte buffer5[18];
  193. block = 10;
  194.  
  195. status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 10, &key, &(mfrc522.uid)); //line 834
  196. if (status != MFRC522::STATUS_OK) {
  197. Serial.print(F("Authentication failed: "));
  198. Serial.println(mfrc522.GetStatusCodeName(status));
  199. return;
  200. }
  201.  
  202. status = mfrc522.MIFARE_Read(block, buffer5, &len);
  203. if (status != MFRC522::STATUS_OK) {
  204. Serial.print(F("Reading failed: "));
  205. Serial.println(mfrc522.GetStatusCodeName(status));
  206. return;
  207. }
  208.  
  209. //PRINT MANAGER
  210. for (uint8_t i = 0; i < 16; i++) {
  211. Serial.write(buffer5[i] );
  212. }
  213. Serial.println(" ");
  214. Serial.println("Card read successfully. Uploading to Google Sheet...");
  215.  
  216. //-------------------------------------------------------------------------------------------------
  217.  
  218. WiFiClient client;
  219.  
  220. //Send RFID card data to Pushingbox API, then Google Sheets
  221. if (client.connect(WEBSITE, 80))
  222. {
  223. client.print("GET /pushingbox?devid="); //Upload data to Google Sheet.
  224. client.print(devid);
  225.  
  226. client.print("&LastName=");
  227. for (uint8_t i = 0; i < 16; i++)
  228. {
  229. client.print(buffer2[i] );
  230. }
  231.  
  232. client.print("&FirstName=");
  233. for (uint8_t i = 0; i < 16; i++)
  234. {
  235. client.print(buffer1[i] );
  236. }
  237.  
  238. client.print("&IdNumber=");
  239. for (uint8_t i = 0; i < 16; i++)
  240. {
  241. client.print(buffer3[i] );
  242. }
  243.  
  244. client.print("&Grade=");
  245. for (uint8_t i = 0; i < 16; i++)
  246. {
  247. client.print(buffer4[i] );
  248. }
  249.  
  250. client.print("&Manager=");
  251. for (uint8_t i = 0; i < 16; i++)
  252. {
  253. client.print(buffer5[i] );
  254. }
  255.  
  256.  
  257. Serial.println(F("\n**Card data successfully sent to Google Sheet.**\n"));
  258.  
  259. //Necessary Info for Upload
  260. client.println(" HTTP/1.1");
  261. client.print("Host: ");
  262. client.println(WEBSITE);
  263. client.println("User-Agent: ESP8266/1.0");
  264. client.println("Connection: close");
  265. client.println();
  266.  
  267.  
  268. delay(1000); //change value if you want to read cards faster
  269.  
  270. mfrc522.PICC_HaltA();
  271. mfrc522.PCD_StopCrypto1();
  272.  
  273. }
  274. }
RAW Paste Data