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);
}







