Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CTBot.h"
- CTBot myBot;
- String ssid = "S10+Dario"; // REPLACE mySSID WITH YOUR WIFI SSID
- String pass = "1234567890"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
- String token = "xx"; // 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)
- char ciao [9];
- int num;
- void setup() {
- // initialize the Serial
- Serial.begin(9600);
- 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;
- while (Serial.available()==false){
- int a=Serial.readBytes(ciao,9);
- num=atoi(ciao);
- // stringa = String(a);
- Serial.print(num);
- // if there is an incoming message...
- if (myBot.getNewMessage(msg)) {
- if (msg.text.equalsIgnoreCase("LIGHT ON")) { // if the received message is "LIGHT ON"...
- //Serial.readBytes(stringa,20);
- //Serial.println(stringa);
- digitalWrite(led, LOW); // turn on the LED (inverted logic!)
- myBot.sendMessage(msg.sender.id, num); // 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement