3
votes

I am new here, so please don't be mad for my stupidity. I have a project where I have to read RFID UID code (this part works like a charm), then send it to database that is connected to C# application, and then receive the response (0 or 1). My problem (at the moment) is that arduino doesn't send any data. Maybe someone could help me?

Here is my arduino code:

#include <EtherCard.h>

#include <SPI.h>      // RC522 Module uses SPI protocol
#include <MFRC522.h>   // Library for Mifare RC522 Devices
#include <DS1302RTC.h>
#include <Time.h>

static byte mymac[] = {0x74,0xDD,0xDD,0x00,0x00,0x01};
static byte server_IP[] = {192,168,0,102};
static int server_Port = 7745;
byte Ethernet::buffer[600];


// Init the DS1302 (external RTC)
// Set pins:  CE, IO,CLK
DS1302RTC RTC(45, 43, 41);

// Optional connection for RTC module
#define DS1302_GND_PIN 39
#define DS1302_VCC_PIN 37

#define COMMON_ANODE
#define LED_ON HIGH
#define LED_OFF LOW

#define redLed 3
#define greenLed 5
#define blueLed 7
#define relay 4


int successRead; // Variable integer to keep if we have Successful Read from Reader

byte readCard[4];           // Stores scanned ID read from RFID Module


/* We need to define MFRC522's pins and create instance
 * Pin layout should be as follows (on Arduino Mega 2560):
 * MOSI: Pin 51 / ICSP-4
 * MISO: Pin 50 / ICSP-1
 * SCK : Pin 52 / ICSP-3
 * SS : Pin 46 (Configurable)
 * RST : Pin 34 (Configurable)
 * look MFRC522 Library for
 * pin configuration for other Arduinos.
 */

#define SS_PIN1 46
#define RST_PIN1 34
MFRC522 mfrc522(SS_PIN1, RST_PIN1);	// Create MFRC522 instance.


///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
  Serial.begin(9600);	 // Initialize serial communications with PC
    Serial.print("MAC: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();
  if (ether.begin(sizeof Ethernet::buffer, mymac, 53)==0) {
    Serial.println( "Failed to access Ethernet controller");
    //while(1); 
  } else Serial.println("Ethernet controller initialized");
 
  if (!ether.dhcpSetup()) {
    Serial.println("Failed to get configuration from DHCP");
    //while(1);
  } else Serial.println("DHCP configuration done:"); 
 
  ether.printIp("My IP: ", ether.myip);
  ether.printIp("Netmask: ", ether.netmask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
  Serial.println();
  
  ether.copyIp(ether.hisip, server_IP);
  ether.hisport = server_Port;
  
  //Arduino Pin Configuration
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW); // Make sure door is locked
  digitalWrite(redLed, LED_OFF); // Make sure led is off
  digitalWrite(greenLed, LED_OFF); // Make sure led is off
  digitalWrite(blueLed, LED_OFF); // Make sure led is off

  //Protocol Configuration
  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); //Set Antenna Gain to Max- this will increase reading distance

  // Activate RTC module
  digitalWrite(DS1302_GND_PIN, LOW);
  pinMode(DS1302_GND_PIN, OUTPUT);

  digitalWrite(DS1302_VCC_PIN, HIGH);
  pinMode(DS1302_VCC_PIN, OUTPUT);


  RTC.haltRTC(1); //clock enable (1), clock disable (0)
  RTC.writeEN(0); //Write protection OFF (1), write protection ON (0)

  //Time set
  //setTime(1,19,0,16,12,2014);
  //time_t t = now();
  //RTC.set(t);

  delay(200);
}

///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
  ether.packetLoop(ether.packetReceive());
  do {
    successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0

      normalModeOn(); // Normal mode, blue Power LED is on, all others are off
    
  }
  while (!successRead); //the program will not go further while you not get a successful read
  
  openDoor();

}

///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
int getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
    return 0;
  }
  //Serial.println("Scanned PICC's UID:");
  for (int i = 0; i < 4; i++) {  //
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
  }
  Serial.println(readCard[4],HEX);
  char string_temp[7];
  dtostrf (readCard[2], 4, 2, string_temp);
  Stash stash;
  byte sd = stash.create();
  stash.print(string_temp);
  stash.print('\r');
  stash.save();     
  Stash::prepare(PSTR("$H"), sd);
  ether.tcpSend();
    
  Serial.println("Packet sent!");
  
  Serial.println("");
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}

//////////////////////////////////////// Normal Mode Leds  ///////////////////////////////////
void normalModeOn () {
  digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
  digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
  digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
  digitalWrite(relay, LOW); // Make sure Door is Locked
}


///////////////////////////////////////// Unlock Door   ///////////////////////////////////
void openDoor() {
  digitalWrite(blueLed, LED_OFF); // Turn off blue LED
  digitalWrite(redLed, LED_OFF); // Turn off red LED
  digitalWrite(greenLed, LED_ON); // Turn on green LED
  digitalWrite(relay, HIGH); // Unlock door!
  delay(1000); // Hold door lock open for given seconds
  digitalWrite(relay, LOW); // Relock door
  delay(1000); // Hold green LED on for 2 more seconds
}

///////////////////////////////////////// Failed Access  ///////////////////////////////////
void failed() {
  digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  digitalWrite(redLed, LED_ON); // Turn on red LED
  delay(2000);

}

I use EtherCard.h library and ENC28J60 ethernet module for arduino.

1

1 Answers

0
votes

ENC28J60 is not compatible with Arduino UNO and most likely with MEGA2560 when used with other SPI devices.

You should use the default Arduino Ethernet Shield that uses W5100 instead.

I have run into the same issue with ENC28J60, tried a lot of setup and finally got a W5100 and it work straight out of the box with the default Ethernet library.