#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
// DHT22 ayarları
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// LED ayarları
const int ledPin = 2;
bool ledState = LOW;
unsigned long ledTurnOnTime = 0;
const unsigned long ledDuration = 5000; // 5 saniye
// WiFi bilgileri
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Telegram BOT ayarları
#define BOTtoken "7685692805:AAFp2s6oqgQ49LTtm4NxjnS6zxdBrn5rSdI"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Mesaj kontrol süresi
int botRequestDelay = 1000;
unsigned long lastTimeBotRan = 0;
// Nem verileri (ilk 5 gün)
int humidityData[5] = {50, 53, 51, 56, 58};
// Nem eşiği
const int humidityThreshold = 60;
bool notificationSent = false;
// Kullanıcı chat_id kaydı
String chatId = "";
// Basit Hareketli Ortalama (SMA)
float calculateSMA(int data[], int n) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += data[i];
}
return sum / n;
}
// Yeni Telegram mesajlarını işleme
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Misafir";
// Kullanıcı chat_id kaydet
chatId = chat_id;
if (text == "/statussicaklik") {
float t = dht.readTemperature();
bot.sendMessage(chat_id, "Anlık sıcaklık: " + String(t) + " °C", "");
}
if (text == "/statusnem") {
float h = dht.readHumidity();
bot.sendMessage(chat_id, "Nem: " + String(h) + " %", "");
}
if (text == "/start") {
String welcome = "Hoş geldin " + from_name + ".\n";
welcome += "/statussicaklik : Sıcaklık Durumu\n";
welcome += "/statusnem : Nem Durumu\n";
welcome += "/ledon : LED Aç\n";
welcome += "/ledoff : LED Kapat\n";
welcome += "/statusLED : LED Durumu\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
if (text == "/ledon") {
bot.sendMessage(chat_id, "LED açıldı", "");
ledState = HIGH;
digitalWrite(ledPin, ledState);
}
if (text == "/ledoff") {
bot.sendMessage(chat_id, "LED kapatıldı", "");
ledState = LOW;
digitalWrite(ledPin, ledState);
}
if (text == "/statusLED") {
if (digitalRead(ledPin)) {
bot.sendMessage(chat_id, "LED açık", "");
} else {
bot.sendMessage(chat_id, "LED kapalı", "");
}
}
}
}
// Nem tahmini ve LED kontrol
void tahminNem(float predictedHumidity) {
if (predictedHumidity > humidityThreshold && !notificationSent && chatId != "") {
String message = "Tahmini nem eşik değerini aştı. Tahmini nem: " + String(predictedHumidity) + "%.";
bot.sendMessage(chatId, message, "");
ledState = HIGH;
digitalWrite(ledPin, ledState);
ledTurnOnTime = millis();
notificationSent = true;
}
}
// Setup
void setup() {
Serial.begin(115200);
dht.begin();
client.setInsecure();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nWiFi bağlı");
Serial.print("IP adresi: ");
Serial.println(WiFi.localIP());
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
// Loop
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
float predictedHumidity = calculateSMA(humidityData, 5);
Serial.print("Tahmini Nem: ");
Serial.println(predictedHumidity);
Serial.print("Anlık Nem: ");
Serial.println(h);
Serial.print("Anlık Sıcaklık: ");
Serial.println(t);
tahminNem(predictedHumidity);
if (h >= 60 && chatId != "") {
String message = "⚠️ Dikkat! Nem: " + String(h) + "%.";
bot.sendMessage(chatId, message, "");
}
if (ledState == HIGH && millis() - ledTurnOnTime >= ledDuration) {
ledState = LOW;
digitalWrite(ledPin, ledState);
notificationSent = false;
}
if (millis() > lastTimeBotRan + botRequestDelay) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
// Nem verilerini güncelle
for (int i = 0; i < 4; i++) {
humidityData[i] = humidityData[i + 1];
}
humidityData[4] = h;
delay(2000);
}