|
| 1 | +/******************************************************************* |
| 2 | + * An example that uses the WifiManager library for wifi * |
| 3 | + * wifi connection. bot that receives commands and turns on and * |
| 4 | + * off an LED. * |
| 5 | + * * |
| 6 | + * written by Brian Lough * |
| 7 | + *******************************************************************/ |
| 8 | + |
| 9 | +#include <ESP8266WiFi.h> |
| 10 | +#include <WiFiClientSecure.h> |
| 11 | +#include <UniversalTelegramBot.h> |
| 12 | + |
| 13 | +#include <EEPROM.h> |
| 14 | + |
| 15 | +#include <DNSServer.h> //Local DNS Server used for redirecting all rs to the configuration portal |
| 16 | +#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal |
| 17 | +#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic |
| 18 | + |
| 19 | +#define BOT_TOKEN_LENGTH 46 |
| 20 | + |
| 21 | +const int ledPin = D4; |
| 22 | +const int resetConfigPin = D3; //When high will reset the wifi manager config |
| 23 | + |
| 24 | +char botToken[BOT_TOKEN_LENGTH] = ""; |
| 25 | + |
| 26 | +WiFiClientSecure client; |
| 27 | +UniversalTelegramBot *bot; |
| 28 | + |
| 29 | +int Bot_mtbs = 1000; //mean time between scan messages |
| 30 | +long Bot_lasttime; //last time messages' scan has been done |
| 31 | +bool Start = false; |
| 32 | +int ledStatus = 0; |
| 33 | + |
| 34 | +//flag for saving data |
| 35 | +bool shouldSaveConfig = false; |
| 36 | + |
| 37 | +//callback notifying us of the need to save config |
| 38 | +void saveConfigCallback () { |
| 39 | + Serial.println("Should save config"); |
| 40 | + shouldSaveConfig = true; |
| 41 | +} |
| 42 | + |
| 43 | +void readBotTokenFromEeprom(int offset){ |
| 44 | + for(int i = offset; i<BOT_TOKEN_LENGTH; i++ ){ |
| 45 | + botToken[i] = EEPROM.read(i); |
| 46 | + } |
| 47 | + EEPROM.commit(); |
| 48 | +} |
| 49 | + |
| 50 | +void writeBotTokenToEeprom(int offset){ |
| 51 | + for(int i = offset; i<BOT_TOKEN_LENGTH; i++ ){ |
| 52 | + EEPROM.write(i, botToken[i]); |
| 53 | + } |
| 54 | + EEPROM.commit(); |
| 55 | +} |
| 56 | + |
| 57 | +void handleNewMessages(int numNewMessages) { |
| 58 | + Serial.println("handleNewMessages"); |
| 59 | + Serial.println(String(numNewMessages)); |
| 60 | + for(int i=0; i<numNewMessages; i++) { |
| 61 | + String chat_id = String(bot->messages[i].chat_id); |
| 62 | + String text = bot->messages[i].text; |
| 63 | + if (text == "/ledon") { |
| 64 | + digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) |
| 65 | + ledStatus = 1; |
| 66 | + bot->sendMessage(chat_id, "Led is ON", ""); |
| 67 | + } |
| 68 | + if (text == "/ledoff") { |
| 69 | + ledStatus = 0; |
| 70 | + digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level) |
| 71 | + bot->sendMessage(chat_id, "Led is OFF", ""); |
| 72 | + } |
| 73 | + if (text == "/status") { |
| 74 | + if(ledStatus){ |
| 75 | + bot->sendMessage(chat_id, "Led is ON", ""); |
| 76 | + } else { |
| 77 | + bot->sendMessage(chat_id, "Led is OFF", ""); |
| 78 | + } |
| 79 | + } |
| 80 | + if (text == "/start") { |
| 81 | + String welcome = "Wellcome from FlashLedBot, your personal Bot on ESP8266 board \n"; |
| 82 | + welcome = welcome + "/ledon : to switch the Led ON \n"; |
| 83 | + welcome = welcome + "/ledoff : to switch the Led OFF \n"; |
| 84 | + welcome = welcome + "/status : Returns current status of LED \n"; |
| 85 | + bot->sendMessage(chat_id, welcome, "Markdown"); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +void setup() { |
| 92 | + Serial.begin(115200); |
| 93 | + |
| 94 | + EEPROM.begin(BOT_TOKEN_LENGTH); |
| 95 | + pinMode(resetConfigPin, INPUT); |
| 96 | + pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output. |
| 97 | + delay(10); |
| 98 | + digitalWrite(ledPin, LOW); //initilase pin as off |
| 99 | + |
| 100 | + Serial.println("read bot token"); |
| 101 | + readBotTokenFromEeprom(0); |
| 102 | + Serial.println(botToken); |
| 103 | + |
| 104 | + WiFiManager wifiManager; |
| 105 | + wifiManager.setSaveConfigCallback(saveConfigCallback); |
| 106 | + //Adding an additional config on the WIFI manager webpage for the bot token |
| 107 | + WiFiManagerParameter custom_bot_id("botid", "Bot Token", botToken, 50); |
| 108 | + wifiManager.addParameter(&custom_bot_id); |
| 109 | + //If it fails to connect it will create a TELEGRAM-BOT access point |
| 110 | + wifiManager.autoConnect("TELEGRAM-BOT"); |
| 111 | + |
| 112 | + strcpy(botToken, custom_bot_id.getValue()); |
| 113 | + if (shouldSaveConfig) { |
| 114 | + writeBotTokenToEeprom(0); |
| 115 | + } |
| 116 | + |
| 117 | + bot = new UniversalTelegramBot(botToken, client); |
| 118 | + Serial.println(""); |
| 119 | + Serial.println("WiFi connected"); |
| 120 | + Serial.println("IP address: "); |
| 121 | + IPAddress ip = WiFi.localIP(); |
| 122 | + Serial.println(ip); |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +void loop() { |
| 127 | + if ( digitalRead(resetConfigPin) == LOW ) { |
| 128 | + Serial.println("Reset"); |
| 129 | + WiFi.disconnect(); |
| 130 | + Serial.println("Dq"); |
| 131 | + delay(500); |
| 132 | + ESP.reset(); |
| 133 | + delay(5000); |
| 134 | + } |
| 135 | + if (millis() > Bot_lasttime + Bot_mtbs) { |
| 136 | + int numNewMessages = bot->getUpdates(bot->last_message_recived + 1); |
| 137 | + while(numNewMessages) { |
| 138 | + Serial.println("got response"); |
| 139 | + handleNewMessages(numNewMessages); |
| 140 | + numNewMessages = bot->getUpdates(bot->last_message_recived + 1); |
| 141 | + } |
| 142 | + Bot_lasttime = millis(); |
| 143 | + } |
| 144 | +} |
0 commit comments