Thank you for your response, well here s my problem, there s a rasberri PI wiznet w5500 as a master and arduino nano rp2040 connect as slave, the slave role is to mesure either WIFI or BLE depending on specific command received by I2C protocol, here s the slave code :
#include <WiFiNINA.h>
#include <ArduinoBLE.h>
#include <Wire.h>
#define SLAVE_ADDRESS 8
constexpr long TIMEOUT_DURATION = 3000; // 3 seconds timeout for BLE reading
constexpr int OUTPUT_SIZE = 21;
char output[OUTPUT_SIZE]; // Fixed-size buffer for output
char command = '\0';
bool mesure_done = false;
bool mesure_done_slave = false;
bool results_sent = false;
unsigned long debut_mesures;
extern "C" char* sbrk(int incr);
int freeMemory() {
char stack_dummy = 0;
return &stack_dummy - sbrk(0);
}
void setup() {
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
// Set up LED pins if needed
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
activateBLE();
Serial.print("Initial Free Memory: ");
Serial.println(freeMemory()); // Print initial free memory
}
void loop() {
Serial.print("Free Memory in Loop: ");
Serial.println(freeMemory()); // Monitor free memory periodically in loop
if (command == 'W') {
listNetworks();
Serial.print("Free Memory after Wi-Fi scan: ");
Serial.println(freeMemory()); // Check memory after Wi-Fi scan
command = '\0';
} else if (command == 'B') {
readBLE();
Serial.print("Free Memory after BLE read: ");
Serial.println(freeMemory()); // Check memory after BLE read
}
delay(1000); // Delay to avoid overwhelming the Serial monitor
}
void receiveEvent(int howMany) {
if (howMany > 0) {
command = Wire.read();
Serial.println("Received command via I2C");
}
}
void requestEvent() {
if (!mesure_done_slave) {
Wire.write('F');
Serial.println("Measurement not done yet");
} else {
if (results_sent) {
Wire.write(output, OUTPUT_SIZE);
memset(output, 0, OUTPUT_SIZE); // Clear buffer
results_sent = false;
mesure_done_slave = false;
} else {
Wire.write('S');
Serial.println("Sent 'S' to indicate data ready");
results_sent = true;
}
}
}
void listNetworks() {
BLE.end();
delay(100); // Allow time for BLE to deactivate
WiFi.end();
delay(100);
int herbsrssi = 0;
int Linkyrssi = 0;
debut_mesures = millis();
int numSsid = WiFi.scanNetworks();
if (numSsid == -1) {
Serial.println("Couldn't get a WiFi connection");
return;
}
// Scan for specific networks and get RSSI values
for (int i = 0; i < numSsid; i++) {
if (strcmp(WiFi.SSID(i), "Linksys02978") == 0) {
Linkyrssi = WiFi.RSSI(i);
}
if (strcmp(WiFi.SSID(i), "HERBS_AP") == 0) {
herbsrssi = WiFi.RSSI(i);
}
}
// Build output message using snprintf for memory efficiency
snprintf(output, OUTPUT_SIZE, "%s,%s,%s,%lu",
(herbsrssi != 0) ? String(herbsrssi).c_str() : "KO",
(Linkyrssi != 0) ? String(Linkyrssi).c_str() : "KO",
"/",
millis() - debut_mesures);
while (strlen(output) < OUTPUT_SIZE - 1) {
strcat(output, "*"); // Pad with '*' to fill remaining buffer
}
mesure_done_slave = true;
}
void readBLE() {
activateBLE();
memset(output, 0, OUTPUT_SIZE); // Clear output buffer
strcpy(output, "/,/,");
BLE.scanForName("Arduino Nano RP2040", true);
debut_mesures = millis();
while (!mesure_done && ((millis() - debut_mesures) < TIMEOUT_DURATION)) {
BLEDevice device = BLE.available();
if (device) {
snprintf(output + strlen(output), OUTPUT_SIZE - strlen(output), "/,/,%d,", device.rssi());
mesure_done = true;
}
}
if (!mesure_done) {
strcat(output, "/,/,KO");
}
snprintf(output + strlen(output), OUTPUT_SIZE - strlen(output), "%lu", millis() - debut_mesures);
while (strlen(output) < OUTPUT_SIZE - 1) {
strcat(output, "*");
}
mesure_done_slave = true;
mesure_done = false;
command = '\0';
}
void activateBLE() {
BLE.end();
delay(100);
WiFi.end();
delay(100);
if (!BLE.begin()) {
Serial.println("Failed to initialize BLE!");
while (true);
}
}
After some execution time, the slave crashes and a blinking orange LED start appearing. In the begining, i suspected it maybe be I2C bus problem but after doing a sample ping pong program for i2c exchange over 8 hours no problem happened. The **CODE I PROVIDED ABOVE ** is a revised one after getting ride of string type and all possible dynamic memory allocation it greatly reduce how frequently the problem happened, i suspect that alternating wifi and BLE is causing a large chunk of memory to be occupied maybe? I didn t find a specific library or method that allows to monitor free memory, i tried the one in the code i posted but it was returing a fixe large negative value.