Arduino UNO + Ethernet Shield + RC522 connexion failed to Xampp server

Hi, I’m new to arduino, first of all, excuse my english…

I have an Arduino UNO connected with an Ethernet Shield + rc522 RFID reader, I would like to get the Tag ID card and send it using GET mounted to a local server with XAMPP.

  • the apache server is online (checked)
  • WS receives the parameter is at http: //localhost/dir1/obtienevalor.php (checked)
  • rc522 reader reads TagId I connected on ethernet shield works! (checked)
  • ethernet shield works alone (checked)

I need to read the tag id and send using GET to obtienevalor.php to process,the problem is that the connection to the server fails, and I can not send TagId.

apparently ethernet shield not working with Reader rc522, rc522 reader settings this within my code:

/**
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 7
* RST: Pin 9
*/
#include <SPI.h>
#include <RFID.h>
#include <Ethernet.h>

#define SS_PIN 7
#define RST_PIN 9


// Mac unica de cada EthernetShield (deben cambiarla)
byte mac[] = { 0x90,0xA2,0xDA,0x0F,0x49,0x90 };

// Descomentar esta linea si se desea un IP fijo
IPAddress ip(192,168,0,50);

// Descomentar esta linea para asignar un DNS fijo
//IPAddress myDns(192,168,1,1);

// Inicializa la instancia client
EthernetClient client;

// Direccion del servidor
char server[] = "http://localhost/dir1/";


RFID rfid(SS_PIN, RST_PIN); 

// Setup variables:
    int serNum0;
    int serNum1;
    int serNum2;
    int serNum3;
    int serNum4;
    int finalnumber;

void setup()
{ 
  Serial.begin(9600);
// disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  // disable w5100 SPI
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  
  // Espera 1 segundo para que se inicie la tarjeta Ethernet
  delay(1000);
  // Si deseas usar un ip fijo y un DNS fijo descomentar esta linea y comentar la linea 39
  // Ethernet.begin(mac, ip, myDns);
  // Inicializa la tarjeta ethernet mediante DHCP
  EthernetServer server(80);
  Ethernet.begin(mac,ip);
  server.begin();
  // Imprime la direccion IP de la tarjeta
  Serial.print("Direccion IP: ");
  Serial.println(Ethernet.localIP());
  
  SPI.begin(); 
  rfid.init();
  
}

void loop()
{
    
    if (rfid.isCard()) {
        if (rfid.readCardSerial()) {
            if (rfid.serNum[0] != serNum0
                && rfid.serNum[1] != serNum1
                && rfid.serNum[2] != serNum2
                && rfid.serNum[3] != serNum3
                && rfid.serNum[4] != serNum4
            ) {
                /* With a new cardnumber, show it.*/
                Serial.println(" ");
                //Serial.println("Card found");
                serNum0 = rfid.serNum[0];
                serNum1 = rfid.serNum[1];
                serNum2 = rfid.serNum[2];
                serNum3 = rfid.serNum[3];
                serNum4 = rfid.serNum[4];
                finalnumber= 1;
              delay(1000);
              if (client.connect(server,80)) {

                    // Envia el requerimiento al servidor via GET
                    Serial.println("Iniciando conexion...");
                    client.print("GET /obtienevalor.php?idcard=finalnumber");
                    //client.print(tempC);
                    client.println(" HTTP/1.1");
                    client.print("Host: ");
                    client.println(server);
                    client.println("User-Agent: Arduino-Ethernet");
                    client.println("Connection: close");
                    client.println();
                  } 
                  else {
                    // Si la conexion fallo se desconecta
                    Serial.println("Error al conectarse al servidor");
                    Serial.println("Desconectando...");
                    client.stop();
                  }             
              

             } else {
               /* If we have the same ID, just write a dot. */
               Serial.print(".");
             }
          }
    }
    
    rfid.halt();
}

someone could give me a hint? attached photo of monitor serial and arduino settings

Thanks in advance

cap1.jpg

char server[] = "http://localhost/dir1/";

The name localhost means that the server is running on the machine that the client is running on. Clearly, that is NOT the case for you. You MUST supply the actual IP address of the server or the actual name of the server.

Hi PaulS

thanks for your answer, but the problem continued.

Excuse my ignorance,

now, the file that receives the parameter is in -> http://elrecreo.cl/arduino/prueba1/obtienevalor.php, the ip of the server is 206,221,177,194 , the sketch looking at the root of the server to send the file obtienevalor.php value.

then,the correct way would ->

char server[] = "http://elrecreo.cl/arduino/prueba1/";

is that correct?,I've done this way but does not work

I set something else to ethernet shield? I think it does not work with rc522, the configuration of the pins is correct because I can read the TagId.

Thanks in advance

Hi PaulS
Thanks for your help, achieves connect to the server = D,the solution is to assign an ip by dhcp server using only → Ethernet.begin (mac); not Ethernet.begin (mac,ip);

Now, my problem is the GET parameter is not received by the file obtienevalor.php, I checked that the parameter comes empty, for this reason you can not insert into mysql

my arduino code

/**
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 9
*
* Script is based on the script of Miguel Balboa. 
* New cardnumber is printed when card has changed. Only a dot is printed
* if card is the same.
*
* @version 0.1
* @author Henri de Jong
* @since 06-01-2013
*/

#include <SPI.h>
#include <RFID.h>
#include <Ethernet.h>

#define SS_PIN 7
#define RST_PIN 9


// Mac unica de cada EthernetShield (deben cambiarla)
byte mac[] = { 0x90,0xA2,0xDA,0x0F,0x49,0x90 };

// Descomentar esta linea si se desea un IP fijo
//IPAddress ip(192,168,0,50);

// Descomentar esta linea para asignar un DNS fijo
//IPAddress myDns(192,168,1,1);

byte server[] = {206,221,177,194}; 
String locationPost = "http://elrecreo.cl/arduino/prueba1/obtienevalor.php?idcard="; // 

// Inicializa la instancia client
EthernetClient client;

RFID rfid(SS_PIN, RST_PIN); 

// Setup variables:
    int serNum0;
    int serNum1;
    int serNum2;
    int serNum3;
    int serNum4;
    int serNumInt0;
    int serNumInt1;
    int serNumInt2;
    int serNumInt3;
    int serNumInt4;
    int finalnumber;
    byte IDCard[5];

void setup()
{ 
  Serial.begin(9600);
// disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  // disable w5100 SPI
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  
  // Espera 1 segundo para que se inicie la tarjeta Ethernet
  delay(1000);
  // Si deseas usar un ip fijo y un DNS fijo descomentar esta linea y comentar la linea 39
  // Ethernet.begin(mac, ip, myDns);
  // Inicializa la tarjeta ethernet mediante DHCP
  //EthernetServer server(80);
  Ethernet.begin(mac);
  //server.begin();
  // Imprime la direccion IP de la tarjeta
  Serial.print("Direccion IP: ");
  Serial.println(Ethernet.localIP());
  
  SPI.begin(); 
  rfid.init();
  
}

