Arvand Tabatabaie
Published

Manage your home globally with just 50 Euro

Once you hear about controlling electronic devices in your home whether office, it's not just hard by the time you find me!

IntermediateProtip1 hour38
Manage your home globally with just 50 Euro

Things used in this project

Hardware components

nodeMCU esp8266 Ch340
×1
4 channel Relay Module 5V
×1
NodeMCU esp8266 Shield
×1

Software apps and online services

telegram app

Story

Read more

Code

Code snippet #1

Plain text
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
 
// Initialize Wifi connection to the router
char ssid[] = "****************";     // یوزر نیم مودم یا روتر وای فای
char password[] = "**************"; // پسوورد روتر یا مودم وای فای
 
// Initialize Telegram BOT
#define BOTtoken "******************************"  // توکن ربات تلگرام
 
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
 
int Bot_mtbs = 1000;
long Bot_lasttime;  
bool Start = false;
 
const int relay1 = D1;
const int relay2 = D2;
const int relay3 = D3;
const int relay4 = D4;
const int relay5 = D5;
const int relay6 = D6;
const int relay7 = D7;
const int relay8 = D8;
int relaystatus = 0;
 
 
 
 
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";
 
    
    if (text == "/hi_digi") {
        String keyboardJson = "[[\"1\", \"1-off\"],[\"2\", \"2-off\"],[\"3\", \"3-off\"],[\"4\", \"4-off\"],[\"5\", \"5-off\"],[\"6\", \"6-off\"],[\"7\", \"7-off\"],[\"8\", \"8-off\"]]";
        bot.sendMessageWithReplyKeyboard(chat_id, "از صفحه کلید استفاده کنید", "", keyboardJson, true);
        bot.sendChatAction(chat_id, "typing");
        delay(4000);
        bot.sendMessage(chat_id, "شماره اتاق مورد نظر را انتخاب کنید.");
    }
 
          if (text == "1") {
      digitalWrite(relay1, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 1 is on", "");
          }
 
       if (text == "1-off") {
      digitalWrite(relay1, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 1 turned OFF", "");
       }
 
          if (text == "2") {
      digitalWrite(relay2, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 2 is on", "");
          }
 
       if (text == "2-off") {
      digitalWrite(relay2, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 2 turned OFF", "");
       }
 
          if (text == "3") {
      digitalWrite(relay3, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 3 is on", "");
          }
 
       if (text == "3-off") {
      digitalWrite(relay3, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 3 turned OFF", "");
       }
 
       
          if (text == "4") {
      digitalWrite(relay4, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 4 is on", "");
          }
 
       if (text == "4-off") {
      digitalWrite(relay4, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 4 turned OFF", "");
       }
 
    if (text == "5") {
      digitalWrite(relay5, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 5 is on", "");
          }
 
       if (text == "5-off") {
      digitalWrite(relay5, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 5 turned OFF", "");
       }
 
    if (text == "6") {
      digitalWrite(relay6, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 6 is on", "");
          }
 
       if (text == "6-off") {
      digitalWrite(relay6, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 6 turned OFF", "");
       }
 
    if (text == "7") {
      digitalWrite(relay7, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 7 is on", "");
          }
 
       if (text == "7-off") {
      digitalWrite(relay7, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 7 turned OFF", "");
       }
 
    if (text == "8") {
      digitalWrite(relay8, LOW);   // turn the LED on (HIGH is the voltage level)
      relaystatus = 1;
      bot.sendMessage(chat_id, "Room 8 is on", "");
          }
 
       if (text == "8-off") {
      digitalWrite(relay8, HIGH);    // turn the LED off (LOW is the voltage level)
      relaystatus = HIGH;
      bot.sendMessage(chat_id, "Room 8 turned OFF", "");
       }
 
 
 if (text == "1" ) {
        bot.sendChatAction(chat_id, "typing");
        delay(4000);
        bot.sendMessage(chat_id, "oh, Thats great, check Digispark.ir");
 }
        if (text == "3" ) {
        bot.sendChatAction(chat_id, "typing");
        delay(4000);
        bot.sendMessage(chat_id, "oh, Thats great, check digispark.ir/w5100-running-arduino-ip-printing/");
 }
 
       if (text == "7" ) {
        bot.sendChatAction(chat_id, "typing");
        delay(4000);
        bot.sendMessage(chat_id, "oh, Thats great, check http://digispark.ir/arduino-iot-telegram-bot-relay/");
 }
 
 
 if (text == "no") {
        bot.sendChatAction(chat_id, "typing");
        delay(4000);
        bot.sendMessage(chat_id, "Ok, How about digispark.ir/telegram-bot-iot-arduino/");
 }
    
 
        // You can't use own message, just choose from one of bellow
 
        //typing for text messages
        //upload_photo for photos
        //record_video or upload_video for videos
        //record_audio or upload_audio for audio files
        //upload_document for general files
        //find_location for location data
 
        //more info here - https://core.telegram.org/bots/api#sendchataction
    
 
    if (text == "/start") {
      String welcome = "Welcom to Digispark onlie IOT Chat" + from_name + ".\n";
      welcome += "/hi_digi : This is Digispark IOT Chat.\n\n";
      bot.sendMessage(chat_id, welcome);
    }
  }
}
 
 
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);
  WiFi.begin(ssid, password);
 
  pinMode(D1, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay1, HIGH); // initialize pin as off
 
  pinMode(D2, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay2, HIGH); // initialize pin as off
 
  pinMode(D3, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay3, HIGH); // initialize pin as off
 
  pinMode(D4, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay4, HIGH); // initialize pin as off
 
  pinMode(D5, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay5, HIGH); // initialize pin as off
 
  pinMode(D6, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay6, HIGH); // initialize pin as off
 
  pinMode(D7, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay7, HIGH); // initialize pin as off
 
  pinMode(D8, OUTPUT); // initialize digital ledPin as an output.
  delay(10);
  digitalWrite(relay8, HIGH); // initialize pin as off
 
 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void loop() {
  if (millis() > Bot_lasttime + Bot_mtbs)  {
    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);
    }
 
    Bot_lasttime = millis();
  }
}

Credits

Arvand Tabatabaie
10 projects • 10 followers
My real life is stitch to IoT hardware and Platforms. everyday i find something new to work with and enjoy. some of my projects will be here
Contact

Comments

Please log in or sign up to comment.