#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
// Wi-Fi Credentials
const char* ssid = "moshimoshi";
const char* password = "mochimochi";
// Telegram Bot
#define BOT_TOKEN "7775666822:AAHpyMZpjARwU893O666YEY1r-vzqlP8pZM"
#define CHAT_ID "7885472273"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// BLE Settings
//const char* MAC_ID = "64:41:e6:4e:cd:7c"; //MAC ID
const char* SERVICE_UUID = "FEED"; // Common HID UUID
const int RSSI_THRESHOLD = -55; // Calibrate for ~5m distance (lower = farther)
bool phoneInRange = false;
BLEScan* pBLEScan;
// Pins
const int photoPin = 34;
// Light Threshold (calibrate for your room)
int lightThreshold = 2000;
unsigned long lastMessageTime = 0;
const long MESSAGE_INTERVAL = 60000; // 1 minute cooldown
void setup() {
Serial.begin(115200);
connectToWiFi();
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setActiveScan(true);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
connectToWiFi();
}
checkBLEProximity();
checkLightStatus();
delay(5000);
}
void checkBLEProximity() {
int strongestRSSI = -100;
BLEScanResults* foundDevices = pBLEScan->start(1);
for (int i = 0; i < foundDevices->getCount(); i++) {
BLEAdvertisedDevice device = foundDevices->getDevice(i);
if (device.haveManufacturerData()) {
String data = device.getManufacturerData();
uint16_t company_id = (data[1] << 8) | data[0];
if (company_id == 0x004C) {
if (device.getRSSI() > strongestRSSI) {
strongestRSSI = device.getRSSI();
}
}
}
}
if(strongestRSSI > RSSI_THRESHOLD)
{
phoneInRange = true;
}
else
{
phoneInRange = false;
}
Serial.print(F("Strongest iPhone RSSI: "));
Serial.println(strongestRSSI);
pBLEScan->clearResults();
}
// Check light status and trigger alerts
void checkLightStatus() {
int lightValue = analogRead(photoPin);
//Serial.println(lightValue);
if (!phoneInRange && lightValue > lightThreshold && millis() - lastMessageTime > MESSAGE_INTERVAL) { // Phone is away // Light is ON
bot.sendMessage(CHAT_ID, "Lights are ON, and you're away!", "");
Serial.print(F("message sent"));
lastMessageTime = millis();
}
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F("WiFi Connected!"));
}
Here's a code to detect if a person leave his/her room and the light is not shut. It'll notify the person with a Telegram Bot message, but it's not working. I have a test version, which is initiated by pressing a button (instead of detecting the distance of the person's mobile via BLE):
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Wi-Fi Credentials
const char* ssid = "moshimoshi";
const char* password = "mochimochi";
// Telegram Bot
#define BOT_TOKEN "7775666822:AAHpyMZpjARwU893O666YEY1r-vzqlP8pZM"
#define CHAT_ID "7885472273"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// Pins
const int photoPin = 33;
const int buttonPin = 2;
// Light Threshold (calibrate for your room)
int lightThreshold = 2000;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
connectToWiFi();
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
}
void loop() {
// Check if button is pressed (manual leaving trigger)
if (digitalRead(buttonPin) == HIGH) {
int lightValue = analogRead(photoPin);
Serial.println(lightValue);
delay(1000);
if (lightValue > lightThreshold) { // Light is ON
bot.sendMessage(CHAT_ID, "Room light is ON! Did you forget to turn it off?", "");
Serial.print("message sent");
}
}
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected!");
}
Any help would be appreciated, thanks!