Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WebServer.h>
- #include <ESP8266HTTPClient.h>
- #include <WiFiClient.h>
- #include <HttpClient.h>
- #include <SPI.h>
- #include <MFRC522.h> // https://github.com/miguelbalboa/rfid
- #define SS_PIN D4 // SDA - D2
- #define RST_PIN D3 // RST - D1
- MFRC522 mfrc522(SS_PIN, RST_PIN); // Создаём экземпляр класса MFRC522.
- #define ON_Board_LED 2 // Светодиод горит пока карта поднесена к считывателю
- const char* ssid = "wifi";
- const char* password = "Heslowifi";
- ESP8266WebServer server(80); // Сервер на 80 порту
- int readsuccess;
- byte readcard[4];
- char str[32] = "";
- String StrUID;
- int akce = 0; // toto změnit podle toho jaké tlačítko bylo zmáčknuto
- bool prichodBtn = false;
- bool odchodBtn = false;
- void setup() {
- Serial.begin(9600); // Скорость серийного монитора
- SPI.begin(); // Инициализация шины SPI
- mfrc522.PCD_Init(); // Инициализация считывателя MFRC522
- delay(500);
- WiFi.begin(ssid, password); // Соединение с WiFi роутером
- Serial.println("");
- pinMode(D0, INPUT_PULLUP); // prichod
- pinMode(D1, INPUT_PULLUP); // odchod
- pinMode(D2, INPUT_PULLUP); // registrace
- pinMode(ON_Board_LED, OUTPUT);
- digitalWrite(ON_Board_LED, HIGH); // Выключаем светодиод
- Serial.print("Подключение ");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- // Мигает при подключении к роутеру
- digitalWrite(ON_Board_LED, LOW);
- delay(250);
- digitalWrite(ON_Board_LED, HIGH);
- delay(250);
- }
- digitalWrite(ON_Board_LED, HIGH); // Выключаем светодиод после соединения с роутером
- // Выводим IP адрес при подключении к WIFI сети
- Serial.println("");
- Serial.print("Подключение к сети : ");
- Serial.println(ssid);
- Serial.print("IP адрес: ");
- Serial.println(WiFi.localIP());
- Serial.println("Приложите карту или брелок, чтобы увидеть UID");
- Serial.println("");
- }
- void loop() {
- WiFiClient c;
- HTTPClient http; // Получаем ID карты в функции getid() и сохраняем в переменную
- if (readsuccess) {
- digitalWrite(ON_Board_LED, LOW);
- // Объявляем объект класса HttpClient
- // prichod
- if (digitalRead(D0) == LOW){
- Serial.println("Zmacknuty prichod");
- String UIDresultSend;
- UIDresultSend = StrUID;
- String postData;
- postData = "akce=prichod&UIDresult=" + UIDresultSend;
- http.begin(c,"http://192.168.1.249/updateUID.php");
- http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Специальный заголовок типа содержимого
- int httpCode = http.POST(postData); // Отправляем запрос
- Serial.println(UIDresultSend); // Печатаем UID
- Serial.println(httpCode); // Печатаем код возврата HTTP
- http.end(); // Закрываем соединение
- }
- // odchod
- if (digitalRead(D1) == LOW){
- Serial.println("Zmacknuty odchod");
- String UIDresultSend;
- UIDresultSend = StrUID;
- String postData;
- postData = "akce=odchod&UIDresult=" + UIDresultSend;
- http.begin(c,"http://192.168.1.249/updateUID.php");
- http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Специальный заголовок типа содержимого
- int httpCode = http.POST(postData); // Отправляем запрос
- Serial.println(UIDresultSend); // Печатаем UID
- Serial.println(httpCode); // Печатаем код возврата HTTP
- http.end(); // Закрываем соединение
- }
- // Переменная для хранения запроса "UIDresult=UIDresultSend"
- // načtení informací - stránka "Čtení karty"
- delay(1000);
- digitalWrite(ON_Board_LED, HIGH);
- }
- }
- //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
- int getid() {
- if (!mfrc522.PICC_IsNewCardPresent()) {
- return 0;
- }
- if (!mfrc522.PICC_ReadCardSerial()) {
- return 0;
- }
- Serial.print("UID ОТСКАНИРОВАННОЙ КАРТЫ : ");
- for (int i = 0; i < 4; i++) {
- readcard[i] = mfrc522.uid.uidByte[i];
- array_to_string(readcard, 4, str);
- StrUID = str;
- }
- mfrc522.PICC_HaltA();
- return 1;
- }
- void array_to_string(byte array[], unsigned int len, char buffer[]) {
- for (unsigned int i = 0; i < len; i++)
- {
- byte nib1 = (array[i] >> 4) & 0x0F;
- byte nib2 = (array[i] >> 0) & 0x0F;
- buffer[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
- buffer[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
- }
- buffer[len * 2] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement