Skip to content

Commit 19d7aa5

Browse files
committed
Adding example for WifiManager library
1 parent 9d07dc9 commit 19d7aa5

File tree

3 files changed

+161
-2
lines changed

3 files changed

+161
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ESP8266 - Flashled Bot
2+
3+
This is a basic example of how to receive commands using UniversalTelegramBot for ESP8266 based boards.
4+
5+
The application will turn on and off an LED based on commands received via telegram.
6+
7+
Application originally written by [Giancarlo Bacchio]([email protected]) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
8+
9+
Adapted by [Brian Lough](https://github.com/witnessmenow)
10+
11+
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
12+
13+
## License
14+
15+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

src/UniversalTelegramBot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String
213213
while (millis()<sttime+8000) { // loop for a while to send the message
214214
String command="bot"+_token+"/sendMessage?chat_id="+chat_id+"&text="+text+"&parse_mode="+parse_mode;
215215
String response = sendGetToTelegram(command);
216-
Serial.println(response);
216+
//Serial.println(response);
217217
sent = checkForOkResponse(response);
218218
if(sent){
219219
break;
@@ -287,7 +287,7 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
287287
while (millis()<sttime+8000) { // loop for a while to send the message
288288
String command = "bot"+_token+"/sendMessage";
289289
String response = sendPostToTelegram(command, payload);
290-
Serial.println(response);
290+
//Serial.println(response);
291291
sent = checkForOkResponse(response);
292292
if(sent){
293293
break;

0 commit comments

Comments
 (0)