Skip to content

Instantly share code, notes, and snippets.

@teos0009
Created January 8, 2017 14:10
Show Gist options
  • Save teos0009/677db627fe957dcf9a8fdbf53f4b4976 to your computer and use it in GitHub Desktop.
Save teos0009/677db627fe957dcf9a8fdbf53f4b4976 to your computer and use it in GitHub Desktop.
/*******************************************************************
* An example of bot that receives commands and turns on and off *
* an LED. Mod to control AC with SSR, and read from DHT22 *
* *
* written by Giacarlo Bacchio (Gianbacchio on Github) *
* adapted by Brian Lough *
* mod by teos00009 *
*******************************************************************/
//arduinojson on v1.8.0
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <DHT.h> // DHT.h library from adafruit
// Initialize Wifi connection to the router
char ssid[] = "YOUR_SSID_HERE"; // your network SSID (name)
char password[] = "YOUR_PASS"; // your network key
#define DHTPIN 4 //GPIO4 = D2
#define DHTTYPE DHT22
const int ledPin = 14;//GPI14 = D5
//bot URL = telegram.me/YOUR_BOT_NAME
// Initialize Telegram BOT
#define BOTtoken "YOUR_BOT_TOKEN" // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
DHT dht(DHTPIN, DHTTYPE);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
bool Start = false;
int ledStatus = 0;
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
//UniversalTelegramBot.h ref struct telegramMessage{}
for(int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String from_name = String(bot.messages[i].from_name);
String text = bot.messages[i].text;
if (text == "/ssron") {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
ledStatus = 1;
bot.sendMessage(chat_id, "SSR is ON", "");
}
if (text == "/ssroff") {
ledStatus = 0;
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "SSR is OFF", "");
}
if (text == "/stat") {
if(ledStatus){
bot.sendMessage(chat_id, "SSR is ON", "");
} else {
bot.sendMessage(chat_id, "SSR is OFF", "");
}
}
if (text == "/tmp") {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
String dht22msg = "";
if (isnan(humidity) || isnan(temperature)) {
dht22msg = dht22msg + "isnan\n";
}
dht22msg = dht22msg + "dht22 temperature " + temperature + " degC and humidity " + humidity + " % " + "\n";
bot.sendMessage(chat_id, dht22msg, "");
}
if (text == "/man") {
// String from_name = message.from_name; // error: 'message' was not declared in this scope
if (from_name == "") from_name = "Anonymous";
String welcome = "Welcome " + from_name + " from YOUR_NAME_bot, your C&C Bot on ESP8266\n";
welcome = welcome + "/ssron : to switch SSR ON\n";
welcome = welcome + "/ssroff : to switch SSR OFF\n";
welcome = welcome + "/tmp : return DHT22 readings\n";
welcome = welcome + "/stat : returns status of SSR\n";
bot.sendMessage(chat_id, welcome, "");
}
}
}
void setup() {
Serial.begin(115200);
//start DHT
dht.begin();
delay(1000);
// 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.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
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