#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN D2
#define SS_PIN D1
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
const char* ssid ="IoT4143" ;
const char* password ="redminote4";
WiFiServer server(81);
void setup() {
Serial.begin(115200);
delay(10);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
// Connection to wireless network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
server.begin();
server.setNoDelay(true);
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available();
if(client)
{
Serial.println("new client");
while(client.connected())
{
if(client.available())
{
// Read the first line of the request
String request = client.readStringUntil('\n');
Serial.println(request);
client.setNoDelay(true);
for(int i=0;i<=10;i++)
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
//Show UID on serial monitor
Serial.println();
Serial.print(" UID tag :");
String content= "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
client.print(content);
delay(2000);
}
Serial.println("Client Disconnected");
client.println("Reading stopped");
client.stop();
}
}
}
}
What I want to achieve is when I send "Readstart" from the app the NodeMCU should send RFID card data until I send "Readstop".
But the problem is when I connect the app to NodeMCU's IP (192.168.43.179) at port 80 it disconnects immediately sometimes and it sends the partial card data (data of 2 cards and disconnects or data of 3 cards and disconnects).
I want n number of cards UIDs until I send "Readstop" string as request. I was trying to achieve it using while loop.
while(request!="Readstop")
{
//rfid code
}
But no data was sent. Then I tried the for loop. This is also not working..
I am attaching screenshot of Mobile app: