-->
Page 1 of 1

HTTPClient error -1 while PN532 NFC module reading on SPI

PostPosted: Mon Jun 22, 2020 5:08 pm
by beic
Hi there,

After a two week of struggling and without a solution I figured out that the PN532 library is interfering somehow with HTTPClient library while calling GET() function and it's freezing the whole code for 3-5 seconds and giving randomly -1, -2, -3 and -11 http error codes.

If I comment the RFID reading part from the Sketch loop, everything is working as it should.

Code: Select allreadRFID(); to //readRFID();


I'm using WeMos D1 mini with PN532 over SPI, Arduino IDE v1.8.2, ESP8266 Core v2.3.0 and v2.7.1.

Tried Adafruits and Seeed-Studios PN532 library too, but getting the same odd result.
https://github.com/adafruit/Adafruit-PN532/releases/tag/1.0.4
https://github.com/Seeed-Studio/PN532

Also tried to put away PN532 module from WeMos D1 mini device on 10cm dupont cable (tested with 20cm dupont cable too), but again same weird results.

Yes, also tried external power supply, adding capacitors...and nothing.

Bellow you can see the sketch, results and the hardware setup.

Any help would be appreciated,

Kind regards,
Viktor

Used Sketch / Code:

Code: Select allextern "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 definition 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[i] <= 0xF) {
        userRFID += "0";
      }
      userRFID += String(uid[i] & 0xFF, HEX);
    }
    userRFID.toUpperCase();
    Serial.println(userRFID);
    delay(400);
    userRFID = "";
  }
}


Curl Result:

Code: Select allcurl -D - http://www.beicelectronics.com/version.php

HTTP/1.1 200 OK
Date: Mon, 22 Jun 2020 21:47:01 GMT
Server: Apache/2.4.43 (cPanel) OpenSSL/1.1.1g mod_jk/1.2.41 mod_bwlimited/1.4 mod_fcgid/2.3.9 Phusion_Passenger/5.3.7
X-Powered-By: PHP/5.5.38
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Content-Length: 3
Content-Type: text/plain

3.9


Serial Monitor Output:

Image

Hardware Setup:

Image