#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <WiFi.h>
#include <WiFiMulti.h>
#include <DHT11.h>
DHT11 dht11(15);
#include <CTBot.h>
CTBot miBot;
WiFiMulti wifiMulti;
const char *token = "6690611788:AAGq0ELx30JZK1xgUdX03nptYGhirfkbB3M";
const char *ssid1 = "POCO-F3";
const char *password1 = "Jose Rizo";
const char *ssid2 = "Casa Dalmata";
const char *password2 = "Primos2021";
const char *ssid3 = "WiFi-Estudiante";
const char *password3 = "UNANManagua*";
int dinero = 0;
boolean estado = false;
unsigned long tiempo = 0;
int segundos = 0;
void setup() {
Serial.begin(115200);
pinMode(15,INPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(12, INPUT);
lcd.init();
lcd.backlight();
lcd.print("CARGANDO");
for( int i =0; i<=3; i++){
lcd.print(".");
delay(100);
}
wifiMulti.addAP(ssid1, password1);
wifiMulti.addAP(ssid2, password2);
wifiMulti.addAP(ssid3, password3);
Serial.print("CONECTANDO A WIFI...");
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.print("Conectado a la red: ");
Serial.println(WiFi.SSID());
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Conexión WiFi exitosa");
}
else{
Serial.print("ERROR DE CONEXION WIFI VERIFIQUE LA CONEXION ");
delay(500);
}
miBot.wifiConnect(WiFi.SSID().c_str(), WiFi.psk().c_str());
miBot.setTelegramToken(token);
if (miBot.testConnection()) {
Serial.println("CONECTADO A TELEGRAM");
digitalWrite(14, HIGH);
digitalWrite(13, LOW);
} else {
Serial.println("PROBLEMAS DE CONEXION CON TELEGRAM");
digitalWrite(13, HIGH);
digitalWrite(14, LOW);
}
analogReadResolution(10);
lcd.print(".");
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TEMPERATURA ");
}
void loop() {
tiempo = millis();
segundos = ( tiempo / 1000);
if (wifiMulti.run() == WL_CONNECTED) {
Serial.println("CONECTADO");
digitalWrite(14, HIGH);
digitalWrite(13, LOW);
}
else {
digitalWrite(13, HIGH);
digitalWrite(14, LOW);
Serial.println("DESCONECTADO");
}
int temperatura = dht11.readTemperature();
if (temperatura != DHT11::ERROR_CHECKSUM && temperatura != DHT11::ERROR_TIMEOUT)
{ lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(12,0);
lcd.print(temperatura);
lcd.print(" C ");
lcd.setCursor(0,1);
lcd.print(" TIEMPO ");
lcd.print( segundos );
delay(100);
}
else
{
lcd.setCursor(0,1);
lcd.print("DHT DESCONECTADO");
delay(200);
}
if (!miBot.testConnection()) {
int intentos = 0;
const int maxIntentos = 5;
while (!miBot.testConnection() && intentos < maxIntentos) {
delay(2000);
intentos++;
}
}
TBMessage msg;
if (CTBotMessageText == miBot.getNewMessage(msg)) {
String text = msg.text;
text.toLowerCase();
if (msg.text.equalsIgnoreCase("url")) {
String direccion = "Bienvenido "+msg.sender.firstName + "\n\nDirección IP Local: http://192.168.0.100";
direccion += "\n\nRecuerde!\nPara poder acceder a la página web, tiene que estar conectado a la red WiFi → " + WiFi.SSID();
direccion += "\n\nGracias por su tiempo ♡";
miBot.sendMessage(msg.sender.id, direccion);
} else if (text.startsWith("cuenta ")) {
String accountIDStr = text.substring(7);
int accountID = accountIDStr.toInt();
if (accountID >= 1000 && accountID <= 9999) {
bool found = false;
String response;
do {
Serial.println(accountID);
if (Serial.available() > 0) {
int dinero = Serial.parseInt(); // Lee el número enviado desde el emisor
if (dinero > 0 && dinero != 10000001) {
response = "Bienvenido Cliente → 2 \n\n";
response += "Cuenta → " + String(accountID) + "\n";
response += "El saldo de tu cuenta es: C$ " + String(dinero) + "\n\n";
response += "Gracias por su tiempo ♡";
found = true;
estado = false;
} else {
estado = true;
found = true;
}
}
} while (!found );
if (found && estado == false) {
miBot.sendMessage(msg.sender.id, response);
} else if(estado == true) {
miBot.sendMessage(msg.sender.id, "LA CUENTA NO SE ENCONTRO");
estado = false;
}
} else {
miBot.sendMessage(msg.sender.id, "EL ID DE LA CUENTA TIENE UN RANGO ENTRE 1000 Y 9999 \nEJEMPLO → cuenta 1000 o cuenta 9999 ");
}
} else {
miBot.sendMessage(msg.sender.id, "Bienvenido → " + msg.sender.firstName + "\n\nINTENTA USAR\n\nURL → Para conocer la Direccion de la pagina\n\nCuenta XXXX → Para conocer el saldo de la su Cuenta\nReemplaze XXXX Por el ID de su cuenta\n ");
}
}
}