0% found this document useful (0 votes)
15 views4 pages

Telegram IOT

The document describes code for an ESP8266 board that controls devices like relays and displays sensor readings on an LCD screen via Telegram commands. It connects to WiFi, imports necessary libraries, defines pins and tokens, displays the IP address on startup, and has commands to control devices and check sensor readings.

Uploaded by

Jamaludin Anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Telegram IOT

The document describes code for an ESP8266 board that controls devices like relays and displays sensor readings on an LCD screen via Telegram commands. It connects to WiFi, imports necessary libraries, defines pins and tokens, displays the IP address on startup, and has commands to control devices and check sensor readings.

Uploaded by

Jamaludin Anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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>

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

LiquidCrystal_I2C lcd(0x3f, 16, 2);


DHT dht(DHTPIN, DHTTYPE);

// Initialize Wifi connection to the router


char ssid[] = "***"; // diisi nama wifi
char password[] = "***"; // diisi password wifi

// Initialize Telegram BOT


#define BOTtoken "1207146563:AAESPIjpmIfcANFmE1K40wA5gT5g4Kkpglc" // 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, "");
}

//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", "");
}

//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 += "/lampon : Nyalakan lampu\n";
welcome += "/lampoff : Matikan lampu\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(2,00);
lcd.begin();
// 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();
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();
}
}

You might also like