Advertisement
safwan092

[CLEAN] ESP32 + UHF RFID KLM900S + Ultrasonic + Servo - [NO DELAY]

Aug 5th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. /*
  2. Connections:
  3. ------------------------------
  4. Arduino Uno <---> RF-KLM900S
  5. ******************************
  6. 3.3V <---> EN
  7. 5V <---> 5V
  8. GND <---> GND
  9. 3 <---> TTL_R
  10. 2 <---> TTL_T
  11. ------------------------------
  12. */
  13.  
  14. // Libraries
  15. #include <Servo.h>
  16. #include <WiFi.h>
  17. #include <AsyncTCP.h>
  18. #include <ESPAsyncWebServer.h>
  19. #include <AsyncElegantOTA.h>
  20.  
  21. // Pin Definitions
  22. #define trigPin 5 // ✔
  23. #define echoPin 18 // ✔
  24. #define rxPin 16 // ✔
  25. #define txPin 17 // ✔
  26. #define servoPin 13 // ✔
  27.  
  28. // Objects and Communication Chanel
  29. Servo servo1;
  30. AsyncWebServer server(80);
  31.  
  32. // Command to Read RFID Tags Using RF-KLM900S TTL Connector
  33. // Single Read BB 00 22 00 00 22 7E
  34. byte A[] = {0xBB, 0x00, 0x22, 0x00, 0x00, 0x22, 0x7E};
  35.  
  36. // Saved RFID Tags in one String Array
  37. // [1] Card Tag UID : 30 08 33 B2 DD D9 01 40 00 00 00 00
  38. // [2] Smart Tag 1 UID : E2 80 68 94 00 00 50 20 91 08 58 7E
  39. // [3] Smart Tag 2 UID : E2 80 68 94 00 00 50 20 91 08 58 88
  40. String TagNo[] = {"30 08 33 B2 DD D9 01 40 00 00 00 00",
  41. "E2 80 68 94 00 00 50 20 91 08 58 7E",
  42. "E2 80 68 94 00 00 50 20 91 08 58 88"
  43. };
  44.  
  45. // Variables and Flags
  46. const char* ssid = "network";
  47. const char* password = "123456789";
  48. String TagID_RX = "";
  49. static String inputString;
  50. static unsigned long currentTime = 0;
  51. static unsigned long lastTime = 0;
  52. static unsigned long lastTime2 = 0;
  53. static unsigned long lastCharArrivalTime = 0;
  54. long distance;
  55. long duration;
  56. int flag = 0; // Tag was Identifyed before [Flag]
  57. int flag3min = 0; // Show Timer Started Message [Flag]
  58. int count = 0;
  59. int numOfTags = 3;
  60.  
  61.  
  62. void setup() {
  63.  
  64. // Communication Chanel Between Arduino and PC/Laptop/Android Phone
  65. Serial.begin(115200);
  66.  
  67. // Communication Chanel Between Arduino and RF-KLM900S RFID Reader
  68. Serial2.begin(115200, SERIAL_8N1, rxPin, txPin);
  69.  
  70. WiFi.mode(WIFI_STA);
  71. WiFi.begin(ssid, password);
  72. // Wait for connection
  73. while (WiFi.status() != WL_CONNECTED) {
  74. delay(500);
  75. Serial.print(".");
  76. }
  77. Serial.println("");
  78. Serial.print("Connected to ");
  79. Serial.println(ssid);
  80. Serial.print("IP address: ");
  81. Serial.println(WiFi.localIP());
  82.  
  83. server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  84. request->send(200, "text/plain", "Hi! This is a sample response.");
  85. });
  86.  
  87. AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA
  88. server.begin();
  89. Serial.println("HTTP server started");
  90.  
  91.  
  92. // Assign Inputs and Outputs
  93. pinMode(trigPin, OUTPUT);
  94. pinMode(echoPin, INPUT);
  95. servo1.attach(servoPin);
  96.  
  97. // Close Servo Gate
  98. servo1.write(0);
  99.  
  100.  
  101. delay(2000);
  102. }
  103.  
  104. void loop() {
  105.  
  106. AsyncElegantOTA.loop();
  107. // Messure Distance from Ultrasonic Sensor to Object
  108. ultra();
  109.  
  110. lastCharArrivalTime = 0;
  111.  
  112. //////////////////////////////////////////////////////////////////////////////////////////////
  113. //------------------------[Send Read Command To RFID Reader]----------------------------------
  114. //////////////////////////////////////////////////////////////////////////////////////////////
  115. if (millis() - lastTime > 50 && inputString == "") {
  116. Serial2.write(A, sizeof(A));
  117. lastTime = millis();
  118. }
  119. //////////////////////////////////////////////////////////////////////////////////////////////
  120. //------------------------[Read RFID DATA and Convert to HEX]---------------------------------
  121. //////////////////////////////////////////////////////////////////////////////////////////////
  122. if (Serial2.available())
  123. {
  124. byte incomingByte = Serial2.read();
  125. inputString += "0123456789ABCDEF"[incomingByte / 16];
  126. inputString += "0123456789ABCDEF"[incomingByte % 16];
  127. inputString += ' ';
  128. lastCharArrivalTime = millis();
  129. }
  130. //////////////////////////////////////////////////////////////////////////////////////////////
  131. //--------------------------[When Read is Done Analyze Data]----------------------------------
  132. //////////////////////////////////////////////////////////////////////////////////////////////
  133. if (lastCharArrivalTime != 0 && millis() - lastCharArrivalTime >= 10) {
  134.  
  135. // Extract Tag UID from Received RFID Data Stream
  136. TagID_RX = inputString.substring(24, 59);
  137.  
  138. // Loop To Compare All Saved Tag UIDs to the Received tag UID
  139. for (int i = 0; i < numOfTags; i++) {
  140.  
  141. // Compare Saved Tag UID to the Received tag UID (AND) Distance must be less or equal to 200cm
  142. if (!TagID_RX.compareTo(TagNo[i]) && distance <= 200) {
  143. Serial.print("Tag No [");
  144. Serial.print(i + 1);
  145. Serial.println("] Found.");
  146. Serial.println("------------------");
  147.  
  148. // Open Servo Gate
  149. servo1.write(90);
  150.  
  151. delay(1000);
  152.  
  153. // Activate Tag was Identifyed before [Flag]
  154. flag = 1;
  155.  
  156. // Rest Flags and timer
  157. flag3min = 0;
  158. count = 0;
  159. lastTime2 = millis();
  160. }
  161.  
  162. // If tag not found (OR) Distance more than 200cm
  163. else {
  164. // If Tag not Received by the reader for 2,5 Seconds (AND) Tag was Identifyed before
  165. if (count > 50 && flag == 1) {
  166.  
  167. // If Tag is removed from Reader for more than 2,5 Seconds Show message ONE time
  168. if (flag3min == 0) {
  169. Serial.println("3 min Timer Started !!");
  170.  
  171. // Activate Show Timer Started Message [Flag]
  172. flag3min = 1;
  173. }
  174.  
  175. // If the current time after the Timer is activated is more than 3 Minutes Do Commands
  176. currentTime = millis();
  177. if (currentTime - lastTime2 > 180000) { // 180000 = 180,000 milliSecond = 180 Seconds = 3 Minutes
  178.  
  179. // Close Servo Gate
  180. servo1.write(0);
  181.  
  182. // Rest Flags and timer
  183. count = 0;
  184. flag = 0;
  185. flag3min = 0;
  186. lastTime2 = millis();
  187. }
  188. }
  189.  
  190. // Increase the count every 50 ms
  191. count++;
  192.  
  193. }
  194. }
  195.  
  196. // Rest Variable and timer
  197. inputString = "";
  198. lastCharArrivalTime = 0;
  199.  
  200. }
  201. //////////////////////////////////////////////////////////////////////////////////////////////
  202. }
  203.  
  204. void ultra() {
  205. digitalWrite(trigPin, LOW);
  206. delayMicroseconds(2);
  207. digitalWrite(trigPin, HIGH);
  208. delayMicroseconds(10);
  209. digitalWrite(trigPin, LOW);
  210. duration = pulseIn(echoPin, HIGH);
  211. distance = duration * 0.034 / 2;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement