Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1.  
  2. //-----------------------------------------include libraries
  3.  
  4. #include <Arduino.h>
  5.  
  6. #include <ESP8266WiFi.h>
  7.  
  8. #include <ESP8266HTTPClient.h>
  9.  
  10. #include <UniversalTelegramBot.h>
  11.  
  12. #include <WiFiClientSecure.h>
  13.  
  14. //---------------------------------------------------------//
  15.  
  16. //--------------------------------------------------------//
  17.  
  18. const char* ssid = "aurora_ofic"; // The SSID (name) of the Wi-Fi network you want to connect to
  19. const char* password = "xxx"; // The password of the Wi-Fi network
  20.  
  21. #define TELEGRAM_BUTTON_PIN D5
  22.  
  23. // ------- Telegram config --------
  24. #define BOT_TOKEN "905254614:AAGr7qdH7Llmt037lbRTyvlby2rTo0Y9CI8" // your Bot Token (Get from Botfather)
  25. #define CHAT_ID "-758929974" // Chat ID of where you want the message to go (You can use MyIdBot to get the chat ID)
  26.  
  27. // SSL client needed for both libraries
  28. WiFiClientSecure client;
  29.  
  30. UniversalTelegramBot bot(BOT_TOKEN, client);
  31.  
  32. String ipAddress = "";
  33.  
  34. volatile bool telegramButtonPressedFlag = false;
  35.  
  36. //--------------------------------------------------------//
  37.  
  38. //--------------------------------------------------------//
  39.  
  40. void setup() {
  41.  
  42. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  43. delay(10);
  44. Serial.println('\n');
  45.  
  46. // Initlaze the buttons
  47. pinMode(TELEGRAM_BUTTON_PIN, INPUT);
  48.  
  49.  
  50. // NOTE:
  51. // It is important to use interupts when making network calls in your sketch
  52. // if you just checked the status of te button in the loop you might
  53. // miss the button press.
  54. attachInterrupt(TELEGRAM_BUTTON_PIN, telegramButtonPressed, RISING);
  55.  
  56. // Initlaze the network
  57.  
  58. WiFi.begin(ssid, password); // Connect to the network
  59. Serial.print("Connecting to ");
  60. Serial.print(ssid); Serial.println(" ...");
  61.  
  62. int i = 0;
  63. while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
  64. delay(1000);
  65. Serial.print(++i); Serial.print(' ');
  66. }
  67.  
  68. Serial.println('\n');
  69. Serial.println("Connection established!");
  70. Serial.print("IP address:\t");
  71. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  72.  
  73. void telegramButtonPressed() {
  74. Serial.println("telegramButtonPressed");
  75. int button = digitalRead(TELEGRAM_BUTTON_PIN);
  76. if(button == HIGH)
  77. {
  78. telegramButtonPressedFlag = true;
  79. }
  80. return;
  81. }
  82.  
  83.  
  84.  
  85. void sendTelegramMessage() {
  86. String message = "SSID: ";
  87. message.concat(ssid);
  88. message.concat("\n");
  89. message.concat("IP: ");
  90. message.concat(ipAddress);
  91. message.concat("\n");
  92. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  93. Serial.println("TELEGRAM Successfully sent");
  94. }
  95. telegramButtonPressedFlag = false;
  96. }
  97.  
  98. }
  99.  
  100.  
  101. void loop() {
  102.  
  103. if ( telegramButtonPressedFlag ) {
  104. sendTelegramMessage();
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement