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