Zu meinen Modulen:
EthernetShield:https://www.reichelt.de/Erweiterungsboards/ARDUINO-SHD-ETH2/3/index.html?ACTION=3&LA=2&ARTICLE=159410&GROUPID=6669&artnr=ARDUINO+SHD+ETH2&SEARCH=%2A
RC522:https://www.reichelt.de/Weiteres-Zubehoer/DEBO-RFID-RC522/3/index.html?ACTION=3&GROUPID=6671&ARTICLE=192147
Auf der Arduino Webseite stehen MISO,MOSI,usw. des Arduino Mega und des Shields: https://www.arduino.cc/en/Reference/Ethernet
So nun mein Sketch: In diesem Sketch sind beide Module angeschlossen.
Das RFID-Modul habe ich an auf dem EthernetShield angeschlossen. Sprich an pin 11-13.
Der Code:
- Code: Select all
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x11, 0x2E, 0x46
};
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "AntwortString "; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
//MFRC522
#include <MFRC522.h>
constexpr uint8_t RST_PIN = 6;
constexpr uint8_t SS_PIN = 2;
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
SPI.begin();
mfrc522.PCD_Init();
Serial.begin(9600);
Serial.println("Setup fertig");
}
void loop() {
String content;
// put your main code here, to run repeatedly:
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
int packetSize = Udp.parsePacket();
if (packetSize)
{
int i = 0;
//String von VisualStudio empfangen
IPAddress remote = Udp.remoteIP();
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println(packetBuffer);
for (byte i = 0; i < mfrc522.uid.size; i++) {
content.concat(String(mfrc522.uid.uidByte[i] <0x10 ? "0" : " "));
content.concat(String(mfrc522.uid.uidByte[i],HEX));
}
content.toUpperCase();
String UID= content.substring(1);
Serial.println(UID);
//Antwort zurücksenden
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
Die Ausgabe der seriellen Schnittstelle ist beim Starten jene:
Setup fertig
BEREIT (Das ist der String den mein C#-Programm an den Arduino sendet)
Funktioniert also alles bis zum Auslesen der ID der Magnetkarte.
Hoffe ihr könnt mit dieser Angabe eineiges anfangen und mir helfen.
Mfg Felix