#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
// <-------- Start Display -------->
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// <-------- End Display -------->
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
// <-------- Start Temperature Variables -------->
// Der NTC ist mit dem GPIO 27 verbunden
const int ntcPin = 32;
// Variable um den Wert des NTC zu speichern
int ntcValue = 0;
double Vs = 5; // Ausgangsspannung
double adcMax = 4095.0; // Analog-Digital Teiler
int R1 = 10000; // 10kOhm
const float Beta = 3950; // Umrechnungsfakor des NTC
double To = 298.15; // Temperatur in Kelvin für 25° Celsius
double Ro = 10000.0; // Widerstand des NTC bei 25° Celsius
// <-------- End Temperature Variables -------->
// <-------- Start Display -------->
#include <Adafruit_GFX.h>
// <-------- End Display -------->
//ThingSpeak Webservice
#include <ThingSpeak.h>
#define CHANNEL_ID 2385197
#define CHANNEL_API_KEY "525R59BZS64T01I8"
WiFiClient client;
#define ErrorLED 25
#define statusLED 4
#define httpLED 2
WebServer server(80);
String phValue = "1";
void sendHtml() {
String response = R"(
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #ffffff; /* Change background color to white */
font-family: sans-serif;
font-size: 2em;
text-align: center;
}
.iframe-container {
display: flex;
flex-direction: column;
gap: 20px; /* Add some space between the iframes */
}
</style>
</head>
<body>
<div class="iframe-container">
<iframe width="450" height="260" style="border: none;" src="https://thingspeak.com/channels/2385197/widgets/774036"></iframe>
<iframe width="450" height="260" style="border: none;" src="https://thingspeak.com/channels/2385197/charts/1?bgcolor=%23ffffff&color=%23d62020&dynamic=true&results=60&type=line&update=15"></iframe>
</div>
</body>
</html>
)";
// Den NTC-Wert lesen
ntcValue = analogRead(ntcPin);
// Formel zur Umrechung des Wertes
double Vout = ntcValue * Vs/adcMax;
double Rt = R1 * Vout / (Vs - Vout);
double T = 1/(1/To + log(Rt/Ro)/Beta); // Temperature in Kelvin
phValue = T - 273.15; // Celsius
ThingSpeak.writeField(CHANNEL_ID, 1, phValue, CHANNEL_API_KEY);
// response.replace("PH-VALUE", phValue);
server.send(200, "text/html", response);
}
void display_on_OLED(u8_t heap, String ph){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,05);
display.println(String(heap) + "% Free memory");
display.setCursor(0,15);
display.println("------------");
display.println("| PH Value |");
display.println("------------");
display.println("| " + String(ph) + " |");
display.println("------------");
display.display();
}
void setup(void) {
Serial.begin(115200);
pinMode(ErrorLED, OUTPUT);
pinMode(statusLED, OUTPUT);
pinMode(httpLED, OUTPUT);
digitalWrite(statusLED, HIGH);
// <-------- Start Temperature Setup -------->
delay(1000);
// <-------- End Temperature Setup -------->
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", sendHtml);
try{
server.begin();
Serial.println("HTTP server started (http://localhost:8180)");
digitalWrite(httpLED, HIGH);
throw("Server couldnt start");
}catch(String exception){
Serial.println(exception);
}
//ThingSpeak setup
ThingSpeak.begin(client);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop(void) {
server.handleClient();
// Den NTC-Wert lesen
ntcValue = analogRead(ntcPin);
// Formel zur Umrechung des Wertes
double Vout = ntcValue * Vs/adcMax;
double Rt = R1 * Vout / (Vs - Vout);
double T = 1/(1/To + log(Rt/Ro)/Beta); // Temperature in Kelvin
phValue = T - 273.15; // Celsius
if(T - 273.15 >= 40){
digitalWrite(ErrorLED, HIGH);
}else{
digitalWrite(ErrorLED, LOW);
}
uint8_t heapPercent = ESP.getFreeHeap() * 100 / ESP.getHeapSize();
display_on_OLED(heapPercent, phValue);
delay(20);
}