您好,
经过两周的努力并且没有解决方案,我发现 PN532 库在调用 GET() 函数时以某种方式干扰了 HTTPClient 库,它冻结了整个代码 3-5 秒并随机给出 -1,- 2、-3 和 -11 http 错误代码。
如果我评论 Sketch 循环中的 RFID 读取部分,一切都会按预期进行。
代码:
全选readRFID(); to //readRFID();
我在 SPI、Arduino IDE v1.8.2、ESP8266 Core v2.3.0 和 v2.7.1 上使用带有 PN532 的 WeMos D1 mini。
也尝试了 Adafruits 和 Seeed-Studios PN532 库,但得到了同样奇怪的结果。
还尝试在 10cm 杜邦电缆上从 WeMos D1 迷你设备收起 PN532 模块(也用 20 厘米杜邦电缆进行了测试),但同样的奇怪结果。
是的,还尝试了外部
电源、添加电容器……但什么也没有。
代码:
全选extern "C" {
#include "user_interface.h"
}
#include "SPI.h"
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
#include "Adafruit_PN532.h"
const char* ssid = "YourSsid";
const char* password = "YourPassword";
const char* devhostname = "DEVICE-001";
uint8_t Mac[] = {0xBE, 0x1C, 0x00, 0x00, 0x01, 0x01};
// SPI pin defini
tion for WeMos D1 mini
#define SS_RFID_PIN (0)
Adafruit_PN532
NFC(SS_RFID_PIN);
String userRFID = "";
// Custom Delay of 1s in Loop procedure (Non-blocking)
int upd_ready_period = 10000 + millis();
unsigned long upd_ready_time_now = 0;
int nfc_ready_period = 200;
unsigned long nfc_time_now = 0;
void setup()
{
delay(10);
Serial.begin(9600);
while (!Serial)
{
delay(10);
}
Serial.println("Booting up...");
delay(10);
nfc.begin();
delay(10);
Serial.println("Searching for RFID module...");
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find any RFID module!");
while (1) delay(100); // halt
}
// Got ok data, print it out!
Serial.print("Found RFID module PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print("Firmware Version: "); Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
// Non-blocking procedure
nfc.setPassiveActivationRetries(0x01);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for RFID Card...");
// Fix for "Can't reconnect to the router"
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
// =======================================
// Set Wifi MAC, Hostanme, Ssid and Passowrd
wifi_set_macaddr(STATION_IF, &Mac[0]);
WiFi.hostname(devhostname);
WiFi.begin(ssid, password);
Serial.print(F("Connecting to "));
Serial.print(ssid); Serial.println(F(" ..."));
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(++i);
Serial.print(' ');
}
Serial.println("");
Serial.println(F("Connected!"));
}
void loop()
{
if (millis() - nfc_time_now > nfc_ready_period)
{
nfc_time_now = millis();
readRFID();
}
if (millis() - upd_ready_time_now > upd_ready_period)
{
upd_ready_time_now = millis();
checkForUpdates();
}
}
void checkForUpdates()
{
if (WiFi.status() == WL_CONNECTED) {
String fwVerURL = "http://www.beicelectronics.com/version.php";
HTTPClient updHttp;
updHttp.setReuse(true);
updHttp.begin(fwVerURL);
int httpCode = updHttp.GET();
Serial.println("HTTP Response Code: " + String(httpCode));
if (httpCode == 200) {
String newFwTmpVersion = updHttp.getString();
Serial.println("HTTP Update Version: " + newFwTmpVersion);
} else {
Serial.println("Error HTTP Update!");
}
updHttp.end();
}
}
void readRFID(void)
{
boolean success;
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0};
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success)
{
for (uint8_t i = 0; i < uidLength; i++)
{
if (uid
<= 0xF) {
userRFID += "0";
}
userRFID += String(uid & 0xFF, HEX);
}
userRFID.toUpperCase();
Serial.println(userRFID);
delay(400);
userRFID = "";
}
}