#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <phyphoxBle.h>
#include "DFRobot_PH.h"
#include <EEPROM.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define PH_PIN 32
float voltage, phValue, temperature = 25;
DFRobot_PH ph;
void setup() {
Serial.begin(115200);
pinMode(PH_PIN, INPUT);
delay(1000);
Wire.begin(21, 22); // SDA, SCL
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
// Start the BLE server, argument is visible name
PhyphoxBLE::start("PH-Meter");
// Experiment
PhyphoxBleExperiment phMeter;
phMeter.setTitle("PH-Meter");
phMeter.setCategory("ESP32 Experiment");
phMeter.setDescription("Plot the PH value over time.");
// View
PhyphoxBleExperiment::View view;
view.setLabel("Raw data");
// Graph
PhyphoxBleExperiment::Graph phGraph;
phGraph.setLabel("PH Value");
phGraph.setUnitX("s");
phGraph.setUnitY("pH");
phGraph.setLabelX("time");
phGraph.setLabelY("PH");
phGraph.setChannel(0, 1);
view.addElement(phGraph); // attach graph to view
phMeter.addView(view); // attach view to experiment
PhyphoxBLE::addExperiment(phMeter); // attach experiment to server
ph.begin();
}
void loop() {
static unsigned long timepoint = millis();
if(millis() - timepoint > 1000U) { // time interval: 1s
timepoint = millis();
voltage = analogRead(PH_PIN) / 4095.0 * 3300; // read the voltage, adjusted for 3.3V
phValue = ph.readPH(voltage, temperature); // convert voltage to pH with temperature compensation
display.clearDisplay();
display.setTextSize(1); // Kleinere Schriftgröße für "PH:"
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0); // Linke obere Ecke
display.print("PH:");
display.setTextSize(2); // Größere Schriftgröße für den Wert
display.setCursor((SCREEN_WIDTH - 6 * 12) / 2 + 10, (SCREEN_HEIGHT - 16) / 2); // Zentriert und nach rechts verschoben
display.print(phValue);
display.display();
PhyphoxBLE::write(phValue);
}
ph.calibration(voltage, temperature); // calibration process by Serial CMD
delay(500);
}