andretafta

IOT_Telegram_Pameran

Mar 18th, 2022 (edited)
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************
  2.     BOARD ESP 2.5.0
  3.     ArduinoJson versi 5.13.5
  4.  *******************************************************************/
  5.  
  6. // The version of ESP8266 core needs to be 2.5 or higher
  7. // or else your bot will not connect.
  8.  
  9. // ----------------------------
  10. // Standard ESP8266 Libraries
  11. // ----------------------------
  12.  
  13. // ----------------------------
  14. // Additional Libraries - each one of these will need to be installed.
  15. // ----------------------------
  16.  
  17. #include <ESP8266WiFi.h>
  18. #include <WiFiClientSecure.h>
  19. #include <UniversalTelegramBot.h>
  20. #include "DHT.h"
  21. #include <Wire.h>
  22. #include <LiquidCrystal_I2C.h>
  23. #include <MQUnifiedsensor.h>
  24. #define         Board                   ("ESP8266")
  25. #define         Pin                     (A0)  //Analog input 3 of your arduino
  26. #define         Type                    ("MQ-135") //MQ135
  27. #define         Voltage_Resolution      (5) // 3V3 <- IMPORTANT
  28. #define         ADC_Bit_Resolution      (10) // For ESP8266
  29. #define         RatioMQ135CleanAir      (3.6)
  30.  
  31. //SCL I2C = D3
  32. //SLA I2C = D4
  33. #define DHTPIN D7
  34. #define relay1 D5
  35. #define relay2 D6
  36. #define DHTTYPE DHT11   // DHT 11
  37.  
  38. LiquidCrystal_I2C lcd(0x27, 16, 2);
  39. DHT dht(DHTPIN, DHTTYPE);
  40. MQUnifiedsensor MQ135(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
  41.  
  42.  
  43. // Initialize Wifi connection to the router
  44. char ssid[] = "esp";     // diisi nama wifi
  45. char password[] = "12345678"; // diisi password wifi
  46.  
  47. // Initialize Telegram BOT
  48. #define BOTtoken "5188621896:AAGTLg9-Jkr0Ak3uJ2sdn4NVZbrIhJia3-I" // diisi Token Bot (Dapat dari Telegram Botfather)
  49.  
  50. WiFiClientSecure client;
  51. UniversalTelegramBot bot(BOTtoken, client);
  52.  
  53. //Checks for new messages every 1 second.
  54. int botRequestDelay = 1000;
  55. unsigned long lastTimeBotRan;
  56.  
  57. void handleNewMessages(int numNewMessages) {
  58.   Serial.println("handleNewMessages");
  59.   Serial.println(String(numNewMessages));
  60.  
  61.   for (int i=0; i<numNewMessages; i++) {
  62.     String chat_id = String(bot.messages[i].chat_id);
  63.     String text = bot.messages[i].text;
  64.  
  65.     String from_name = bot.messages[i].from_name;
  66.     if (from_name == "") from_name = "Guest";
  67.  
  68.   //Menyalakan dan Mematikan Lampu Background LCD
  69.     if (text == "/lcdon") {
  70.       lcd.backlight();
  71.       bot.sendMessage(chat_id, "Backlight LCD ON", "");
  72.     }
  73.     if (text == "/lcdoff") {
  74.       lcd.noBacklight();
  75.       bot.sendMessage(chat_id, "Backlight LCD OFF", "");
  76.     }
  77.  
  78.     //Cek Pembacaan Sensor DHT11
  79.     if (text == "/ceksuhu") {
  80.       int t = dht.readTemperature()-2;
  81.       int h = dht.readHumidity();
  82.        String temp = "Suhu saat ini : ";
  83.        temp += int(t);
  84.        temp +=" *C\n";
  85.        temp +="Kelembaban: ";
  86.        temp += int(h);
  87.        temp += " %";
  88.      
  89.       bot.sendMessage(chat_id,temp, "");
  90.     }
  91.  
  92.     //Cek Pembacaan Sensor MQ135
  93.     if (text == "/cekudara") {
  94.       MQ135.setA(605.18); MQ135.setB(-3.937);
  95.       float CO = MQ135.readSensor();
  96.       MQ135.setA(110.47); MQ135.setB(-2.862);
  97.       float CO2 = MQ135.readSensor();
  98.        String temp = "Kualitas CO saat ini : ";
  99.        temp += float(CO);
  100.        temp +=" PPM\n";
  101.        temp +="CO2: ";
  102.        temp += float(CO2);
  103.        temp += " PPM";
  104.      
  105.       bot.sendMessage(chat_id,temp, "");
  106.     }
  107.  
  108.     //Kontrol Modul Relay (nyala lampu)
  109.     if (text == "/lampon") {
  110.       digitalWrite(relay1, LOW);
  111.       bot.sendMessage(chat_id, "Lampu sudah nyala", "");
  112.     }
  113.      if (text == "/lampoff") {
  114.       digitalWrite(relay1, HIGH);
  115.       bot.sendMessage(chat_id, "Lampu sudah mati", "");
  116.     }
  117.    
  118.     //Cek Command untuk setiap aksi
  119.     if (text == "/start") {
  120.       String welcome = "✨✨Selamat Datang di IDN Boarding School Both✨✨ \n";
  121.       welcome += "➡️ Silahkan pilih menu Mengakses Weather Station dibawah ini: \n";
  122.       welcome += "/lcdoff : Mematikan backlight LCD\n";
  123.       welcome += "/lcdon : Menyalakan backlight LCD\n";
  124.       welcome += "/ceksuhu : Cek Suhu & Kelembapan\n";
  125.       welcome += "/cekudara : Cek Kualitas Udara\n";
  126.       welcome += "/lampon : Nyalakan lampu\n";
  127.       welcome += "/lampoff : Matikan lampu\n";
  128.       bot.sendMessage(chat_id, welcome, "Markdown");
  129.     }
  130.   }
  131. }
  132.  
  133.  
  134. void setup() {
  135.  
  136.   //pinMode(led, OUTPUT);
  137.   pinMode(relay1, OUTPUT);
  138.   pinMode(relay2, OUTPUT);
  139. //  digitalWrite(led, HIGH); // turn off the led (inverted logic!)
  140.   digitalWrite(relay1, HIGH);
  141.   digitalWrite(relay2, HIGH);
  142.   Serial.begin(9600);
  143.   MQ135.setRegressionMethod(1); //_PPM =  a*ratio^b
  144.   MQ135.setA(102.2); MQ135.setB(-2.473);
  145.   MQ135.init();
  146.   float calcR0 = 0;
  147.   for(int i = 1; i<=10; i ++)
  148.   {
  149.     MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
  150.     calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
  151.     Serial.print(".");
  152.   }
  153.   MQ135.setR0(calcR0/10);
  154.   delay(10);
  155.   dht.begin();
  156.   Wire.begin(2,00);
  157.   lcd.begin();
  158.   lcd.backlight();
  159.   // This is the simplest way of getting this working
  160.   // if you are passing sensitive information, or controlling
  161.   // something important, please either use certStore or at
  162.   // least client.setFingerPrint
  163.   client.setInsecure();
  164.  
  165.   // Set WiFi to station mode and disconnect from an AP if it was Previously
  166.   // connected
  167.   WiFi.mode(WIFI_STA);
  168.   WiFi.disconnect();
  169.   delay(100);
  170.  
  171.   // attempt to connect to Wifi network:
  172.   Serial.print("Connecting Wifi: ");
  173.   Serial.println(ssid);
  174.   WiFi.begin(ssid, password);
  175.   lcd.print("Connecting...");
  176.  
  177.   while (WiFi.status() != WL_CONNECTED) {
  178.     Serial.print(".");
  179.     delay(500);
  180.   }
  181.  
  182.   Serial.println("");
  183.   Serial.println("WiFi connected");
  184.   Serial.print("IP address: ");
  185.   Serial.println(WiFi.localIP());
  186.   lcd.clear();
  187.   lcd.setCursor(0,0);
  188.   lcd.print("Connected");
  189.   lcd.setCursor(0,1);
  190.   lcd.print(WiFi.localIP());
  191.   delay(500);
  192.   lcd.clear();
  193. }
  194.  
  195. void loop() {
  196.   int t = dht.readTemperature()-2;
  197.   int h = dht.readHumidity();
  198.   MQ135.update();
  199.   MQ135.setA(605.18); MQ135.setB(-3.937);
  200.   float CO = MQ135.readSensor();
  201.   // Pembacaan nilai CO2 oleh sensor MQ-135
  202.   MQ135.setA(110.47); MQ135.setB(-2.862);
  203.   float CO2 = MQ135.readSensor();
  204.   lcd.setCursor(0,0);
  205.   lcd. print("TEMPERATUR:");
  206.   lcd.setCursor(13,0);
  207.   lcd.print(t);
  208.   lcd.setCursor(15,0);
  209.   lcd.print("C");
  210.   lcd.setCursor(0,1);
  211.   lcd.print("HUMIDITY  :");
  212.   lcd.setCursor(13,1);
  213.   lcd.print(h);
  214.   lcd.setCursor(15,1);
  215.   lcd.print("%");
  216.   if (millis() > lastTimeBotRan + botRequestDelay)  {
  217.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  218.  
  219.     while(numNewMessages) {
  220.       Serial.println("got response");
  221.       handleNewMessages(numNewMessages);
  222.       numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  223.     }
  224.  
  225.     lastTimeBotRan = millis();
  226.   }
  227. }
Add Comment
Please, Sign In to add comment