I have written below code for Arduino Uno to scan an RFID card using a RC522 module and an ESP8266 module to connect to my router.
Now when I scan any card it should read the card number and send a request to my server's IP address and get the response.
But after successfully reading the RFID card, the connection to the server via TCP does not work:
esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
So I am not able to send a request to my server.
But when I remove the RC522 code (for testing) it is working!
What is the problem using the RC522 and the ESP8266 together?
#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;
}
Forum link - https://forum.arduino.cc/index.php?topic=538180.0