Caricare un array da file .txt su SD

salve a tutti,
sto cercando di caricare un array di un indirizzo MAC prendendo i singoli byte da un file di testo salvato su SD
sto usando arduino con M5STACK (esp32) ed espnow
con questo codice


#include <M5Stack.h>

#include <esp_now.h>
#include <WiFi.h>

esp_now_peer_info_t slave;

//uint8_t remoteMac[] = {0x50, 0x02, 0x91, 0xA0, 0x63, 0xFD};
uint8_t remoteMac[6];// PROVA MAC ADDRESS DA SD
int arrayC = 0;

const uint8_t maxDataFrameSize = 200;

// must match the controller struct
struct __attribute__((packed)) DataStruct {

  int X;
  int Y;
  int Z;

};
DataStruct myData;
const esp_now_peer_info_t *peer = &slave;
uint8_t dataToSend[maxDataFrameSize];

unsigned long lastSentMillis;
unsigned long sendIntervalMillis = 10;
unsigned long sentMicros;
unsigned long ackMicros;


void setup() {

  M5.begin();
  Serial.begin(115200);
  M5.update();

  Serial.print("\r\n\r\n");
  WiFi.mode(WIFI_STA);
  Serial.println( WiFi.softAPmacAddress() );

  readMAC();

  WiFi.disconnect();
  if (esp_now_init() == ESP_OK)
  {
    Serial.println("ESP NOW INIT!");
  }
  else
  {
    Serial.println("ESP NOW INIT FAILED....");
  }
  memcpy( &slave.peer_addr, &remoteMac, 6 );


  slave.channel = 1; // canale

  slave.encrypt = 0;
  if ( esp_now_add_peer(peer) == ESP_OK)
  {
    Serial.println("Added Peer!");
    Serial.println();
  }
  esp_now_register_send_cb(OnDataSent);
}

void readMAC() {

  File fh = SD.open("/MAC.txt");
  char netBuffer[15];

  if (!fh)
  {
    Serial.println("SD open fail");
    return;
  }

  int chPos = 0;

  while (fh.available())
  {
    char ch = fh.read();
    if (ch == '\r') {
      chPos = 0;

      //          Serial.print("mac ");
      sscanf(netBuffer, "%2x:%2x:%2x:%2x:%2x:%2x", &remoteMac[0], &remoteMac[1], &remoteMac[2], &remoteMac[3], &remoteMac[4], &remoteMac[5]);

    }
    else if (ch == '\n') {
      // do nothing
    }
    else if (chPos < 14) {
      netBuffer[chPos] = ch;
      chPos++;
      netBuffer[chPos] = 0;
    }
  }

  fh.close();

  int x;

  Serial.print("\r\nmac ");
  for (x = 0; x < 6; x++) {
    Serial.print(remoteMac[x], HEX);
    if (x < 5) Serial.print(":");
  }
    Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:

}

con un file salvato su sd dal nome MAC.txt che contiene una sola riga come la seguente:

50:02:91:A0:63:FD

nel monitor seriale ho un output interessante ma imperfetto :

mac 50:2:91:A0:63:0

e master e slave di espnow non si collegano....

se inverto queste righe master e slave si collegano ma non sto leggendo il MAC address da sd..

uint8_t remoteMac[] = {0x50, 0x02, 0x91, 0xA0, 0x63, 0xFD};
//uint8_t remoteMac[6];// PROVA MAC ADDRESS DA SD

cosa sto sbagliando??

Stai leggendo solo 14 caratteri ma la stringa del mac compresiva dei ':' ha un totale di 17 caratteri (escluso \r e \n).

l'output seriale ora somiglia molto di piu al file!!!

mac 50:2:91:A0:63:FD

anche se mi manca all'appello quello 0 prima del 2...

ma il problena ora è che la scheda crasha con questo errore subito dopo avermi dato l'output seriale del mac address:

Backtrace: 0x4008cebc:0x3ffb1ea0 0x4008d0ed:0x3ffb1ec0 0x400e1398:0x3ffb1ee0 0x400d2c6a:0x3ffb1f00 0x400d3439:0x3ffb1f60 0x400dcec7:0x3ffb1fb0 0x400895d1:0x3ffb1fd0

forse ad espnow non piace questo metodo...

mumble mumble

Lo 0 manca perché la Serial.print() il valore 0x02 lo stampa come 2 e basta.
Per quanto riguarda l'errore, assicurati che l'array netBuffer sia grande abbastanza.

mettendo a 18 netbuffer funziona!!!!

grazie mille!!!!