Created
September 16, 2017 15:10
-
-
Save auckenox/55728c4cbf78b418a38966848c61a00d to your computer and use it in GitHub Desktop.
wemos d1 mini RFID-RC522 send card uid to webserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <SPI.h> | |
#include <MFRC522.h> | |
/* | |
hardware: RFID-RC522, wemos d1 mini, some rfid tags, wifi | |
software: MFRC522 library, webserver (on the other side) | |
wireing: http://lazyzero.de/elektronik/esp8266/rfid_mqtt/start | |
get the MFRC522 arduino library from: | |
get the library at https://github.com/miguelbalboa/rfid | |
this example will send the cards uid to the webserver you define in the host variable. | |
i use it to change playlists on a musicplayer | |
*/ | |
#define SS_PIN D8 //Pin on WeMos D1 Mini | |
#define RST_PIN D3 //Pin on WeMos D1 Mini | |
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class | |
MFRC522::MIFARE_Key key; | |
// network settings | |
const char* ssid = "wifi-ssid"; | |
const char* password = "wifi-password"; | |
const char* host = "192.168.1.105"; | |
const int httpPort = 80; | |
void setup() | |
{ | |
pinMode(D1, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("hello, this is a RFID node"); | |
// Connect to WiFi | |
Serial.print("Connecting to WiFi "); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(624); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("############################################################"); | |
Serial.print("Connected to SSID: "); | |
Serial.println(ssid); | |
Serial.print("My IP: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("send rfuid to host: "); | |
Serial.print(host); | |
Serial.print(":"); | |
Serial.println(httpPort); | |
Serial.println("############################################################"); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println("ready, waiting for rfid card to hit me"); | |
Serial.println(""); | |
SPI.begin(); | |
rfid.PCD_Init(); | |
} | |
void loop(void) { | |
delay(150); | |
handleRFID(); | |
} | |
void handleRFID() { | |
if (!rfid.PICC_IsNewCardPresent()) return; | |
if (!rfid.PICC_ReadCardSerial()) return; | |
String card_uid = printHex(rfid.uid.uidByte, rfid.uid.size); | |
String ausgabe = "/rfuid/" + String(card_uid); | |
Serial.print("sending card_uid to server: "); | |
Serial.print(card_uid); | |
Serial.print(" - "); | |
WiFiClient client; | |
if (!client.connect(host, httpPort)) { | |
Serial.print("[FAILED] (connection to "); | |
Serial.print(host); | |
Serial.println(" failed)"); | |
return; | |
} | |
client.print(String("GET ") + String(ausgabe) + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
Serial.print("[OK]"); | |
delay(1200); | |
} | |
String printHex(byte *buffer, byte bufferSize) { | |
String id = ""; | |
for (byte i = 0; i < bufferSize; i++) { | |
id += buffer[i] < 0x10 ? "0" : ""; | |
id += String(buffer[i], HEX); | |
} | |
return id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment