#include "DHT.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* BOTtoken = "8579483067:AAGq69mY6UpDtuejPvF4NBbx8oyuvMiLdh8";
const char* ChatID = "1908813421";
#define DHT_pin 14
#define DHT_type DHT22
#define Bz_pin 18
#define led_pin 15
const int interval = 2000;
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken,client);
DHT dht(DHT_pin,DHT_type);
float h = 0.0;
float t = 0.0;
float h_t = 0.0;
String data = "";
byte c = 0;
byte p = 0;
unsigned long previousM = 0;
unsigned long currentM = 0;
void setup() {
Serial.begin(9600);
Serial.println("Hello There");
pinMode(Bz_pin, OUTPUT);
pinMode(led_pin, OUTPUT);
//----------------------------------------------------
dht.begin();
//----------------------------------------------------
WiFi.begin(ssid,password);
client.setInsecure();
while(WiFi.status() != WL_CONNECTED) delay(500);
bot.sendMessage(ChatID,"Hi I work","");
}//------------------------------------------------------end setUp
void loop() {
//--reseive data from arduino
while(Serial.available()){
data = Serial.readStringUntil('\n');
data.trim();
Serial.println(data);
if(data.equals("ThereIsMotion")){
cntrolOfVoise();
data = "";
}
}
//----------------------
currentM = millis();
if(currentM - previousM >= interval){
handleWithDHT22();
previousM = currentM;
}
//handle with message which received from telegram
int numberOfMessage = bot.getUpdates(bot.last_message_received + 1);
handleNewMessage(numberOfMessage);
//--------------------------------------------end loop
}
void handleWithDHT22(){
h = dht.readHumidity();
t = dht.readTemperature();
if(isnan(h) || isnan(t)){
Serial.println("error");
}
else{
//for dispaly info on arduino
// p++;
if(p == 0){
Serial.print("temp:");
Serial.println(String(t));
Serial.print("humidity:");
Serial.print(String(h));
Serial.println("$");
// p = 0;
}
//-------------------------------
h_t = dht.computeHeatIndex(t,h,false);
if( (h_t < 15.00 || h_t > 30.00) && c== 0 ){
String s = "realTemp: " + String(h_t) +"C";
bot.sendMessage(ChatID,s,"");
c = 1;
}
else if((h_t > 15.00 && h_t < 30.00) && c==1){
bot.sendMessage(ChatID,"the state is good","");
c = 0;
}else {}
}
}
void handleNewMessage(int numberOfMessage){
for(int i=0; i<numberOfMessage; i++){
String ChatId = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
// Serial.println(text);
if(text == "On"){
cntrolOfVoise();
bot.sendMessage(ChatID,"Ok","");
}
else if(text == "OnLed")
{
digitalWrite(led_pin, HIGH);
bot.sendMessage(ChatID,"It IS On","");
}
else if(text == "OffLed")
{
digitalWrite(led_pin, LOW);
bot.sendMessage(ChatID,"It IS Off","");
}
else if(text == "humidity"){
bot.sendMessage(ChatID,"humidity: " + String(h),"");
}
else if(text =="temperature"){
bot.sendMessage(ChatID,"Temperature: " + String(t),"");
}
}
}
void cntrolOfVoise(){
for(int j=1; j<20; j++){
for(int i=600; i<=1200; i+=20){
tone(Bz_pin,i);
delay(15);
}
for(int i=1200; i>=600; i-=20){
tone(Bz_pin,i);
delay(15);
}
}
noTone(Bz_pin);
}