Advertisement
Guest User

Untitled

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