void loop()
{
    
    if (rfid.isCard()) {
        if (rfid.readCardSerial()) {
            if (rfid.serNum[0] != serNum0
                && rfid.serNum[1] != serNum1
                && rfid.serNum[2] != serNum2
                && rfid.serNum[3] != serNum3
                && rfid.serNum[4] != serNum4
            ) {
                /* With a new cardnumber, show it.*/
                Serial.println(" ");
                //Serial.println("Card found");
                serNum0 = rfid.serNum[0];
                serNum1 = rfid.serNum[1];
                serNum2 = rfid.serNum[2];
                serNum3 = rfid.serNum[3];
                serNum4 = rfid.serNum[4];
            
              finalnumber= 70;
               Serial.println(" ");
               Serial.println(locationPost + finalnumber);
              delay(1000);
              if (client.connect(server,80)) {

                    // Envia el requerimiento al servidor via GET
                    Serial.println("Iniciando conexion...");
                    client.print("GET ");
                    client.println(locationPost + finalnumber + " HTTP/1.1");
                    client.println("User-Agent: Arduino-Ethernet");
                    client.println("Connection: close");
                    client.println();
                  } 
                  else {
                    // Si la conexion fallo se desconecta
                    Serial.println("Error al conectarse al servidor");
                    Serial.println("Desconectando...");
                    client.stop();
                  }             
              

             } else {
               /* If we have the same ID, just write a dot. */
               Serial.print(".");
             }
          }
    }
    
    rfid.halt();
}

my php code in the file http://elrecreo.cl/arduino/prueba1/obtienevalor.php

<?php

$mysql_servidor = "localhost";
$mysql_base = "elrecreo_dbname";
$mysql_usuario = "elrecreo_dbuser";
$mysql_clave = "xxxxxxx";

$nombre  = htmlspecialchars($_GET["idcard"],ENT_QUOTES);
//$nombre = isset($_GET['idcard']) ? $_GET['idcard'] : null ;
echo "El Parametro enviado es :" . $nombre . "
";

// Valida que esten presente todos los parametros
if (($nombre!="")) {
	mysql_connect($mysql_servidor,$mysql_usuario,$mysql_clave) or die("Imposible conectarse al servidor.");
	mysql_select_db($mysql_base) or die("Imposible abrir Base de datos");
	$sql = "insert into user (nombre) values ('$nombre')";
	mysql_query($sql);

	echo "El Parametro fue guardado";

}
else
{

	echo "El Parametro no fue guardado";
}

?>

after trying to send data, the second tag reads when the server stops responding (You can see it in the screenshot 1)
the code to insert data into mysql works (You can see it in the screenshot 2)
the response I get in http://elrecreo.cl/arduino/prueba1/obtienevalor.php (You can see it in the screenshot 3)

I’m doing wrong?
I appreciate your patience and willingness

thank you very much

cap1.jpg

cap2.jpg

cap3.jpg

                    client.print("GET ");
                    client.println(locationPost + finalnumber + " HTTP/1.1");

What does this ACTUALLY send to the client?

GET http://elrecreo.cl/arduino/prueba1/obtienevalor.php?idcard=70 HTTP/1.1

which is WRONG. The name of the script to execute is NOT http://anything. The name of the script is elrecreo.cl/arduino/prueba1/obtienevalor.php, which is what the GET request should be requesting, with ? as the argument separator, and idcard=70 as the argument.

Hi Pauls
thanks for your valuable response certainly helped me to improve my code, but the problem continued, I need to read a RFID tag and send the TagId a php file in http://elrecreo.cl/arduino/prueba1/ obtienevalor.php for processing.I am getting the parameter in php using GET → name = _GET [‘idcard’];. I can capture the data with good rc522 reader but in the php file, I get an empty field.

you think the cause is a conflict configuraciion Pin interlaced between the ethernet shield and RC522 RFID? I can see that the server connects, but I get the value in php

There is another alternative to achieve this?
all your answers are valuable

my code below and stay tuned for a reply, a hug and thanks in advance.

#include <SPI.h>
#include <RFID.h>
#include <Ethernet.h>

#define SS_PIN 7
#define RST_PIN 9

// Mac unica de cada EthernetShield (deben cambiarla)
byte mac[] = { 0x90,0xA2,0xDA,0x0F,0x49,0x90 };
// Descomentar esta linea si se desea un IP fijo
IPAddress ip(192,168,1,40);
byte server[] = {155,94,214,98}; //ip Address of the server you will connect to206,221,177,194:
// Inicializa la instancia client
EthernetClient client;

RFID rfid(SS_PIN, RST_PIN); 

// Setup variables:
    int serNum0;
    int serNum1;
    int serNum2;
    int serNum3;
    int serNum4;
    int finalnumber;

void setup()
{ 
  Serial.begin(9600);
// disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  // disable w5100 SPI
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  // Espera 1 segundo para que se inicie la tarjeta Ethernet
  delay(1000);
  Ethernet.begin(mac);
  // Imprime la direccion IP de la tarjeta
  Serial.print("Direccion IP: ");
  Serial.println(Ethernet.localIP());
  SPI.begin(); 
  rfid.init();
  
}

void loop()
{
    
    if (rfid.isCard()) {
        if (rfid.readCardSerial()) {
            if (rfid.serNum[0] != serNum0
                && rfid.serNum[1] != serNum1
                && rfid.serNum[2] != serNum2
                && rfid.serNum[3] != serNum3
                && rfid.serNum[4] != serNum4
            ) {
                /* With a new cardnumber, show it.*/
                Serial.println(" ");
                //Serial.println("Card found");
                serNum0 = rfid.serNum[0];
                serNum1 = rfid.serNum[1];
                serNum2 = rfid.serNum[2];
                serNum3 = rfid.serNum[3];
                serNum4 = rfid.serNum[4];
                finalnumber= serNum0+serNum1+serNum2+serNum3+serNum4;
                Serial.println(finalnumber);

if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET arduino/tutoiot/iot.php?valor=1234567 HTTP/1.1");
    client.println("Host: www.elrecreo.cl");   
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
          } else {
               /* If we have the same ID, just write a dot. */
               Serial.print(".");
             }
          }
    }
  //client.stop();
  client.flush();
  rfid.halt();
}

cap1.jpg

I’m missing something. You say that you are invoking the obtienevalor.php script at http://elrecreo.cl/arduino/prueba1/, but your code says:

    client.println("GET arduino/tutoiot/iot.php?valor=1234567 HTTP/1.1");

on host:

    client.println("Host: www.elrecreo.cl");

So, which is it?

The only name = value pair being passed to that script is valor = 1234567, but you say that you are looking for the value associated with the name idcard. Seems to me that it’s no wonder it can’t find the value.

yes, thank you very much for your help, you are right, I made a mistake in the code, the number 1234567 was only a test, the correct code:

client.println("GET arduino/tutoiot/iot.php?valor=");
client.print(finalnumber);
client.println("HTTP/1.1");

thus the problem persiste..yo still running tests, but I appreciate its huge and valuable help, I am attentive to any improvement

best regards!

Try the code below. It actually displays the server response. Maybe that will help you. It also closes the connection correctly. I put comments showing what is added.

#include <SPI.h>
#include <RFID.h>
#include <Ethernet.h>

#define SS_PIN 7
#define RST_PIN 9

// Mac unica de cada EthernetShield (deben cambiarla)
byte mac[] = { 0x90,0xA2,0xDA,0x0F,0x49,0x90 };
// Descomentar esta linea si se desea un IP fijo
IPAddress ip(192,168,1,40);
byte server[] = {155,94,214,98}; //ip Address of the server you will connect to206,221,177,194:
// Inicializa la instancia client
EthernetClient client;

RFID rfid(SS_PIN, RST_PIN); 

// Setup variables:
    int serNum0;
    int serNum1;
    int serNum2;
    int serNum3;
    int serNum4;
    int finalnumber;

void setup()
{ 
  Serial.begin(9600);
// disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  // disable w5100 SPI
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  // Espera 1 segundo para que se inicie la tarjeta Ethernet
  delay(1000);
  Ethernet.begin(mac);
  // Imprime la direccion IP de la tarjeta
  Serial.print("Direccion IP: ");
  Serial.println(Ethernet.localIP());
  SPI.begin(); 
  rfid.init();
  
}

