Can´t read FC-28 data sensor using ESP32

I use the FC-28 sensor and I can't read data when using it in my main code, but in a sensor-only code it works great. Does anyone have an idea on the issue?

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <SoftwareSerial.h>
#include <UniversalTelegramBot.h>
// Datos de la red WiFi
#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "PASSWORD"
//Token de Telegram BOT
#define BOT_TOKEN "TOKEN"
#define ID_Chat "CHAT_ID"//ID_Chat se obtiene de telegram
const unsigned long tiempo = 1000; //tiempo medio entre escaneo de mensajes
String datos;
String chat_id;
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long tiempoAnterior; //última vez que se realizó el análisis de mensajes
const int pin22 = 22;//Pin de la bomba de agua
int humedad = 14;//Pin del sensor
int bombaStatus = 0;
int estadoM = 1;
int inicio = 1;
int lectura;
int por;

void setup()
{
  Serial.begin(115200);
//  dht.begin();//Inicializar el sensor DHT
  pinMode(humedad, INPUT_PULLDOWN); //Se configura como entrada para el sensor
  pinMode(pin22, OUTPUT); //Inicializar pin 22 como salida para en control de la bomba de agua.
  digitalWrite(pin22, LOW);// La colocamos en estado bajo
  // Intenta conectarse a la red wifi
  Serial.print("Conectando a la red ");
  Serial.print(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); //Agregar certificado raíz para api.telegram.org
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nConectado a la red wifi. Dirección IP: ");
  Serial.println(WiFi.localIP());
   if(inicio == 1){
    Serial.println("Sistema preparado");
    bot.sendMessage(ID_Chat, "Sistema preparado!!!, escribe /Ayuda para ver las opciones", ""); //Enviamos un mensaje a telegram para informar que el sistema está listo
    inicio = 0;
    }
}

void mensajesNuevos(int numerosMensajes)
{
  for (int i = 0; i < numerosMensajes; i++)
  {
    chat_id = bot.messages[i].chat_id;
    String text = bot.messages[i].text;

//////////Activa la Bomba de agua activada x 5 segundos//////
    if (text == "/Activar")
    {
      digitalWrite(pin22, HIGH); 
      bombaStatus = 1;
      bot.sendMessage(chat_id, "Bomba de agua activada x 5 segundos", "");
      delay(5000);
      digitalWrite(pin22, LOW);
      bombaStatus = 0;
      bot.sendMessage(chat_id, "Bomba de agua desactivada automaticamente después de 5 Segundos", "");
    }
 
////////Estado de la bomba de agua/////////     
 
    if (text == "/Estado")
    {
      if (bombaStatus)
      {
        bot.sendMessage(chat_id, "Estado actual: Bomba de agua encendida", "");
      }
      else
      {
        bot.sendMessage(chat_id, "Estado actual: Bomba de agua apagada", "");
      }
    }

////////Consulta el estado de humedad//////
    if (text == "/Humedad")
    {
      lecturaSensor();
    }
    
////////Imprime el menú de ayuda//////////
    if (text == "/Ayuda")
    {
      String ayuda = "Bienvenido al sistema de Internet de las cosas, con ESP32 " ".\n";
      ayuda += "Estas son tus opciones.\n\n";
      ayuda += "/Activar: Activa el la bomba durante 5 segundos \n";
      ayuda += "/Humedad : Devuelve el porcentaje de humedad en la tierra\n";
      ayuda += "/Ayuda: Imprime este menú \n";
      ayuda += "Recuerda el sistema distingue entre mayuculas y minusculas \n";
      bot.sendMessage(chat_id, ayuda, "");
    }    
  }
}

 
void loop()
{

  //Verifica si hay datos nuevos en telegram cada 1 segundo
  if (millis() - tiempoAnterior > tiempo)
  {
    int numerosMensajes = bot.getUpdates(bot.last_message_received + 1);
 
    while (numerosMensajes)
    {
      Serial.println("Comando recibido");
      mensajesNuevos(numerosMensajes);
      numerosMensajes = bot.getUpdates(bot.last_message_received + 1);
    }
 
    tiempoAnterior = millis();
  }
  
}

void lecturaSensor(){

  lectura = analogRead(humedad);
  delay(500);
  por = map(lectura, 4095, 500, 0, 100);
  String porstr = (String) por;

  Serial.print("La lectura es: ");
  Serial.println(lectura);
  Serial.print("El porcentaje de humdedad es: ");
  Serial.print(por);
  Serial.println("%");
  delay(500);

  bot.sendMessage(chat_id, "El porcentaje de humedad es: " + porstr + "%", ""); 
}

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