Hello Guys !
I've created a code that uses BLE to get the MAC addresses of the surrounding devices, and then uploads them into a simple WebServer.
The program works fine, but after a while it stops working.
I'm using an arduino nano 33 iot.
This is my code:
#include <ArduinoBLE.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include "utility/wifi_drv.h"
#include "arduino_secrets.h"
#define ARRAYSIZE 7
String mac[ARRAYSIZE];
int rssi_val[ARRAYSIZE];
String v1; //MAC
String v2; //RSSI
String v3; //TS
int entries;
int WIFItimer;
int cont;
int found=0;
int count = 0;
int devices = 0;
int tests=0;
int contador;
int teste = 0;
char ssidName[] = SECRET_SSID;
char ssidPass[] = SECRET_PASS;
int status_wifi = WL_IDLE_STATUS;
WiFiServer server( 80 );
int status = WL_IDLE_STATUS;
void setup()
{
Serial.begin( 9600 );
while ( !Serial );
}
void loop()
{
contador=0;
while(tests<9)
{
bleInit();
bleMode();
Serial.println("------------------------");
for (int s = 0; s <= found-1; s++)
{
//Serial.println("TESTE");
Serial.println(mac[s]);
Serial.println(rssi_val[s]);
}
Serial.println("------------------------");
delay(1000);
tests++;
if(tests==9){
tests=0;
switch2WiFiMode();
wifiMode();
//WiFiInit();
//wifiMode();
found=0;
delay(10000);
}
}
}
void bleInit() {
//begin initialization
while (!BLE.begin()) // BLE.begin() 0-failed 1-Success
{
Serial.println("starting BLE failed!");
}
Serial.println("BLE scan");
BLE.scan(); // BLE.scan() 0-failed 1-Success
}
void bleMode(){
// check if a peripheral has been discovered
//int i = 0;
BLEDevice peripheral = BLE.available();
Serial.println("Searching Device");
if (peripheral) {
Serial.println("Found one device");
if (found != ARRAYSIZE){
found++;
}
String v1 = peripheral.address();
int v2 = peripheral.rssi();
mac[devices] = v1;
rssi_val[devices] = v2;
Serial.println("Valor lido de RSSI");
Serial.println(v2);
devices++;
if (devices==7){ //se o array de devices estiver cheio vai rescrever no primeiro slot do array
devices=0;
}
}
delay(1000);
}
void wifiConnect(){
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssidName);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssidName, ssidPass);
// wait 10 seconds for connection:
delay(10000);
}
printWiFiStatus();
}
void WiFiInit()
{
BLE.stopScan();
BLE.stopAdvertise();
BLE.end();
status_wifi = WL_IDLE_STATUS;
wiFiDrv.wifiDriverDeinit();
wiFiDrv.wifiDriverInit();
while (status_wifi != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssidName);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status_wifi = WiFi.begin(ssidName, ssidPass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWiFiStatus();
}
void wifiMode()
{
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssidName);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssidName, ssidPass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWiFiStatus();
Serial.println("Aguardando conexão ao servidor web");
delay(10000);
// Serial.print("Contador:");
// Serial.print(contador);
// Serial.println();
contador=0;
do {
delay(2000);
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
// an HTTP request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the HTTP request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard HTTP response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.println("<p>");
cont = 0;
do {
client.println("<p>");
// client.println("MAC: " + mac[cont] + " RSSI " + rssi_arr[cont]);
client.println("MAC: " + mac[cont] + " RSSI " + rssi_val[cont]);
client.println("<p>");
//client.println();
//client.println("<br />");
cont = cont + 1;
//Serial.print("Cont:");
//Serial.println(cont);
} while (cont != (ARRAYSIZE));
Serial.println("-----------------------");
client.println("<br />");
client.println("</html>");
//Serial.println("Establecendo conexão...");
Serial.println("Espere...");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(10000);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
delay(1000);
contador++;
Serial.print("Contador final:");
Serial.println(contador);
} while (contador < 5);
}
bool switch2BleMode()
{
if ( !BLE.begin() )
{
Serial.println("starting BLE failed!");
return false;
}
Serial.println("BLE scan");
//BLE.advertise();
// start scanning for peripheral
BLE.scan();
delay(1000);
return true;
}
bool switch2WiFiMode()
{
BLE.stopAdvertise();
BLE.end();
status = WL_IDLE_STATUS;
// Re-initialize the WiFi driver
// This is currently necessary to switch from BLE to WiFi
wiFiDrv.wifiDriverDeinit();
wiFiDrv.wifiDriverInit();
return true;
}
void printWiFiStatus()
{
Serial.print( "SSID: " );
Serial.println( WiFi.SSID() );
IPAddress ip = WiFi.localIP();
Serial.print( "IP address: " );
Serial.println( ip );
long rssi_wifi = WiFi.RSSI();
Serial.print( "Signal strength (RSSI):" );
Serial.print( rssi_wifi );
Serial.println( " dBm" );
}
Thank you so much