Upload files to "/"
This commit is contained in:
commit
36d2f6c680
|
@ -0,0 +1,200 @@
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <OneWire.h>
|
||||||
|
#include <DallasTemperature.h>
|
||||||
|
#include <ThingerESP32.h>
|
||||||
|
|
||||||
|
#define TURBIDITY_PIN 35
|
||||||
|
#define ONE_WIRE_BUS 16
|
||||||
|
#define relayPin1 19
|
||||||
|
#define relayPin2 18
|
||||||
|
#define relayPin3 17
|
||||||
|
|
||||||
|
#define USERNAME "karel"
|
||||||
|
#define DEVICE_ID "esp32"
|
||||||
|
#define DEVICE_CREDENTIAL "esp32_TugasAkhir"
|
||||||
|
|
||||||
|
OneWire oneWire(ONE_WIRE_BUS);
|
||||||
|
DallasTemperature sensors(&oneWire);
|
||||||
|
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
|
||||||
|
|
||||||
|
long time_1 = 0;
|
||||||
|
int interval = 1000;
|
||||||
|
float turbidityNTU = 0;
|
||||||
|
short ntuMin = 0;
|
||||||
|
short ntuMax = 30;
|
||||||
|
int sensorMin = 0;
|
||||||
|
int sensorMax = 4095;
|
||||||
|
|
||||||
|
float turbidityMin = 0.0;
|
||||||
|
float turbidityMax = 100.0;
|
||||||
|
|
||||||
|
unsigned long lastNotificationTime1 = 0;
|
||||||
|
unsigned long lastNotificationTime2 = 0;
|
||||||
|
unsigned long lastNotificationTime3 = 0;
|
||||||
|
const unsigned long notificationInterval = 60000;
|
||||||
|
|
||||||
|
unsigned long relayToggleTime = 0;
|
||||||
|
bool toggleRelay = false;
|
||||||
|
bool isDraining = false;
|
||||||
|
|
||||||
|
bool isHeating = false; // New variable to track heating state
|
||||||
|
bool isCooling = false; // New variable to track cooling state
|
||||||
|
|
||||||
|
float reverseMapFloat(float x, float in_min, float in_max, float out_min, float out_max) {
|
||||||
|
return (x - in_min) * (out_min - out_max) / (in_max - in_min) + out_max;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
pinMode(relayPin1, OUTPUT);
|
||||||
|
pinMode(relayPin2, OUTPUT);
|
||||||
|
pinMode(relayPin3, OUTPUT);
|
||||||
|
digitalWrite(relayPin1, HIGH);
|
||||||
|
digitalWrite(relayPin2, HIGH);
|
||||||
|
digitalWrite(relayPin3, HIGH);
|
||||||
|
sensors.begin();
|
||||||
|
|
||||||
|
// Koneksi Ke Wifi
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
thing.add_wifi("grahadewisri", "navas123");
|
||||||
|
thing.handle();
|
||||||
|
#ifdef ESP32
|
||||||
|
// client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
|
||||||
|
#endif
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(1000);
|
||||||
|
Serial.println("Connecting to WiFi..");
|
||||||
|
}
|
||||||
|
// Print ESP32 Local IP Address
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
thing["Temperature"] >> [](pson& out) {
|
||||||
|
out = sensors.getTempCByIndex(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
thing["Turbidity"] >> [](pson& out) {
|
||||||
|
out = turbidityNTU;
|
||||||
|
};
|
||||||
|
|
||||||
|
thing["sensorData"] >> [](pson& out) {
|
||||||
|
float Temperature = sensors.getTempCByIndex(0);
|
||||||
|
float Turbidity = turbidityNTU;
|
||||||
|
|
||||||
|
out["Temperature"] = Temperature;
|
||||||
|
out["Turbidity"] = Turbidity;
|
||||||
|
};
|
||||||
|
|
||||||
|
thing["relay1"] << [](pson& in) {
|
||||||
|
if (in.is_empty()) { // If no input, return current state
|
||||||
|
} else {
|
||||||
|
digitalWrite(relayPin1, LOW);
|
||||||
|
delay(35000);
|
||||||
|
digitalWrite(relayPin1, HIGH); // Update relay based on state
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
thing["relay2"] << [](pson& in) {
|
||||||
|
if (in.is_empty()) { // If no input, return current state
|
||||||
|
} else {
|
||||||
|
digitalWrite(relayPin2, LOW);
|
||||||
|
delay(35000);
|
||||||
|
digitalWrite(relayPin2, HIGH); // Update relay based on state
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
unsigned long currentTime = millis();
|
||||||
|
if (millis() >= time_1 + interval) {
|
||||||
|
time_1 = millis();
|
||||||
|
thing.handle();
|
||||||
|
|
||||||
|
int sensorValue = analogRead(TURBIDITY_PIN);
|
||||||
|
sensorValue = constrain(sensorValue, sensorMin, sensorMax); // Limit sensor value to the range (0-1023)
|
||||||
|
turbidityNTU = reverseMapFloat(sensorValue, sensorMin, sensorMax, turbidityMin, turbidityMax); // Map sensor reading to NTU range (0-100)
|
||||||
|
|
||||||
|
Serial.print("Turbidity Value: ");
|
||||||
|
Serial.print(turbidityNTU);
|
||||||
|
Serial.println(" NTU");
|
||||||
|
Serial.println(sensorValue);
|
||||||
|
|
||||||
|
sensors.requestTemperatures();
|
||||||
|
float Celsius = sensors.getTempCByIndex(0);
|
||||||
|
Serial.print("Temperature: ");
|
||||||
|
Serial.print(Celsius);
|
||||||
|
Serial.println(" C");
|
||||||
|
readSensor(Celsius); // Pass Celsius to readSensor()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toggle relay 1 and relay 2 every 5 seconds if draining or cooling
|
||||||
|
if ((isDraining || isCooling) && currentTime - relayToggleTime >= 5000) {
|
||||||
|
relayToggleTime = currentTime;
|
||||||
|
toggleRelay = !toggleRelay;
|
||||||
|
digitalWrite(relayPin1, toggleRelay ? LOW : HIGH);
|
||||||
|
digitalWrite(relayPin2, toggleRelay ? HIGH : LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void readSensor(float Celsius) { // Accept Celsius as parameter
|
||||||
|
unsigned long currentTime = millis();
|
||||||
|
|
||||||
|
if (turbidityNTU > ntuMax) { // If turbidity is above maximum, consider as dirty water
|
||||||
|
Serial.println("Dirty Water");
|
||||||
|
isDraining = true; // Start draining water
|
||||||
|
} else if (turbidityNTU < 25) { // If turbidity is below threshold, stop draining water
|
||||||
|
Serial.println("Clear Water");
|
||||||
|
isDraining = false; // Stop draining water
|
||||||
|
if (!isCooling) {
|
||||||
|
digitalWrite(relayPin1, HIGH);
|
||||||
|
digitalWrite(relayPin2, HIGH);
|
||||||
|
toggleRelay = false; // Reset toggle relay status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Control the heater relay based on the temperature
|
||||||
|
if (isHeating && Celsius > 33) {
|
||||||
|
digitalWrite(relayPin3, HIGH);
|
||||||
|
isHeating = false;
|
||||||
|
} else if (!isHeating && Celsius < 25) {
|
||||||
|
digitalWrite(relayPin3, LOW);
|
||||||
|
isHeating = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Control cooling based on the temperature
|
||||||
|
if (!isCooling && Celsius > 34) {
|
||||||
|
isCooling = true;
|
||||||
|
} else if (isCooling && Celsius < 30) {
|
||||||
|
isCooling = false;
|
||||||
|
digitalWrite(relayPin1, HIGH);
|
||||||
|
digitalWrite(relayPin2, HIGH);
|
||||||
|
toggleRelay = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mengirim Notifikasi Telegram dengan delay 1 menit
|
||||||
|
if (turbidityNTU > ntuMax) {
|
||||||
|
// Send notification immediately if first time or after interval
|
||||||
|
if (lastNotificationTime1 == 0 || currentTime - lastNotificationTime1 >= notificationInterval) {
|
||||||
|
thing.call_endpoint("Notifikasi_Telegram_Turbidity");
|
||||||
|
lastNotificationTime1 = currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Check temperature condition and send notification
|
||||||
|
if (Celsius < 25) {
|
||||||
|
// Send notification immediately if first time or after interval
|
||||||
|
if (lastNotificationTime2 == 0 || currentTime - lastNotificationTime2 >= notificationInterval) {
|
||||||
|
thing.call_endpoint("Notifikasi_Telegram_Suhu");
|
||||||
|
lastNotificationTime2 = currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check temperature condition and send notification
|
||||||
|
if (Celsius > 34) {
|
||||||
|
// Send notification immediately if first time or after interval
|
||||||
|
if (lastNotificationTime3 == 0 || currentTime - lastNotificationTime3 >= notificationInterval) {
|
||||||
|
thing.call_endpoint("Notifikasi_Telegram_Suhu_Up");
|
||||||
|
lastNotificationTime3 = currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue