Hello,
Within the scope of smart home automation, I use the "Bots" of the Telegram application to notify my mobile phone of any door status via wifi.
type or paste code here
in Arduino program
1- When wifi connection is provided, a one-time "Bot started up" message is sent in VOID Setup.
2- As the door status changes in the loop, a status message is sent as "open" or "closed".
Problem is: I can't receive status change messages on my phone even though the first message came to my phone just fine.
Everything is normal in the serial port, the program flow goes through all the Loop commands. But
The "Bot.Sendmessage" command is dysfunctional or the phone is not detected.
Any help with the solution would be greatly appreciated.
Arduino Sketch excerpt
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Set GPIOs for LED and reedswitch
const int reedSwitch = 4;
const int led = 2; //optional
// Detects whenever the door changed state
bool changeState = false;
// Holds reedswitch state (1=opened, 0=close)
bool state;
String doorState;
// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart)
unsigned long previousMillis = 0;
const long interval = 1500;
const char* ssid = "Kitap";
const char* password = "12345678";
// Initialize Telegram BOT your Bot Token (Get from Botfather)
#define BOTtoken "5026400662:AAF1zr5OXMKaT1yXfFp_pykt3YN43zmYc0w" // evimBot
//#define BOTtoken "5035250269:AAHBg-L73AGHBMJLC-H_X6wDJiBvBGSSLNk" // evimdeBot
//#define BOTtoken "5161374031:AAHOyv1Mv84vmkxHAnHd-zt-loqEQZkLCRA" // evimbirBot
// 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 "1526975541"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Runs whenever the reedswitch changes state
ICACHE_RAM_ATTR void changeDoorStatus() {
// Serial.println("State changed");
changeState = true;
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
// Read the current door state
pinMode(reedSwitch, INPUT_PULLUP);
state = digitalRead(reedSwitch);
// Set LED state to match door state
pinMode(led, OUTPUT);
digitalWrite(led, !state);
// Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode
attachInterrupt(digitalPinToInterrupt(reedSwitch), changeDoorStatus, CHANGE);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop() {
if (changeState){
changeState = false;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// If a state has occured, invert the current door state
state = !state;
if(state) {
doorState = "closed";
}
else{
doorState = "open";
}
digitalWrite(led, !state);
// changeState = false;
Serial.println(state);
Serial.println(doorState);
//Send notification
bot.sendMessage(CHAT_ID, "The door is " + doorState, "");
Serial.println(CHAT_ID);
}
}
}
I just tested using the below code and it worked without error, sending the initial "Bot started up" message from setup() and then the millis() value every 10 seconds after that from loop()
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Wifi network station credentials
#define WIFI_SSID "YOUR_SSID"
#define WIFI_PASSWORD "YOUR_PASSWORD"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
// Use @myidbot (IDBot) 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 "175753388"
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
void setup()
{
Serial.begin(115200);
Serial.println();
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop()
{
delay(10000);
String myText = (String)millis();
Serial.println(myText);
bot.sendMessage(CHAT_ID, myText, "");
}
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
//#include <ArduinoJson.h>
// Wifi network station credentials
#define WIFI_SSID "YOUR_SSID"
#define WIFI_PASSWORD "YOUR_PASSWORD"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
// Use @myidbot (IDBot) 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 "175753388"
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
void setup()
{
Serial.begin(115200);
Serial.println();
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop()
{
delay(20000);
String myText = (String)millis();
Serial.println(myText);
bot.sendMessage(CHAT_ID, myText, "");
}
After the "Bot started up" message it will take about 20 seconds for the next and subsequent ones to arrive.
If the first message arrives you would expect everything is working okay and other messages should also arrive for you. I cannot think of anything else to suggest apart from re-installing the ESP32 core (I'm running 1.0.6 core, not the latest one) and UniversalTelegramBot library. also check the UniversalTelegramBot github as this issue seems to match your problem.
Hi
The link you gave is exactly my problem. However, what I read there is beyond my software knowledge.
I had success on another PC today. This shows that the problem is in the library files. I will compare the library versions on my first pc with the other pc.
The easy answer is to use the Boards Manager in the Arduino IDE to install a different version of the ESP32 core. Sounds like your running core 2.0.2 and downgrading to use 2.0.1 will fix your problem.