Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Uses Universal Telegram Bot library and Thread controller library
- #include <UniversalTelegramBot.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <Thread.h>
- #include <ThreadController.h>
- ThreadController controll = ThreadController();
- //My Thread (as a pointer)
- Thread* myThread = new Thread();
- //His Thread (not pointer)
- Thread hisThread = Thread();
- char ssid[] = "";
- char password[] = "";
- #define BOT_TOKEN ""
- #define CHAT_ID ""
- WiFiClientSecure client;
- UniversalTelegramBot bot(BOT_TOKEN, client);
- String ipAddress = "";
- int detectionValue = 0;
- int alarmStatus = 0;
- int activeMonitor = 0;
- const int readingInterval = 10; //ms
- void sendTelegramMessage() {
- String message = "SSID: ";
- message.concat(ssid);
- message.concat("\n");
- message.concat("Motion detected - START TIMER");
- message.concat("\n");
- message.concat("New commands will be executed after 20 seconds");
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- }
- void sendTelegramMessage2() {
- String message = "SSID: ";
- message.concat(ssid);
- message.concat("\n");
- message.concat("END TIMER - Alarm activated");
- message.concat("\n");
- message.concat("Disable alarm with /alarmoff");
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- }
- void sendTelegramMessage3() {
- String message = "SSID: ";
- message.concat(ssid);
- message.concat("\n");
- message.concat("Motion detected - ALARM ALREADY ON");
- message.concat("\n");
- message.concat("No action will be taken");
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- }
- void sendTelegramMessage4() {
- String message = "Alarm disabled before execution";
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- }
- void sendTelegramMessage5() {
- String message = "The system has rebooted. Please reactivate your alarm.";
- if(bot.sendMessage(CHAT_ID, message, "Markdown")){
- Serial.println("TELEGRAM Successfully sent");
- }
- }
- void handleNewMessages(int numNewMessages) {
- 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 == "/alarmon") {
- alarmStatus = 1;
- bot.sendMessage(chat_id, "Alarm sound has been activated", "");
- digitalWrite(13, HIGH);
- }
- if (text == "/activate") {
- activeMonitor = 1;
- bot.sendMessage(chat_id, "Security watchdog activated", "");
- }
- if (text == "/deactivate") {
- activeMonitor = 0;
- bot.sendMessage(chat_id, "Security watchdog deactivated", "");
- }
- if (text == "/alarmoff") {
- alarmStatus = 0;
- digitalWrite(13, LOW);
- bot.sendMessage(chat_id, "Alarm sound has been deactivated", "");
- }
- if (text == "/status") {
- if(alarmStatus){
- bot.sendMessage(chat_id, "Alarm sound is ON", "");
- } else {
- bot.sendMessage(chat_id, "Alarm sound is OFF", "");
- }
- }
- if (text == "/start") {
- String welcome = "Welcome to SecuriBot, \n \n";
- welcome += "/alarmon : to switch the alarm sound ON\n";
- welcome += "/alarmoff : to switch the alarm sound OFF\n";
- welcome += "/status : Returns current status of the alarm\n";
- welcome += "/activate : Activate the security watchdog\n";
- welcome += "/deactivate : Deactivate the security watchdog\n";
- bot.sendMessage(chat_id, welcome, "Markdown");
- }
- }
- }
- void checkSensors(){
- if (activeMonitor == 1) {
- if (1 == digitalRead(12) && alarmStatus == 0) {
- sendTelegramMessage();
- alarmStatus = 1;
- delay(20000);
- 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);
- }
- delay(500);
- if (alarmStatus == 1) {
- sendTelegramMessage2();
- digitalWrite(13, HIGH);
- } else {
- sendTelegramMessage4();
- }
- }
- if (1 == digitalRead(12) && alarmStatus == 1) {
- sendTelegramMessage3();
- }
- } else {
- }
- }
- void checkMessages (){
- 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);
- }
- }
- void setup(){
- Serial.begin(38400);
- pinMode(12, INPUT_PULLUP); //d6
- pinMode(13, OUTPUT); //d7
- 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());
- delay(10);
- sendTelegramMessage5();
- delay(50);
- myThread->onRun(checkSensors);
- myThread->setInterval(5);
- hisThread.onRun(checkMessages);
- hisThread.setInterval(250);
- controll.add(myThread);
- controll.add(&hisThread); // & to pass the pointer to it
- }
- void loop(){
- controll.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment