I have the problem that in the nRF Connect app (bluetooth terminal) in my smartphone I can't see the services and characteristics. What my program needs to do is, fill an array with analog reads. After this, the array goes to an FFT funtion (this function works perfectly). This function gives a resut wich I want to send via BLE. This is the loop and has to be refreshed arround every ten seconds.
This is my setup and my loop, can anyone tell me why I cant see the services and characteristics in the app??
void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600); // se define la velocidad de comunicacion por el puerto serie
Serial.println("Started");
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("Programa_Ignacio"); // se define el nombre del programa para visualizar en app
BLE.setAdvertisedService(resultado);
resultado.addCharacteristic(frecuencia);
BLE.addService(resultado);
frecuencia_respiracion=("0");
frecuencia.setValue(frecuencia_respiracion);
BLE.advertise();
Serial.println("BLE FRECUENCY");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
for (int i=0; i<65; i++) {
datos[i]=analogRead(sensorPin); // lectura analogica y almacenamiento de datos en un vector de 64 huecos
delay(100); // retraso en la lectura para poder muestrear a 10 Hz
}
FFT(datos,64,10); // se pasa el vector con los datos de lectura analógica por la funcion FFT (transformada de Fourier)
delay(1000);
frecuencia_respiracion=String(f_peaks[0]*60); // se
Serial.println(frecuencia_respiracion);
frecuencia.setValue(frecuencia_respiracion);
}
}
Would it be possible for you to edit your post and use the </> button to encase your code so it looks like this:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
The nRF Connect app works well with the Arduinos, however, I can see that your code does not have a section with while (central.connected()) { which is vital to ensure proper communication.
Also, BLE connections do not like the use of dealy() so you will need to find a way to get your code to run without them.
Please have a look at the example I posted a while ago for an environmental sensing service. This will show you a couple of tricks that will help you make your sketch work, removing the need for delays and while loops.
A BLE peripheral application layer does not send data, it updates it characteristics and that is it.
The central device is responsible for getting the data. It can read/write the data and get notifications when it subscribes to a characteristic.
As matt_lee_smith said, communication stacks do not like delay().
About FFT, to get a good FFT result you need equidistant sampling. That will not work with using delay or millis(). Luckily the nRF52840 is awesome at doing this on its own. You will need to do some programming. Have a look at the examples I posted in the following thread.
They use a hardware timer and the PPI system to trigger the ADC automatically and tell you when a sample is ready. You will need to adapt this for your use case and integrate it with BLE. The functions are independent and most of it runs in hardware after the setup.
One last tip. Avoid programming in 3 languages. C/C++ and English is enough. I am not a native English speaker and after a little while you will be happy you stopped inserting another cause of confusion. And it helps other people understand your code better.