shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
147 stars 34 forks source link

Help!! with the connection to the telegram server!!! #42

Closed javierferwolf closed 4 years ago

javierferwolf commented 4 years ago

Hi everyone!!

I have a small program that works via GET statements rather than a library. I use the ESP8266 core 2.4.2 and as everyone knows this version of core has stopped working for telegram. and I've been looking for libraries that still work with version 2.4.2. and I found this interesting library CTbot I'm interested to know what this library has that continues to work in this core??

I tried to add to the code const uint8_t fingerprint[20] = { 0xBB, 0xDC, 0x45, 0x2A, 0x07, 0xE3, 0x4A, 0x71, 0x33, 0x40, 0x32, 0xDA, 0xBE, 0x81, 0xF7, 0x72, 0x6F, 0x4A, 0x2B, 0x6B }; Use client.setFingerprint(fingerprint); in setup block. but without success!!
could someone tell me what would I have to add or remove in my code to make it work again?

include

include

include "DHT.h"

// Definimos los parámetros de conexión a la WIFI const char ssid = "intrusos"; // no superior a 32 caracteres const char pass = "xxxxxxxxxx"; // contraseña wifi int status = WL_IDLE_STATUS;

// Datos del Bot de Telegram String BOTtoken = "bot2xxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String Chat_id = "xxxxxxxxx"; // Chat_id String Texto_enviar = ""; String Texto_recibido = ""; String Update_id = ""; String anterior_upd = ""; String Nueva_upd = ""; String Respuesta = "";

int Inicio; int Termino; int Intervalo = 15000; unsigned long elapsed = 0; unsigned long previous; boolean respondio = false;

define DHTPIN 4

define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE);

WiFiClientSecure client; // inicio del cliente seguro IPAddress server(149, 154, 167, 200); // IP de api.telegram.org

void setup() {

Serial.begin(115200);

// Conecta a la WIFI WiFi.begin(ssid, pass); /// } client.setInsecure();

while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("Conectado a la red WiFi"); Serial.println("Dirección IP: "); Serial.println(WiFi.localIP());

if (client.connect(server, 443)) { Serial.println(".... conectado a Telegram"); }

Enviar_texto(" Inicio del Sistema .......");

Ultimo_msg(); previous = millis();

}

void loop() { elapse(); Leer_msg(); // leemos el ultimo mensaje

if (elapsed > 500) {

anterior_upd = Update_id; // Guardamos la anterior Update
Ultimo_msg (); // comprobamos el ultimo mensaje
delay(1000); // Esperamos a recibir los datos
Leer_msg(); // Leemos los datos
busca_upd_id(Respuesta); // buscamos la Update_id y la guardamos
busca_texto(Respuesta); // Buscamos el Texto del mensaje
// Si ha cambiado la Update_id seguimos con el codigo
if (anterior_upd != Nueva_upd) {
  //Serial.println("Es diferente Update");
  Responder_mensaje(Texto_recibido);
} else {
} 

} }

void busca_texto( String Rsp ) { Texto_recibido = ""; int start = Rsp.indexOf("text") + 7 ; // Buscamos el indice ( numero ) de la palabra "text" y le añadimos 7 int fin = Rsp.indexOf("}}]}") - 1; // Buscamos el indice del texto }}]} y le restamos uno Texto_recibido = (Rsp.substring(start, fin)); // Guardamos el resultado en la variable }

void busca_upd_id( String Rsp ) { anterior_upd = Update_id; // Guardamos la anterior Update_id para comprobar int start = Rsp.indexOf("update_id") + 11 ; // Buscamos el indice del texto y le añadimos 11 int fin = Rsp.indexOf("message") - 2; // Buscamos el indice del texto y le restamos 2 Update_id = Rsp.substring(start, fin); // Guardamos la Update_id Nueva_upd = Rsp.substring(start, fin); // Volvemos a guardar la Update_id pero en la variable de nueva }

// Orden para pedir el ultimo mensaje, vemos que se usa el Offset=-1&limit=1 para mostrar solo el ultimo void Ultimo_msg () { if (client.connect(server, 443)) { // client.println("GET /botxxxx/getUpdates?offset=-1&limit=1"); client.println("GET /" + BOTtoken + "/getUpdates?offset=-1&limit=1"); } previous = millis(); // Guardamos los milisegundos para comprobar que haya pasado X tiempo entre lecturas }

void Leer_msg () { Respuesta = ""; // Vaciamos la variable while (client.available()) { // Mientras no lo lea todo seguira leyendo char inChar = client.read(); // Lee el caracter Respuesta += inChar; // Añadimos caracter a caracter el mensaje } }

void elapse() { elapsed = millis() - previous; }

void Enviar_texto( String Texto_enviar ) { if (client.connect(server, 443)) { client.println("GET /" + BOTtoken + "/sendMessage?chat_id=" + Chat_id + "&text=" + Texto_enviar + ""); } }

void Responder_mensaje ( String mensaje ) {

if (mensaje == "Estado") { Enviar_texto("Conectado"); respondio = true; } else if (mensaje == "Temperatura") { float t = dht.readTemperature(); // Obtiene la Temperatura en Celsius Enviar_texto(String(t) + "ºC"); respondio = true; } else if (mensaje == "Humedad") { float h = dht.readHumidity(); // Obtiene la Humedad Enviar_texto(String(h) + "%"); respondio = true; }

if (respondio == true) { // mostramos el texto que se ha entendio Serial.println("El Texto : " + mensaje + " Lo he entendio perfectamente"); } else { Serial.println("El Texto : " + mensaje + " No Lo he entendio");

} respondio = false ; // Dejamos en falso que entendio el mensaje }

shurillu commented 4 years ago

Hola Javier, this is not a CTBot library issue, anyway I'll try to help you. I guess you don't update the ESP8266 core for some compatibility issue versus old code, but I strongly suggest you to update. BTW the core of the CTBot library is the sendCommand() method: it is "self contained". In other word that method:

Stefano

javierferwolf commented 4 years ago

Hola Stefano!! gracias por tu respuesta! CTbot es una biblioteca estupenda! =)

Yes is true it is not a library error! sorry! but I was so interested to know how the library CTbot works so that it still works in version 2.4.2. For reasons of library compatibility, I cannot update the version of core 2.4.2.

if I understood correctly, at the beginning of the sketch I have to write: const uint8_t fingerprint[20] = { 0xBB, 0xDC, 0x45, 0x2A, 0x07, 0xE3, 0x4A, 0x71, 0x33, 0x40, 0x32, 0xDA, 0xBE, 0x81, 0xF7, 0x72, 0x6F, 0x4A, 0x2B, 0x6B }; and BearSSL::WiFiClientSecure telegramServer; then in the setup block: telegramServer.setFingerprint(m_fingerprint); but i have a question where does this variable "m_fingerprint" come from?

grazie per il tuo aiuto!!=)

shurillu commented 4 years ago

ehm... copy/paste error :-D As you said:

Try and let me know ;-)

Stefano

javierferwolf commented 4 years ago

Stefano, now it works but in the first cycle, that is, the first connection to the api.telegram.org server works and can send the first message! but then when performing the same action again "Connection 149.154.167.198:443... Connection to the server failed!" what would be the problem?

shurillu commented 4 years ago

Javier, I see in your first posted code a lot of client.connect() but you don't close any connections. The error that pop out is likely a connection issue due an already established connection. My advice is to build up a procedure that do:

Let the CTBot library sendCommand() method inspire you ;-)

Stefano