this is the Master side of the code, i just toggle the value of a variable "nano_mode" that either has "W" or "B".
TwoWire Wire0(i2c1, 6, 27); // Custom I2C pins: SCL on pin 6, SDA on pin 27
// Master code snippet
void sendData() {
Wire0.beginTransmission(8);
Wire0.write(nano_mode); // "W" or "B" command
int error = Wire0.endTransmission();
if (error == 0) {
Serial.println("Transmission successful");
} else {
Serial.print("Transmission error: ");
Serial.println(error);
digitalWrite(28, HIGH); // Red LED indication on error
}
}
void requestData_from_salve() {
Wire0.requestFrom(8,20);
String taille_msg = ""; // Initialize a string to store the message
char d;
while(Wire0.available()) {
d=Wire0.read();
taille_msg += d; // Append the character to the received message
}
message+=taille_msg;
}
and this is the slave code :
#define PASSWORD_WIFI1 "HERBS_AP" //minimum 8 caractéres (limitation librairie)
#define SSID_WIFI1 "HERBS_AP" // minimum 8 caractéres (limitation librairie)
#define PASSWORD_WIFI2 "dqp0mku7kt" //minimum 8 caractéres (limitation librairie)
#define SSID_WIFI2 "Linksys02978" // minimum 8 caractéres (limitation librairie)
#define ID_GATEWAY 1
int herbsrssi;
int Linkyrssi;
char output[21];
long timeout_duration = 1000; // 1 seconds timeout
char command = '\0';
unsigned long debut_mesures;
bool mesure_done = false;
void setup() {
Wire.begin(8); // Join I2C bus with address #8
Wire.onReceive(receiveEvent); // Register receive event
Wire.onRequest(requestEvent); // Register request event
Serial.begin(9600); // Start serial communication at 9600 baud
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
activateBLE();
}
void loop() {
if (command == 'W') {
listNetworks();
command='\0';
Serial.println("this is the output of WIFI");
Serial.println(output);
} else if(command == 'B') {
activateBLE();
strcpy(output, "/,/,");
Serial.println(" i m scanning now for the BLE i m seeking");
BLE.scanForName("Arduino Nano RP2040", true);
debut_mesures=millis();
while (!mesure_done && ((millis() - debut_mesures) < timeout_duration)) {
BLEDevice device = BLE.available();
if (device) {
char rssiStr[8];
snprintf(rssiStr, sizeof(rssiStr), "%d,", device.rssi());
strcat(output, rssiStr);
mesure_done = true;
} else {
//Serial.println("waiting for measurement of BLE");
}
}
if (!mesure_done) {
strcat(output, "KO,");
}
char timeStr[5];
snprintf(timeStr, sizeof(timeStr), "%lu", millis() - debut_mesures);
strcat(output, timeStr);
command='\0';
mesure_done=false;
// Serial.println("this is the output of BLE");
//Serial.println(output);
}
}
void receiveEvent(int howMany) {
if (howMany > 0) {
command = Wire.read();
}
}
void requestEvent() {
Serial.println("this is before checking ");
Serial.println(output);
while (strlen(output) < 20) {
strcat(output, "*");
}
Wire.write(output,20);
Serial.println("this is after checking");
Serial.println(output);
output[0] = '\0';
}
void listNetworks() {
BLE.end();
delay(100);
WiFi.end();
delay(100);
int herbsrssi = 0;
int Linkyrssi = 0;
debut_mesures=millis();
Serial.println("i m gonna start measurement");
int numSsid = WiFi.scanNetworks();
if (numSsid == -1) {
Serial.println("Couldn't get a WiFi connection");
while (true);
}
// print the network number and name for each network found:
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
if (strcmp(WiFi.SSID(thisNet), "Linksys02978") == 0) {
Linkyrssi = WiFi.RSSI(thisNet);
}
if (strcmp(WiFi.SSID(thisNet), "HERBS_AP") == 0) {
herbsrssi = WiFi.RSSI(thisNet);
}
}
if (herbsrssi != 0) {
char rssiStr[5];
snprintf(rssiStr, sizeof(rssiStr), "%d,", herbsrssi);
strcat(output, rssiStr);
} else {
strcat(output, "KO,");
}
if (Linkyrssi != 0) {
char rssiStr[5];
snprintf(rssiStr, sizeof(rssiStr), "%d,", Linkyrssi);
strcat(output, rssiStr);
} else {
strcat(output, "KO,");
}
char timeStr[6];
snprintf(timeStr, sizeof(timeStr), "/,%lu", millis() - debut_mesures);
strcat(output, timeStr);
}
void activateBLE() {
BLE.end();
delay(100);
WiFi.end();
delay(100);
if (!BLE.begin()) {
Serial.println("Failed to initialize BLE!");
while (1);
}
Serial.println("BLE Initialized.");
}