Skip to content

Instantly share code, notes, and snippets.

@Links2004
Forked from Hassanbenlebsir/RFID_esp8266_wifi
Last active December 6, 2015 12:13
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/5fe3c69964840216ca71 to your computer and use it in GitHub Desktop.
Save Links2004/5fe3c69964840216ca71 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 <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
// declaration
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
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");
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 = "192.168.0.21"; // ip or dns
do {
successRead = getID();
delay(0); // new
} while(!successRead);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if(!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
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
{
client.print("username + :has connected.:Connect:+lastname+:+txt_Time.getText()"); //normal message
Success(); //calling the success function
for(int i = 0; i < 4; i++)
dummy = readCard[i]; // removing previous stored value from the readCard variable
} else {
Error(); //calling the error function
}
// This will send the request to the server
//read back one line from server
String line = client.readStringUntil('\r');
client.println(line);
Serial.println("closing connection");
//client.stop();
Serial.println("wait 5 sec...");
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment