#include <SPI.h>
#include <MFRC522.h>
#include <Bridge.h>
#include <HttpClient.h>
#define SS_PIN 10
#define RST_PIN 9
/*
* Variables
*/
MFRC522 mfrc522(SS_PIN, RST_PIN); //Create MFRC522 instance
const int ledPin = 13; //The Arduino LED Pin
String Rfuid = "";
String getString = "";
String byteSizeToSend = "";
HttpClient client;
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
Bridge.begin();
Serial.begin(9600); //Begins Serial communications to the Wifi module
//while (!Serial);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Scan card to get UID"));
}
void loop() {
Rfuid = "";
getString = "";
byteSizeToSend = "";
while (client.available()) {
char c = client.read();
Serial.print(c);
}
Serial.flush();
digitalWrite(3, HIGH);
//Loops through the program constantly
//THIS IS THE NFC PART OF THE CODE
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return; //If no new card is found loop through again
} //Continue if new card is found
//
if ( ! mfrc522.PICC_ReadCardSerial()) //
{ //
return; //If no card is read loop through again
} //Continue if a card is successfully read
for (int i = 0; i < 4; i++) {
Rfuid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
Rfuid += String(mfrc522.uid.uidByte[i], HEX);
}
Rfuid.toUpperCase();
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
cardUpload(); //Calls the cardUpload() Function
}
//THIS IS THE WIFI PART OF THE CODE
void cardUpload() //Uploads the card information to the Database
{
getString += String("192.168.0.2/insertTest.php?cardUid=");
getString += Rfuid;
Serial.print("Card UID:");
Serial.println(Rfuid);
client.get(getString);
serialRead();
delay(500); //Introduce a delay to avoid overloading the wifi module with commands
digitalWrite(4, LOW);
}
void serialRead()
{
while (client.available()) {
char c = client.read();
Serial.print(c);
}
Serial.flush();
}
So I have this code where I'm using an NFC scanner and the Yún. I scan the card and it sends the data to the server. It all works fine, however if I have the serial monitor closed it takes 7-10 seconds for the request to complete. However, with the serial monitor open it will take 0.5 seconds to about 1 second max.
Can anyone explain why this might be happening?
I've tried removing all the serial commands from the sketch however it still does the same thing, slow upload to the server.
If anyone could shed light upon this problem, or point me in the right direction I'd appreciate it
Thanks