Error conexión bluetooth con más de un sensor /bluetooth error with more than one sensor

Tengo un problema con la conexión Bluetooth entre el arduino y la app. Tengo más de un sensor, y al rato de ir mostrando los valores de las variables en diferentes labels, me sale un error de que busca un item con numero superior a la longitud de la lista. He probado de varias maneras., poniendo un marcador al final y al inicio, un condicional con la longitud de la lista pero no consigo que funcione correctament. He visto por internet que varia gente tiene el mismo problema y no he encontrado la solución. Si alguien me pudiera ayudar. Muchas gracias

Tengo un problema con la conexión Bluetooth entre el arduino y la app. Tengo más de un sensor, y al rato de ir mostrando los valores de las variables en diferentes labels, me sale un error de que busca un item con numero superior a la longitud de la lista. He probado de varias maneras., poniendo un marcador al final y al inicio, un condicional con la longitud de la lista pero no consigo que funcione correctament. He visto por internet que varia gente tiene el mismo problema y no he encontrado la solución. Si alguien me pudiera ayudar. Muchas gracias

I have a problem with the Bluetooth connection between the arduino and the app. I have more than one sensor, and after a while showing the values of the variables in different labels, I get an error that you are looking for an item with a number greater than the length of the list. I have tried in several way, Putting a marker at the end and at the beginning, a conditional with the length of the list but I don’t get it to work properly. I have seen online that several people have the same problem and I have not found the solution. If someone could help me. Thank you

Hello Gerard
We would need to see your Code Blocks and the Ardunio .ino Sketch.

Yes, sorry

#include "DHT.h"
//Mac hc05: 98:D3:51:FD:99:66
#define DHTTYPE DHT11



const byte DHTpin=30;  //Pin sensor DHT

DHT dht(DHTpin,DHTTYPE);  //Parámetres del sensor DHT11

const int Bat=A0;  //Pin analògic bateries
const int Vbat=A0;  //Pin analògic voltatge bateries
const int Ibat=A1;  //Pin analògic intensitat bateries
const int Vplac=A2;  //Pin analògic voltatge plaques
const int Iplac=A3;  //Pin analògic intensitat plaques
const int Vdomi=A4;  //Pin analògic voltatge casa
const int Idomi=A5;  //Pin analògic intensitat casa
const int Tplac=A6;  //Pin analògic temperatura placa
const int Radsolar=A7;  //Pin analògic radiació solar
const int Radplacas=A8;  //Pin analògic radiació plaques
const int Tbat=A9;  //Pin analògic temperatura bateries

int valorBat;  //Valor analògic estat bateries
int valorVbat;  //Valor analògic voltatge bateries
int valorIbat;  //Valor analògic intensitat bateries
int valorVdomicilio;  //Valor analògic voltatge casa
int valorIdomicilio;  //Valor analògic intensitat casa
int valorVplacas;  //Valor analògic voltatge plaques
int valorIplacas;  //Valor analògic intensitat plaques
int valorTbat;  //Valor analògic temperatura bateries
int valorRadsolar;  //Valor analògic radiació solar
int valorTplacas;  //Valor analògic temperatura plaques
int valorRadplacas;  //Valor analògic radiació plaques
int porcentajeBat;  //Valor porcentatge bateries

float SimTbat;  //Valor simulat temperatura bateries
float SimTplacas;  //Valor simulat temperatura plaques
float SimRadsolar;  //Valor simulat radiació solar
float SimRadplacas;  //Valor simulat radiació plaques
float SimVbaterias;  //Valor simulat voltatge bateries
float SimIbaterias;  //Valor simulat intensitat bateries
float SimVdomicilio;  //Valor simulat voltatge casa
float SimIdomicilio;  //Valor simulat intensitat casa
float SimVplacas;  //Valor simulat voltatge plaques
float SimIplacas;  //Valor simulat intensitat plaques
float t;  //Valor de temperatura del sensor
float ComparacionRad;  //Diferència dues radiacions per comprovar brutícia



void setup(){
Serial1.begin(9600);
dht.begin();  //Iniciar sensor DHT
pinMode(DHTpin,INPUT);
   
}
 
void loop(){
 
 Baterias();
 temperaturambient();
 RadiacionSolar();
 Tbaterias();
 RadiacionPlacas();
 Tplacas();
 Vbaterias();
 Ibaterias();
 Vplacas();
 Iplacas();
 Vdomicilio();
 Idomicilio();
 /*Bruticia();*/

 Serial1.print ("I");
 Serial1.print("|");
 Serial1.print (porcentajeBat);
 Serial1.print ("|");
 Serial1.print (t);
 Serial1.print ("|");
 Serial1.print (SimTbat);
 Serial1.print ("|");
 Serial1.print (SimTplacas);
 Serial1.print ("|");
 Serial1.print (SimVbaterias);
 Serial1.print ("|");
 Serial1.print (SimIbaterias);
 Serial1.print ("|");
 Serial1.print (SimVplacas);
 Serial1.print ("|");
 Serial1.print (SimIplacas);
 Serial1.print ("|");
 Serial1.print (SimVdomicilio);
 Serial1.print ("|");
 Serial1.print (SimIdomicilio);
 Serial1.print ("|");
 Serial1.print (SimRadsolar);
 Serial1.print ("|");
 Serial1.print (SimRadplacas);
 Serial1.print ("|");
 Serial1.print (ComparacionRad);
 Serial1.print ("|");
 Serial1.print ("F"); 
 Serial1.print("|\n");
 delay(550);
}

void Baterias() {  //Funció que retorna el porcentaje de les baterias (A0)
    
 valorBat = analogRead(Bat);  // Realitzar la lectura analógica raw
 porcentajeBat = map(valorBat, 0, 1023, 0, 101);  // Convertir a porcentaje
 return porcentajeBat;
}

void Vbaterias() {  //Funció que retorna voltaje baterias (A0)
    
 valorVbat = analogRead(Bat);  // Realitzar la lectura analógica raw
 SimVbaterias=valorVbat/42.625;  //Convertir a valors reals de voltatge entre 0 i 24 V
 /*SimVbaterias = map(valorVbat, 0, 1023, 0, 24);  // 0-24 V*/
 return SimVbaterias;
}

void Ibaterias() {  //Funció que retorna intesitat baterias (A1)
    
 valorIbat = analogRead(Ibat);  //Realitzar la lectura analógica raw
 SimIbaterias=valorIbat/204.6;  //Convertir a valors reals d'intensitat entre 0 i 5 A
 /*SimIbaterias = map(valorIbat, 0, 1023, 0, 5);  // 0-5 A*/
 return SimIbaterias;
}


void Iplacas() {  //Funció que retorna intensitat plaques solars (A3)
    
 valorIplacas = analogRead(Iplac);// Realitzar la lectura analógica raw
 SimIplacas=valorIplacas/170.5;  //Convertir a valors reals d'intensitat entre 0 i 6 A
 /*SimIplacas = map(valorIplacas, 0, 1023, 0, 6);  // 0-6 A*/
 return SimIplacas;
}


void Vplacas() {  //Funció que retorna voltaje plaques solars (A2)
    
 valorVplacas = analogRead(Vplac);  //Realitzar la lectura analógica raw
 SimVplacas=valorVplacas/20.46;  //Convertir a valors reals de voltatge entre 0 i 50 V
 /*SimVplacas = map(valorVplacas, 0, 1023, 0, 50);  // 0-50 V*/
 return SimVplacas;
}


void Idomicilio() {  //Funció que retorna intensitat domicili (A5)
    
 valorIdomicilio = analogRead(Idomi);  //Realitzar la lectura analógica raw
 SimIdomicilio=valorIdomicilio/204.6;  //Convertir a valors reals d'intensitat entre 0 i 5 A
 /*SimIdomicilio = map(valorIdomicilio, 0, 1023, 0, 5);  // 0-5 V*/
 return SimIdomicilio;
}


void Vdomicilio() {  //Funció que retorna voltaje domicili (A4)
    
 valorVdomicilio = analogRead(Vdomi);  //Realitzar la lectura analógica raw
 SimVdomicilio=valorVdomicilio/2.5575;  //Convertir a valors reals de voltatge entre 0 i 400 V
 /*SimVdomicilio = map(valorBat, 0, 1023, 0, 400);  // 0-400 V*/
 return SimVdomicilio;
}


void Tplacas() {  //Funció que retorn temperatura plaques solars (A6)
    
 valorTplacas = analogRead(Tplac);//Realitzar la lectura analógica raw
 SimTplacas = map(valorTplacas, 0, 1023, 5, 76);  //Convertir a valors reals de temperatura entre 5 y 75 graus
 return SimTplacas;
}


void RadiacionSolar() {  //Funció que retorna radiació solar (A7)
    
 valorRadsolar = analogRead(Radsolar);  //Realitzar la lectura analógica raw
 SimRadsolar=valorRadsolar/0.2046;  //Convertir a valors reals de radiació entre 0 i 5000
 /*SimRadsolar = map(valorRadsolar, 0, 1023, 0, 5000);  // convertir a porcentaje*/
 return SimRadsolar;
}


void RadiacionPlacas() {  //Funció que retorna radiació plaques solars (A8)
    
 valorRadplacas = analogRead(Radplacas);//Realitzar la lectura analógica raw
 SimRadplacas=valorRadplacas/0.2046;  //Convertir a valors reals de radiació entre 0 i 5000
 /*SimRadplacas = map(valorRadplacas, 0, 1023, 0, 5000);  // convertir a porcentaje*/
 return SimRadplacas;
}

