Skip to content

Instantly share code, notes, and snippets.

@Avotrix
Created July 7, 2020 15:40
Show Gist options
  • Save Avotrix/d9a0741067ae9763194943a3f329aca6 to your computer and use it in GitHub Desktop.
Save Avotrix/d9a0741067ae9763194943a3f329aca6 to your computer and use it in GitHub Desktop.
Home Automation using ESP8266
//by Avotrix
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
char ssid[] = "YOUR_WIFI_SSID";
char password[] = "YOUR_PASSWORD";
#define BOTtoken "YOUR_TELEGRAM_TOKEN"; //Bot Token Telegram
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000;
long Bot_lasttime;
bool Start = false;
void handleNewMessages(int numNewMessages)
{
Serial.print("NewMessages ");
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 == "test")
{
bot.sendMessage(chat_id, "Hello");
}
if (text == "lamp on")
{
bot.sendMessage(chat_id, "Lamp ON");
Serial.println("Lamp ON");
digitalWrite(16, 0);
}
if (text == "lamp off")
{
bot.sendMessage(chat_id, "Lamp OFF");
Serial.println("Lamp Off");
digitalWrite(16, 1);
}
}
}
void setup()
{
Serial.begin(115200);
pinMode(16, OUTPUT);
digitalWrite(16, 1);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(10);
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(50);
}
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("");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
Bot_lasttime = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment