gpt4 book ai didi

arduino - 带有 ESP8266 的 RC522 不工作 Arduino uno

转载 作者:行者123 更新时间:2023-12-02 04:30:17 31 4
gpt4 key购买 nike

我为 Arduino Uno 编写了以下代码,使用 RC522 模块和 ESP8266 模块扫描 RFID 卡以连接到我的路由器。

现在,当我扫描任何卡时,它应该读取卡号并向我的服务器的 IP 地址发送请求并获得响应。

但是成功读取RFID卡后,通过TCP与服务器的连接不起作用:

esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

所以我无法向我的服务器发送请求。

但是当我删除 RC522 代码(用于测试)时它可以正常工作!

RC522和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;
}

论坛链接 - https://forum.arduino.cc/index.php?topic=538180.0

最佳答案

不幸的是,您没有显示工作代码(部分,TCP 连接)和不工作代码(根本没有,或者只是 TCP 连接?)之间的区别。

when i remove the Rc522 code testing then it is working !

尝试更详细地描述哪些有效,哪些无效。您还可以提供调试输出并添加一些注释。

此外,我会尝试删除更多非必要代码(如显示)以帮助缩小原因范围。这可能已经让您走上正确的轨道来自己修复它,但它也会使您的代码更容易阅读(另请参阅 How to create a Minimal, Complete, and Verifiable example 尽管我怀疑许多人是否有重现您的问题的确切部分 - 但您可能会很幸运).

但是,根据您提供的代码,这只是一个猜测:

Arduino 可能没有从 ESP8266 模块接收数据,因为您正在将 RX 引脚重新配置为输出(但您没有使用它!?):

#define mainLock 2
...
pinMode(mainLock, OUTPUT);

冲突

SoftwareSerial esp(2, 3);// RX, TX

因此,我建议仔细检查您的 pin 使用和连接(另外,提供此信息会增加您发现错误的机会)。

关于arduino - 带有 ESP8266 的 RC522 不工作 Arduino uno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584382/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com