/
lightBot.ino
69 lines (58 loc) · 2.45 KB
/
lightBot.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Name: lightBot.ino
Created: 17/01/2018
Author: Stefano Ledda <shurillu@tiscalinet.it>
Description: a simple example that do:
1) parse incoming messages
2) if "LIGHT ON" message is received, turn on the onboard LED
3) if "LIGHT OFF" message is received, turn off the onboard LED
4) otherwise, reply to sender with a welcome message
*/
#include "CTBot.h"
CTBot myBot;
String ssid = "mySSID"; // REPLACE mySSID WITH YOUR WIFI SSID
String pass = "myPassword"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "myToken"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
uint8_t led = 2; // the onboard ESP8266 LED.
// If you have a NodeMCU you can use the BUILTIN_LED pin
// (replace 2 with BUILTIN_LED)
void setup() {
// initialize the Serial
Serial.begin(115200);
Serial.println("Starting TelegramBot...");
// connect the ESP8266 to the desired access point
myBot.wifiConnect(ssid, pass);
// set the telegram bot token
myBot.setTelegramToken(token);
// check if all things are ok
if (myBot.testConnection())
Serial.println("\ntestConnection OK");
else
Serial.println("\ntestConnection NOK");
// set the pin connected to the LED to act as output pin
pinMode(led, OUTPUT);
digitalWrite(led, HIGH); // turn off the led (inverted logic!)
}
void loop() {
// a variable to store telegram message data
TBMessage msg;
// if there is an incoming message...
if (CTBotMessageText == myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("LIGHT ON")) { // if the received message is "LIGHT ON"...
digitalWrite(led, LOW); // turn on the LED (inverted logic!)
myBot.sendMessage(msg.sender.id, "Light is now ON"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("LIGHT OFF")) { // if the received message is "LIGHT OFF"...
digitalWrite(led, HIGH); // turn off the led (inverted logic!)
myBot.sendMessage(msg.sender.id, "Light is now OFF"); // notify the sender
}
else { // otherwise...
// generate the message for the sender
String reply;
reply = (String)"Welcome " + msg.sender.username + (String)". Try LIGHT ON or LIGHT OFF.";
myBot.sendMessage(msg.sender.id, reply); // and send it
}
}
// wait 500 milliseconds
delay(500);
}