void Tbaterias() {  //Funció que retorna temperatura bateries (A9)
    
 valorTbat = analogRead(Tbat);  //Realitzar la lectura analógica raw
 SimTbat = map(valorTbat, 0, 1023, -20, 51);  //Convertir a valors reals de temperatura entre -20 i 50 graus
 return SimTbat;
}

void temperaturambient(){  //Funció per medir temperatura ambient i humitat amb sensor DHT11
  t = dht.readTemperature();
  return t;
} 

void Bruticia() {
    RadiacionPlacas();
    RadiacionSolar();
    ComparacionRad=SimRadplacas - SimRadsolar;  //Difference between two radiations
    ComparacionRad=abs(ComparacionRad);  //Absolute value of difference
    return ComparacionRad;
  }

Hi Gerard,

I send large amounts of data from Arduino without any problems.

I would suggest you replace all the Serial1.print (“”); commands with only 1 Serial1.println(“”) command. Notice Serial1.println not Serial1.print.

I would first store all your values to a string, then send them.

I would also include Start and End tags, so you can check the data in the app.

Something like this:

BatValues = ‘”Start” +"|"+ valorBat +"|"+ valorVbat +"|"+ valorIbat +"|"+ valorVdomicilio +"|"+ ”End”’;

Serial1.println (BatValues);
BatValues =””; //clear the string
delay(550);

Also you don’t nee to include: Serial1.print("|\n");

Check for the Start and End tags, then do something with the data, if the tags aren’t both there, then do something else like send a request back to Arduino to resend the data.

Also it is best to change your Global List to a local List, that way it is re-initialized empty each time.

Also make sure you disable the timer while you are working with the data, then restart it when you are done.

Mike.

See attached for how to receive text only one full message at a time.

I added 2 blocks and changed one block.

You should be able to drag the blocks into your Blocks Editor.

Hello Mike

Your recommendations are opposite to what I would normally recommend with Bluetooth :slight_smile: In most cases it is best not to send the values as one String because the Device may not have a large enough buffer to accommodate, although some devices will permit an MTU increase (BLE devices). Also, it does not help to have Pre and Post values to indicate the start and finish - that is an extra task in the App yet it proves nothing at all about the validity of the data.

Gerard, Most failures are caused by timing. If the data is sent too quickly or frequently, the App does not have enough time to process it. Values from sensors needn’t be displayed every 550 milliseconds - the human eye cannot watch changes that are so fast! Also, we do not want to use Delay() in the main loop as it stops everything. So we record elapsed time instead. The trick is to get everything working at reasonable speed and then, if necessary, tweak later (both the App and the script).

So:

unsigned long CurrentMillis;
unsigned long PreviousMillis = 1;
unsigned long IntervalMillis = 4000;

void setup()
{
         Serial1.begin(9600);
         dht.begin();  //Iniciar sensor DHT
         pinMode(DHTpin,INPUT);
}
 
void loop()
{ 
         Baterias();
         temperaturambient();
         RadiacionSolar();
         Tbaterias();
         RadiacionPlacas();
         Tplacas();
         Vbaterias();
         Ibaterias();
         Vplacas();
         Iplacas();
         Vdomicilio();
         Idomicilio();
         /*Bruticia();*/

         CurrentMillis = millis();
         if( (CurrentMillis - PreviousMillis) >= IntervalMillis ) 
         {
               PreviousMillis = CurrentMillis;

               Serial1.print (porcentajeBat);
               Serial1.print ("|");
               Serial1.print (t);
               Serial1.print ("|");
               Serial1.print (SimTbat);
               Serial1.print ("|");
               Serial1.print (SimTplacas);
               Serial1.print ("|");
               Serial1.print (SimVbaterias);
               Serial1.print ("|");
               Serial1.print (SimIbaterias);
               Serial1.print ("|");
               Serial1.print (SimVplacas);
               Serial1.print ("|");
               Serial1.print (SimIplacas);
               Serial1.print ("|");
               Serial1.print (SimVdomicilio);
               Serial1.print ("|");
               Serial1.print (SimIdomicilio);
               Serial1.print ("|");
               Serial1.print (SimRadsolar);
               Serial1.print ("|");
               Serial1.print (SimRadplacas);
               Serial1.print ("|");
               Serial1.print (ComparacionRad);
               Serial1.print ("|");
               Serial1.println();
         }
}

I think the above .ino modifications along with ABG’s Blocks modifications should work, with the exception of the “start” and “end” checks which should be superfluous.

Note that ABG has added 10 as the Bluetooth data delimiter byte (= Line Feed or Print Line) and this is matched-up in the Sketch by using a (blank) Serial1.println(), telling your App “End of Data”.

However, since the Sketch is Looping, your code should be driven by a Clock Timer that is set to fire at a shorter interval than the data is sent. For now, I have set the Loop at 4000. Set your Clock Timer to 3000. Only run the Clock for the first time when you have a connection;

Preset Clock

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.