Telegram, Error sending the message

Good day to everyone,

currently I'm working on Arduino project for University. In particular, I'm developing an environment which consists in Arduino Uno R4 Wifi, 2 led, a force sensor and a Telegram bot. Basically, the force sensor detects a force and the first led power up; then, when the force is detected longer than 10 sec, the second led light up. In this case, I want to receive a notification from my Telegram bot on my smartphone. The problem is that Arduino doesn't send any message (or Telegram doesn't receive?). Belove the sketch I am using

#include <WiFiS3.h>
#include <WiFiSSLClient.h>
#include <UniversalTelegramBot.h>

const int pinPressureSensor = A0;
const int pinGreenLED = 11;
const int pinRedLED = 12;

// Wi-Fi credentials
const char* ssid = "xxxxxxxxxxxxxx";
const char* password = "xxxxxxxxx";

// Telegram bot data
#define BOT_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define CHAT_ID "xxxxxxxxxxxx"

WiFiClient client;
UniversalTelegramBot bot(BOT_TOKEN, client);

unsigned long startTime = 0;
bool forceDetected = false;

void setup() {
  pinMode(pinGreenLED, OUTPUT);
  pinMode(pinRedLED, OUTPUT);
  digitalWrite(pinGreenLED, LOW);
  digitalWrite(pinRedLED, LOW);
  Serial.begin(9600);

  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to Wi-Fi...");
  }
  Serial.println("Connected to Wi-Fi!");
}

void loop() {
  int sensorValue = analogRead(pinPressureSensor);
  Serial.println(sensorValue);

  if (sensorValue > 1) {
    if (!forceDetected) {
      forceDetected = true;
      startTime = millis();
      digitalWrite(pinGreenLED, HIGH); // Turn on the green LED
    } else {
      if (millis() - startTime >= 10000) {  // After 10 seconds
        digitalWrite(pinGreenLED, LOW);  // Turn off the green LED
        digitalWrite(pinRedLED, HIGH);   // Turn on the red LED
        
        // Send the message to Telegram
Serial.println("Sending message to Telegram...");
bool result = bot.sendMessage(CHAT_ID, "TEST", "");
if (!result) {
            Serial.println("Error sending the message. Check the parameters.");
        }
      }
    }
  } else {
    forceDetected = false;
    digitalWrite(pinGreenLED, LOW);   // Turn off the green LED
    digitalWrite(pinRedLED, LOW);     // Turn off the red LED
  }

  delay(100);  // Stabilize sensor readings
}`  

The code is working regarding the sensor and led, but fails in sending the notification via Telegram. I've already created my bot and start a conversation, I also written some code in Pyhton to better customize it. Any help in this matter is welcome. I thank you in advance.
Regards

have you tested sending "hello world" to your Telegram bot?

Honestly no. I've tried with a simple "TEST" message like in the sketch bool result = bot.sendMessage(CHAT_ID, "TEST", "");. Doing this I receive back the error message that I printed, meaning that the the message wasn't sent.

you should be able to send a msg in setup() without any other code. you need to get the telegram code to work before expecting the force detection msg. (i know this sounds obvious)

Telegram bot was operative (bot code written in Python was running in background), if are you referring at this. Otherwise, I don't understand what you referring to :frowning:

you need to get Telegram to work on your Arduino before you try using it with other things (force detection).

don't understand why you mention Python. Python doesn' run on Arduino

Yes, I mentioned Python only because I "customized" my both to answer some specific questions. But this has nothing to do with Arduino. What you mean to get Telegram to work on my Arduino?

on what ever board you're using. gessing an esp board

Yes, I'm usinging Arduino Uno R4 Wifi. Do I need additional components?

no

find some example code that does this

Ok. Understand, I will try with a simple code like this to verify if the system works.

void setup() {
 
  Serial.begin(115200);

    WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to Wi-Fi...");
  }
  Serial.println("Connected to Wi-Fi");

   bot.sendMessage(CHAT_ID, "Hello World", "");
}

void loop() {
  }

yes. if that doesn't work, you need to figure out what is missing.

did you see the "Hello World" example in Telegram Bot Library. are you missing bot.begin();?

Solve!! At the end the error was in the code instead of

WiFiClient client;
UniversalTelegramBot bot(BOT_TOKEN, client);

I put

WiFiSSLCClient client;
UniversalTelegramBot bot(BOT_TOKEN, client);

Now I receive the notifications on my Telegram bot.

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