I have written below code to scan the RFID card using RC522 module and ESp8266 module to connect my router.
Now when i scan any card it will read the card number and send request to my ip and get the response.
But Here my ESp8266 is connected to my router but after get the RFID card my this line is not working :
esp.println("AT+CIPSTART="TCP","" + server + "",80");//start a TCP connection.
So i am not able to send request to my ip.
Now when i remove the Rc522 code testing then it is working !
What the problem with RC522 with ESP8266 ?
#include <SPI.h>
#include <MFRC522.h>
#include "SoftwareSerial.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//I2C pins declaration
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define SS_PIN 9
#define RST_PIN 7
#define mainLock 2
String ssid = "MYSSID";
String password = "PASSWORD";
SoftwareSerial esp(2, 3);// RX, TX
String server = "192.168.1.102"; //Your Host
String uri = "/get_data.php?rfid_key=";
//#define LED_G 4 //define green LED pin
//#define LED_R 5 //define red LED
#define BUZZER 6 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String rfidUid = "";
#define DEBUG true
void setup()
{
- // Serial.begin(9600); // Initiate a serial communication*
- esp.begin(9600);*
- Serial.begin(9600);*
- SPI.begin(); // Initiate SPI bus*
- mfrc522.PCD_Init(); // Initiate MFRC522*
- connectWifi();*
- httpget();*
- delay(1000);*
- Serial.println("Put your card to the reader...");*
- Serial.println();*
- pinMode(mainLock, OUTPUT);*
- lcd.begin(16, 2); //Defining 16 columns and 2 rows of lcd display*
- lcd.backlight();//To Power ON the back light*
}
void connectWifi() { - sendData("AT+RST\r\n", 2000, DEBUG); //This command will reset module to default*
- sendData("AT+CWMODE=3\r\n", 1000, DEBUG);*
- String cmd = "AT+CWJAP="" + ssid + "","" + password + """;*
- esp.println(cmd);*
- delay(4000);*
- if (esp.find("OK")) {*
- Serial.println("Connected!");*
- }*
- else {*
- Serial.println("Cannot connect to wifi ! Connecting again...");*
- connectWifi();*
- }*
}
/////////////////////////////GET METHOD///////////////////////////////
void httpget() {
// // 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.print("UID tag :");*
- String content = "";*
- byte letter;*
- rfidUid = "";*
- for (byte i = 0; i < mfrc522.uid.size; i++)*
- {*
_ Serial.print(mfrc522.uid.uidByte < 0x10 ? " 0" : " ");_
_ Serial.print(mfrc522.uid.uidByte*, HEX);
content.concat(String(mfrc522.uid.uidByte < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte, HEX));
rfidUid += String(mfrc522.uid.uidByte < 0x10 ? "0" : "");
rfidUid += String(mfrc522.uid.uidByte, HEX);
}
Serial.println();
content.toUpperCase();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(rfidUid);
esp.println("AT+CIPSTART="TCP","" + server + "",80");//start a TCP connection.
if ( esp.find("OK")) {
Serial.println("TCP connection ready");
} delay(1000);*_
* if ( esp.find("OK")) {*
* Serial.println("TCP connection ready");*
* } delay(1000);*
* String getRequest =*
* "GET " + uri + rfidUid + " HTTP/1.0\r\n" +*
* "Host: " + server + "\r\n" +*
_ "Accept: " + "/" + "\r\n" +
* "Content-Type: text/plain\r\n" +
"\r\n";*_
* String sendCmd = "AT+CIPSEND=";*
* esp.print(sendCmd);*
* esp.println(getRequest.length());*
* delay(500);*
* if (esp.find(">")) {*
* Serial.println("Sending..");*
* esp.print(getRequest);*
* if (esp.find("SEND OK")) {*
* Serial.println("Packet sent");*
* while (esp.available()) {*
* String response = esp.readString();*
* Serial.println("response.." + response);*
* }*
* esp.println("AT+CIPCLOSE");*
* }*
* }*
}
void loop()
{
* httpget();*
}
String sendData(String command, const int timeout, boolean debug) // Function to send the data to the esp8266
{
* String response = "";*
* esp.print(command); // Send the command to the ESP8266*
* long int time = millis();*
* while ( (time + timeout) > millis()) // ESP8266 will wait for some time for the data to receive*
* {*
* while (esp.available()) // Checking whether ESP8266 has received the data or not*
* {*
* char c = esp.read(); // Read the next character.*
* response += c; // Storing the response from the ESP8266*
* }*
* }*
* if (debug)*
* {*
* Serial.print(response); // Printing the response of the ESP8266 on the serial monitor.*
* }*
* return response;*
}