Advertisement
Guest User

Untitled

a guest
Sep 11th, 2022
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1.  
  2. #include <ESP8266WebServer.h>
  3. #include <ESP8266HTTPClient.h>
  4. #include <WiFiClient.h>
  5. #include <HttpClient.h>
  6. #include <SPI.h>
  7. #include <MFRC522.h> // https://github.com/miguelbalboa/rfid
  8.  
  9. #define SS_PIN D4 // SDA - D2
  10. #define RST_PIN D3 // RST - D1
  11. MFRC522 mfrc522(SS_PIN, RST_PIN); // Создаём экземпляр класса MFRC522.
  12.  
  13. #define ON_Board_LED 2 // Светодиод горит пока карта поднесена к считывателю
  14.  
  15. const char* ssid = "wifi";
  16. const char* password = "Heslowifi";
  17.  
  18. ESP8266WebServer server(80); // Сервер на 80 порту
  19.  
  20. int readsuccess;
  21. byte readcard[4];
  22. char str[32] = "";
  23. String StrUID;
  24. int akce = 0; // toto změnit podle toho jaké tlačítko bylo zmáčknuto
  25. bool prichodBtn = false;
  26. bool odchodBtn = false;
  27.  
  28.  
  29. void setup() {
  30. Serial.begin(9600); // Скорость серийного монитора
  31. SPI.begin(); // Инициализация шины SPI
  32. mfrc522.PCD_Init(); // Инициализация считывателя MFRC522
  33. delay(500);
  34.  
  35. WiFi.begin(ssid, password); // Соединение с WiFi роутером
  36. Serial.println("");
  37. pinMode(D0, INPUT_PULLUP); // prichod
  38. pinMode(D1, INPUT_PULLUP); // odchod
  39. pinMode(D2, INPUT_PULLUP); // registrace
  40. pinMode(ON_Board_LED, OUTPUT);
  41. digitalWrite(ON_Board_LED, HIGH); // Выключаем светодиод
  42.  
  43. Serial.print("Подключение ");
  44. while (WiFi.status() != WL_CONNECTED) {
  45. Serial.print(".");
  46. // Мигает при подключении к роутеру
  47. digitalWrite(ON_Board_LED, LOW);
  48. delay(250);
  49. digitalWrite(ON_Board_LED, HIGH);
  50. delay(250);
  51. }
  52. digitalWrite(ON_Board_LED, HIGH); // Выключаем светодиод после соединения с роутером
  53. // Выводим IP адрес при подключении к WIFI сети
  54. Serial.println("");
  55. Serial.print("Подключение к сети : ");
  56. Serial.println(ssid);
  57. Serial.print("IP адрес: ");
  58. Serial.println(WiFi.localIP());
  59.  
  60. Serial.println("Приложите карту или брелок, чтобы увидеть UID");
  61. Serial.println("");
  62. }
  63.  
  64.  
  65. void loop() {
  66. WiFiClient c;
  67. HTTPClient http; // Получаем ID карты в функции getid() и сохраняем в переменную
  68.  
  69. if (readsuccess) {
  70. digitalWrite(ON_Board_LED, LOW);
  71. // Объявляем объект класса HttpClient
  72. // prichod
  73. if (digitalRead(D0) == LOW){
  74. Serial.println("Zmacknuty prichod");
  75. String UIDresultSend;
  76. UIDresultSend = StrUID;
  77. String postData;
  78. postData = "akce=prichod&UIDresult=" + UIDresultSend;
  79. http.begin(c,"http://192.168.1.249/updateUID.php");
  80. http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Специальный заголовок типа содержимого
  81. int httpCode = http.POST(postData); // Отправляем запрос
  82. Serial.println(UIDresultSend); // Печатаем UID
  83. Serial.println(httpCode); // Печатаем код возврата HTTP
  84. http.end(); // Закрываем соединение
  85. }
  86. // odchod
  87. if (digitalRead(D1) == LOW){
  88. Serial.println("Zmacknuty odchod");
  89. String UIDresultSend;
  90. UIDresultSend = StrUID;
  91. String postData;
  92. postData = "akce=odchod&UIDresult=" + UIDresultSend;
  93. http.begin(c,"http://192.168.1.249/updateUID.php");
  94. http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Специальный заголовок типа содержимого
  95. int httpCode = http.POST(postData); // Отправляем запрос
  96. Serial.println(UIDresultSend); // Печатаем UID
  97. Serial.println(httpCode); // Печатаем код возврата HTTP
  98. http.end(); // Закрываем соединение
  99. }
  100.  
  101. // Переменная для хранения запроса "UIDresult=UIDresultSend"
  102.  
  103. // načtení informací - stránka "Čtení karty"
  104.  
  105. delay(1000);
  106. digitalWrite(ON_Board_LED, HIGH);
  107. }
  108. }
  109. //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
  110. int getid() {
  111. if (!mfrc522.PICC_IsNewCardPresent()) {
  112. return 0;
  113. }
  114. if (!mfrc522.PICC_ReadCardSerial()) {
  115. return 0;
  116. }
  117.  
  118. Serial.print("UID ОТСКАНИРОВАННОЙ КАРТЫ : ");
  119.  
  120. for (int i = 0; i < 4; i++) {
  121. readcard[i] = mfrc522.uid.uidByte[i];
  122. array_to_string(readcard, 4, str);
  123. StrUID = str;
  124. }
  125. mfrc522.PICC_HaltA();
  126. return 1;
  127. }
  128.  
  129.  
  130. void array_to_string(byte array[], unsigned int len, char buffer[]) {
  131. for (unsigned int i = 0; i < len; i++)
  132. {
  133. byte nib1 = (array[i] >> 4) & 0x0F;
  134. byte nib2 = (array[i] >> 0) & 0x0F;
  135. buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
  136. buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
  137. }
  138. buffer[len * 2] = '\0';
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement