0
void setup() {
 Serial.begin(9600); // Initialize serial communications with the PC
 // disable SD SPI
 pinMode(4, OUTPUT);
 digitalWrite(4, HIGH);
 // disable w5100 SPI
 pinMode(10, OUTPUT);
 digitalWrite(10, HIGH);
 SPI.begin(); // Init SPI bus
 mfrc522.PCD_Init(); // Init MFRC522 card
 //Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks.");
 Ethernet.begin(mac, ip); //we used a STATIC address to start ETHERNET
 // print your local IP address:
 Serial.print("My IP address: ");
 for (byte thisByte = 0; thisByte < 4; thisByte++) {
 // print the value of each byte of the IP address:
 Serial.print(Ethernet.localIP()[thisByte], DEC);
 Serial.print(".");
 }
 // give the Ethernet shield a second to initialize:
 delay(1000);
 Serial.println("connecting...");
}
void loop() {
 // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
 MFRC522::MIFARE_Key key;
 key.keyByte[0] = 0xFF;
 key.keyByte[1] = 0xFF;
 key.keyByte[2] = 0xFF;
 key.keyByte[3] = 0xFF;
 key.keyByte[4] = 0xFF;
 key.keyByte[5] = 0xFF;
 // Look for new cards
 if ( ! mfrc522.PICC_IsNewCardPresent()) {
 return;
 }
 // Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial()) {
 return;
 }
 // Now a card is selected. The UID and SAK is in mfrc522.uid.
 // Dump UID
 //Serial.print("Card UID:");
 //Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
 // In this sample we use the second sector (ie block 4-7). the first sector is = 0
 // scegliere settore di lettura da 0 = primo settore 
 byte sector = 0;
 // block sector 0-3(sector0) 4-7(sector1) 8-11(sector2)
 // blocchi di scrittura da 0-3(sector0) 4-7(sector1) 8-11(sector2)
 byte valueBlockA = 0;
 byte valueBlockB = 1;
 byte valueBlockC = 2;
 byte trailerBlock = 3;
 byte status;
 // Authenticate using key A.
 // avvio l'autentificazione A
 //Serial.println("Authenticating using key A...");
 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
 if (status != MFRC522::STATUS_OK)
 {
 Serial.print("PCD_Authenticate() failed: "); 
 Serial.println(mfrc522.GetStatusCodeName(status));
 return;
 }
 //No KEY B Authentication 
 int val1 =(mfrc522.uid.uidByte[0]);
 int val2 =(mfrc522.uid.uidByte[1]);
 int val3 =(mfrc522.uid.uidByte[2]);
 int val4 =(mfrc522.uid.uidByte[3]);
 String valA=String(val1);
 String valB=String(val2);
 String valC=String(val3);
 String valD=String(val4);
 uID=valA+valB+valC+valD;
 Serial.print(uID);
 Serial.println();
 counter=counter+1; 
 Serial.print(counter); 
 //} 
 Serial.println();
 // Halt PICC
 mfrc522.PICC_HaltA();
 // Stop encryption on PCD
 mfrc522.PCD_StopCrypto1();
 if (counter>first)
 {//delay ifs
 // enable w5100 SPI 
 if (client.connect(server, 80)) { //start of IF
 Serial.println("connected");
 client.print("GET /accessdenyrfid.php?rfid="); //dont make these println 
 client.print(uID); //dont make these println
 client.print("&&");
 client.print("ip=mech1");
 //client.print( ip );
 //client.print("&&");
 //client.print("ipkey=");
 //client.print( ipkey );
 client.println(" HTTP/1.1"); //println
 client.println("Host: "); //println
 client.print(server);//don't make println
 client.println("Connection: close"); //println
 client.println(); //terminates the query
 Serial.print("SENT");
 Serial.println();
 first++;
 Serial.print(first);
 Serial.println();
 // add this
 while(client.connected()) {
 while(client.available()) {
 char ch = client.read();
 Serial.write(ch);
 }
 }
 client.stop();
 client.flush();
 } //end OF IF
 }//delay ifs 
}

The code above works perfectly with another php. that either saves or denies a card into a database. The arduino sends and receives data back and forth from and to php in less than a second. But the problem is that whenever the function client.stop is called. It takes approx. 8 seconds before another data is sent in the loop function. I need the loop to continue with just 2 second interval, which is enough or even faster. I don't think that the mysql query in the php is the one that makes it longer cause all the queries are made before the delay starts. (The server has already sent it's response, before the delay starts, which i think is in the client.stop.) Please help me on other alternatives on stopping the client or any way I can do this faster. Thanks, I'm really in trouble right now. I'm using Arduino UNO. Data sent back and forth are just a couple of letters and numbers.

BrettFolkins
4,4411 gold badge15 silver badges26 bronze badges
asked Jan 11, 2015 at 15:28
1
  • Could you post the full code? There's a bit missing... Commented Feb 10, 2016 at 19:04

1 Answer 1

1

I would bet the problem relies on

if (counter>first)

As seems like you never reset counter, there might be weird things going on

answered Jan 13, 2015 at 7:38
3
  • Can you please edit your answer to explain why this causes the code in question and how it should be fixed? Thanks! Commented Apr 16, 2015 at 1:01
  • 1
    @AnnonomusPenguin Edited Commented Apr 16, 2015 at 3:01
  • 1
    I can't even see where counter is declared, nor first. Commented Jul 15, 2015 at 5:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.