How do you receive data from the ESP32 for the application?

Hi everyone! I'm working on a college project involving a portable pH meter. My goal is to transfer data from the ESP32 to an app and then into a Google Sheets spreadsheet. I've watched some videos and tried various methods, but nothing works. Any help would be greatly appreciated.

I'll put the codes here for you to see.

// ===== pHmetro com Bluetooth =====
// Envia automaticamente pH e temperatura via Bluetooth (HC-05 ou ESP32 Bluetooth)

#include <Arduino.h>
#include <Preferences.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "BluetoothSerial.h"

// ---------- CONFIGURAÇÕES DO PH ----------
const int PH_ADC_PIN = 34;
const int ADC_MAX = 4095;
const float VREF = 3.3f;

// ---------- LCD ----------
const uint8_t LCD_ADDR = 0x27; // ou 0x3F
LiquidCrystal_I2C lcd(LCD_ADDR, 16, 2);

// ---------- FILTRO DO PH ----------
const int SAMPLE_COUNT = 8;
float phSamples[SAMPLE_COUNT];
int sampleIdx = 0;

// ---------- CALIBRAÇÃO ----------
Preferences prefs;
float calibA = 1.0f;
float calibB = 0.0f;
bool haveCalibration = false;

// ---------- DS18B20 ----------
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// ---------- BLUETOOTH ----------
BluetoothSerial SerialBT;

// ---------- DISPLAY ----------
unsigned long lastDisplay = 0;
const unsigned long DISPLAY_INTERVAL = 1000; // atualização a cada 1 segundo

// ---------- FUNÇÕES ----------
float readVoltageFromADC() {
  int raw = analogRead(PH_ADC_PIN);
  return (raw * VREF) / (float)ADC_MAX;
}

float filteredVoltage() {
  float v = readVoltageFromADC();
  phSamples[sampleIdx] = v;
  sampleIdx = (sampleIdx + 1) % SAMPLE_COUNT;
  float sum = 0;
  for (int i = 0; i < SAMPLE_COUNT; i++) sum += phSamples[i];
  return sum / SAMPLE_COUNT;
}

void saveCalibration() {
  prefs.begin("phcal", false);
  prefs.putFloat("a", calibA);
  prefs.putFloat("b", calibB);
  prefs.putBool("have", haveCalibration);
  prefs.end();
}

void loadCalibration() {
  prefs.begin("phcal", true);
  calibA = prefs.getFloat("a", 1.0f);
  calibB = prefs.getFloat("b", 0.0f);
  haveCalibration = prefs.getBool("have", false);
  prefs.end();
}

// ---------- COMPENSAÇÃO DE TEMPERATURA ----------
float compensateA(float A25, float tempC) {
  return A25 * ((tempC + 273.15) / (25.0 + 273.15));
}

// ---------- SETUP ----------
void setup() {
  Serial.begin(115200);
  delay(200);

  Wire.begin();
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("pHmetro v0.4");
  lcd.setCursor(0, 1);
  lcd.print("Inicializando...");
  delay(700);

  for (int i = 0; i < SAMPLE_COUNT; i++) phSamples[i] = 0.0f;
  loadCalibration();

  sensors.begin();

  // Inicia Bluetooth
  SerialBT.begin("pHmetro_ESP32"); // nome que aparece no celular
  Serial.println("Bluetooth iniciado. Pronto para parear!");

  lcd.clear();
  lcd.print("Pronto");
  delay(500);
  lcd.clear();

  Serial.println("pHmetro com compensacao termica e Bluetooth iniciado.");
}

// ---------- LOOP ----------
void loop() {
  // Leitura de temperatura
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);

  // Leitura de pH
  float V = filteredVoltage();
  float ph = 7.0;

  if (haveCalibration) {
    float A_temp = compensateA(calibA, tempC);
    ph = A_temp * V + calibB;
  }

  // Atualizar LCD e enviar via Bluetooth
  if (millis() - lastDisplay > DISPLAY_INTERVAL) {
    lastDisplay = millis();

    // LCD
    lcd.setCursor(0, 0);
    lcd.print("pH:");
    lcd.print(ph, 2);
    lcd.print("     ");
    lcd.setCursor(0, 1);
    lcd.print("T:");
    lcd.print(tempC, 1);
    lcd.print((char)223);
    lcd.print("C   ");

    // Serial (para debug)
    Serial.print("pH: ");
    Serial.print(ph, 2);
    Serial.print(" | Temp: ");
    Serial.print(tempC, 1);
    Serial.println("C");

    // Bluetooth
    SerialBT.print(ph, 2);
    SerialBT.print("|");
    SerialBT.print(tempC, 1);
  }

  delay(1000);
}

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.

...

Here is an updated blocks sample illustrating these ideas ...

BlueTooth_delimiter_sample.aia(3.4 KB) global message

...