App keeps giving List index too large

I am making an app for a device that uses esp32, and it is supposed to display the readings of four sensors on the app and do other tasks. When testing the app, it would work just fine until I connect the ESP to the app via Bluetooth, the app would spam an error message saying "List index is too large" and close the app.

How do you make sure the lista with length 5?

I really don't know. Someone helped me with this part

image
at the arrow position, add IF (LENGTH OF LIST list LISTA > 5) THEN...

but this only prevent the pop up of that error message, you need to check from ESP why the data send is not completed.

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

I did what you suggested, and it did get rid of the error message, but the readings still don't show up in the app. I fixed the code of the ESP and replaced .print() with .println(). In an error message, it did show each reading separately, but now I'm trying to show the readings in the app.

Export and upload your new .aia file here.

Also copy all your sketch text and paste it here.

Asthma_Buddy.aia (265.9 KB)

And here is the sketch code (Its not yet finished so some errors will be found)

#include <DHT.h>
#include <MQUnifiedsensor.h>
#include <Wire.h>
#include "BluetoothSerial.h"
#include "MAX30105.h"
#include "heartRate.h"

#define Voltage_Resolution 5 //voltage to power MQ-9 sensor
#define operating_voltage 3.3 //Voltage to operate the GP2Y10 dust sensor

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

int Dust_measure = 35; // assign pin 35 to Dust sensor's analog pin
int Dust_LED = 32;   // assign pin 32 to Dust sensor's LED's digital pin  
constexpr uint8_t DHT_PIN = 33; // assign pin 33 to DHT11
constexpr uint8_t MQ_PIN = 25; // assign pin 25 to MQ-9 

//Reseting the Values measured by Dust sensor
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;

int sensorADC;
float sensorVoltage;
float sensorDustDensity;
float zeroSensorDustDensity = 0.6; 

int counter = 0; //Counts the number of times the patient recieved an alarm

DHT dht(DHT_PIN, DHT22);
MQUnifiedsensor MQ9("ESP32", Voltage_Resolution, 12, MQ_PIN, "MQ-9");
MAX30105 particleSensor;
BluetoothSerial SerialBT;

const byte RATE_SIZE = 4;
byte rates[RATE_SIZE];
byte rateSpot = 0;
long lastBeat = 0;

// Variables used by MAX30102
float beatsPerMinute;
int beatAvg;  

// Timer variable to track elapsed time
unsigned long timer = 0;
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
unsigned long time4 = 0;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32-Bluetooth");

  pinMode(Dust_LED, OUTPUT);
  digitalWrite(Dust_LED, LOW);

  dht.begin();
  MQ9.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ9.init();
  
  Serial.print("Calibrating please wait.");
  float calcR0 = 0;
  for (int i = 1; i <= 10; i++) {
    MQ9.update();
    calcR0 += MQ9.calibrate(9.6); // Using RatioMQ9CleanAir value
    Serial.print(".");
  }
  MQ9.setR0(calcR0 / 10);
  Serial.println("  done!.");

  if (isinf(calcR0)) {
    Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
    while (1);  //Infinite loop to halt the program
  }
  if (calcR0 == 0) {
    Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
    while (1);   //Infinite loop to halt the program
  }

  Serial.println("** Values from MQ-9 ****");
  Serial.println("CO  | ");

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) // Use default I2C port, 400kHz speed
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);  //Infinite loop to halt the program
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");

  particleSensor.setup(); // Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); // Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); // Turn off Green LED
}

void loop() {
  unsigned long currentTime = millis();
  unsigned long currentMillis = millis();

  if (currentTime - timer >= 604800000) {
    Serial.println(counter);
    // Reset the timer
    timer = currentTime;
    // Reset the counter
    counter = 0;

  }
  else {
    if (currentMillis - time1 > 2){
      max();
      time1 = currentMillis;

    }
    if (currentMillis - time2 > 1){
      dust();
      time2 = currentMillis;

    }
    if (currentMillis - time3 > 1){
      dht11();
      time3 = currentMillis;

    }
    if (currentMillis - time4 > 1){
      mq9();
      time4 = currentMillis;

    }
  }

  //Threshold of each parameter
  if(sensorDustDensity >= 35.00){
    counter++;

  }

  if(temp < 10){
    counter++;

  }
  else if (temp > 30){
    counter++;

  }

  if(humidity > 41){
    counter++;

  }

  if(beatAvg > 100){
    counter++;

  }

  if(CO > 100){
    counter++;

  }
}

void dust ()
{
   for (int i = 0 ; i < 30 ; i++){
    digitalWrite(Dust_LED, HIGH);
    delayMicroseconds(280);
    sensorADC += analogRead(Dust_measure);
    digitalWrite(Dust_LED, LOW);
  }
  sensorADC = sensorADC / 10;
  sensorVoltage = (operating_voltage / 1024.0) * sensorADC * 11;

  if (sensorVoltage < zeroSensorDustDensity){
    sensorDustDensity = 0;
  }
  else {
    sensorDustDensity = 0.17 * sensorVoltage - 0.1;
  }

  Serial.print(sensorVoltage);
  Serial.print(" ");
  Serial.print(sensorDustDensity);
  Serial.print(" μg/m3 | ");
  SerialBT.println(sensorDustDensity);
}


void mq9 ()
{
  MQ9.update();
  MQ9.setA(599.65);
  MQ9.setB(-2.244);
  float CO = MQ9.readSensor();

  Serial.print("CO: ");
  Serial.print(CO);
  Serial.println(" PPM");
  SerialBT.println(CO);
}

void dht11 () 
{
  float temp = dht.readTemperature();
  float humidity = dht.readHumidity();

  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print(" C ");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" % | ");
  SerialBT.println(humidity);
  SerialBT.println(temp);

}


void max () 
{
  long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true )
  {
    //We sensed a beat! 
    long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0 );

    if (beatsPerMinute < 255 && beatsPerMinute > 20 )
    {
      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array 
      rateSpot %= RATE_SIZE; //Wrap variable

      //Take average of readings 
      beatAvg = 0 ;
       for (byte x = 0 ; x < RATE_SIZE ; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
      beatAvg = beatAvg * 2;
    }
  }

  Serial.print( "IR=" );
  Serial.print(irValue);
  Serial.print( ", BPM=" );
  Serial.print(beatsPerMinute);
  Serial.print( ", Avg BPM=" );
  Serial.print(beatAvg);

  if (irValue < 50000 )
    Serial.print( " No finger?" );
  
  Serial.print(" | ");
  SerialBT.println(beatAvg);

}

Here is your Clock Timer block:

It expects a BT message containing 5 readings, separated by ';' (semicolon character),
but it does not ask for complete messages (ending in \n) using the numberOfBytes=-1 code.

I looked at your transmitting code.
It does not send any ';' to the BT print stream SerialBT, and it terminates each reading with a \n (println), defeating any attempt to determine which reading is which.

Have the person who wrote the sketch and the person who wrote the AI2 code sit down together and agree on message formats.

:cry:

well, could you help me by telling me how to change message format of the app to be appropriate with the transmitting code and what exactly should I add or change for the app to accept the code that uses println.

I have fixed the SerialBT.print() part of the code and now the app shows the readings, but now the readings aren't quiet accurate or similar to what is shown on the serial monitor. I did another test in which I turned off the serial monitor and used an external battery, the app functioned a lot faster but the readings were still different. Is there any baud rate that I should change?

The only reasons I can think of for variation of reading values between your serial monitor and the AI2 display are:

  • Time lag between println() and data reception in AI2, caused by running the AI2 Clock Timer too slow (ms high)
  • Failure to identify which reading is which in the messages, instead relying on their order, and slipping a cog because end of message delimiters (-1) were not implemented properly.

Only with access to current sending and receiving full code could any one diagnose any further.

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