Kabar Terkini

TelegramBot DHT 11 Cuaca dan IrigasiBot

 _______________________________

Coding Rev-4

_______________________________

/*******************************************************************
    BOARD ESP 2.5.0
    ArduinoJson versi 5.13.5
*******************************************************************/

// The version of ESP8266 core needs to be 2.5 or higher
// or else your bot will not connect.

// ----------------------------
// Standard ESP8266 Libraries
// ----------------------------

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <ArduinoOTA.h>

#define SCLPIN D1
#define SDAPIN D2

#define DHTPIN D5

#define relay1 D6
#define relay2 D7
#define DHTTYPE DHT11   // DHT 11

//LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);

// Initialize Wifi connection to the router
char ssid[] =  "Yyyyyyyyyyyytttt";     // diisi nama wifi
char password[] = "Zzzzzzzzzzz"; // diisi password wifi

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Initialize Telegram BOT
#define BOTtoken "6600660280:AAFgfmLn8TAAkvdamx_4SRMqIexxxxxxxxxxxx" // diisi Token Bot (Dapat dari Telegram Botfather)

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

//Checks for new messages every (default) 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;







void handleNewMessages(int numNewMessages) {
  Serial.println("handleNewMessages");
  Serial.println(String(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 = "Guest";

  //Menyalakan dan Mematikan Lampu Background LCD
    if (text == "/lcdon") {
      lcd.backlight();
      bot.sendMessage(chat_id, "Backlight LCD ON", "");
    }
    if (text == "/lcdoff") {
      lcd.noBacklight();
      bot.sendMessage(chat_id, "Backlight LCD OFF", "");
    }

    //Cek Pembacaan Sensor DHT11
    if (text == "/ceksuhu") {
      int t = dht.readTemperature()-0;
      int h = dht.readHumidity();
       String temp = "Suhu saat ini : ";
       temp += int(t);
       temp +=" *C\n";
       temp +="Kelembaban: ";
       temp += int(h);
       temp += " %";
     
      bot.sendMessage(chat_id,temp, "");
    }

    //Kontrol Modul Relay (nyala Pompa)
    if (text == "/pumpon") {
      digitalWrite(relay1, LOW);
      bot.sendMessage(chat_id, "Pompa sudah nyala", "");
    }
     if (text == "/pumpoff") {
      digitalWrite(relay1, HIGH);
      bot.sendMessage(chat_id, "Pompa sudah mati", "");
    }
   
    //Kontrol Modul Relay (doorlock aktif)
    if (text == "/dooropen") {
      digitalWrite(relay2, LOW);
      bot.sendMessage(chat_id, "Doorlock On (terbuka)", "");
    }
     if (text == "/doorlock") {
      digitalWrite(relay2, HIGH);
      bot.sendMessage(chat_id, "Doorlock Off (terkunci)", "");
    }

    //Cek Command untuk setiap aksi
    if (text == "/start") {
      String welcome = "Welcome  " + from_name + ".\n";
      welcome += "/lcdoff : Mematikan backlight LCD\n";
      welcome += "/lcdon : Menyalakan backlight LCD\n";
      welcome += "/ceksuhu : Cek suhu ruangan\n";
      welcome += "/pumpon : Nyalakan pompa\n";
      welcome += "/pumpoff : Matikan pompa\n";
      welcome += "/dooropen : Doorlock On\n";
      welcome += "/doorlock : Doorlock Off\n";
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
  }
}


void setup() {

  //pinMode(led, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
//  digitalWrite(led, HIGH); // turn off the led (inverted logic!)
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  Serial.begin(115200);
  dht.begin();
  Wire.begin(SDAPIN, SCLPIN);
   
   
   
   
   
lcd.begin(16,2);

   
   

//lcd.init();
lcd.backlight();
   
/*   

  lcd.setCursor(0, 0);
  lcd.print("Smart Garden");
  lcd.setCursor(0, 1);
  lcd.print("Pertanian Pintar");

*/   
   
   

   
      // This is the simplest way of getting this working
  // if you are passing sensitive information, or controlling
  // something important, please either use certStore or at
  // least client.setFingerPrint
  client.setInsecure();

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
    lcd.setCursor(0, 0);
  lcd.print("Connecting WIFI:");
   
   
    lcd.setCursor(0, 1);
//  lcd.print("WIFI=");
  lcd.print(ssid);
   
   
   //ssid[]
   

 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  delay(500);
   
   
/*
   
lcd.init();
lcd.backlight();
   
  lcd.setCursor(0, 0);
  lcd.print("Smart Garden");
  lcd.setCursor(0, 1);
  lcd.print("Pertanian Pintar");
   
*/   
   
   
   
   
   
  
   
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Connected");
  lcd.setCursor(0,1);
  lcd.print(WiFi.localIP());
  delay(3000);
  lcd.clear();
   
   
ArduinoOTA.begin();   

}
       






void loop() {

   
  int t = dht.readTemperature()-0;
  int h = dht.readHumidity();
  lcd.setCursor(0,0);
  lcd. print("TEMPERATUR:");
  lcd.setCursor(13,0);
  lcd.print(t);
  lcd.setCursor(15,0);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("HUMIDITY  :");
  lcd.setCursor(13,1);
  lcd.print(h);
  lcd.setCursor(15,1);
  lcd.print("%");
  if (millis() > lastTimeBotRan + botRequestDelay)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    lastTimeBotRan = millis();
  }
   

ArduinoOTA.handle();

}


_______________________________

Coding Rev-1

_______________________________

/*******************************************************************
    BOARD ESP 2.5.0
    ArduinoJson versi 5.13.5
*******************************************************************/

// The version of ESP8266 core needs to be 2.5 or higher
// or else your bot will not connect.

// ----------------------------
// Standard ESP8266 Libraries
// ----------------------------

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

// TelegramBot DHT 11 Cuaca dan IrigasiBot Rev4



#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <ArduinoOTA.h>

#define SCLPIN D1
#define SDAPIN D2

#define DHTPIN D5

#define relay1 D6
#define relay2 D7
#define DHTTYPE DHT11   // DHT 11

//LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);

// Initialize Wifi connection to the router
char ssid[] =  "wificarjurnalku";     // diisi nama wifi
char password[] = "12345678"; // diisi password wifi

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Initialize Telegram BOT
#define BOTtoken "6600660111:ZZZZZZZn8TAAkvdamx_4SRMqIe1HshxlgwI" // diisi Token Bot (Dapat dari Telegram Botfather)

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

//Checks for new messages every (default) 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;







void handleNewMessages(int numNewMessages) {
  Serial.println("handleNewMessages");
  Serial.println(String(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 = "Guest";

  //Menyalakan dan Mematikan Lampu Background LCD
    if (text == "/lcdon") {
      lcd.backlight();
      bot.sendMessage(chat_id, "Backlight LCD ON", "");
    }
    if (text == "/lcdoff") {
      lcd.noBacklight();
      bot.sendMessage(chat_id, "Backlight LCD OFF", "");
    }

    //Cek Pembacaan Sensor DHT11
    if (text == "/ceksuhu") {
      int t = dht.readTemperature()-0;
      int h = dht.readHumidity();
       String temp = "Suhu saat ini : ";
       temp += int(t);
       temp +=" *C\n";
       temp +="Kelembaban: ";
       temp += int(h);
       temp += " %";
     
      bot.sendMessage(chat_id,temp, "");
    }

    //Kontrol Modul Relay (nyala Pompa)
    if (text == "/pumpon") {
      digitalWrite(relay1, LOW);
      bot.sendMessage(chat_id, "Pompa sudah nyala", "");
    }
     if (text == "/pumpoff") {
      digitalWrite(relay1, HIGH);
      bot.sendMessage(chat_id, "Pompa sudah mati", "");
    }
   
    //Kontrol Modul Relay (doorlock aktif)
    if (text == "/dooropen") {
      digitalWrite(relay2, LOW);
      bot.sendMessage(chat_id, "Doorlock On (terbuka)", "");
    }
     if (text == "/doorlock") {
      digitalWrite(relay2, HIGH);
      bot.sendMessage(chat_id, "Doorlock Off (terkunci)", "");
    }

    //Cek Command untuk setiap aksi
    if (text == "/start") {
      String welcome = "Welcome  " + from_name + ".\n";
      welcome += "/lcdoff : Mematikan backlight LCD\n";
      welcome += "/lcdon : Menyalakan backlight LCD\n";
      welcome += "/ceksuhu : Cek suhu ruangan\n";
      welcome += "/pumpon : Nyalakan pompa\n";
      welcome += "/pumpoff : Matikan pompa\n";
      welcome += "/dooropen : Doorlock On\n";
      welcome += "/doorlock : Doorlock Off\n";
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
  }
}


void setup() {

  //pinMode(led, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
//  digitalWrite(led, HIGH); // turn off the led (inverted logic!)
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  Serial.begin(115200);
  dht.begin();
  Wire.begin(SDAPIN, SCLPIN);
   
   
   
   
   
lcd.begin(16,2);

   
   

//lcd.init();
lcd.backlight();
   
/*   

  lcd.setCursor(0, 0);
  lcd.print("Smart Garden");
  lcd.setCursor(0, 1);
  lcd.print("Pertanian Pintar");

*/   
   
   

   
      // This is the simplest way of getting this working
  // if you are passing sensitive information, or controlling
  // something important, please either use certStore or at
  // least client.setFingerPrint
  client.setInsecure();

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
    lcd.setCursor(0, 0);
  lcd.print("Connecting...");
   

 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  delay(500);
   
   
/*
   
lcd.init();
lcd.backlight();
   
  lcd.setCursor(0, 0);
  lcd.print("Smart Garden");
  lcd.setCursor(0, 1);
  lcd.print("Pertanian Pintar");
   
*/   
   
   
   
   
   
  
   
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Connected");
  lcd.setCursor(0,1);
  lcd.print(WiFi.localIP());
  delay(3000);
  lcd.clear();
   
   
ArduinoOTA.begin();   

}
       






void loop() {

   
  int t = dht.readTemperature()-0;
  int h = dht.readHumidity();
  lcd.setCursor(0,0);
  lcd. print("TEMPERATUR:");
  lcd.setCursor(13,0);
  lcd.print(t);
  lcd.setCursor(15,0);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("HUMIDITY  :");
  lcd.setCursor(13,1);
  lcd.print(h);
  lcd.setCursor(15,1);
  lcd.print("%");
  if (millis() > lastTimeBotRan + botRequestDelay)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    lastTimeBotRan = millis();
  }
   

ArduinoOTA.handle();

}

No comments

Featured Post

Pengelolaan Kinerja Guru (PKG) tahun 2025

📑 [PKG 2025] Berikut ini materi/panduan untuk Penilaian Pengelolaan Kinerja Guru (PKG) tahun 2025. 1. Pengelolaan Kinerja Guru 2. Pengelola...