Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------include libraries
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <UniversalTelegramBot.h>
- #include <WiFiClientSecure.h>
- //---------------------------------------------------------//
- //--------------------------------------------------------//
- const char* ssid = "aurora_ofic"; // The SSID (name) of the Wi-Fi network you want to connect to
- const char* password = "xxx"; // The password of the Wi-Fi network
- #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); // Start the Serial communication to send messages to the computer
- delay(10);
- Serial.println('\n');
- // 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);
- // Initlaze the network
- WiFi.begin(ssid, password); // Connect to the network
- Serial.print("Connecting to ");
- Serial.print(ssid); Serial.println(" ...");
- int i = 0;
- while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
- delay(1000);
- Serial.print(++i); Serial.print(' ');
- }
- Serial.println('\n');
- Serial.println("Connection established!");
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
- void telegramButtonPressed() {
- Serial.println("telegramButtonPressed");
- int button = digitalRead(TELEGRAM_BUTTON_PIN);
- if(button == HIGH)
- {
- telegramButtonPressedFlag = true;
- }
- return;
- }
- void 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