Anda di halaman 1dari 4

#include <SPI.

h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"//Memasukkan library DHT11
#define DHTPIN D5 //Menentukan pin untuk data
#define DHTTYPE DHT11 //Mendefinisikan tipe DHT yang dipakai

// Wifi network station credentials


#define WIFI_SSID "bayeg_airlangga"
#define WIFI_PASSWORD "P@ssw0rd"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "6192349639:AAFW21BxZ0F-OqNoea2cOhNz54Z10YHGxeA"

const unsigned long BOT_MTBS = 1000; // mean time between scan messages

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done

DHT dht(DHTPIN, DHTTYPE); //Pengenalan sensor DHT


float jarak;
int LED;

void telegram(int numNewMessages)


{
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = 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";

if (text == "/temperature")
{
String t= String(dht.readTemperature(),2);
Serial.print("cek poin 1: suhu " + t);Serial.println(char(247) + "C");
bot.sendMessage(chat_id, t, "");
}

if (text == "/humidity")
{
String h= String(dht.readHumidity(),2);
Serial.print("cek poin 2: kelembaban " + h);Serial.println("%");
bot.sendMessage(chat_id, h, "");
}

if (text == "/distance")
{
String jarak= String(ultrasonik(),2);
Serial.print("cek poin 3: jarak " + jarak);Serial.println("cm");
bot.sendMessage(chat_id, jarak, "");
}

if (text == "/ledon")
{
Serial.println("cek poin 1: led on");
digitalWrite(D4, HIGH); // turn the LED on (HIGH is the voltage level)
LED = 1;
bot.sendMessage(chat_id, "Led is ON", "");
}

if (text == "/ledoff")
{
Serial.println("cek poin 2: led off");
digitalWrite(D4, LOW); // turn the LED off (LOW is the voltage level)
LED = 0;
bot.sendMessage(chat_id, "Led is OFF", "");
}

if (text == "/status")
{
if (LED)
{
bot.sendMessage(chat_id, "Led is ON", "");
}
else
{
bot.sendMessage(chat_id, "Led is OFF", "");
}
}

if(text=="/menu")
{
String welcome = "Welcome to Universal Arduino Telegram Bot library, " +
from_name + ".\n";
welcome += "choose one: \n\n";
welcome += "/temperature : to read temperature\n";
welcome += "/humidity : to read humidity\n";
welcome += "/distance : to read object distance LED\n\n";
welcome += "This is LED controller\n";
welcome += "/ledon : to switch the Led ON\n";
welcome += "/ledoff : to switch the Led OFF\n";
welcome += "/status : Returns current status of LED\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}

else if(text=="/temperature"||text=="/humidity"||text=="/distance"||text=="/
ledon"||text=="/ledoff"||text=="/status")
{
String welcome = "Welcome to Universal Arduino Telegram Bot library, " +
from_name + ".\n";
welcome += "choose one: \n\n";
welcome += "/temperature : to read temperature\n";
welcome += "/humidity : to read humidity\n";
welcome += "/distance : to read object distance LED\n\n";
welcome += "This is LED controller\n";
welcome += "/ledon : to switch the Led ON\n";
welcome += "/ledoff : to switch the Led OFF\n";
welcome += "/status : Returns current status of LED\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
else if(text)
{
String welcome = "Welcome to Universal Arduino Telegram Bot library, " +
from_name + ".\n";
welcome += "choose one: \n\n";
welcome += "/menu : monitor and control\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}

void setup()
{
Serial.begin(115200);
Serial.println();
dht.begin();delay(2000);
pinMode(D4, OUTPUT);
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setTrustAnchors(&cert); // Add root certificate for
api.telegram.org

while (WiFi.status() != WL_CONNECTED)


{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}

void loop()
{
if (millis() - bot_lasttime > BOT_MTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

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

bot_lasttime = millis();
}
}
/*
void OLED()
{
Serial.println("tampilkan ke OLED");
jarak = ultrasonik();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Jarak, suhu, dan kelembaban");display.println();
display.print("Jarak = ");display.print(jarak);display.println(" cm");//beda
antara display.print dengan display.prinln
display.print("Suhu =
");display.print(DHTsuhu(t));display.print((char)247);display.println("C");//
display.print memasukkan karakter dan melanjutkan karakter di kanannya
display.print("Kelembaban = ");display.print(DHTkelembaban(h));display.println("
%");//display.println memasukkan karakter dan melanjutkan karakter di bawahnya
display.display();

}*/

float ultrasonik()
{
//deklarasi pin sensor ultrasonik
const int TRIGPIN = D6;
const int ECHOPIN = D7;
long timer;
//deklarasi pin echo sbg input dan trig sbg output
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
//sensor ultrasonik mulai kerja
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
timer = pulseIn(ECHOPIN, HIGH);
jarak = timer/58;
Serial.print("Jarak:");Serial.print(jarak);Serial.println(" cm");
delay(1000);
return jarak;
}

Anda mungkin juga menyukai