Advertisement
safwan092

Untitled

Feb 18th, 2024 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include <Adafruit_Fingerprint.h>
  4.  
  5. #define greenLEDpin 21
  6. #define redLEDpin 19
  7. #define buzzerPin 18
  8. #define RXD2 16
  9. #define TXD2 17
  10.  
  11. #define mySerial Serial2
  12. Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
  13. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  14. const char * ssid = "network6";
  15. const char * password = "123456789";
  16. String GOOGLE_SCRIPT_ID = "------mKRkBEuUqM-f3K7aYwZFHVy8osDsOB8MDdOSNWbfgLQLsaXF4AXGh_rfkO1kEnO1";
  17.  
  18. String userName [3] = {"1Safwan%20First",
  19. "2Mhmad%20Second",
  20. "%D9%85%D8%AD%D9%85%D8%AF%20%D8%B1%D8%AC%D8%A8",
  21. };
  22. String userIDNumber [3] = {"11223344",
  23. "21223344",
  24. "32334455"
  25. };
  26. int userState [3] = {0,
  27. 0,
  28. 0
  29. };
  30. String SteteToSend[2] = {"Signed%20OUT", "Signed%20IN"};
  31.  
  32. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  33.  
  34. void setup() {
  35. Serial.begin(9600);
  36. pinMode(greenLEDpin, OUTPUT);
  37. pinMode(redLEDpin, OUTPUT);
  38. pinMode(buzzerPin, OUTPUT);
  39. digitalWrite(greenLEDpin, LOW);
  40. digitalWrite(redLEDpin, LOW);
  41. digitalWrite(buzzerPin, LOW);
  42. controlOutputs(1, 0, 0, 100);
  43. controlOutputs(0, 1, 0, 100);
  44. controlOutputs(0, 0, 1, 100);
  45. controlOutputs(0, 1, 0, 100);
  46. initFingerPrint();
  47. WiFi_Setup();
  48. }//end of Setup
  49.  
  50. void loop() {
  51. getFingerprintID();
  52. }//end of LOOP
  53.  
  54.  
  55. void WiFi_Setup() {
  56. WiFi.mode(WIFI_STA);
  57. WiFi.begin(ssid, password);
  58. Serial.print("Connecting to Wi-Fi");
  59. while (WiFi.status() != WL_CONNECTED) {
  60. delay(500);
  61. Serial.print(".");
  62. }
  63. Serial.println("OK");
  64. }
  65.  
  66.  
  67. void Send_Data_To_Google_Sheets(String AA, String BB, String CC) {
  68. String param;
  69. param = "dataA=" + AA;
  70. param += "&dataB=" + BB;
  71. param += "&dataC=" + CC;
  72. write_to_google_sheet(param);
  73. }
  74.  
  75. void write_to_google_sheet(String params) {
  76. HTTPClient http;
  77. String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?" + params;
  78. //Serial.print(url);
  79. Serial.println("Posting Data to Google Sheets");
  80. //---------------------------------------------------------------------
  81. //starts posting data to google sheet
  82. http.begin(url.c_str());
  83. http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  84. int httpCode = http.GET();
  85. Serial.print("HTTP Status Code: ");
  86. Serial.println(httpCode);
  87. //---------------------------------------------------------------------
  88. //getting response from google sheet
  89. String payload;
  90. if (httpCode > 0) {
  91. payload = http.getString();
  92. Serial.println("Payload: " + payload);
  93. }
  94. //---------------------------------------------------------------------
  95. http.end();
  96. }
  97.  
  98.  
  99.  
  100. void initFingerPrint() {
  101. Serial2.begin(57600, SERIAL_8N1, RXD2, TXD2);
  102. while (!Serial);
  103. delay(100);
  104. Serial.println("\n\nAdafruit finger detect test");
  105. delay(5);
  106. if (finger.verifyPassword()) {
  107. Serial.println("Found fingerprint sensor!");
  108. } else {
  109. Serial.println("Did not find fingerprint sensor :(");
  110. while (1) {
  111. delay(1);
  112. }
  113. }
  114.  
  115. Serial.println(F("Reading sensor parameters"));
  116. finger.getParameters();
  117. Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  118. Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  119. Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  120. Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  121. Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  122. Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  123. Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);
  124.  
  125. finger.getTemplateCount();
  126.  
  127. if (finger.templateCount == 0) {
  128. Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
  129. }
  130. else {
  131. Serial.println("Waiting for valid finger...");
  132. Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  133. }
  134. }
  135.  
  136.  
  137.  
  138. uint8_t getFingerprintID() {
  139. uint8_t p = finger.getImage();
  140. switch (p) {
  141. case FINGERPRINT_OK:
  142. Serial.println("Image taken");
  143. controlOutputs(0, 1, 1, 250);
  144. break;
  145. case FINGERPRINT_NOFINGER:
  146. Serial.println("No finger detected");
  147. return p;
  148. case FINGERPRINT_PACKETRECIEVEERR:
  149. Serial.println("Communication error");
  150. return p;
  151. case FINGERPRINT_IMAGEFAIL:
  152. Serial.println("Imaging error");
  153. return p;
  154. default:
  155. Serial.println("Unknown error");
  156. return p;
  157. }
  158.  
  159. // OK success!
  160.  
  161. p = finger.image2Tz();
  162. switch (p) {
  163. case FINGERPRINT_OK:
  164. Serial.println("Image converted");
  165. break;
  166. case FINGERPRINT_IMAGEMESS:
  167. Serial.println("Image too messy");
  168. return p;
  169. case FINGERPRINT_PACKETRECIEVEERR:
  170. Serial.println("Communication error");
  171. return p;
  172. case FINGERPRINT_FEATUREFAIL:
  173. Serial.println("Could not find fingerprint features");
  174. return p;
  175. case FINGERPRINT_INVALIDIMAGE:
  176. Serial.println("Could not find fingerprint features");
  177. return p;
  178. default:
  179. Serial.println("Unknown error");
  180. return p;
  181. }
  182.  
  183. // OK converted!
  184. p = finger.fingerSearch();
  185. if (p == FINGERPRINT_OK) {
  186. Serial.println("Found a print match!");
  187. } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
  188. Serial.println("Communication error");
  189. return p;
  190. } else if (p == FINGERPRINT_NOTFOUND) {
  191. //Serial.println("Did not find a match");
  192. for (int i = 0; i < 3; i++) {
  193. controlOutputs(0, 1, 1, 250);
  194. controlOutputs(0, 0, 0, 50);
  195. }
  196. controlOutputs(0, 1, 0, 50);
  197. return p;
  198. } else {
  199. Serial.println("Unknown error");
  200. return p;
  201. }
  202. if ((finger.fingerID) != -1) {
  203. controlOutputs(1, 0, 1, 250);
  204. controlOutputs(1, 0, 0, 50);
  205. /////////////////////////////////////////////////
  206. if (userState[(finger.fingerID - 1)] == 0) {
  207. userState[(finger.fingerID - 1)] = 1;
  208. }
  209. else {
  210. userState[(finger.fingerID - 1)] = 0;
  211. }
  212. Send_Data_To_Google_Sheets(userName[(finger.fingerID - 1)], userIDNumber[(finger.fingerID - 1)], SteteToSend[userState[(finger.fingerID - 1)]]);
  213. delay(500);
  214. controlOutputs(0, 1, 0, 50);
  215. }
  216. /////////////////////////////////////////////////
  217. // found a match!
  218. Serial.print("Found ID #"); Serial.print(finger.fingerID);
  219. Serial.print(" with confidence of "); Serial.println(finger.confidence);
  220. return finger.fingerID;
  221. }
  222.  
  223. void controlOutputs(int green, int red, int buzzer, int delayTime) {
  224. digitalWrite(greenLEDpin, green);
  225. digitalWrite(redLEDpin, red);
  226. digitalWrite(buzzerPin, buzzer);
  227. delay(delayTime);
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement