Skip to content

Instantly share code, notes, and snippets.

@digideskio
Forked from dennisvandalen/RFIDTableTennis.ino
Created February 10, 2016 13:46
Show Gist options
  • Save digideskio/eb309c05df19f6601cac to your computer and use it in GitHub Desktop.
Save digideskio/eb309c05df19f6601cac to your computer and use it in GitHub Desktop.
RFID and 433mhz to webserver WeMos D1
/*
----------------------------------------------------------------------------
Using MFRC522 library https://github.com/miguelbalboa/rfid
Using ESP8266WiFi library https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi
Using ESP8266HTTPClient library https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266HTTPClient
Using RemoteReceiver library https://github.com/jccprj/RemoteSwitch-arduino-library
MFRC522
Pin layout used:
--------------------------------------
MFRC522 WeMos
Reader/PCD D1
Signal Pin Pin
--------------------------------------
RST/Reset RST D2
SPI SS SDA(SS) SDA / D4
SPI MOSI MOSI D11 / MOSI
SPI MISO MISO D12 / MISO
SPI SCK SCK D13 / SCK
--------------------------------------
433Mhz RF
Pin layout used:
--------------------------------------
433Mhz RF WeMos
Receiver D1
Signal Pin Pin
--------------------------------------
Data Data D8
--------------------------------------
*/
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <RemoteReceiver.h>
#include <SPI.h>
// WiFi Network SSID
const char *ssid = "ssid";
// WiFi Network Password
const char *pass = "password";
// Server IP
const char *ip = "192.168.x.x";
// Server Port
const int port = 80;
// Flag to hold if someone scored
String scored = "0";
// Reset pin (WeMos D1 Pin D2)
#define RST_PIN 2
// SS pin (WeMos D1 Pin D4/SDA/D14)
#define SS_PIN 4
// Init MFRC522
MFRC522 mfrc522(SS_PIN, RST_PIN);
/**
Convert uid buffer to String
*/
String getUidAsString(byte *buffer, byte bufferSize) {
String uid = "";
for (byte i = 0; i < bufferSize; i++) {
uid += buffer[i] < 0x10 ? "0" : "";
uid += String(buffer[i], HEX);
}
return uid;
}
/**
Show RFID Reader details
*/
void showReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
}
}
/**
Handle the remote interrupt, set the scored flag
todo: Remove magic numbers
*/
void handleRemoteInterrupt(unsigned long receivedCode, unsigned int period) {
String player = "";
switch (receivedCode) {
case 265353:
case 265349:
player = "1";
break;
case 266793:
case 266789:
player = "2";
break;
}
if (player != "") {
scored = player;
}
}
/**
Ecexute HTTP Request
*/
void httpRequest(String path) {
HTTPClient http;
http.begin(ip, port, path); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
if (httpCode) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == 200) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
}
/**
SetUp the WeMos, init and setup everything
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
delay(250);
Serial.println(F("Booting...."));
Serial.println();
// Initiate the remote receiver, when interupt execute handleRemoteInterrupt()
RemoteReceiver::init(0, 2, handleRemoteInterrupt);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(F("======================================================"));
showReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Ready!"));
Serial.println(F("======================================================"));
Serial.println(F("Scan for Card"));
}
/**
Loop
*/
void loop() {
// if scored then send request
if (scored != "0") {
httpRequest("/score/" + scored + "/");
scored = "0";
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Get uid, stop reading and send request for joining game
String uid = getUidAsString(mfrc522.uid.uidByte, mfrc522.uid.size);
mfrc522.PICC_HaltA();
httpRequest("/join/" + uid + "/");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment