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);
}