Hi,
I have a strange behavior with the UniversalTelegramBot Lib on a ESP32.
First of all, the code was running on a ESP8266 like a charm. Then I started to port it to my ESP32 and now I only receive a message if it is already available for the first loop run. In the loop runs afterwards I do not receive anything.
I have no clue what's wrong with the code. Does not look so complicated.
I use the following versions of the libs.
ESP board v2.0.2
UniversalTelegramBot v 1.3.0
Arduino JSON v 6.18.0
I hope you can help me on this.
#include <analogWrite.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
/* ******************** configurtion ********************* */
// Replace with your network credentials
const char* p_ssid = "Skynet";
const char* p_password = "1234";
// Initialize Telegram BOT
#define BOTtoken "1234"
// 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 "1234"
/* ****************** public variables ******************** */
WiFiClientSecure p_TelegramClient;
UniversalTelegramBot p_bot(BOTtoken, p_TelegramClient);
// Checks for new messages every 1 second.
int p_botRequestDelay = 1000;
unsigned long p_lastTimeBotRan;
/* **************** function declarations **************** */
void handleNewMessages(int numNewMessages);
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(p_ssid, p_password);
p_TelegramClient.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi: ");
Serial.println(p_ssid);
Serial.print("Local IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
// Telegram
if (millis() > p_lastTimeBotRan + p_botRequestDelay)
{
int numNewMessages = p_bot.getUpdates(p_bot.last_message_received + 1);
while (numNewMessages)
{
handleNewMessages(numNewMessages);
numNewMessages = p_bot.getUpdates(p_bot.last_message_received + 1);
}
p_lastTimeBotRan = millis();
}
}
//Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages)
{
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
// String to search for
String text_ratio = "Current ratio: ";
// Chat id of the requester
String chat_id = String(p_bot.messages[i].chat_id);
if ( chat_id != CHAT_ID )
{
p_bot.sendMessage(chat_id, "I don't know you.", "");
p_bot.sendMessage(chat_id, "Whatever...", "");
}
else
{
// Print the received message
String text = p_bot.messages[i].text;
Serial.println(text);
String from_name = p_bot.messages[i].from_name;
if (text == "/start")
{
String welcome = "Welcome, " + from_name + ".\n";
p_bot.sendMessage(CHAT_ID, welcome, "");
}
}
}
}