Hi to all,
I want to create a little meteorologicel station with Arduino. The first idea was used NANO 33 SENSE to provide information about temperature, humidity and barometric pressure (peripheric) to NANO 33 BLE (central). I tried several times but the program did not work (it did not connect between devices). So I have changed my idea and have simplify it.
Now both devices get conection with BLE, but it do not go ahead (not send/receive infromation).
I show you both codes that I programmed on them. Please, can you check my codes and give me any information that you find on it or code that I did not develop yet?
If you know another tutorial of BLE library that appears in Arduino's web page, please, can you share? Thanks in advance.
Here appears both codes and the serial monitor show when them are working. Sorry, you will find all comments and serial messages in Spanish, I hope it will not be a problem for you.
Code of Arduino NANO 33 BLE, central device:
#include <ArduinoBLE.h> //libreria Bluetooth
BLEIntCharacteristic Caracteristica1("0002", BLERead | BLENotify); //declaramos caracteristica numero al servicio Servicio
unsigned int mseg = 4000;
bool ScanActive = 0;
int cuenta;
void setup() {
// put your setup code here, to run once:
delay(mseg);
Serial.begin(9600); //inicializamos la comunicacion monitor serie
if (!BLE.begin()) { //inicializamos Bluetooth
Serial.println("Fallo al inicializar BLE");
while (1);
}
Serial.println("Inicializamos el dispositivo BlueTooth central");
delay(mseg);
BLE.setLocalName("MiCentral"); //set nombre local
BLE.setConnectable(true); //el periferico es conectable después de publicitarlo
Serial.println("Indicamos dispositivo BLE central conectable");
delay(mseg);
}
void loop() {
// put your main code here, to run repeatedly:
if (!ScanActive) {
BLE.scan();
Serial.println("Escaneamos en busca de dispositivo BLE periferico");
ScanActive = 1;
delay(mseg);
}
BLEDevice periferico = BLE.available(); //dispositivo periferico disponible
Serial.println(periferico);
if (periferico) { //el dispositivo BLE esta disponible
Serial.println("Dispositivo BLE periferico esta disponible");
Serial.println(periferico);
delay(mseg);
if (periferico.hasLocalName()) { //dispone de localName?
Serial.print("Local name: ");
Serial.println(periferico.localName());
} //end haslocalname
else Serial.println("Local name: no encontrado");
if (periferico.hasAdvertisedServiceUuid()) { //dispone de Servicios?
Serial.print("Service UUIds: ");
for (int i = 0; i < periferico.advertisedServiceUuidCount(); i++) {
Serial.print(periferico.advertisedServiceUuid(i));
Serial.print(" ");
} //end for
Serial.println();
} //end hasadvertisedserviceuuid
else Serial.println("Service UUIDs: no encontrados");
delay(mseg);
if (periferico.localName() == "MiPeriferico") { //encontramos el dispositivo BLE que buscamos
BLE.stopScan(); //paramos el escaneo de dispositivos.
Serial.println("Conectando...");
delay(mseg);
if (periferico.connect()) { //Conectamos al dispositivo BLE encontrado
Serial.println("Conectado");
delay(mseg);
//añadimos descubrir atributos
Serial.println("Discovering attributes ...");
if (periferico.discoverAttributes()) {
Serial.println("Atributos descubiertos");
} else {
Serial.println("Fallo al descubrir atributos");
}
delay(mseg);
//fin del añadido descubrir atributos
Serial.print("Device name: ");
Serial.println(periferico.deviceName());
Serial.print("Local name: ");
Serial.println(periferico.localName());
delay(mseg);
if (Caracteristica1.valueUpdated()) {
cuenta = Caracteristica1.value();
Serial.println(cuenta);
delay(mseg);
} //end valueupdated
} //end periferico.connect
else {
Serial.println("Fallo en la conexion");
// BLE.disconnect();
periferico.disconnect();
Serial.println("Desconectado");
ScanActive = 0;
} //end else
delay(mseg);
} //end periferico.localName
} //end periferico
else {
Serial.println("Dispositivo BLE periferico no esta disponible");
Serial.println(periferico);
BLE.disconnect();
ScanActive = 0;
delay(mseg);
} //end else
} //end loop
Code of Arduino NANO 33 SENSE, peripherical device:
#include <ArduinoBLE.h> //libreria Bluetooth
BLEService Servicio("0001"); //declaramos el servicio Servicio
BLEIntCharacteristic Caracteristica1("0002", BLERead | BLENotify); //declaramos caracteristica numero al servicio Servicio
int cuenta = 0;
unsigned int mseg = 4000;
bool Add = 0;
void setup() {
// put your setup code here, to run once:
delay(mseg);
Serial.begin(9600); //inicializamos la comunicacion monitor serie
if (!BLE.begin()) { //inicializamos Bluetooth
Serial.println("fallo al inicializar BLE");
while (1);
}
Serial.println("Inicializamos el dispositivo BlueTooth periferico");
delay(mseg);
BLE.setLocalName("MiPeriferico"); //set nombre local
BLE.setAdvertisedService(Servicio); //indicamos que el servicio tambien se publicite
Servicio.addCharacteristic(Caracteristica1); //añadimos caracteristica Caracteristica al servicio Servicio
BLE.addService(Servicio); //añadimos el servicio
BLE.setConnectable(true); //el periferico es conectable después de publicitarlo
Serial.println("Indicamos dispositivo BLE periferico conectable");
delay(mseg);
}
void loop() {
// put your main code here, to run repeatedly:
if (!Add) {
if (BLE.advertise()) { //publicitando periferico
Serial.println("Dispositivo BLE periferico visible");
Add = 1;
}
else {
Serial.println("Dispositivo BLE periferico no visible");
Add = 0;
}
delay(mseg);
}
BLEDevice zentral = BLE.central(); //preguntamos si central se ha conectado
Serial.println("Dispositivo BLE central conectando...");
delay(mseg);
if (zentral) { //si Lector esta disponible
Serial.println("Dispositivo BLE central conectado");
delay(mseg);
while (zentral.connected()) { //y mientras central permanezca conectado
Caracteristica1.writeValue(cuenta);
Serial.println(cuenta);
cuenta++;
if (cuenta == 30000) {
cuenta = 0;
}
}
}
else {
Serial.println("Dispositivo BLE central no conectado");
Add = 0;
}
delay(mseg);
}
Serial monitor messages from central device:
20:20:31.429 -> Indicamos dispositivo BLE central conectable
20:20:35.469 -> Escaneamos en busca de dispositivo BLE periferico
20:20:39.469 -> 1
20:20:39.469 -> Dispositivo BLE periferico esta disponible
20:20:39.469 -> 1
20:20:43.449 -> Local name: MiPeriferico
20:20:43.449 -> Service UUIds: 0001
20:20:47.489 -> Conectando...
20:20:51.489 -> Conectado
20:20:55.480 -> Discovering attributes ...
20:21:05.079 -> Fallo al descubrir atributos
20:21:09.079 -> Device name:
20:21:09.079 -> Local name: MiPeriferico
20:21:17.078 -> 0
20:21:17.078 -> Dispositivo BLE periferico no esta disponible
20:21:17.078 -> 0
20:21:22.109 -> Escaneamos en busca de dispositivo BLE periferico
20:21:26.109 -> 1
20:21:26.109 -> Dispositivo BLE periferico esta disponible
20:21:26.109 -> 1
20:21:30.109 -> Local name: MiPeriferico
20:21:30.109 -> Service UUIds: 0001
20:21:34.093 -> Conectando...
20:21:38.253 -> Conectado
20:21:42.253 -> Discovering attributes ...
20:21:50.172 -> Fallo al descubrir atributos
20:21:54.172 -> Device name:
20:21:54.172 -> Local name: MiPeriferico
20:22:02.172 -> 0
20:22:02.172 -> Dispositivo BLE periferico no esta disponible
20:22:02.172 -> 0
Serial monitor messages from peripherical device:
20:23:11.770 -> Inicializamos el dispositivo BlueTooth periferico
20:23:15.769 -> Indicamos dispositivo BLE periferico conectable
20:23:19.799 -> Dispositivo BLE periferico visible
20:23:23.800 -> Dispositivo BLE central conectando...
20:23:27.813 -> Dispositivo BLE central no conectado
20:23:31.821 -> Dispositivo BLE periferico no visible
20:23:35.810 -> Dispositivo BLE central conectando...
20:23:39.820 -> Dispositivo BLE central no conectado
20:23:43.819 -> Dispositivo BLE periferico no visible
20:23:47.819 -> Dispositivo BLE central conectando...
20:23:51.819 -> Dispositivo BLE central no conectado
20:23:55.859 -> Dispositivo BLE periferico visible
20:23:59.840 -> Dispositivo BLE central conectando...
20:24:03.873 -> Dispositivo BLE central conectado
20:24:07.874 -> 0
20:24:07.874 -> 1
...