189 lines
6.1 KiB
C++
189 lines
6.1 KiB
C++
#define BLYNK_TEMPLATE_ID "TMPL6jOeJtdWS"
|
|
#define BLYNK_TEMPLATE_NAME "PALING AKHIR"
|
|
|
|
#define BLYNK_PRINT Serial
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <BlynkSimpleEsp8266.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
#include "CTBot.h"
|
|
#include <NTPClient.h>
|
|
#include <WiFiUdp.h>
|
|
#include <TimeLib.h>
|
|
|
|
char auth[] = "f_-VsN8jII8HDoku_MxUJB-4zTC76FxE"; // Ganti dengan token otentikasi Blynk Anda
|
|
String ssid = "JTI12"; // Ganti dengan nama WiFi Anda
|
|
String pass = "12345678"; // Ganti dengan password WiFi Anda
|
|
|
|
// Pin data sensor DS18B20 terhubung ke pin D4 (GPIO2) pada ESP8266
|
|
const int oneWireBus = 2; // D4 pada NodeMCU
|
|
|
|
OneWire oneWire(oneWireBus);
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
CTBot myBot;
|
|
String token = "7246926173:AAFE3s8hO86IIgqN5zbrs_k3apSR9wzfBuk"; // Token bot telegram yang telah dibuat
|
|
int64_t chat_id = 1333761230; // Ganti dengan chat ID Anda
|
|
|
|
// NTPClient setup
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP, "id.pool.ntp.org", 25300, 60000); // GMT+7 offset (seconds), polling interval
|
|
|
|
// Last notification times
|
|
int lastNotificationHour = -1;
|
|
int lastNotificationMinute = -1;
|
|
|
|
// Sensor data variables
|
|
float temperature = 0.0;
|
|
float humidity = 0.0;
|
|
float pHValue = 0.0;
|
|
|
|
// Notification tracking variables
|
|
int highTempNotificationCount = 0;
|
|
bool notificationSent = false;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
// Convert String to const char*
|
|
const char* ssid_cstr = ssid.c_str();
|
|
const char* pass_cstr = pass.c_str();
|
|
|
|
// Initialize Blynk
|
|
Blynk.begin(auth, ssid_cstr, pass_cstr); // Use converted SSID and password
|
|
sensors.begin(); // Inisialisasi sensor suhu
|
|
|
|
// Connect to WiFi
|
|
WiFi.begin(ssid_cstr, pass_cstr);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.println("Connecting to WiFi...");
|
|
}
|
|
|
|
Serial.println("Connected to WiFi.");
|
|
|
|
// Initialize NTPClient
|
|
timeClient.begin();
|
|
|
|
// Initialize Telegram bot
|
|
myBot.wifiConnect(ssid_cstr, pass_cstr);
|
|
myBot.setTelegramToken(token);
|
|
|
|
// Check if all things are ok
|
|
if (myBot.testConnection()) {
|
|
Serial.println("\ntestConnection OK");
|
|
myBot.sendMessage(chat_id, "Sistem Notifikasi Tambak Sudah Aktif!");
|
|
} else {
|
|
Serial.println("\ntestConnection NOK");
|
|
myBot.sendMessage(chat_id, "Failed to connect to Telegram bot.");
|
|
}
|
|
// Handle incoming Telegram messages
|
|
TBMessage msg;
|
|
if (myBot.getNewMessage(msg)) {
|
|
String reply = "Sistem Notifikasi Tambak Sudah Aktif!";
|
|
myBot.sendMessage(msg.sender.id, reply);
|
|
}
|
|
}
|
|
|
|
|
|
void loop() {
|
|
TBMessage msg;
|
|
|
|
if (myBot.getNewMessage(msg)) {
|
|
String reply;
|
|
reply = "Sistem Notifikasi Tambak Sudah Aktif!";
|
|
myBot.sendMessage(msg.sender.id, reply);
|
|
}
|
|
|
|
Blynk.run();
|
|
timeClient.update();
|
|
|
|
// Get current time
|
|
time_t rawTime = timeClient.getEpochTime();
|
|
setTime(rawTime); // Set time based on NTP
|
|
int currentHour = hour();
|
|
int currentMinute = minute();
|
|
|
|
// Membaca data dari sensor DS18B20
|
|
sensors.requestTemperatures();
|
|
float temperatureC = sensors.getTempCByIndex(0);
|
|
|
|
// Tampilkan suhu DS18B20 ke Serial Monitor
|
|
Serial.print("Suhu DS18B20 (C): ");
|
|
Serial.println(temperatureC);
|
|
|
|
// Kirim nilai suhu DS18B20 ke widget Value Display di Blynk
|
|
Blynk.virtualWrite(V3, temperatureC); // V3 adalah pin virtual yang digunakan di aplikasi Blynk
|
|
|
|
// Periksa apakah ada data dari Arduino Uno
|
|
if (Serial.available() > 0) {
|
|
String data = Serial.readStringUntil('\n');
|
|
sscanf(data.c_str(), "%f,%f,%f", &temperature, &humidity, &pHValue);
|
|
|
|
// Tampilkan data yang diterima dari Arduino Uno ke Serial Monitor
|
|
Serial.print("Suhu DHT11 (C): ");
|
|
Serial.println(temperature);
|
|
Serial.print("Kelembaban (%): ");
|
|
Serial.println(humidity);
|
|
Serial.print("pH Air: ");
|
|
Serial.println(pHValue);
|
|
|
|
// Kirim nilai ke Blynk
|
|
Blynk.virtualWrite(V0, temperature); // V0 untuk Suhu dari DHT11
|
|
Blynk.virtualWrite(V1, humidity); // V1 untuk Kelembaban dari DHT11
|
|
Blynk.virtualWrite(V2, pHValue); // V2 untuk pH
|
|
}
|
|
|
|
// Format date and time
|
|
String formattedDate = String(day()) + "/" + String(month()) + "/" + String(year());
|
|
String formattedTime = String(hour()) + ":" + String(minute()) + ":" + String(second());
|
|
|
|
// Display date and time to Serial Monitor
|
|
Serial.print("Tanggal: ");
|
|
Serial.println(formattedDate);
|
|
Serial.print("Waktu: ");
|
|
Serial.println(formattedTime);
|
|
|
|
// Check if it's time to send notification
|
|
if ((currentMinute == 0 || currentMinute == 20 || currentMinute == 40) &&
|
|
(currentHour == 7 || currentHour == 12 || currentHour == 18) &&
|
|
(currentHour != lastNotificationHour || currentMinute != lastNotificationMinute)) {
|
|
|
|
// Format and send notification via Telegram
|
|
String notificationMessage = "Data Sensor:\n";
|
|
notificationMessage += "Suhu Air: " + String(temperatureC, 2) + " C\n";
|
|
notificationMessage += "Suhu Sekitar: " + String(temperature, 2) + " C\n";
|
|
notificationMessage += "Kelembaban Sekitar: " + String(humidity, 2) + " %\n";
|
|
notificationMessage += "pH Air: " + String(pHValue, 2) + "\n" + "\n";
|
|
notificationMessage += "Tanggal: " + formattedDate + "\n";
|
|
notificationMessage += "Waktu: " + formattedTime + "\n";
|
|
|
|
myBot.sendMessage(chat_id, notificationMessage);
|
|
|
|
// Update last notification time
|
|
lastNotificationHour = currentHour;
|
|
lastNotificationMinute = currentMinute;
|
|
}
|
|
|
|
// Check if the temperature exceeds 30 degrees
|
|
if (temperatureC > 30.0 && highTempNotificationCount < 3 && !notificationSent) {
|
|
String alertMessage = "Perhatian! Suhu air melebihi 30 derajat Celcius.\n";
|
|
alertMessage += "Suhu saat ini: " + String(temperatureC, 2) + " C\n";
|
|
alertMessage += "Tanggal: " + formattedDate + "\n";
|
|
alertMessage += "Waktu: " + formattedTime + "\n";
|
|
|
|
myBot.sendMessage(chat_id, alertMessage);
|
|
highTempNotificationCount++; // Tingkatkan jumlah notifikasi yang telah dikirim
|
|
notificationSent = true; // Setel flag notifikasi sudah dikirim
|
|
}
|
|
|
|
// Reset flag if the temperature goes back below 30 degrees
|
|
if (temperatureC <= 30.0) {
|
|
notificationSent = false; // Reset flag notifikasi
|
|
highTempNotificationCount = 0; // Reset counter notifikasi
|
|
}
|
|
|
|
delay(1000); // Delay 1 detik
|
|
}
|