Hello guys,
I am in the process of building an alarm module for home usage.
I have coded it with both the Blynk and Telegram Bot interface so I could remotely control it from anywhere in the world (I hope), since last time I left Canada and was in Asia, my Blynk app was showing my system as offline.
Right now, there is 3 ways to activate and monitor it, by the keypad at home, by the Blynk interface or by sending a text command with Telegram bot.
The problem I am facing right now is that when I am trying to arm the system with the local pinpad, that simply send a 1 sec low pulse to pin 16, it will not arm or disarm all the time, something like 1 out 4 attempts.
I was able to pinpoint what part of the sketch is causing this issue, and it is the one located inside the void myTimerEvent1, that is taking care of reading any incoming text message by Telegram.
When I stop this part from running (by simply adding // in front of the timer.setInterval(1000L, myTimerEvent1); my pinpad will arm and disarm the system every single time, but by doing so the sketch won't respond anymore to incoming Telegram bot message to control the system.
My question is how to fix this?
I already tried to change the timer event with different time value and also the botRequest Delay = 100; that are attached with that section of the sketch but still it behave the same way.
I was reading somewhere that the esp32 have 2 cores and some part could run on one and the other part run on the other but have no idea how this can be achieve....
// New alarm template as per March 26th 2025 to test a new version to fix the virtual away mode not syncing with Blink when armed locally
#define BLYNK_TEMPLATE_ID "**********"
#define BLYNK_TEMPLATE_NAME "Simple Alarm Test"
#define BLYNK_AUTH_TOKEN "G-a2Alt**********UlAYZ5n7ZvY"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
char ssid[] = "*********";
char pass[] = "*******";
#define BOTtoken "786361303**************APr37bF4Cw84" // your Bot Token (Get from Botfather)
#define CHAT_ID "*************"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int ledState = LOW;
int btnNew;
int btnOld = 1;
const int ledPin = 4;
const int btnPin = 16;
int botRequestDelay = 100;
unsigned long lastTimeBotRan;
int Reading;
BlynkTimer timer;
void checkPhysicalButton();
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V2);
}
BLYNK_WRITE(V0) {
ledState = param.asInt();
digitalWrite(ledPin, ledState);
}
void setup() {
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
bot.sendMessage(CHAT_ID, "ALARM SYSTEM HAVE JUST REBOOTED", "");
Blynk.virtualWrite(V1, "ALARM SYSTEM HAVE JUST REBOOTED");
Serial.println("ALARM SYSTEM HAVE JUST REBOOTED");
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
digitalWrite(ledPin, ledState);
timer.setInterval(50L, checkPhysicalButton);
timer.setInterval(1000L, myTimerEvent1);
}
void handleNewMessages(int numNewMessages)
{
Serial.print("Handle New Messages: ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID)
{
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start")
{
String welcome = "Welcome , " + from_name + "\n";
welcome += "Use the following commands to interact with the MARCH25ALARMTESTSIMPLIFY \n";
welcome += "/arm : Arm your Alarm by Telegram Bot\n";
welcome += "/disarm : Disarm your Alarm by Telegram Bot\n";
//welcome += "/lighton : Turn Emergency light ON\n";
//welcome += "/lightoff : Turn Emergency light OFF\n";
welcome += "/status : Get the Arm/Disarm Status of the Alarm \n";
bot.sendMessage(CHAT_ID, welcome, "");
}
if (text == "/arm")
{
Blynk.virtualWrite(V1, "ALARM ARMED FROM TELEGRAM");
Blynk.virtualWrite(V0, 1);
bot.sendMessage(CHAT_ID, "ALARM ARMED FROM TELEGRAM", "");
Serial.println("ALARM ARM BY TELEGRAM");
ledState = 1;
digitalWrite(ledPin, HIGH);
}
if (text == "/disarm")
{
Blynk.virtualWrite(V1, "ALARM CANCELLED FROM TELEGRAM");
Blynk.virtualWrite(V0, 0);
bot.sendMessage(CHAT_ID, "ALARM CANCELLED FROM TELEGRAM", "");
Serial.println("ALARM CANCELLED BY TELEGRAM");
ledState = 0;
digitalWrite(ledPin, LOW);
}
}
}
void myTimerEvent1() {
if (millis() > lastTimeBotRan + botRequestDelay)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan = millis();
}
}
void checkPhysicalButton() {
btnNew = digitalRead(btnPin);
if (btnOld == 0 && btnNew == 1) {
if (ledState == 0) {
digitalWrite(ledPin, HIGH);
Serial.println("ALARM ARM BY KEYPAD");
Blynk.virtualWrite(V1, "ALARM ARMED BY KEYPAD");
Blynk.virtualWrite(V0, 1);
bot.sendMessage(CHAT_ID, "ALARM ARMED FROM HOME", "");
ledState = 1;
} else {
digitalWrite(ledPin, LOW);
Serial.println("LED OFF BY SWITCH");
Serial.println("ALARM CANCEL BY KEYPAD");
Blynk.virtualWrite(V1, "ALARM CANCEL BY KEYPAD");
Blynk.virtualWrite(V0, 0);
bot.sendMessage(CHAT_ID, "ALARM CANCEL BY KEYPAD", "");
ledState = 0;
}
delay(100);
}
btnOld = btnNew;
}
void loop() {
Blynk.run();
timer.run();
}