Wednesday, June 8, 2022

esp8266 telegram

 #include <ESP8266WiFi.h>

#include <WiFiClientSecure.h>

#include <UniversalTelegramBot.h>

#include <ArduinoJson.h>


// Replace with your network credentials

const char* ssid = "rkk";

const char* password = "rkk@1234";


// Initialize Telegram BOT

#define BOTtoken "5527844175:AAEh2wPs0BtABXxfJSTjN_1wC-rr1JFu_oo"  // your Bot Token (Get from Botfather)


// Use @myidbot to find out the chat ID of an individual or a group

// Also note that you need to click "start" on a bot before it can

// message you

#define CHAT_ID "911786722"


X509List cert(TELEGRAM_CERTIFICATE_ROOT);

WiFiClientSecure client;

UniversalTelegramBot bot(BOTtoken, client);


const int motionSensor = 14; // PIR Motion Sensor

bool motionDetected = false;


// Indicates when motion is detected

void ICACHE_RAM_ATTR detectsMovement() {

  //Serial.println("MOTION DETECTED!!!");

  motionDetected = true;

}


void setup() {

  Serial.begin(115200);

  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP

  client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org


  // PIR Motion Sensor mode INPUT_PULLUP

  pinMode(motionSensor, INPUT_PULLUP);

  // Set motionSensor pin as interrupt, assign interrupt function and set RISING mode

  attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);


  // Attempt to connect to Wifi network:

  Serial.print("Connecting Wifi: ");

  Serial.println(ssid);


  WiFi.mode(WIFI_STA);

  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());


  bot.sendMessage(CHAT_ID, "Bot started up", "");

}


void loop() {

  if(motionDetected){

    bot.sendMessage(CHAT_ID, "Motion detected!!", "");

    Serial.println("Motion Detected");

    motionDetected = false;

  }

}

No comments:

Post a Comment

Control led light using remote

 // Include IR Remote Library by Ken Shirriff #include <IRremote.h> // Define sensor pin const int RECV_PIN = 4; // Define LED pin #de...