#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MFRC522.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#define RST_PIN 20 // RST-PIN for RC522 - RFID - SPI - Module GPIO15
#define SS_PIN 2 // SDA-PIN for RC522 - RFID - SPI - Module GPIO2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
#define Relay 10
#define BlueLed 15
#define GreenLed 0
#define RedLed 3
//SSD1306 display(0x3c, 4, 5);
#define OLED_RESET D5
/* Object named display, of the class Adafruit_SSD1306 */
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//Wireless name and password
const char* ssid = "******"; // replace with you wireless network name
const char* password = "****"; //replace with you wireless network password
// Remote site information
String host = "****"; // IP address of your local server
String url = "/test.txt"; // folder location of the txt file with the RFID cards identificatio, p.e. "/test.txt" if on the root of the server
int time_buffer = 5000; // amount of time in miliseconds that the relay will remain open
void setup() {
pinMode(Relay, OUTPUT);
digitalWrite(Relay, 0);
//display.init();
//display.flipScreenVertically();
//display.setContrast(255);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); /* Initialize display with address 0x3C */
Serial.begin(115200); // Initialize serial communications
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
// We start by connecting to a WiFi network
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
wifi_connected();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
leds_off();
delay(3000);
}
void leds_off() {
analogWrite(BlueLed, 0); // turn the LED off
analogWrite(GreenLed, 0); // turn the LED off
analogWrite(RedLed, 0); // turn the LED off
//display.clear();
//display.display();
display.clearDisplay(); /* Clear display */
display.display();
}
void reject() {
// Align text vertical/horizontal center
//display.clear();
// display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
// display.setFont(ArialMT_Plain_16);
// display.drawString(DISPLAY_WIDTH / 2, 10 + (DISPLAY_HEIGHT / 4), "NOT");
// display.drawString(DISPLAY_WIDTH / 2, 30 + (DISPLAY_HEIGHT / 4), "AUTHORIZED");
// display.display();
display.clearDisplay(); /* Clear display */
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("TEXT GOES HERE");
display.setCursor(0, 30);
display.println("TEXT GOES HERE");
display.setCursor(0, 30);
display.println("TEXT GOES HERE");
display.setCursor(0, 40);
display.println("TEXT GOES HERE");
display.display();
analogWrite(RedLed, 767); // turn the Red LED on
delay(2000);
leds_off();
}
void authorize() {
// Align text vertical/horizontal center
// display.clear();
// display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
// display.setFont(ArialMT_Plain_16);
// display.drawString(DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2, "AUTHORIZED");
// display.display();
display.clearDisplay(); /* Clear display */
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("AUTHORIZED");
display.setCursor(0, 30);
display.println("AUTHORIZED");
display.setCursor(0, 30);
display.println("AUTHORIZED");
display.setCursor(0, 40);
display.println("AUTHORIZED");
display.display();
analogWrite(GreenLed, 767); // turn the Green LED on
digitalWrite(Relay, 1);
delay(time_buffer); // wait for a second
digitalWrite(Relay, 0);
leds_off();
}
void wifi_connected () {
// Align text vertical/horizontal center
//display.clear();
// display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
// display.setFont(ArialMT_Plain_16);
// display.drawString(DISPLAY_WIDTH / 2, 10 + (DISPLAY_HEIGHT / 4), "WI-FI");
// display.drawString(DISPLAY_WIDTH / 2, 20 + (DISPLAY_HEIGHT / 2), "CONNECTED");
// display.display();
display.clearDisplay(); /* Clear display */
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("WIFI CONNECTED");
display.setCursor(0, 30);
display.println("HOST: "+host);
display.setCursor(0, 30);
display.println("DOSYA: ");
display.setCursor(0, 40);
display.println(url);
display.display();
delay(3000);
}
void connection_failed() {
// Align text vertical/horizontal center
// display.clear();
// display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
// display.setFont(ArialMT_Plain_16);
// display.drawString(DISPLAY_WIDTH / 2, 10 + (DISPLAY_HEIGHT / 4), "CONNECTION");
// display.drawString(DISPLAY_WIDTH / 2, 20 + (DISPLAY_HEIGHT / 2), "FAILED");
// display.display();
display.clearDisplay(); /* Clear display */
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("FAILED");
display.setCursor(0, 30);
display.println("FAILED");
display.setCursor(0, 30);
display.println("FAILED");
display.setCursor(0, 40);
display.println("FAILED");
display.display();
delay(3000);
leds_off();
}
// Helper routine to dump a byte array as hex values to Serial
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
void loop() {
int authorized_flag = 0;
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
delay(50);
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
delay(50);
return;
}
////-------------------------------------------------RFID----------------------------------------------
// Shows the card ID on the serial console
String content = "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println();
content.toUpperCase();
Serial.println("Cart read:" + content);
////-------------------------------------------------SERVER----------------------------------------------
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
connection_failed();
return;
}
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1rn" + "Host: " + host + "rn" + "Connection: closernrn");
delay(10);
// Read all the lines of the reply from server and print them to Serial
String line;
while (client.available()) {
line = client.readStringUntil('n');
if (line == content) {
authorized_flag = 1;
}
}
if (authorized_flag == 1) {
Serial.println("AUTHORIZED");
authorize();
}
else {
Serial.println("NOT AUTHORIZED");
reject();
}
} ESP8266 ile RFID kart okuyucu yetkilendirme
1
●127
- 25-03-2020, 17:43:23Kimlik doğrulama veya yönetimden onay bekliyor.Merhabalar esp8266 ile kart okuyucu projem var ama text dosyasına kartın ID lerini düzgün bir şekilde yazmama rağmen kod reject yiyip serial monitörde "NOT AUTHORIZED " yazıyor. Kartların Idsi hiçbir şekilde okunmuyor. Dosyayı esp'ye upload ettim, xampp ile test.txt dosyasına local serverdan ulaşmaya çalıştım ama nafile. Reject olayını nasıl çözebilirim ?
- 26-03-2020, 02:16:14cok fazla kod var.. o yüzden direk sondan bakmaya basladim..
not authorized demesinin sebebi flag degerinin 1 haricinde bir deger olmasi, hemen üstündeki while döngüsü dikkatimi cekti, tabi client'dan gelen string ne bilmiyorum ama bence o "line" degiskenini yazdirip kontrol etsen o zaman content ile esit olup olmadigini anlarsin belki whitespace karakter yüzünden yapabilir (trim komutu kullanabilirsin mesela).. Kisacasi server ne yolluyor onu bir ögrenip sonra content ile karsilastirman daha mantikli..