/*
Name: lightBot.ino
Created: 17/01/2018
Author: Stefano Ledda <[email protected]>
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 = "Michillo"; // REPLACE mySSID WITH YOUR WIFI SSID
String pass = "1qaz2wsx"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "6798406254:AAGJx6HdvvTZHUM_Ec46gZpFUjvmk8r3o4c"; // 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)) {
Serial.println("llego dato");
if (msg.text.equalsIgnoreCase("Apaga")) { // if the received message is "LIGHT ON"...
digitalWrite(led, LOW); // turn on the LED (inverted logic!)
myBot.sendMessage(msg.sender.id, "Led apagado"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("Enciende")) { // if the received message is "LIGHT OFF"...
digitalWrite(led, HIGH); // turn off the led (inverted logic!)
myBot.sendMessage(msg.sender.id, "Led encendido"); // notify the sender
}
else {
myBot.sendMessage(msg.sender.id, "hola soy un bot");
}
}
// wait 500 milliseconds
delay(500);
}