Skip to content

Instantly share code, notes, and snippets.

@MDevolution
Created May 3, 2020 16:28
Show Gist options
  • Save MDevolution/0b5185df8fe271ad7294599d2b3dd548 to your computer and use it in GitHub Desktop.
Save MDevolution/0b5185df8fe271ad7294599d2b3dd548 to your computer and use it in GitHub Desktop.
#include "CTBot.h"
#include "Adafruit_Si7021.h"
// Initialize Wifi connection to the router
String ssid = "";
String pass = "";
// Initialize Telegram BOT
String token = "";
uint8_t led = BUILTIN_LED;
Adafruit_Si7021 sensor = Adafruit_Si7021();
CTBot myBot;
void setup() {
// initialize the Serial
Serial.begin(115200);
while (!Serial);
Serial.println("\nStarting 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!)
while(!sensor.begin());
}
void loop() {
// a variable to store telegram message data
TBMessage msg;
// if there is an incoming message...
if (myBot.getNewMessage(msg)) {
Serial.println("\nReceived:");
Serial.println(msg.text);
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 if(msg.text.equalsIgnoreCase("TEMPERATURE")){
float temp = sensor.readTemperature();
String reply = (String)"Temperature: " + (String)temp + (String)"°C";
myBot.sendMessage(msg.sender.id, reply);
}
else {
// 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
}
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment