Split Sensor Data received

Hello, I am very new to building an app and hit sorta a road block. I am creating an app that collects data from a DHT22 and a Vegetronix sensor. I can get the app to recieve just one singular sensor data. However, when I try to split the text. My app gets a problem stating that the "List index is too large". This is my current progress. I have tried to make an empty list and populate it, and that didn't work. I tried to create a list with values and replace those values with the byte values I recieved. I have registered for float, string and int. Which either give give me weird characters in wrapped in [] or end up printing nothing. I am not sure what else I can do, any help would be appreciated.

Fish_Tank_Control (1).aia (340.4 KB)

Here it is list of lists having one item "41", need to approach accordingly.

Edit: Your data is appeared to be like

So it is advised to look how data is constructed, that need to be organized, if you want 41 to be extrected, that can be but code might break other moment.

So the data that is currently being extracted is a temperature and humidity rating. I am trying to split the data, but it wouldn't split it onto the respect labels and continues to print the second part of the sensor reading only. I am not sure how to implement the solution you have suggested. Also I went back and tried to create an empty list.
image

Here is your current event that receives bytes:

The split at | technique is inappropriate for this kind of data stream, where you receive a list of byte values.

Here is a conservative replacement that won't crash ...

Notice how I test the list length before each attempt at selecting a list item by index., so I will never inadvertently fall off the end.

P.S. These blocks can be dragged directly into your Blocks Editor workspace.

See A way to group projects by theme - #10 by ABG
for a demo.

2 Likes

Wow, thank you! That is actually very efficient. I was wondering though, should I go back to my arduino code and remove the "|" and use println instead to try to split by end of line?

Edit: So this method actually helped remove the [] that surrounded my data. However, the moisture rating is still not getting populated. It is currently 73 degrees with a humidity of 45, and yet (Temp_rating) is the only one getting populated. It seems the second reading is replacing the first and not indexing properly.

Maybe it's time to show us your Arduino code?

It helps to have both sides of the data comm agreeing with each other.

#include <DHT.h>
#include <DHT_U.h>
#include <ArduinoBLE.h>
#include "ArduinoTimer.h"
#define Type DHT22

ArduinoTimer Timer1;

int thsens= 4; //using D4 pin on NANO
DHT HT(thsens,Type); //connect to DHT22
float hum;
float tempC;
float tempF;
int delaytime=500; //delay time in milliseconds
int dt=1000;
const int ledPin = LED_BUILTIN; // pin to use for the LED
long previousMillis = 0; // last time the battery level was checked, in ms

BLEService SCGService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);

//*************************************************************************************

void setup() {
Serial.begin(9600);
HT.begin();

while (Serial); //does not wait for PC to open serial port

// set LED pin to output mode
pinMode(thsens, INPUT); //set up pin for input or output this case
//we are receiving data from sensor so input

// set LED pin to output mode
pinMode(ledPin, OUTPUT);

// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");

while (1);

}

// set advertised local name and service UUID:
BLE.setLocalName("SCG");
BLE.setAdvertisedService(SCGService);

// add the characteristic to the service
SCGService.addCharacteristic(switchCharacteristic);
// SCGService.addCharacteristic(humidityChar);

// add service
BLE.addService(SCGService);

// set the initial value for the characeristic: this might need to be changes to read
switchCharacteristic.writeValue(0);
// humiditychar.writeValue[0];

// start advertising
BLE.advertise();

// Serial.println("BLE SCG Peripheral");
}

//****************************************************************************************

void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();

// if a central is connected to peripheral:
if (central) {

// tempF=HT.readTemperature(true);
// Serial.print(tempF);
// Serial.println(" F");
// switchCharacteristic.writeValue(tempF);

// while the central is still connected to peripheral:
while (central.connected()) {
  // if the remote device wrote to the characteristic,
  // use the value to control the LED:
  long currentMillis = millis();
  // if 200ms have passed
  if (currentMillis - previousMillis >= 2000) {
    previousMillis = currentMillis;
    MeasureTemperature();
  }
      
  
  if (switchCharacteristic.written()) {
    if (switchCharacteristic.value()) {   // any value other than 0
      //Serial.println("LED on");
      digitalWrite(ledPin, HIGH);         // will turn the LED on
    } else {                              // a 0 value
      //Serial.println(F("LED off"));
      digitalWrite(ledPin, LOW);          // will turn the LED off
    }
  }
}

}

//if (Timer1.TimePassed_Hours(6, AutoReset - 0) || switchCharacteristic.value() ){ //If a certain time has passed or if the switch characteristic has some value. Call functions
//MeasureTemperature();
////Serial.print(soilsens());
//}
} //End void loop

void MeasureTemperature(){
hum=HT.readHumidity();
tempF=HT.readTemperature(true);
Serial.println((int)tempF);
switchCharacteristic.writeValue(tempF);
Serial.println((int)hum);
switchCharacteristic.writeValue(hum);
}

Sorry I couldn't upload just the program, so I had to sadly copy paste it. I tried to addcharacteristic humidityChar but ended up getting out of the scope when I tried to save humidityChar.writeValue(hum);

To summarize what you have done,

You set up a data stream of Bytes:

You write temperature and humidity alternating to the same data stream using writeValue.

How would you tell which is which when the data arrives?

A print() and println() data stream can be more informative.

Switched everything to primtln(), however I am still not sure on how I can fix this issue. I tried to add a new characteristic. Should I create a new characteristic UUID for humidity?

I have reached the limit of my BLE device knowledge on this.

I defer to people with more experience at this point.

I remember seeing many BLE projects on this board from other power users.

I truly appreciate all you have done! Thank you so much for the help you have provided!

So I have created multiple characteristic UUIDs to send my variable data across. I am currently trying to compare the UUIDs. however my text fields are not getting filled what so ever. I did this to try and send my humidity value through its own humiditycharacteristic and my temperature value through tempcharacteristic

I have tested my data stream on lightblue. It shows the two different characteristicUUIDs and when I read the data it shows that one is sending temperature and the other is sending humidity. I am not sure what I can do next to try to get my app to see everything

Change the file extension to .txt, then it will upload.

That's not strictly necessary. Your Arduino is or should be sending float values. So, receive as floats (or text strings), not bytes.

So when I try to register for floats and use float received, I get blank values. Same thing when I try to send int as well. Bytes has so far been the only thing that worked

Thank you! Let me add it now!

Edit: So when I try to use lightblue to check for the values, my values are sent through the proper characteristic data stream. However, my app does not read them what so ever. Even when I am trying to read for integers recieved and registering for integers. The solution provided by @ABG was helpful, but when I tried to use a logic operator to compare characteristicUUID to the UUID recieved. Nothing is printed at all. I am not sure why

BLETestDHT22.txt (3.8 KB)

Hi

Can you list all your hardware, in fine detail.

DHT22 for humidity and Temperature data, an Arduino Nano 33 iot, and a Vegetronix VH400.

No Android device?

1 Like

The Arduino Nano 33 models are always trouble with BLE :roll_eyes:
Do you have The ArduinoBLE version 1.2.0 installed?

I notice that the Vegetronix VH400 does not support Bluetooth, it uses WiFi. Wouldn't it be best to use WiFi for all data transfer?

1 Like