Page 1 of 1

ESP32 - telegram disable/enable notifications

Posted: Sun Sep 04, 2022 9:33 pm
by levinlinux
Guys, I've already managed to make the telegram bot receive messages from the X condition state as I put it in the code, but in this condition it keeps sending a message all the time, could someone help me how do I disable notifications and enable when, like this does not pollute the chat.

Code: Select all

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid = "ssid";
const char* password = "password";

#define BOTtoken "xxxx:xxxxxx"
#define CHAT_ID "xxxxxx"

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

const int trigPin = 5;
const int echoPin = 18;
//Telegram

//define sound speed in cm/uS
#define SOUND_SPEED 0.034
//#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
 Serial.begin(115200);
   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);

  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) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

 bot.sendMessage(CHAT_ID, "Bot caixa de agua!", "");

}

void loop() {  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  delay (2000);
  
  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  // Convert to inches
//  distanceInch = distanceCm * CM_TO_INCH;
if(  (distanceCm > 0) && (distanceCm <= 40)   )
{
  Serial.println("Caixa 100% cheia!");
 // bot.sendMessage(CHAT_ID, "Caixa 100% cheia!", "");
} else
if(  (distanceCm > 40) && (distanceCm <= 50)  )
{
  Serial.println("Caixa 70% cheia!");
//  bot.sendMessage(CHAT_ID, "Caixa 70% cheia!", "");
}else
if(  (distanceCm > 50) && (distanceCm <= 70)  )
{
  Serial.println("Caixa 50% cheia!");
}else
if(  (distanceCm > 70) && (distanceCm <= 90)  )
{
  Serial.println("Caixa quase vazia!");
}else
if(  (distanceCm > 90) && (distanceCm <= 150)  )
{
  Serial.println("Caixa vazia!");
  bot.sendMessage(CHAT_ID, "Caixa vazia!", "");
}

  // Prints the distance in the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);
//  Serial.print("Distance (inch): ");
//  Serial.println(distanceInch);
  
  delay(1000);
}

Re: ESP32 - telegram disable/enable notifications

Posted: Thu Aug 31, 2023 7:28 pm
by cloudmiracle
In order to set your code to send Telegram notification only once when the event occurs and not continuously, add the following code to the sketch so that the event occurs ----sends notification-----resets event.

bool current_state1 = LOW;
bool previous_state1 = LOW;

void loop();
current_state1 = digitalRead(input1);
if (current_state1 != previous_state1) {
if (current_state1 == HIGH) {
delay (100);
bot.sendMessage(CHAT_ID, "Unit1 is OFF", "");}
if (current_state1 == LOW){
delay (100);
bot.sendMessage(CHAT_ID, "Unit1 is ON", "");}
previous_state1 = current_state1;
}

In this manner the sketch will send notification only once when input1 becomes HIGH. It will only send notification next time when the input1 goes LOW and then HIGH again.