Advertisement
safwan092

Untitled

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