void loop()
{
    
    if (rfid.isCard()) {
        if (rfid.readCardSerial()) {
            if (rfid.serNum[0] != serNum0
                && rfid.serNum[1] != serNum1
                && rfid.serNum[2] != serNum2
                && rfid.serNum[3] != serNum3
                && rfid.serNum[4] != serNum4
            ) {
                /* With a new cardnumber, show it.*/
                Serial.println(" ");
                //Serial.println("Card found");
                serNum0 = rfid.serNum[0];
                serNum1 = rfid.serNum[1];
                serNum2 = rfid.serNum[2];
                serNum3 = rfid.serNum[3];
                serNum4 = rfid.serNum[4];
                finalnumber= serNum0+serNum1+serNum2+serNum3+serNum4;
                Serial.println(finalnumber);

if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET arduino/tutoiot/iot.php?valor=1234567 HTTP/1.1");
    client.println("Host: www.elrecreo.cl");   
    client.println("Connection: close");
    client.println();

// add this
    while(client.connected()) {
        while(client.available()) {
            char ch = client.read();
            Serial.write(ch);
    {
    client.stop();
    Serial.println(F("\r\ndisconnected"));
// end of add
  } 

  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
          } else {
               /* If we have the same ID, just write a dot. */
               Serial.print(".");
             }
          }
    }

  rfid.halt();
}

This is “Perfect World” code. There is no timeout on the “while(client.connected())” loop, so it could become an endless loop if the connection breaks or the server stalls. We will work on that later.

Did anybody used the MFRC522 Library for this Code above?

someone can tell me where to find the library used rfid.h thank you

hola buenas noches aquí están las librerias

RFID.zip (840 KB)

thanks :)

Surfer Tim your code showing this error… :frowning: still i have no solution for my project…

RFID rc522 not working with Ethernet shield . :confused:

.plz help me…

if possible i want to connect rfid rc522 with wireless ir module
and other with Ethernet shield…

#include <SPI.h>
#include <RFID.h>
#include <Ethernet.h>
#define SS_PIN 7
#define RST_PIN 9
byte mac = { 0x90,0xA2,0xDA,0x0F,0x49,0x90 };
IPAddress ip(192,168,0,50);
EthernetClient client;

char server = “http://localhost/dir1/”;
RFID rfid(SS_PIN, RST_PIN);
// Setup variables:
int serNum0;
int serNum1;
int serNum2;
int serNum3;
int serNum4;
int finalnumber;
void setup()
{
Serial.begin(9600);
// disable SD SPI
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
// disable w5100 SPI
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
delay(1000);
Ethernet.begin(mac);
Serial.print(“Direccion IP: “);
Serial.println(Ethernet.localIP());
SPI.begin();
rfid.init();
}
void loop()
{
if (rfid.isCard()) {
if (rfid.readCardSerial()) {
if (rfid.serNum[0] != serNum0
&& rfid.serNum[1] != serNum1
&& rfid.serNum[2] != serNum2
&& rfid.serNum[3] != serNum3
&& rfid.serNum[4] != serNum4
) {
Serial.println(” “);
serNum0 = rfid.serNum[0];
serNum1 = rfid.serNum[1];
serNum2 = rfid.serNum[2];
serNum3 = rfid.serNum[3];
serNum4 = rfid.serNum[4];
finalnumber= serNum0+serNum1+serNum2+serNum3+serNum4;
if (client.connect(server, 80)) {
Serial.println(“connected”);
client.println(“GET arduino/tutoiot/iot.php?valor=1234567 HTTP/1.1”);
client.println(“Host: www.elrecreo.cl”);
client.println(“Connection: close”);
client.println();
}
while(client.connected()) {
while(client.available()) {
char ch = client.read();
Serial.write(ch);
{
client.stop();
Serial.println(F(”\r\ndisconnected”));
}
}
} else {
Serial.print(".");
}
}
}
rfid.halt();
}

Untitled.jpg

Topic died ? We can only regret . topic is very interesting