#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
#include <phyphoxBle.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SensorPin 35
#define button7Pin 32
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40
int pHArray[ArrayLenth];
int pHArrayIndex = 0;
float calibrationValue7 = 0.0;
float phValue = 0.0;
float Offset = 0.0;
void setup() {
pinMode(SensorPin, INPUT);
pinMode(button7Pin, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("PH Meter");
display.display();
EEPROM.get(0, calibrationValue7);
EEPROM.get(2 * sizeof(float), Offset);
// 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
}
void loop() {
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float voltage;
int button7State = digitalRead(button7Pin);
if (button7State == LOW) {
calibrationValue7 = analogRead(SensorPin);
Offset = 7.0 - (3.5 * (calibrationValue7 * 3.3 / 4095));
EEPROM.put(0, calibrationValue7);
EEPROM.put(2 * sizeof(float), Offset);
}
if (millis() - samplingTime > samplingInterval) {
pHArray[pHArrayIndex++] = analogRead(SensorPin);
if (pHArrayIndex == ArrayLenth) pHArrayIndex = 0;
voltage = avergearray(pHArray, ArrayLenth) * 3.3 / 4095;
phValue = 3.5 * voltage + Offset;
samplingTime = millis();
}
if (millis() - printTime > printInterval) {
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);
printTime = millis();
}
}
double avergearray(int* arr, int number) {
int i;
int max, min;
double avg;
long amount = 0;
if (number <= 0) {
return 0;
}
if (number < 5) {
for (i = 0; i < number; i++) {
amount += arr[i];
}
avg = amount / number;
return avg;
} else {
if (arr[0] < arr[1]) {
min = arr[0];
max = arr[1];
} else {
min = arr[1];
max = arr[0];
}
for (i = 2; i < number; i++) {
if (arr[i] < min) {
amount += min;
min = arr[i];
} else if (arr[i] > max) {
amount += max;
max = arr[i];
} else {
amount += arr[i];
}
}
avg = (double)amount / (number - 2);
}
return avg;
}