The modules work themselves, yes. I haven't tried them with example codes, but I have switched them between other units/codes that are working. It must be something in the coding/setup between the ethernet and MFRC522.
This is the code:
#include <SPI.h>
#include <MFRC522.h>
#include <UIPEthernet.h>
#include <PubSubClient.h>
const byte mac[] = {0x74, 0x69, 0x69, 0x2D, 0x30, 0x33};
const IPAddress IP(192, 168, 0, 108);
const IPAddress serverIP(192, 168, 0, 101);
const byte numReaders = 3;
const byte ssPins[] = {2, 3, 4};
const byte resetPin = 9;
int LEDpins[] = {A1, A2, A3};
int numLEDs = 3;
MFRC522 mfrc522[numReaders];
const String correctIDs[] = {"9a8760aa", "5a665caa", "ea8860aa"};
String currentIDs[numReaders];
const byte lockPin = A0;
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient MQTTclient(ethClient);
void callback(char* topic, byte* payload, unsigned int length) {
/* Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
*/
byte* p = (byte*)malloc(length + 1);
memcpy(p, payload, length);
p[length] = 0;
// MQTTclient.publish("toHost/switches", p, length);
// Serial.println("checking");
if (strcmp(p, "SOLVE") == 0) {
onSolve();
}
else if (strcmp(p, "RESET") == 0) {
onReset();
}
// Serial.println("check done");
free(p);
// Serial.println("memory cleared");
}
void reconnect() {
// Loop until we're reconnected
while (!MQTTclient.connected()) {
// Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (MQTTclient.connect("monkeys")) {
// Serial.println("connected");
// Once connected, publish an announcement...
MQTTclient.publish("toHost/monkeys", "connected");
// ... and resubscribe
MQTTclient.subscribe("lab/monkeys");
MQTTclient.subscribe("lab/all");
}
else {
Serial.print("failed, rc=");
Serial.print(MQTTclient.state());
Serial.println(" try again in 5 seconds");
// MQTTclient.publish("lab/monkeys", "connection failed");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, LOW);
SPI.begin();
MQTTclient.setServer(serverIP, 1883);
MQTTclient.setCallback(callback);
// Serial.println("Connecting to network");
if (Ethernet.begin(mac) == 0) {
Ethernet.begin(mac, IP);
}
Serial.print("Connected! IP address: ");
Serial.println(Ethernet.localIP());
for (uint8_t i = 0; i < numReaders; i++) {
mfrc522[i].PCD_Init(ssPins[i], resetPin);
mfrc522[i].PCD_SetAntennaGain(MFRC522::PCD_RxGain::RxGain_max);
Serial.print(F("Reader #"));
Serial.print(i);
Serial.print(F(" initialised on pin "));
Serial.print(String(ssPins[i]));
Serial.print(F(". Antenna strength: "));
Serial.print(mfrc522[i].PCD_GetAntennaGain());
Serial.print(F(". Version : "));
mfrc522[i].PCD_DumpVersionToSerial();
delay(100);
}
for (int i = 0; i < numLEDs; i++) {
pinMode(LEDpins[i], OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
boolean puzzleSolved = true;
boolean changedValue = false;
if (!MQTTclient.connected()) {
reconnect();
}
MQTTclient.loop();
for (uint8_t i = 0; i < numReaders; i++) {
mfrc522[i].PCD_Init();
String readRFID = "";
if (mfrc522[i].PICC_IsNewCardPresent() && mfrc522[i].PICC_ReadCardSerial()) {
readRFID = dump_byte_array(mfrc522[i].uid.uidByte, mfrc522[i].uid.size);
}
if (readRFID != currentIDs[i]) {
changedValue = true;
currentIDs[i] = readRFID;
}
if (currentIDs[i] != correctIDs[i]) {
puzzleSolved = false;
digitalWrite(LEDpins[i], LOW);
}
else {
digitalWrite(LEDpins[i], HIGH);
}
mfrc522[i].PICC_HaltA();
mfrc522[i].PCD_StopCrypto1();
}
/*if(changedValue) {
for (uint8_t i=0; i<numReaders; i++) {
Serial.print(F("Reader #"));
Serial.print(String(i));
Serial.print(F(" on Pin #"));
Serial.print(String((ssPins[i])));
Serial.print(F(" detected tag: "));
Serial.println(currentIDs[i]);
}
Serial.println(F("---"));
}*/
if (puzzleSolved) {
MQTTclient.publish("toHost/monkeys", "solved");
//Serial.println("solved");
onSolve();
}
}
void onSolve() {
// Serial.println (F("Puzzle Solved"));
digitalWrite(lockPin, HIGH);
if (!MQTTclient.connected()) {
reconnect();
}
MQTTclient.loop();
}
void onReset() {
delay(1000);
asm volatile ("jmp 0");
}
String dump_byte_array(byte *buffer, byte bufferSize) {
String read_rfid = "";
for (byte i = 0; i < bufferSize; i++) {
read_rfid = read_rfid + String(buffer[i], HEX);
}
return read_rfid;
}
[/code]