#include <LCD_I2C.h>
#include "WiFi.h"
WiFiClient client;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
#define heater 15
LCD_I2C lcd(0x27, 16, 2);
String thingSpeakAddress = "api.thingspeak.com";
String writeAPIKey;
String tsfield1Name;
String request_string;
void setup() {
lcd.begin();
lcd.backlight();
Serial.begin(9600);
pinMode(heater, OUTPUT);
digitalWrite(heater, LOW);
WiFi.disconnect();
WiFi.begin("Wokwi-GUEST", "");
while ((!(WiFi.status() == WL_CONNECTED))) {
delay(300);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
int sensor_suhu = analogRead(34);
float suhu_kolam = 1 / (log(1 / (4095. / sensor_suhu - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(suhu_kolam);
Serial.println(" ℃");
lcd.setCursor(0, 0);
lcd.print("Suhu kolam :");
lcd.setCursor(0, 1);
lcd.print(suhu_kolam);
if (suhu_kolam > 34){
digitalWrite(heater, LOW);
}else if(suhu_kolam < 26){
digitalWrite(heater, HIGH);
}
// kirim_thingspeak(suhu_kolam);
delay(1000);
}
void kirim_thingspeak(float suhu_kolam) {
if (client.connect("api.thingspeak.com", 80)) {
request_string = "/update?";
request_string += "key=";
request_string += "7QP0W9YVRNEMOO59";
request_string += "&";
request_string += "field1";
request_string += "=";
request_string += suhu_kolam;
Serial.println(String("GET ") + request_string + " HTTP/1.1\r\n" +
"Host: " + thingSpeakAddress + "\r\n" +
"Connection: close\r\n\r\n");
client.print(String("GET ") + request_string + " HTTP/1.1\r\n" +
"Host: " + thingSpeakAddress + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
}