Having Trouble with using Telegram Bot

#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!

Does the spell out that nothing happens?

What happens in that version?

Schematics might tell something.

Just nothing is sent from my telegeam bot.
My partition scheme for the script that's NOT working: NO OTA(2MB APP/2MB SPIFFS)
My partition scheme for the script that's working: Default 4MB with SPIFFS(1.2MB/1.5 SPIFFS)

I changed my 2nd script's partition scheme due to lack of data storage

Nothing is sent....
Time to debug the code. Install temporary debug serial.print of strategic variables.

  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();
}
  1. I test the bool phoneInRange by putting my iPhone on a further place and it works
  2. Photoresistor works in the test version
  3. the if statement did get triggered every one minute

the script did print out "message sent" every minute (if the conditions above satisfied), so I think the only problem is the telegram bot. Do you know how will the Schematics affect my variables, as TELEGRAM_CERTIFICATE_ROOT is a very long string, and I didnt declare it on the script as it's in one of the header files

In case there is any electricel overload making circuits malfunction anything can happen.

That's fatal for some microcontrollers.

But its working in the test script

Interesting.Do you know how does OTA affect the telegram bot? As I set my partition scheme as NO OTA(2MB APP/2MB SPIFFS)

#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 "7881663115:AAH9cbnHLxrvyRkegM3WDRiW9UzyG7rYI5A"
#define CHAT_ID "7885472273"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);

// 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);
}

void loop() {
  checkLightStatus();
  delay(5000);
}

// Check light status and trigger alerts
void checkLightStatus() {
  int lightValue = analogRead(photoPin);
  //Serial.println(lightValue);

  if (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!"));
}

I also made a debug script, and my telegram bot works well so its not the problem of constantly sending message nor the photoresistor. Right now im testing only the BLE function with partition scheme NO OTA(2MB APP/2MB SPIFFS)

Unfortunately not. That's out of my comfort zone.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.