Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. #include <UniversalTelegramBot.h>
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <ESP8266WiFi.h>
  5. #include <ESP8266mDNS.h>
  6. #include <WiFiUdp.h>
  7. #include <ArduinoOTA.h>
  8.  
  9.  
  10. #define TELEGRAM_BUTTON_PIN5 D5
  11. #define TELEGRAM_BUTTON_PIN6 D6
  12. #define TELEGRAM_BUTTON_PIN7 D7
  13.  
  14. #ifndef STASSID
  15. #define STASSID "****"
  16. #define STAPSK "******"
  17. #endif
  18.  
  19.  
  20. const char* ssid = STASSID;
  21. const char* password = STAPSK;
  22.  
  23.  
  24. // ------- Telegram config --------
  25. #define BOTtoken "732393078:AAHQHaSKb4e-eiC5Qw0MSuj8Kjv1nnyHefY" // your Bot Token (Get from Botfather)
  26. #define CHAT_ID "758929974" // Chat ID of where you want the message to go (You can use MyIdBot to get the chat ID)
  27.  
  28. // SSL client needed for both libraries
  29. WiFiClientSecure client;
  30. UniversalTelegramBot bot(BOTtoken, client);
  31.  
  32. int Bot_mtbs = 1000; //mean time between scan messages
  33. long Bot_lasttime; //last time messages' scan has been done
  34. bool Start = false;
  35.  
  36. void ICACHE_RAM_ATTR handleNewMessages(int numNewMessages) {
  37. Serial.println("handleNewMessages");
  38. Serial.println(String(numNewMessages));
  39.  
  40. for (int i=0; i<numNewMessages; i++) {
  41. String chat_id = String(bot.messages[i].chat_id);
  42. String text = bot.messages[i].text;
  43.  
  44. String from_name = bot.messages[i].from_name;
  45. if (from_name == "") from_name = "Guest";
  46.  
  47. if (text == "/Oasis_Ip") {
  48. bot.sendChatAction(chat_id, "typing");
  49. delay(4000);
  50. bot.sendMessage(chat_id, WiFi.localIP().toString() );
  51.  
  52. }
  53.  
  54. if (text == "/Oasis_Gateway") {
  55. bot.sendChatAction(chat_id, "typing");
  56. delay(4000);
  57. bot.sendMessage(chat_id, WiFi.gatewayIP().toString() );
  58.  
  59. }
  60.  
  61.  
  62. if (text == "/start") {
  63. String welcome = "Welcome to Oasis, " + from_name + ".\n";
  64. welcome += "Use the commands below.\n";
  65. welcome += "/Oasis_Ip \n";
  66. welcome += "/Oasis_Gateway \n";
  67. bot.sendMessage(chat_id, welcome);
  68. }
  69. }
  70. }
  71.  
  72.  
  73. String ipAddress = "";
  74.  
  75. volatile bool telegramButtonPressedFlag1 = false;
  76. volatile bool telegramButtonPressedFlag2 = false;
  77. volatile bool telegramButtonPressedFlag3 = false;
  78.  
  79.  
  80. void ICACHE_RAM_ATTR telegramButton5Pressed() {
  81. Serial.println("telegramButton5Pressed");
  82. int button = digitalRead(TELEGRAM_BUTTON_PIN5);
  83. if(button == LOW)
  84. {
  85. telegramButtonPressedFlag1 = true;
  86. }
  87. return;
  88. }
  89.  
  90. void ICACHE_RAM_ATTR telegramButton6Pressed() {
  91. Serial.println("telegramButton6Pressed");
  92. int button = digitalRead(TELEGRAM_BUTTON_PIN6);
  93. if(button == HIGH)
  94. {
  95. telegramButtonPressedFlag2 = true;
  96. }
  97. return;
  98. }
  99.  
  100. void ICACHE_RAM_ATTR telegramButton7Pressed() {
  101. Serial.println("telegramButton7Pressed");
  102. int button = digitalRead(TELEGRAM_BUTTON_PIN6);
  103. if(button == HIGH)
  104. {
  105. telegramButtonPressedFlag3 = true;
  106. }
  107. return;
  108. }
  109.  
  110.  
  111. void ICACHE_RAM_ATTR sendTelegramMessage1() {
  112. String message = "OASIS";
  113. // message.concat(ssid);
  114. // message.concat("\n");
  115. // message.concat("IP: ");
  116. // message.concat(ipAddress);
  117. message.concat("\n");
  118. message.concat("Alarm is ON");
  119. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  120. Serial.println("TELEGRAM Successfully sent");
  121. }
  122. telegramButtonPressedFlag1 = false;
  123. }
  124.  
  125. void ICACHE_RAM_ATTR sendTelegramMessage2() {
  126. String message = "OASIS";
  127. // message.concat(ssid);
  128. // message.concat("\n");
  129. // message.concat("IP: ");
  130. // message.concat(ipAddress);
  131. message.concat("\n");
  132. message.concat("Alarm is OFF");
  133. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  134. Serial.println("TELEGRAM Successfully sent");
  135. }
  136. telegramButtonPressedFlag2 = false;
  137. }
  138.  
  139. void ICACHE_RAM_ATTR sendTelegramMessage3() {
  140. String message = "OASIS";
  141. // message.concat(ssid);
  142. // message.concat("\n");
  143. // message.concat("IP: ");
  144. // message.concat(ipAddress);
  145. message.concat("\n");
  146. message.concat("Alarm SIREN!");
  147. if(bot.sendMessage(CHAT_ID, message, "Markdown")){
  148. Serial.println("TELEGRAM Successfully sent");
  149. }
  150. telegramButtonPressedFlag3 = false;
  151. }
  152.  
  153. void setup() {
  154.  
  155. Serial.begin(115200);
  156.  
  157. // Initlaze the buttons
  158. pinMode(TELEGRAM_BUTTON_PIN5, INPUT);
  159. pinMode(TELEGRAM_BUTTON_PIN6, INPUT);
  160. pinMode(TELEGRAM_BUTTON_PIN7, INPUT);
  161.  
  162.  
  163. // NOTE:
  164. // It is important to use interupts when making network calls in your sketch
  165. // if you just checked the status of te button in the loop you might
  166. // miss the button press.
  167. attachInterrupt(TELEGRAM_BUTTON_PIN5, telegramButton5Pressed, FALLING);
  168. attachInterrupt(TELEGRAM_BUTTON_PIN6, telegramButton6Pressed, RISING);
  169. attachInterrupt(TELEGRAM_BUTTON_PIN7, telegramButton7Pressed, RISING);
  170.  
  171. // Set WiFi to station mode and disconnect from an AP if it was Previously
  172. // connected
  173. WiFi.mode(WIFI_STA);
  174. WiFi.disconnect();
  175. delay(100);
  176.  
  177. // Attempt to connect to Wifi network:
  178. Serial.print("Connecting Wifi: ");
  179. Serial.println(ssid);
  180. WiFi.begin(ssid, password);
  181. while (WiFi.status() != WL_CONNECTED) {
  182. Serial.print(".");
  183. delay(500);
  184. }
  185. Serial.println("");
  186. Serial.println("WiFi connected");
  187. Serial.println("IP address: ");
  188. IPAddress ip = WiFi.localIP();
  189. Serial.println(ip);
  190.  
  191. ipAddress = ip.toString();
  192.  
  193. client.setInsecure();
  194.  
  195. // Port defaults to 8266
  196. // ArduinoOTA.setPort(8266);
  197.  
  198. // Hostname defaults to esp8266-[ChipID]
  199. // ArduinoOTA.setHostname("myesp8266");
  200.  
  201. // No authentication by default
  202. // ArduinoOTA.setPassword("admin");
  203.  
  204. // Password can be set with it's md5 value as well
  205. // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  206. // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
  207.  
  208. ArduinoOTA.onStart([]() {
  209. String type;
  210. if (ArduinoOTA.getCommand() == U_FLASH)
  211. type = "sketch";
  212. else // U_SPIFFS
  213. type = "filesystem";
  214.  
  215. // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  216. Serial.println("Start updating " + type);
  217. });
  218. ArduinoOTA.onEnd([]() {
  219. Serial.println("\nEnd");
  220. });
  221. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  222. Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  223. });
  224. ArduinoOTA.onError([](ota_error_t error) {
  225. Serial.printf("Error[%u]: ", error);
  226. if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  227. else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  228. else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  229. else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  230. else if (error == OTA_END_ERROR) Serial.println("End Failed");
  231. });
  232. ArduinoOTA.begin();
  233. Serial.println("Ready");
  234. Serial.print("IP address: ");
  235. Serial.println(WiFi.localIP());
  236. }
  237.  
  238. void loop() {
  239.  
  240. ArduinoOTA.handle();
  241.  
  242. if (millis() > Bot_lasttime + Bot_mtbs) {
  243. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  244.  
  245. while(numNewMessages) {
  246. Serial.println("got response");
  247. handleNewMessages(numNewMessages);
  248. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  249. }
  250.  
  251. Bot_lasttime = millis();
  252. }
  253.  
  254. if ( telegramButtonPressedFlag1 ) {
  255. sendTelegramMessage1();
  256. }
  257.  
  258. if ( telegramButtonPressedFlag2 ) {
  259. sendTelegramMessage2();
  260. }
  261.  
  262. if ( telegramButtonPressedFlag3 ) {
  263. sendTelegramMessage3();
  264. }
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement