nimasapAyubKEPHanifAlHafizhdiahwr
Published © GPL3+

Smart Home Control with Wemos D1 Mini Use Telegram App

This project used to the telegram bot API and Wemos D1 Mini that will be used make the smart home to control the lamp and the door.

BeginnerFull instructions provided5,683
Smart Home Control with Wemos D1 Mini Use Telegram App

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
High Brightness LED, White
High Brightness LED, White
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Custom parts and enclosures

Design Mechanic

Schematics

Schematic

Code

Source Code

Arduino
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Servo.h>
Servo myservo;
char ssid[] = "KAMAR KOS 7";     // your network SSID (name)
char password[] = "dasarlemahkaunak"; // your network key
#define BOTtoken "883873080:AAGkP-oNlzMgOS44X1jhE43C0_9JwwMCBPw"  // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime;   //last time messages scan has been done
bool Start = false;
int pir = 13;
const int ledPin1 = 0;
const int ledPin2 = 2;
int ledStatus = 0;
void handleNewMessages(int numNewMessages) {
  Serial.print("status pesan = ");
  Serial.print(String(numNewMessages));
  Serial.println(",pesan berhasil diterima\n");
  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";
    if (text == "/open") {
      myservo.write(180);
      bot.sendMessage(chat_id, "All led is OFF", "");
    }
    if (text == "/close") {
      myservo.write(0);
      bot.sendMessage(chat_id, "All led is OFF", "");
    }
    if (text == "/led1on") {
      digitalWrite(ledPin1, HIGH);   // turn the LED on (HIGH is the voltage level)
      digitalWrite(ledPin2, LOW);
      ledStatus = 1;
      bot.sendMessage(chat_id, "Led 1 is ON", "");
    }
    if (text == "/led1off") {
      ledStatus = 0;
      digitalWrite(ledPin1, LOW);    // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Led 1 is OFF", "");
    }
    if (text == "/led2on") {
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin1, LOW);// turn the LED on (HIGH is the voltage level)
      ledStatus = 1;
      bot.sendMessage(chat_id, "Led 2 is ON", "");
    }
    if (text == "/led2off") {
      ledStatus = 0;
      digitalWrite(ledPin2, LOW);    // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Led 2 is OFF", "");
    }
    
    if (text == "/alloff") {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);    // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "All led is OFF", "");
    }
    if (text == "/allon") {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);    // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "All led is ON", "");
    }
    if (text == "/rules") {
      bot.sendMessage(chat_id, "Perintah yang dapat diproses adalah perintah yang tertera dalam menu /help, Akep_bot juga tidak mentolerir adanya kesalahan pengetikan.","");
    }
    if (text == "Akep") {
      bot.sendMessage(chat_id, "Hai " + from_name + " aku disini, ketikan /start untuk memulai bot");
    }
    if (text == "/status1") {
      if(ledStatus){
        bot.sendMessage(chat_id, "Led is ON", "");
      } else {
        bot.sendMessage(chat_id, "Led is OFF", "");
      }
    }
       if (text == "/status2") {
      if(ledStatus){
        bot.sendMessage(chat_id, "Led is ON", "");
      } else {
        bot.sendMessage(chat_id, "Led is OFF", "");
      }
    }
    if (text == "/start") {
      bot.sendMessage(chat_id,"Selamat datang di Aplikasi Bot Telegram\n hai "+ from_name +", ketikkan /help untuk melihat perintah apa saja yang tersedia pada bot");
    }
    if (text == "/help") {
      String welcome = "Hai " + from_name + " :) \n";
      welcome += "berikut perintah yang tersedia pada bot\n";
      welcome += "/rules     : aturan memakai bot\n";
      welcome += "/led1on   : menyalakan led 1\n";
      welcome += "/led1off  : mematikan led 1\n";
      welcome += "/led2on   : menyalakan led 2\n";
      welcome += "/led2off  : mematikan led 2\n";
      welcome += "/status1  : cek status led 1\n";
      welcome += "/status2  : cek status led 2\n";
      welcome += "/allon     : menyalakan kedua led\n";
      welcome += "/alloff    : mematikan kedua led\n";
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
    }
  }
  
void setup() {
  Serial.begin(115200);
  // 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);
  Serial.println("ketikan nama bot anda pada telegram");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi terhubung");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("ketikan nama bot anda pada telegram ");
  Serial.println("");
  pinMode(pir, INPUT);
  pinMode(ledPin1, OUTPUT); // initialize digital ledPin as an output.
  pinMode(ledPin2, OUTPUT);
  delay(10);
  digitalWrite(ledPin1, LOW); // initialize pin as off
  digitalWrite(ledPin2, LOW);
  myservo.attach(12);
  myservo.write(180);
}
void loop() {
  if (digitalRead(pir) == 1){
    Serial.println("..");
    delay(100);
    digitalWrite(ledPin1, HIGH);
    ledStatus=1;
  }
  if (millis() > Bot_lasttime + Bot_mtbs)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    while(numNewMessages) {
      Serial.println("Pesan Diterima");
      Serial.println("sedang diproses.....");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }
    Bot_lasttime = millis();
  }
}

Credits

nimasap
0 projects • 1 follower
AyubKEP
0 projects • 1 follower
HanifAlHafizh
0 projects • 1 follower
diahwr
1 project • 1 follower

Comments