• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Arduino UNO + moduł ETH W5500 + RFID RC522
#1
Dzień dobry,
mam problem z moim projektem. Korzystam z arduino UNO, do którego podłączony jest moduł ethernet W5500 oraz moduł czytnika RFID RC522. Mój projekt polega na wysyłaniu poprzez Modbus TCP/IP informacji o ID karty. Oba moduły działają poprawnie, ale jak podłączone są dwa na raz to nagle moduł ethernetu wysyła komunikat "Ethernet shield was not found.". Oczywiście korzystam z innych pinów SS dla obu urządzeń. W momencie odłączenia pinu MISO czytnika RC522 moduł W5500 zaczyna poprawnie działać. 
Poniżej kod:
Kod:
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
#include <MFRC522.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 235);

EthernetServer ethServer(502);
ModbusTCPServer modbusTCPServer;

const int ledPin = 7; // LED_BUILTIN;
const int analogPin = A0;

#define RST_PIN         9          // Configurable, see typical pin layout
#define SS_PIN          8         // Configurable, see typical pin layout

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  Ethernet.init(10); // Most Arduino shields
  Serial.begin(9600);
  while (!Serial) { /* wait for serial */ }

  // Ethernet setup
  Ethernet.begin(mac, ip);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.");
    while (true) { delay(1); }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  ethServer.begin();

  if (!modbusTCPServer.begin()) {
    Serial.println("Failed to start Modbus TCP Server!");
    while (1) { delay(1); }
  }

  // Modbus setup
  modbusTCPServer.configureHoldingRegisters(0x00, 2);

  // RFID setup
  SPI.begin();
  mfrc522.PCD_Init();
  delay(4); // Optional delay
  mfrc522.PCD_DumpVersionToSerial();

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop() {
  EthernetClient client = ethServer.available();

  if (client) {
    Serial.println("New client");
    modbusTCPServer.accept(client);

    while (client.connected()) {
      modbusTCPServer.poll();
      updateLED();

      // RFID read and write to Modbus register
      if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
        String uidStr = "";
        for (byte i = 0; i < mfrc522.uid.size; i++) {
          uidStr += String(mfrc522.uid.uidByte[i], HEX);
        }

        // Write the UID to Modbus holding register at address 0x00
        modbusTCPServer.holdingRegisterWrite(0x00, uidStr.toInt()); // Convert UID to integer

        mfrc522.PICC_HaltA(); // Halt card after reading
      }

      delay(100); // Delay to reduce polling frequency
    }

    Serial.println("Client disconnected");
  } else {
    delay(100);
  }
}

void updateLED() {
  // Read the current value of the holding register at address 0x00
  int registerValue = modbusTCPServer.holdingRegisterRead(0x00);

  if (registerValue) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}
 
Odpowiedź
  


Wiadomości w tym wątku
Arduino UNO + moduł ETH W5500 + RFID RC522 - przez Cezsta - 29-04-2024, 11:12

Skocz do:


Przeglądający: 1 gości