1

I'm developing a program in Arduino Ide (v. 1.8.13) which takes brightness values and send them to ESP32 Web Server. Also, I want to add the posibility of seeing the data via Telegram. At the moment I just want to send '/start' to the Telegram bot and Arduino program answer with a welcome message. However, the bot doesn't receive my messages. I read this is a versions issue between ArduinoJson, Universal Telegram Bot, Arduino Ide and ESP32 Core version. Could someone tell me what versions work fine in ESP32? I'm getting crazy.

I add my actual code.

These are the versions I'm using: Arduino Ide:1.8.13, Arduino Json:6.18, Universal Telegram Bot:1.3.0, Wifi Client Secure:1.0, ESP32:1.04.

Thanks in advance :)

// Import required libraries
#include <WiFi.h>
#include <Wire.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>

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

#include <TimeAlarms.h>
#include <Time.h>
#include <TimeLib.h>


   
const char* ssid_STA = "XXXXXX";
const char* password_STA = "XXXXXX";
IPAddress ip(192,168,1,44);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

#define BOTtoken "XXXXXX"
#define CHAT_ID "XXXXXXX"

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

time_t fecha;

int botRequestDelay = 1000;
unsigned long lastTimeBotRan;

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);


void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
  
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  //tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */  
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("Auto");
  Serial.print  ("Timing:       "); Serial.println("13 ms");
  Serial.println("------------------------------------");
}

String readLuz() {
 
 /* Get a new sensor event */ 
  sensors_event_t event;
  tsl.getEvent(&event);
 
  /* Display the results (light is measured in lux) */
  
  Serial.print(event.light); Serial.println(" lux");
  
  return String(event.light);
  
  
}
void Informacion(){
  Serial.println("voy a enviar");
  sensors_event_t event;
  tsl.getEvent(&event);
  //bot.sendMessage(chat_ID, "La luminosidad en tu jardín es de " ,"");
  Serial.println("enviado");  
}

void handleNewMessages(int numNewMessages) {
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));

  for (int i = 0; i < numNewMessages; i++) {
    String chat_id = String(bot.messages[i].chat_id);
    Serial.println(chat_id);
    if (chat_id != CHAT_ID){
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    //COger texto
    String text = bot.messages[i].text;
    Serial.println(text);
    //Coger nombre
    String from_name = bot.messages[i].from_name;
    
    //if (text == "/get_info") {
      
    //}    
    if (text == "/start") {
      String welcome = "Bienvenida "+from_name+"\n";
      welcome += "Properties.\n\n";
      welcome += "/get_info : Obtener información actual del jardín\n";
      
      bot.sendMessage(chat_id, welcome, "");
    }
  }
}
/**************************************************************************/
/*
    Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void) 
{
  
  Serial.begin(115200);
//  client.setInsecure();
  /* Initialise the sensor */
  //use tsl.begin() to default to Wire, 
  //tsl.begin(&Wire2) directs api to use Wire2, etc.
  if(!tsl.begin())
  {
    /* There was a problem detecting the TSL2561 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }  
  /* Display some basic information on this sensor and  Setup the sensor gain and integration time */
  //displaySensorDetails();
  configureSensor();

   
  ///EJEMPLO SREVER
 
  // Initialize SPIFFS
  if(!SPIFFS.begin()){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
 ;
  // Connect to Wi-Fi
  //WiFi.softAP(ssid_AP, password_AP);
  WiFi.mode(WIFI_STA);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid_STA, password_STA);
  
  
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(3000);
    Serial.println("Connecting to WiFi..");
  }
  
  // Print ESP32 Local IP Address
  Serial.println("IP");
  Serial.println(WiFi.localIP());
  

  // Route for root / web page
 
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html");
  });
  
  server.on("/luz", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readLuz().c_str());
  });
  
  // Start server
  server.begin();

  /*
  //ALARMS
  //Coger la hora real al reiniciar
  //fecha = now();
  //Serial.print(hour(fecha));  Serial.print(minute(fecha)); Serial.print(second(fecha)); Serial.print(day(fecha)); Serial.print(month(fecha)); Serial.println(year(fecha));
  //setTime(hour(fecha), minute(fecha),second(fecha), day(fecha), month(fecha),year(fecha));
  setTime(12, 42, 00, 16, 6, 21);
  
  // Crear las alarmas y configurar las funciones correspondientes a cada una
  Alarm.alarmRepeat(8, 0, 0, Informacion);  // Evento a las 14:00 diario
  Alarm.alarmRepeat(12,43, 0, Informacion);  // Evento a las 14:05 diario
  Alarm.alarmRepeat(21, 0, 0, Informacion);  // Evento a las 14:10 diario
  */
}

/**************************************************************************/
/*
    Arduino loop function, called once 'setup' is complete (your own code
    should go here)
*/
/**************************************************************************/
void loop(void){  
  if (millis() > lastTimeBotRan + botRequestDelay)  {
    Serial.println("loop");
    Serial.println(bot.last_message_received);
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    lastTimeBotRan= millis();
  }
  // Mostrar el reloj en el monitor serial
  //digitalClockDisplay();
 
  // Esperar 1 segundo y procesar las Alarmas mientras tanto...
  //Alarm.delay(1000);
}

/*
void digitalClockDisplay() {
  Serial.print(hour());
  printDigits(minute());
  //printDigits(second());
  Serial.println();
}
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
*/
Lau
  • 11
  • 2

1 Answers1

2

I suppose you are missing the statement

// Add root certificate for api.telegram.org    
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);