Created
December 12, 2017 15:16
-
-
Save vongomben/81ea047fccded68a4bb5a51d5133ceae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************* | |
An example of bot that receives commands and turns on and off | |
an LED. | |
* * | |
written by Giacarlo Bacchio (Gianbacchio on Github) | |
adapted by Brian Lough | |
*******************************************************************/ | |
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <UniversalTelegramBot.h> | |
// Initialize Wifi connection to the router | |
char ssid[] = "WIFI SSID"; // your network SSID (name) | |
char password[] = "WIFI PASSWORD"; // your network key | |
// Initialize Telegram BOT | |
#define BOTtoken "TOKEN" // your Bot Token (Get from Botfather) | |
WiFiClientSecure client; | |
UniversalTelegramBot bot(BOTtoken, client); | |
int Bot_mtbs = 1000; //mean time between scan messages | |
long Bot_lasttime; //last time messages' scan has been done | |
bool Start = false; | |
const int ledPin = BUILTIN_LED; | |
int ledStatus = 0; | |
#include "DHT.h" | |
#define DHTPIN D4 // what pin we're connected to | |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
DHT dht(DHTPIN, DHTTYPE); | |
void handleNewMessages(int numNewMessages) { | |
delay(2000); | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit (isFahrenheit = true) | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// Compute heat index in Fahrenheit (the default) | |
float hif = dht.computeHeatIndex(f, h); | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(t, h, false); | |
Serial.println("handleNewMessages"); | |
Serial.println(String(numNewMessages)); | |
for (int i = 0; i < numNewMessages; i++) { | |
String chat_id = String(bot.messages[i].chat_id); | |
String text = bot.messages[i].text; | |
String from_name = bot.messages[i].from_name; | |
if (from_name == "") from_name = "Guest"; | |
if (text == "/ledon") { | |
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) | |
ledStatus = 1; | |
bot.sendMessage(chat_id, "Led is ON", ""); | |
} | |
if (text == "/ledoff") { | |
ledStatus = 0; | |
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level) | |
bot.sendMessage(chat_id, "Led is OFF", ""); | |
} | |
if (text == "/status") { | |
if (ledStatus) { | |
bot.sendMessage(chat_id, "Led is ON", ""); | |
} else { | |
bot.sendMessage(chat_id, "Led is OFF", ""); | |
} | |
} | |
if (text == "/temp") { | |
bot.sendMessage(chat_id, String(hic), ""); | |
Serial.print(" *C "); | |
Serial.print(f); | |
} | |
if (text == "/start") { | |
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n"; | |
welcome += "This is Flash Led Bot example.\n\n"; | |
welcome += "/ledon : to switch the Led ON\n"; | |
welcome += "/ledoff : to switch the Led OFF\n"; | |
welcome += "/status : Returns current status of LED\n"; | |
bot.sendMessage(chat_id, welcome, "Markdown"); | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("DHTxx test!"); | |
dht.begin(); | |
// Set WiFi to station mode and disconnect from an AP if it was Previously | |
// connected | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
delay(100); | |
// attempt to connect to Wifi network: | |
Serial.print("Connecting Wifi: "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output. | |
delay(10); | |
digitalWrite(ledPin, LOW); // initialize pin as off | |
} | |
void loop() { | |
if (millis() > Bot_lasttime + Bot_mtbs) { | |
int numNewMessages = bot.getUpdates(bot.last_message_received + 1); | |
while (numNewMessages) { | |
Serial.println("got response"); | |
handleNewMessages(numNewMessages); | |
numNewMessages = bot.getUpdates(bot.last_message_received + 1); | |
} | |
Bot_lasttime = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment