Skip to content

Commit f34c250

Browse files
committed
Improved FlashledBot example. More readable.
1 parent 0e411d8 commit f34c250

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

examples/ESP8266/FlashledBot/FlashledBot.ino

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ int ledStatus = 0;
2929
void handleNewMessages(int numNewMessages) {
3030
Serial.println("handleNewMessages");
3131
Serial.println(String(numNewMessages));
32-
for(int i=0; i<numNewMessages; i++) {
32+
33+
for (int i=0; i<numNewMessages; i++) {
3334
String chat_id = String(bot.messages[i].chat_id);
3435
String text = bot.messages[i].text;
3536

@@ -41,23 +42,27 @@ void handleNewMessages(int numNewMessages) {
4142
ledStatus = 1;
4243
bot.sendMessage(chat_id, "Led is ON", "");
4344
}
45+
4446
if (text == "/ledoff") {
4547
ledStatus = 0;
4648
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
4749
bot.sendMessage(chat_id, "Led is OFF", "");
4850
}
51+
4952
if (text == "/status") {
5053
if(ledStatus){
5154
bot.sendMessage(chat_id, "Led is ON", "");
5255
} else {
5356
bot.sendMessage(chat_id, "Led is OFF", "");
5457
}
5558
}
59+
5660
if (text == "/start") {
57-
String welcome = "Welcome, " + from_name + ", from FlashLedBot, your personal Bot on ESP8266\n";
58-
welcome = welcome + "/ledon : to switch the Led ON\n";
59-
welcome = welcome + "/ledoff : to switch the Led OFF\n";
60-
welcome = welcome + "/status : Returns current status of LED\n";
61+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
62+
welcome += "This is Flash Led Bot example.\n\n";
63+
welcome += "/ledon : to switch the Led ON\n";
64+
welcome += "/ledoff : to switch the Led OFF\n";
65+
welcome += "/status : Returns current status of LED\n";
6166
bot.sendMessage(chat_id, welcome, "Markdown");
6267
}
6368
}
@@ -77,31 +82,32 @@ void setup() {
7782
Serial.print("Connecting Wifi: ");
7883
Serial.println(ssid);
7984
WiFi.begin(ssid, password);
85+
8086
while (WiFi.status() != WL_CONNECTED) {
8187
Serial.print(".");
8288
delay(500);
8389
}
90+
8491
Serial.println("");
8592
Serial.println("WiFi connected");
86-
Serial.println("IP address: ");
87-
IPAddress ip = WiFi.localIP();
88-
Serial.println(ip);
93+
Serial.print("IP address: ");
94+
Serial.println(WiFi.localIP());
8995

9096
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
9197
delay(10);
9298
digitalWrite(ledPin, LOW); // initialize pin as off
93-
9499
}
95100

96101
void loop() {
97-
98102
if (millis() > Bot_lasttime + Bot_mtbs) {
99103
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
104+
100105
while(numNewMessages) {
101106
Serial.println("got response");
102107
handleNewMessages(numNewMessages);
103108
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
104109
}
110+
105111
Bot_lasttime = millis();
106112
}
107113
}

0 commit comments

Comments
 (0)