Skip to content

Instantly share code, notes, and snippets.

@Links2004
Forked from Hassanbenlebsir/RFIDreader_esp
Last active December 7, 2015 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Links2004/886ce7b3b86a4231631f to your computer and use it in GitHub Desktop.
Save Links2004/886ce7b3b86a4231631f to your computer and use it in GitHub Desktop.
/*
* This sketch sends a message to a TCP server
*
*/
#include <SPI.h>
#include "MFRC522.h"
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
// declaration
WiFiClient client;
byte myCards[] = { 0x06, 0x28, 0xFE, 0xE5, 0x44, 0x71, 0x2D, 0xA4 };
int successRead; //if we get a succesful read of the card this variable turn to 1 and if we didn't 0
int cansend = 1;
byte dummy = 0x00;
byte readCard[4]; // Stores scanned ID read from RFID Module
#define RST_PIN 4 // RST-PIN für RC522 - RFID - SPI - Modul GPIO15
#define SS_PIN 2 // SDA-PIN für RC522 - RFID - SPI - Modul GPIO2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
//fonc
void setup() {
Serial.begin(115200);
delay(10);
SPI.begin();
mfrc522.PCD_Init(); // Init MFRC522
// We start by connecting to a WiFi network
//WiFiMulti.addAP("virginmedia8953826", "pmafvcty");
WiFiMulti.addAP("VHRLuna", "1VhrLondon2");
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
int getID() {
// Getting ready for Reading PICCs
if(!mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if(!mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
//Serial.println("Scanned UID:");
for(int i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
//Serial.print(readCard[i], HEX);
}
//Serial.println("");
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
void Success() {
}
void Error() {
Serial.println("Stranger");
}
void loop() {
const uint16_t port = 2222;
const char * host = "10.0.0.124"; // ip or dns
bool cleanup = false;
do {
successRead = getID();
delay(0);
} while(!successRead);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
if(readCard[0] == myCards[4] && readCard[1] == myCards[5] && readCard[2] == myCards[6] && readCard[3] == myCards[7]) //checking for blue card
{
Serial.println("Jose Molina perez");
Success();
for(int i = 0; i < 4; i++)
dummy = readCard[i]; // removing previous stored value from the readCard variable
successRead = 0;
} else if(readCard[0] == myCards[0] && readCard[1] == myCards[1] && readCard[2] == myCards[2] && readCard[3] == myCards[3]) //checking for white card
{
if(cansend == 1) {
if(!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
client.print("username + :has connected.:Connect:+lastname+:+txt_Time.getText()");
client.flush();
cansend = 2;
cleanup = true;
} else if(cansend == 2) {
if(client.connected()) {
client.print("username + : :Disconnect:+ lastname + :ClockedOut at :+txt_Time.getText()");
client.flush();
client.stop();
}
cansend = 1;
cleanup = true;
}
if(cleanup) {
for(int i = 0; i < 4; i++)
dummy = readCard[i]; // removing previous stored value from the readCard variable
} else {
//calling the success function
Success(); //calling the success function
}
}
//delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment