Telegram Flame Alarm Bot

by mccolaichuk in Circuits > Remote Control

3543 Views, 6 Favorites, 0 Comments

Telegram Flame Alarm Bot

saisks.jpg

In this article I will present IoT project that allows to sense flame and send notification to user`s Telegram.

What You’ll Need:

  • Flame sensor module
  • Microcontroller NodeMCU ESP8266
  • Power source
  • WiFi
  • Output device with telegram

Hardware

flame-sensor-range.jpg
a82b871155b54da7286ec8fa2a35b0fa.jpg

Flame Sensor Module
This module contains a phototransistor and signal conditioning electronics. A phototransistor conducts more electrical current when exposed to light. Physics taught us that (visible) light comprises of all colors, from red to violet. By coating the phototransistor with black epoxy, it becomes more sensitive to red or even invisible below red or infrared. Interestingly, flame emits infrared radiation. Thus, when this sensor sees flame, it conducts more current.
The NodeMCU is an Arduino-compatible board that features the ESP8266 at its core. It became popular because it is a WiFi-ready microcontroller by itself - no need for an Arduino. The aim of this article is to show all NodeMCU pinout and boards that are currently available. Note that when programming these boards using the ESP core in Arduino IDE, the GPIO numbers are used instead of what's on the board.

Connection

nodemcu-flame-sensor.jpg

Sensor connected to GPIO0 for interrupts.

Interrupts are useful for making things happen automatically in microcontroller programs and can help solve timing problems. With interrupts you don’t need to constantly check the current pin value. When a change is detected, an event is triggered – a function is called. This function is called interrupt service routine (ISR). When an interrupt happens, the processor stops the execution of the main program to execute a task, and then gets back to the main program

Telegram Bot

Telegram_BotFather-600x343.jpeg

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending messages, commands and inline requests. We can control our bots through HTTP APIs provided by Telegram.

A Telegram bot is an application hosted on a server (here it is ESP8266) that uses Telegram bot API to connect to Telegram Messenger clients. A great advantage of Telegram bots is that they have zero install requirements and run seamlessly on all computer or mobile platforms where Telegram Messenger runs. Configure Telegram Bot Install Telegram on your Laptop or Phone and search for BotFather. Through BotFather we can create a new bot. After creating a new bot, we have to note down the token which is the interaction key between device and Telegram bot API.

Software

Download the Telegram Bot library

Open Arduino IDE, Go to “Sketch”, Select “Include Library” and Click on “Add .ZIP Library”.

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266TelegramBOT.h>

// Initialize Wifi connection to the router
char ssid[] = "wifi";              // SSID (name) 
char password[] = "12345678";            // your network key
int status = 0;


// Initialize Telegram BOT

#define BOTtoken "1234567890:AAEsdxDfSL57kpfZz1bduD9j4fddsiyfg"  //token from @BotFather
#define BOTname "ESP8266 FireBot" // bot name
#define BOTusername "esp8266fire_bot" // bot username
TelegramBOT bot(BOTtoken, BOTname, BOTusername);

int Bot_mtbs = 1000; //refresh time
long Bot_lasttime;

bool Start = false;
bool isfire = false;
bool haveid = false;
int var = 0;
String id;


void Bot_EchoMessages() {
  for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)      {
    bot.sendMessage(bot.message[i][4], bot.message[i][5], "");
    bot.message[i][5]=bot.message[i][5].substring(1,bot.message[i][5].length());
       
    if (bot.message[i][5] == "start") {
      String wellcome = "Device connected to your account!";
      bot.sendMessage(bot.message[i][4], wellcome, "");
      Start = true;
    }
  }
  bot.message[0][0] = "";   // All messages have been replied - reset new messages
}

void alarm_if_fire(){
  if (isfire == true){
      String message = "🔥🔥🔥Flame🔥🔥🔥";
      bot.sendMessage(bot.message[1][4], message, "");
      isfire = false;
    }
  }


void setup() {
 
  Serial.begin(115200);
  delay(3000);
 // 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(0, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(0), handleInterrupt, RISING);
  bot.begin();      
}


void loop() 
{ 
  if (millis() > Bot_lasttime + Bot_mtbs)  {
    bot.getUpdates(bot.message[0][1]);     
    Bot_EchoMessages();   // reply to message with Echo
    Bot_lasttime = millis();
  }
    alarm_if_fire();
}

void handleInterrupt() {
  Serial.println("Interrupt Detected");
  isfire=true;
}

Video (in Ukrainian)

ESP8266 flame notifications to Telegram