Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************
- 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 <MQUnifiedsensor.h>
- #define Board ("ESP8266")
- #define Pin (A0) //Analog input 3 of your arduino
- #define Type ("MQ-135") //MQ135
- #define Voltage_Resolution (5) // 3V3 <- IMPORTANT
- #define ADC_Bit_Resolution (10) // For ESP8266
- #define RatioMQ135CleanAir (3.6)
- //SCL I2C = D3
- //SLA I2C = D4
- #define DHTPIN D7
- #define relay1 D5
- #define relay2 D6
- #define DHTTYPE DHT11 // DHT 11
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- DHT dht(DHTPIN, DHTTYPE);
- MQUnifiedsensor MQ135(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
- // Initialize Wifi connection to the router
- char ssid[] = "esp"; // diisi nama wifi
- char password[] = "12345678"; // diisi password wifi
- // Initialize Telegram BOT
- #define BOTtoken "5188621896:AAGTLg9-Jkr0Ak3uJ2sdn4NVZbrIhJia3-I" // diisi Token Bot (Dapat dari Telegram Botfather)
- WiFiClientSecure client;
- UniversalTelegramBot bot(BOTtoken, client);
- //Checks for new messages every 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()-2;
- 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, "");
- }
- //Cek Pembacaan Sensor MQ135
- if (text == "/cekudara") {
- MQ135.setA(605.18); MQ135.setB(-3.937);
- float CO = MQ135.readSensor();
- MQ135.setA(110.47); MQ135.setB(-2.862);
- float CO2 = MQ135.readSensor();
- String temp = "Kualitas CO saat ini : ";
- temp += float(CO);
- temp +=" PPM\n";
- temp +="CO2: ";
- temp += float(CO2);
- temp += " PPM";
- bot.sendMessage(chat_id,temp, "");
- }
- //Kontrol Modul Relay (nyala lampu)
- if (text == "/lampon") {
- digitalWrite(relay1, LOW);
- bot.sendMessage(chat_id, "Lampu sudah nyala", "");
- }
- if (text == "/lampoff") {
- digitalWrite(relay1, HIGH);
- bot.sendMessage(chat_id, "Lampu sudah mati", "");
- }
- //Cek Command untuk setiap aksi
- if (text == "/start") {
- String welcome = "✨✨Selamat Datang di IDN Boarding School Both✨✨ \n";
- welcome += "➡️ Silahkan pilih menu Mengakses Weather Station dibawah ini: \n";
- welcome += "/lcdoff : Mematikan backlight LCD\n";
- welcome += "/lcdon : Menyalakan backlight LCD\n";
- welcome += "/ceksuhu : Cek Suhu & Kelembapan\n";
- welcome += "/cekudara : Cek Kualitas Udara\n";
- welcome += "/lampon : Nyalakan lampu\n";
- welcome += "/lampoff : Matikan lampu\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(9600);
- MQ135.setRegressionMethod(1); //_PPM = a*ratio^b
- MQ135.setA(102.2); MQ135.setB(-2.473);
- MQ135.init();
- float calcR0 = 0;
- for(int i = 1; i<=10; i ++)
- {
- MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
- calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
- Serial.print(".");
- }
- MQ135.setR0(calcR0/10);
- delay(10);
- dht.begin();
- Wire.begin(2,00);
- lcd.begin();
- lcd.backlight();
- // 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.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());
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Connected");
- lcd.setCursor(0,1);
- lcd.print(WiFi.localIP());
- delay(500);
- lcd.clear();
- }
- void loop() {
- int t = dht.readTemperature()-2;
- int h = dht.readHumidity();
- MQ135.update();
- MQ135.setA(605.18); MQ135.setB(-3.937);
- float CO = MQ135.readSensor();
- // Pembacaan nilai CO2 oleh sensor MQ-135
- MQ135.setA(110.47); MQ135.setB(-2.862);
- float CO2 = MQ135.readSensor();
- 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();
- }
- }
Add Comment
Please, Sign In to add comment