From 33ca53f114be79dddb506663bd6542619bb747fe Mon Sep 17 00:00:00 2001 From: nuris Date: Wed, 24 Jul 2024 11:44:34 +0700 Subject: [PATCH] first commit --- 4.thingspeak_esp.ino | 138 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 4.thingspeak_esp.ino diff --git a/4.thingspeak_esp.ino b/4.thingspeak_esp.ino new file mode 100644 index 0000000..62fe783 --- /dev/null +++ b/4.thingspeak_esp.ino @@ -0,0 +1,138 @@ +#include "CTBot.h" +#include +#include +#include + +CTBot myBot; + +String ssid = "subin"; // Sesuaikan dengan nama wifi anda +String pass = "nuris123"; // Sesuaikan dengan password wifi +String token = "7183938345:AAFvCxsZBR-XZdOdn5v_D_Q5FQWSabEKJrs"; // Token bot telegram yang telah dibuat +int64_t chat_id = 6227877658; // Ganti dengan chat ID Anda + +const int pinLED = 2; // Pin LED, sesuaikan dengan pin yang Anda gunakan +String data; +char c; + +WiFiClient client; +unsigned long myChannelNumber = 2555678; // Ganti dengan Channel Number ThingSpeak Anda +const char * myWriteAPIKey = "7BD4FJO9N8OKLWKH"; // Ganti dengan Write API Key ThingSpeak Anda + +bool suhuTinggiTerdeteksi = false; +bool apiTerdeteksi = false; +bool gasBerbahayaTerdeteksi = false; + +void setup() { + Serial.begin(115200); + Serial.println("Starting TelegramBot..."); + + pinMode(pinLED, OUTPUT); + digitalWrite(pinLED, HIGH); + + WiFi.begin(ssid, pass); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.println("Connecting to WiFi..."); + } + + Serial.println("Connected to WiFi."); + ThingSpeak.begin(client); + + myBot.wifiConnect(ssid, pass); + myBot.setTelegramToken(token); + + // check if all things are ok + if (myBot.testConnection()) { + Serial.println("\ntestConnection OK"); + myBot.sendMessage(chat_id, "Telegram bot connected successfully!"); + + // Blink LED to indicate successful connection + for (int i = 0; i < 5; i++) { + digitalWrite(pinLED, LOW); + delay(500); + digitalWrite(pinLED, HIGH); + delay(500); + } + } else { + Serial.println("\ntestConnection NOK"); + myBot.sendMessage(chat_id, "Failed to connect to Telegram bot."); + } +} + +void loop() { + TBMessage msg; + + if (myBot.getNewMessage(msg)) { + String reply; + reply = "Alat Pendeteksi Bahaya Kebakaran Sudah Aktif"; + myBot.sendMessage(msg.sender.id, reply); + } + + while (Serial.available() > 0) { + delay(10); + c = Serial.read(); + data += c; + } + + if (data.length() > 0) { + // Send data to ThingSpeak + String tempData = data; + int sensorApiIndex = tempData.indexOf("Sensor Api: "); + int sensorGasIndex = tempData.indexOf("Sensor Gas: "); + int suhuIndex = tempData.indexOf("Suhu: "); + + if (sensorApiIndex != -1 && sensorGasIndex != -1 && suhuIndex != -1) { + int sensorApiValue = tempData.substring(sensorApiIndex + 12, sensorGasIndex - 1).toInt(); + int sensorGasValue = tempData.substring(sensorGasIndex + 12, suhuIndex - 1).toInt(); + float suhuValue = tempData.substring(suhuIndex + 6).toFloat(); + + ThingSpeak.setField(1, suhuValue); + ThingSpeak.setField(2, sensorApiValue); + ThingSpeak.setField(3, sensorGasValue); + + ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); + + Serial.println("Data sent to ThingSpeak."); + + // Check for individual danger conditions + if (suhuValue > 57) { + if (!suhuTinggiTerdeteksi) { + myBot.sendMessage(chat_id, "Peringatan: Suhu tinggi terdeteksi!"); + suhuTinggiTerdeteksi = true; + } + } else { + suhuTinggiTerdeteksi = false; + } + + if (sensorApiValue > 800) { + if (!apiTerdeteksi) { + myBot.sendMessage(chat_id, "Peringatan: Api terdeteksi!"); + apiTerdeteksi = true; + } + } else { + apiTerdeteksi = false; + } + + if (sensorGasValue > 100) { + if (!gasBerbahayaTerdeteksi) { + myBot.sendMessage(chat_id, "Peringatan: Gas berbahaya terdeteksi!"); + gasBerbahayaTerdeteksi = true; + } + } else { + gasBerbahayaTerdeteksi = false; + } + + // Check for combined danger condition + if (suhuTinggiTerdeteksi && apiTerdeteksi && gasBerbahayaTerdeteksi) { + myBot.sendMessage(chat_id, "Peringatan: Kebakaran terdeteksi! Semua sensor (api, suhu, gas) mengindikasikan bahaya."); + // Reset flags to prevent repeated messages + suhuTinggiTerdeteksi = false; + apiTerdeteksi = false; + gasBerbahayaTerdeteksi = false; + } + } + data = ""; + } + + delay(1000); // Kirim data ke ThingSpeak setiap 1 detik +}