Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <UniversalTelegramBot.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- //------- WiFi Settings -------
- char ssid[] = "aurora_ofic"; // your network SSID (name)
- char password[] = ""; // your network key
- #define TELEGRAM_BUTTON_PIN D5
- // ------- Telegram config --------
- #define BOT_TOKEN "905254614:AAGr7qdH7Llmt037lbRTyvlby2rTo0Y9CI8" // 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(BOT_TOKEN, client);
- String ipAddress = "";
- volatile bool telegramButtonPressedFlag = false;
- void setup() {
- Serial.begin(115200);
- // Initlaze the buttons
- pinMode(TELEGRAM_BUTTON_PIN, 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_PIN, telegramButtonPressed, 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();
- }
- void ICACHE_RAM_ATTR telegramButtonPressed() {
- Serial.println("telegramButtonPressed");
- int button = digitalRead(TELEGRAM_BUTTON_PIN);
- if(button == HIGH)
- {
- telegramButtonPressedFlag = true;
- }
- return;
- }
- void ICACHE_RAM_ATTR sendTelegramMessage() {
- String message = "SSID: ";
- message.concat(ssid);
- message.concat("\n");
- message.concat("IP: ");
- message.concat(ipAddress);
- message.concat("\n");
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- telegramButtonPressedFlag = false;
- }
- void loop() {
- if ( telegramButtonPressedFlag ) {
- sendTelegramMessage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement