/* Rui Santos Complete project details at https://RandomNerdTutorials.com/telegram-esp8266-nodemcu-motion-detection-arduino/ Project created using Brian Lough's Universal Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot */ #include #include #include #include // Replace with your network credentials const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Initialize Telegram BOT #define BOTtoken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // 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 "XXXXXXXXXX" 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; } }