Hello!
First of all I'm sorry if it's the wrong forum section, I'm not sure what causes my problem!
It occurs when I try to run an Uno connected to a PN532 (NFC controller) to a Huzzah ESP8266 (Wifi board) and to a Neopixel strip (addressable LED strip).
Here is the simplified process of my project (corresponding to the code and pictures below): the ESP connects to the wifi, asks (through hardware serial) the Arduino data to connect to a MQTT server and subscribe to topics. Then the Arduino waits for a NFC tag detected by the PN532 then publish a MQTT message through the ESP and change the colour of the LED strip.
I tested the project with no USB connexion. The strip has its dedicated power supply (5V 3A), and the Uno is also powered through the barrel with its own power supply (9V 1A). The PN532 and the ESP are powered by the Arduino 3.3V and 5V.
The issue: when I power all the components at the same time, the ESP connects and exchanges with the Arduino but the PN532 does not detect any tag.
Where it's starting to get strange is that I found several workarounds (after long hours trying to understand and reverting to working versions of the code) that make the project "works":
- Powering the Arduino after the other components (this solution is not visible in the pictures: the ESP has is connected to the strip power supply for that): then everything is working as expected! (but it's not a solution for my project)
- Removing the "strip.show()" in the loop() allows the PN532 to find tags again, and I really don't know why...
- Uncommenting an old code that I want to remove (with pointer array I don't use anymore and debug logs, check the "old code" comment) and everything is working again, which don't makes any sense to me ^^'
#include <SoftwareSerial.h>
#include <Adafruit_DotStar.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
//Use the softserial for debug
SoftwareSerial softSerial(10, 11);
bool debug = true;
//Serial buffer
char serialbuf[64];
const char SOPmarker = '<';
const char EOPmarker = '>';
#define MAX_STRING_LEN 64
//Store MQTT topics and values
char *MQTT_topics[] = {"led/state",
"switch/state",
"switch/connected",
"neurotech/validuid",
"crystal1/validuid"};
char *MQTT_values[] = {"", "", "", "", ""}; //I'm trying to remove this ugly line...
int MQTT_topics_size;
byte index=0;
//State of the ESP8266
char espID = '2';
bool espReady = false;
unsigned long startESPMillis;
//PN532
#define PN532_SCK (2)
#define PN532_MOSI (3)
#define PN532_SS (4)
#define PN532_MISO (5)
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
//PN532 Timers
unsigned long startMillis;
unsigned long currentMillis;
int nfc_period = 1000;
//LED Strip
#define STRIP_PIN 13
#define STRIP_LED_COUNT 30
Adafruit_NeoPixel strip(STRIP_LED_COUNT, STRIP_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
softSerial.begin(9600);
if (debug) {
softSerial.println("Running on the Arduino");
}
//Compute MQTT_topics sizee -- trying to remove that to
MQTT_topics_size = sizeof(MQTT_topics) / sizeof(MQTT_topics[0]);
//PN532
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
softSerial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
softSerial.print("Found chip PN5");
nfc.setPassiveActivationRetries(1);
// configure board to read RFID tags
nfc.SAMConfig();
softSerial.println("Waiting for an ISO14443A Card ...");
//LED Strip
strip.begin();
strip.fill(strip.Color(243,77,9));
strip.setBrightness(50);
strip.show();
}
void loop() {
//ESP => Arduino serial reception
if (Serial.available()) {
static int bufpos = 0;
char inchar = Serial.read();
if (inchar != EOPmarker && inchar != SOPmarker) {
serialbuf[bufpos] = inchar;
bufpos++;
}else if (inchar == SOPmarker) {
bufpos = 0;
}else{
//Package received
serialbuf[bufpos] = 0;
bufpos = 0;
if (debug) {
softSerial.print("received: ");
softSerial.println(serialbuf);
}
//Split the package into header and content
char* header = subStr(serialbuf, "!", 1);
char* content = subStr(serialbuf, "!", 2);
if(strcmp(header, "ESP") == 0) {
//Received a request to id the ESP
if (debug) {
softSerial.print("sent: ");
softSerial.println("<ESP!1>");
}
Serial.print("<ESP!");
Serial.print(espID);
Serial.print(">");
}else if(strcmp(header, "TOPIC") == 0) {
//Received a request to get the topic to subscribe
long index = atol(content);
if(index < MQTT_topics_size) {
char message[] = "<TOPIC!";
strcat(message,MQTT_topics[index]);
strcat(message,">");
if (debug) {
softSerial.print("sent: ");
softSerial.println(message);
}
Serial.print(message);
}else{
// No more topic to subscribe, stop the request loop
if (debug) {
softSerial.print("sent: ");
softSerial.println("<TOPIC!STOP>");
}
Serial.print("<TOPIC!STOP>");
}
}else if(strcmp(header, "READY") == 0) {
//receive that ESP is ready to receive
espReady = true;
}else{
//No specific header = topic update
//==> Old code I'm trying to remove! <==
/*
for (int i = 0; i < MQTT_topics_size; i++) {
if(strcmp(header, MQTT_topics[i]) == 0) {
MQTT_values[i] = content;
}
if (debug) {
softSerial.println(MQTT_values[i]);
}
}
*/
//==> End of old code I'm trying to remove! <==
//Confirm the reception
Serial.print("<CHECK!0>");
//Check the Crystal status
if(strcmp(serialbuf, "crystal1/validuid!powertrue") == 0){
//Color the strip
strip.fill(strip.Color(243,77,9));
strip.show(); //==> Comment this and it's working! <==
//Publish opening
Serial.print("<crystal1/state!yes");
}
}
}
}
currentMillis = millis();
if(espReady){
//Every second, check for NFC tag
if (currentMillis - startMillis >= nfc_period)
{
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
Serial.print("<neurotech/scanuid!TEST>");
}
startMillis = currentMillis;
}
}else{
//If ESP is still not ready, restart configuration
if (currentMillis - startESPMillis >= 3000){
Serial.print("<ESP!");
Serial.print(espID);
Serial.print(">");
startESPMillis = currentMillis;
}
}
}
char* subStr (char* input_string, char *separator, int segment_number) {
char *act, *sub, *ptr;
static char copy[MAX_STRING_LEN];
int i;
strcpy(copy, input_string);
for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
sub = strtok_r(act, separator, &ptr);
if (sub == NULL) break;
}
return sub;
}
Sorry for the long code, I tried to remove as much as possible but as I'm not sure where the issue is, I preferred to keep a good part of it.
I could also publish my ESP8266 code if you think it's useful (but it is working well with different projects), or anything else... (Sorry for the mess on the pictures especially around the PN532, it is welded like this for a previous project but works well with basic codes).
So if you have an idea to explain this weird behaviour that seems (to me) to connect unrelated things, i'm interested!! Thanks for taking the time to read my issue ![]()

