Thanks for the answer.
This is the code that works on the UNO R3 with he Ethernet-Shield.
If I use this code on the Uno/EthernetShield combination it shows the correct Version v.2 for the NFC-Reader
Using this code on the Leonardo ETH doesnt detect the Reader (// When 0x00 or 0xFF is returned, communication probably failed) in the "ShowReaderDetails"
regards
Don
THis is the sketch I am using right now
// KEYPAD --------------
#include <Keypad.h>
// SPI & RFID ----------
#include <SPI.h>
#include <MFRC522.h>
// ETHERNET, WEB & API -
#include <Ethernet.h>
// WEBADMIN PAGE -------
#include <RestClient.h>
// ########## KEYPAD Defs ##########
const byte ROWS = 4;
const byte COLS = 3;
//Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { 3, 2, A5, 0 };
byte colPins[COLS] = { 7, 6, 5 };
// KEYPAD CREATE ---------
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// ########## NFC Defs ##########
#define SS_PIN 8
#define RST_PIN 9
// NFC CREATE -------------
MFRC522 mfrc522(SS_PIN, RST_PIN);
// ########## WEB SERVER DEFS ##########
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
unsigned int port = 80;
EthernetServer server(port);
EthernetClient client;
// ########## WEB API DEFS #########
char apiServer[15+1] = "192.168.1.10";
char apiPath[50+1] = "/keypad";
RestClient restAPI = RestClient(apiServer);
// ########## GLOBAL VARS ##########
unsigned int status; // 0 = idle, 1 = card scanned
char uid[20+1]; // 2char for each byte - 4, 7 and 10 Byte cards possible with 20 chars
char pin[20+1]; // 19 keypresses max!!! 1 char reserve to prevent overflow
// ########## LEDs and Buzzer ##########
const int led1 = A1; //red
const int led2 = A2; //yellow
const int led3 = A3; //green
const int led4 = A4; //blue
const int buzzer = A0;
// ########## TIMEOUT FOR USAGE ##########
unsigned long currentMillis;
unsigned long lastUsageMillis = 0;
unsigned int usageTimeOut = 4000;
// setup
void setup() {
// Open serial
Serial.begin(9600);
// KEYPAD INIT ----------
// LED AND BUZZER INIT
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(buzzer,OUTPUT);
// CS-Lines for SD and Ethernet
pinMode(4, OUTPUT);
pinMode(10, OUTPUT);
// RFID READER INIT -----
SPI.begin();
mfrc522.PCD_Init(); // Init RC522
Serial.print(F("NFC:"));
ShowReaderDetails(); // Show RC522 Card Reader details
// ETHERNET,API & WEB config & other parameters
delay(1000); // !!! Wait before init of ethernet to not conflict with NFC init
EthernetServer server(port);
restAPI = RestClient(apiServer);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
Serial.print(F("IP is:"));
Serial.println(Ethernet.localIP());
Serial.println("READY...");
}
// main loop
void loop() {
currentMillis = millis();
if(currentMillis - lastUsageMillis > usageTimeOut && status == 1){
// no input or card for more than x seconds (x=usageTimeout)
buzz(1,900);
setBack();
}
key();
nfc();
delay(1);
}
// set card uid and pin to zero
void setBack(){
uid[0] = 0;
pin[0] = 0;
status = 0;
buzz(0,0);
led('r', 0, 0, 0, 0);
led('y', 0, 0, 0, 0);
led('g', 0, 0, 0, 0);
led('b', 0, 0, 0, 0);
//freeRam ();
}
// check for pin code
void key (){
if(status == 1){ // 1 = a card was already scanned
char key = kpd.getKey();
if(key)
{
// Serial.println(key);
// prevent timeout
lastUsageMillis = currentMillis;
led('g', 2, 1, 300, 1);
int len = strlen(pin);
pin[len]=key;
pin[len+1]=0;
if(pin[0] == '*'){ //log out request
led('g', 1, 0, 0, 0);
sendRequest();
}
if(strlen(pin) == 4 && pin[0] != '#'){
led('g', 1, 0, 0, 0);
sendRequest();
}
else if(key == '#' && strlen(pin) > 2 && pin[0] == '#'){ //admin command mode
led('g', 1, 0, 0, 0);
sendRequest();
}
else if(strlen(pin) > 19){
// prevent overflowing pin array
led('g',0,0,0,0);
led('y',0,0,0,0);
led('r',1,0,0,0);
buzz(1,900);
delay(1000);
// set back uid and pin to idle mode and unlight led(s)
setBack();
}
}
}
}
// check for nfc card
void nfc(){
// Look for new cards dont work
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
// Show some details of the scanned card
if (mfrc522.uid.size != strlen(uid)/2){
// normally uid = \0 in idle mode so if uid is present ... its new
// prevent timeout
unsigned int i;
lastUsageMillis = currentMillis;
for (i = 0; i < mfrc522.uid.size; i++)
{
char buf[3];
// make hex strings from uidBytes
sprintf(buf, "%02x", mfrc522.uid.uidByte*); // hex 2 digit formatting with leading 0 of Bytes*
// each hex value has 2 "digits" now
_ uid[i*2] = buf[0]; // i=0 -> uid[0], i= 1 -> uid[2]_
_ uid[(i2)+1] = buf[1]; // i=0 -> uid[1], i= 1 -> uid[3]_
}
_uid[mfrc522.uid.size2] = 0;_
Serial.println(uid);
- buzz(1,300);*
- led('y',1,0,0,0);*
- status = 1;*
- // Card Type*
- Serial.print(F("card type: "));*
- byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);*
- Serial.println(mfrc522.PICC_GetTypeName(piccType));*
- }*
}
}
// led functions for visual response/feedback
void led(char color, unsigned int val, unsigned int times, unsigned int msec, unsigned int buzzerOn){
// led = 1-4 (1=red, 2=yellow, 3=green, 4=blue )
unsigned int pin;
switch (color) {
case 'r':
pin = led1;
break;
case 'y':
pin = led2;
break;
case 'g':
pin = led3;
break;
default:
- pin = led4;*
}
// val = 0-2 (0 = off, 1 = on, 2 = pattern)
unsigned int interval = msec/times/2;
unsigned int i;
switch (val) {
case 1:
digitalWrite(pin,HIGH);
break;
case 2:
for (i = 0; i < times; i++)
{
- digitalWrite(pin,HIGH);*
- if(buzzerOn == 1){*
- digitalWrite(buzzer,HIGH);*
- }*
- delay(interval);*
- digitalWrite(pin,LOW);*
- if(buzzerOn == 1){*
- digitalWrite(buzzer,LOW);*
- }*
- if(times > 1){*
- delay(interval);*
- }*
}
break;
default:
- digitalWrite(pin,LOW);*
}
}
// buzzer function for acoustic response/feedback
void buzz(unsigned int times, unsigned int msec){
if(times == 0){
digitalWrite(buzzer,LOW);
}
else{
unsigned int interval = msec/times/2;
unsigned int i;
for (i = 0; i < times; i++)
{
- digitalWrite(buzzer,HIGH);*
- delay(interval);*
- digitalWrite(buzzer,LOW);*
- if(times > 1){*
- delay(interval);*
- }*
}
}
}
// get details about RC522 hardware
void ShowReaderDetails() {
// Get MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("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("=v?.?"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("NFC ERROR"));
}
}
// send HTTP POST request to REST-API server
void sendRequest(){
char reqParm[150];
reqParm[0]=0;
strcat(reqParm,"{'n':'");
strcat(reqParm,uid);
strcat(reqParm,"','p':'");
strcat(reqParm,pin);
strcat(reqParm,"'}");
Serial.println(F("Reqest:"));
Serial.println(reqParm);
String response = "";
- int statusCode = restAPI.post(apiPath, reqParm, &response);*
Serial.print(F("Status:"));
Serial.println(statusCode);
Serial.print(F("Response:"));
Serial.println(response);
if(statusCode == 200){
int len = response.length();
//response[length-1] = '\0';
int delimiter = response.lastIndexOf(':');
String commandName = response.substring(1,delimiter);
String commandValue = response.substring(delimiter+1, len-1);
// split response in command, and value ...
Serial.println("c=" + String(commandName) + " v=" + String(commandValue));
if(commandName == "led"){
/*
char color = commandParameter1.charAt(0);
int type = commandParameter2.toInt();
int times = commandParameter3.toInt();
int interval = commandParameter4.toInt();
int buzzerOn = commandParameter5.toInt();
//char val[20] = commandValue.toChar();
led(color,type,times,interval,buzzerOn);
*/
}
else if(commandName == "buzzer"){
/*
int times = commandParameter3.toInt();
int interval = commandParameter4.toInt();
buzz(times,interval);
*/
}
else{
- Serial.println(F("invalid response"));*
- led('r',2,1,900,1);*
- led('r',1,0,0,0);*
- delay(1500);*
}
// set back uid and pin to idle mode and unlight led(s)
setBack();
}
else{
Serial.println(F("Request failed!"));
led('g',0,0,0,0);
led('y',0,0,0,0);
buzz(1,900);
led('r',1,0,0,0);
delay(1500);
// set back uid and pin to idle mode and unlight led(s)
setBack();
}
}