( tratto da https://github.com/rocrail/Arduino )
Il circuito:
Immagine:
57,53 KB
lo sketch
Codice: Seleziona tutto
/" Typical pin layout used:
 " ---------------------------------------
 " "             MFRC522      Arduino UNO
 "             Reader/PCD
 " Signal      Pin          Pin
 " ----------------------------------------
 " RST/Reset   RST          9
 " SPI SS      SDA(SS)      10
 " SPI MOSI    MOSI         11
 " SPI MISO    MISO         12
 " SPI SCK     SCK          13
 "/
// rev1.00 07.02.2016
// configure Rocrail with INTER-10 command station, settings see Wiki 
// http://forum.rocrail.net/viewtopic.php?f=47&t=11300&start=15#p111849
// For any reason it does only work with Arduino UNO.
// With my genue Arduino Micro nothing is send to Rocrail, but putput to terminal works fine.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN   9
#define SS_PIN    10
#define FBAdr     1     //feedback adress in Rocrail
boolean debug = false;  //false: prepare output for RR, true:huaman readable outputp for terminal window
//"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
// no more user settings after this line 
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
char SerialReceive;
void setup() {
  Serial.begin(19200);
    Serial.flush();
  SPI.begin();
  mfrc522.PCD_Init();   // Init MFRC522
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
}
String oldRFID = "";
String RFID = "";
int sendEndCount = 3;
int Intervall = 0;
void loop() {
  
   if (Serial.available() >= 1) {
    SerialReceive = Serial.read();
    if (debug) Serial.println(SerialReceive);
    
    if (SerialReceive == 0x25) {      //80msec Interval
      Serial.flush();
      mfrc522.PCD_Init();   // Init MFRC522
      mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
      Intervall = 80;
    }
    if (SerialReceive == 0x20) {      //2sec interval, onyl for compatibillity, I guess Rocrail does not support this
      Serial.flush();
      mfrc522.PCD_Init();   // Init MFRC522
      mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
      Intervall = 2000;
    }
  }
  sendEndCount++;
  if (sendEndCount == 3) {  // tag was removed from reader
    if (debug) {
      Serial.print ("0x80 ");
      Serial.println ("FBAdr ");
               }
    else {
      Serial.write(0x80);
      Serial.write(FBAdr);
         }
    oldRFID = "";
  }
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if (mfrc522.PICC_ReadCardSerial()) {
    RFID = mkString(mfrc522.uid.uidByte, mfrc522.uid.size);
    if (RFID == oldRFID) {
      if (debug) {
      //  Serial.print ("0x70 ");
      //  Serial.println ("FBAdr ");
      }
      else {        // due to bug in RR library this must not be send, otherwise RR get out of sync
        //          Serial.write(0x70);
        //          Serial.write(FBAdr);
      }
    }
    if (RFID != oldRFID)
    // some of my tag has only 4Byte UID, 
      if (mfrc522.uid.size = 4) {
        oldRFID = RFID;
        if (debug) {
          Serial.print ("FBAdr ");
          Serial.println(RFID);
          Serial.write(0x00);
                   }
        else {
          Serial.write(FBAdr);
          Serial.write(mfrc522.uid.uidByte, 4);
          Serial.write(0x00);
          
        }
      }
    // others has 7Byte UID, in this case cut the LSB 
      if (mfrc522.uid.size > 4) {
        oldRFID = RFID;
        if (debug) {
          Serial.print ("FBAdr ");
          Serial.println(RFID);
                   }
        else {
          Serial.write(FBAdr);
          Serial.write(mfrc522.uid.uidByte, 5);
             }
      }
    sendEndCount = 0;
  }
  delay(Intervall);
}
// convert Binary data to huaman readable HEX format
String mkString(byte "buffer, byte bufferSize) {
  String UID = "";
  for (byte i = 0; i < bufferSize; i++) {
    if (buffer[i] == 0) {
      UID = UID + "00";
    }
    else {
      UID = UID + String(buffer[i], HEX);
    }
  }
  return UID;
}
Su Rocrail ho configurato un controller di tipo INTER-10 legato alla porta assegnata da sistema alla USB: attenti alla velocita' di trasmissione da settare a 19200 Baud.
Uso dei tag autoadesivi spessore 1 mm e diametro 15-20 mm: ad ogni passaggio del sensore vicino all'antenna del sistema leggo sul server il codice del tag in decimale.
Con un semplice script deduco quale locomotiva sta passando e compio l'azione conseguente ( annuncio di stazione, ad esempio)
Codice: Seleziona tutto
<xmlscript>
  <switch var="%lcid%">
    <case val="252.91.27.219.0">
      <ext  cmd="suona.bat sounds\444.wav" />
    </case>
    <case val="180.170.35.84.0">
      <ext  cmd="suona.bat sounds\646.wav" />
    </case>
    <case val="42.246.35.84.0">
      <ext  cmd="suona.bat sounds\626.wav" />
    </case>
  </switch>
</xmlscript>
Antonello


