Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <UniversalTelegramBot.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #define TELEGRAM_BUTTON_PIN5 D5
- #define TELEGRAM_BUTTON_PIN6 D6
- #define TELEGRAM_BUTTON_PIN7 D7
- #ifndef STASSID
- #define STASSID "****"
- #define STAPSK "******"
- #endif
- const char* ssid = STASSID;
- const char* password = STAPSK;
- // ------- Telegram config --------
- #define BOTtoken "732393078:AAHQHaSKb4e-eiC5Qw0MSuj8Kjv1nnyHefY" // your Bot Token (Get from Botfather)
- #define CHAT_ID "758929974" // Chat ID of where you want the message to go (You can use MyIdBot to get the chat ID)
- // SSL client needed for both libraries
- 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;
- void ICACHE_RAM_ATTR 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 == "/Oasis_Ip") {
- bot.sendChatAction(chat_id, "typing");
- delay(4000);
- bot.sendMessage(chat_id, WiFi.localIP().toString() );
- }
- if (text == "/Oasis_Gateway") {
- bot.sendChatAction(chat_id, "typing");
- delay(4000);
- bot.sendMessage(chat_id, WiFi.gatewayIP().toString() );
- }
- if (text == "/start") {
- String welcome = "Welcome to Oasis, " + from_name + ".\n";
- welcome += "Use the commands below.\n";
- welcome += "/Oasis_Ip \n";
- welcome += "/Oasis_Gateway \n";
- bot.sendMessage(chat_id, welcome);
- }
- }
- }
- String ipAddress = "";
- volatile bool telegramButtonPressedFlag1 = false;
- volatile bool telegramButtonPressedFlag2 = false;
- volatile bool telegramButtonPressedFlag3 = false;
- void ICACHE_RAM_ATTR telegramButton5Pressed() {
- Serial.println("telegramButton5Pressed");
- int button = digitalRead(TELEGRAM_BUTTON_PIN5);
- if(button == LOW)
- {
- telegramButtonPressedFlag1 = true;
- }
- return;
- }
- void ICACHE_RAM_ATTR telegramButton6Pressed() {
- Serial.println("telegramButton6Pressed");
- int button = digitalRead(TELEGRAM_BUTTON_PIN6);
- if(button == HIGH)
- {
- telegramButtonPressedFlag2 = true;
- }
- return;
- }
- void ICACHE_RAM_ATTR telegramButton7Pressed() {
- Serial.println("telegramButton7Pressed");
- int button = digitalRead(TELEGRAM_BUTTON_PIN6);
- if(button == HIGH)
- {
- telegramButtonPressedFlag3 = true;
- }
- return;
- }
- void ICACHE_RAM_ATTR sendTelegramMessage1() {
- String message = "OASIS";
- // message.concat(ssid);
- // message.concat("\n");
- // message.concat("IP: ");
- // message.concat(ipAddress);
- message.concat("\n");
- message.concat("Alarm is ON");
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- telegramButtonPressedFlag1 = false;
- }
- void ICACHE_RAM_ATTR sendTelegramMessage2() {
- String message = "OASIS";
- // message.concat(ssid);
- // message.concat("\n");
- // message.concat("IP: ");
- // message.concat(ipAddress);
- message.concat("\n");
- message.concat("Alarm is OFF");
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- telegramButtonPressedFlag2 = false;
- }
- void ICACHE_RAM_ATTR sendTelegramMessage3() {
- String message = "OASIS";
- // message.concat(ssid);
- // message.concat("\n");
- // message.concat("IP: ");
- // message.concat(ipAddress);
- message.concat("\n");
- message.concat("Alarm SIREN!");
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- telegramButtonPressedFlag3 = false;
- }
- void setup() {
- Serial.begin(115200);
- // Initlaze the buttons
- pinMode(TELEGRAM_BUTTON_PIN5, INPUT);
- pinMode(TELEGRAM_BUTTON_PIN6, INPUT);
- pinMode(TELEGRAM_BUTTON_PIN7, INPUT);
- // NOTE:
- // It is important to use interupts when making network calls in your sketch
- // if you just checked the status of te button in the loop you might
- // miss the button press.
- attachInterrupt(TELEGRAM_BUTTON_PIN5, telegramButton5Pressed, FALLING);
- attachInterrupt(TELEGRAM_BUTTON_PIN6, telegramButton6Pressed, RISING);
- attachInterrupt(TELEGRAM_BUTTON_PIN7, telegramButton7Pressed, RISING);
- // 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);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- IPAddress ip = WiFi.localIP();
- Serial.println(ip);
- ipAddress = ip.toString();
- client.setInsecure();
- // Port defaults to 8266
- // ArduinoOTA.setPort(8266);
- // Hostname defaults to esp8266-[ChipID]
- // ArduinoOTA.setHostname("myesp8266");
- // No authentication by default
- // ArduinoOTA.setPassword("admin");
- // Password can be set with it's md5 value as well
- // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
- // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
- ArduinoOTA.onStart([]() {
- String type;
- if (ArduinoOTA.getCommand() == U_FLASH)
- type = "sketch";
- else // U_SPIFFS
- type = "filesystem";
- // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
- Serial.println("Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- Serial.println("Ready");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- ArduinoOTA.handle();
- 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();
- }
- if ( telegramButtonPressedFlag1 ) {
- sendTelegramMessage1();
- }
- if ( telegramButtonPressedFlag2 ) {
- sendTelegramMessage2();
- }
- if ( telegramButtonPressedFlag3 ) {
- sendTelegramMessage3();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement