shurillu / CTBot

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

Error after a few hours #36

Open migreco opened 4 years ago

migreco commented 4 years ago

with Json 5.13.5 after a few hours the NODEMCU crashes and does not receive/send messages telegram

shurillu commented 4 years ago

Hello, Migreco, thanks for using the library. Could you give me some info about the error? For example:

Could be useful to take a look to the sketch (if it is not so complex).

Stefano

migreco commented 4 years ago

Hi, Stefano

Arduino version: IDE 1.8.10

ESP8266 model: ESP8266 NodeMCU Amica V2 Modulo ESP-12E WiFi Development Board con CP2102

Error: I do not know, I see that the NodeMCU is connected to the router but I don't know how to extract logs during operation without connecting it to a PC

the sketch: Alarm with magnetic sensor, writing on DB MySql events, reading on DB Mysql last state change, Allarm state on webpage HTML and Bot Telegram.....

`

include "CTBot.h"

include

include

include

include

CTBot myBot;

WiFiServer server(80);

// variabili di appoggio pagina web char buffer_riga[80]; int contatore=0; boolean riga_vuota;

String ssid = "xxxxxxx"; // WIFI SSID String pass = "yyyyyyyyyyyyy"; // WIFI PASSWORD String token = "hhhhhhhhhhhhh"; // TELEGRAM BOT TOKEN long chatid=1111111111; // chatid Telegram

//------Definisce i PIN utilizzati in ingresso---------------- int Pin_Apertura = D6;

//---Definisce i Flags (0 = porta chiusa / 1 = porta aperta)----------------

bool Flag_Apertura = 0;

String StatoPorta="";

String datastato="";

String MsgportaBot="";

bool StatoAllarme=0;

const char* host = "miosito.altervista.org"; TBMessage msg;

void setup() { // initialize the Serial Serial.begin(115200); Serial.println("Starting TelegramBot...");

pinMode(Pin_Apertura, INPUT); // Setto il pin come input

server.begin();

// connect the ESP8266 to the desired access point myBot.wifiConnect(ssid, pass);

// set the telegram bot token myBot.setTelegramToken(token);

// check if all things are ok if (myBot.testConnection()) Serial.println("\ntestConnection OK"); else Serial.println("\ntestConnection NOK");

}

void loop() { // a variable to store telegram message data

//--------Legge lo stato della apertura ----- int Status_Apertura = digitalRead(Pin_Apertura);

if (Status_Apertura==1) { MsgportaBot="Porta Aperta"; } if (Status_Apertura==0) { MsgportaBot="Porta Chiusa"; }

//---------Determina lo stato della apertura 1--------- if (StatoAllarme==1) {

if(Status_Apertura==1 && Flag_Apertura==0) { Flag_Apertura = 1; // La porta si e' aperta Serial.println("porta aperta"); StatoPorta="PORTA_APERTA"; MsgportaBot="Porta Aperta"; registra();
myBot.sendMessage(chatid,MsgportaBot); // notify the sender

} if(Status_Apertura==0 && Flag_Apertura==1) { Flag_Apertura = 0; // La porta si e' richiusa Serial.println("porta chiusa"); StatoPorta="PORTA_CHIUSA"; MsgportaBot="Porta Chiusa"; registra(); myBot.sendMessage(chatid,MsgportaBot); // notify the sender

}
}

leggiTelegram();

//--------------

WiFiClient client=server.available(); if (client) { Serial.println("Richiesta da un nuovo client"); leggiUltimoStato(); // inizializzazione variabili di appoggio memset(buffer_riga, 0, sizeof(buffer_riga)); contatore=0; // ogni richiesta http termina sempre con una riga vuota boolean riga_vuota = true; while (client.connected()) { if (client.available()) {

    char c = client.read();
    Serial.write(c);
    // lettura della richiesta HTTP, carattere per carattere
    buffer_riga[contatore]=c;
    if (contatore < sizeof(buffer_riga)-1) contatore++;
    // se si e' arrivati alla fine della riga, e la riga e' vuota,
    // la richiesta http e' terminata. Si puo' quindi procedere
    // con l'invio della risposta
    if (c == '\n' && riga_vuota)
    {        
      String header_risp = "HTTP/1.1 200 OK\r\n";
      header_risp += "Content-Type: text/html\r\n";
      header_risp += "Connection: close\r\n";  // la connessione verra' chiusa dopo la risposta
      header_risp += "\r\n";
      header_risp += "<!DOCTYPE HTML><html><head>\r\n";
      header_risp += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>\r\n";
      header_risp += "<h1>Allarme Roma</h1>\r\n";
          if (Status_Apertura==1) {
          header_risp += "Stato attuale: ";
          header_risp += "<font color='red'>APERTA</font>\r\n";
          }
          else {
          header_risp += "Stato attuale: ";
          header_risp += "<font color='green'>CHIUSA</font>\r\n";
          }
      header_risp += "<br>";
      header_risp += "<p>Stato Allarme: ";
      if (StatoAllarme==1)  {
      header_risp += "<font color='green'>ACCESO</font>\r\n";
      header_risp += "  :  ";
      header_risp += "<a href=\"AllarmeOFF\"><button>OFF</button></a></p>";
      }
      else {
      header_risp += "<font color='red'>SPENTO</font>\r\n";
      header_risp += "  :  ";
      header_risp += "<a href=\"AllarmeON\"><button>ON</button></a></p>";
      }
      header_risp += "Ultimo stato: ";
      header_risp += "<font color='green'>";
      header_risp +=datastato;
      header_risp +="</font>\r\n";
      header_risp += "</html>\n";

      client.print(header_risp);
      break;
    }
            if (c == '\n')
    {
      // inizio di una nuova linea
      riga_vuota = true;
      if (strstr(buffer_riga, "GET /AllarmeON") > 0)
      {
         StatoAllarme=1;   // ATTIVO Allarme                            
         myBot.sendMessage(chatid, "Allarme ON");  // notify the sender

      }
      else if (strstr(buffer_riga, "GET /AllarmeOFF") > 0)
      {
        StatoAllarme=0;   // SPEGNI Allarme                          
        myBot.sendMessage(chatid, "Allarme OFF"); // notify the sender

      }
      // inizio di una nuova riga
      riga_vuota = true;
      memset(buffer_riga, 0, sizeof(buffer_riga));
      contatore=0;
    }
    else if (c != '\r')
    {
      // la riga contiene almeno un carattere
      riga_vuota = false;
    }       
  }
}
// pausa necessaria affinche' il browser possa ricevere i dati
delay(1);

// chiusura della connessione
client.stop();
Serial.println("client disconnesso");

} delay(1000); }

//----------- if there is an incoming message...

void leggiTelegram(void) {

if (myBot.getNewMessage(msg)) {

Serial.println("Leggo messaggi...");
Serial.println(msg.text);

Serial.println("Leggo chatid...");
Serial.println(msg.sender.id);

if (msg.text.equalsIgnoreCase("/AccendiAllarme")) {              
  StatoAllarme=1;   // ATTIVO Allarme                            
  myBot.sendMessage(chatid, "Allarme ON");  // notify the sender
}
else if (msg.text.equalsIgnoreCase("/SpegniAllarme")) {        
  StatoAllarme=0;   // SPEGNI Allarme                          
  myBot.sendMessage(chatid, "Allarme OFF"); // notify the sender
}
 else if (msg.text.equalsIgnoreCase("/Stato")) {        
  leggiUltimoStato();
  String messaggio;
  messaggio="Stato porta= " + MsgportaBot;

  if (StatoAllarme==1) {

          myBot.sendMessage(chatid,datastato);
          myBot.sendMessage(msg.sender.id,"Stato Allarme = Acceso");
          myBot.sendMessage(msg.sender.id,messaggio);

        }
        else {

          myBot.sendMessage(chatid,datastato);
          myBot.sendMessage(chatid,"Stato Allarme = Spento");
          myBot.sendMessage(chatid,messaggio);
        }

}
else if (msg.text.equalsIgnoreCase("/help")) {        
  myBot.sendMessage(chatid, "Comandi Bot : ");             // and send it
  myBot.sendMessage(chatid, "/AccendiAllarme : Attiva Allarme");             // and send it
  myBot.sendMessage(chatid, "/SpegniAllarme : Attiva Allarme");             // and send it
  myBot.sendMessage(chatid, "/Stato: Stato Allarme e Stato Porta");             // and send it
  myBot.sendMessage(chatid, "/help: Comandi Bot");             // and send it
}
else {                                                    // otherwise...
  // generate the message for the sender
  myBot.sendMessage(chatid, "Benvenuto nel tuo bot personale : ");             // and send it
  myBot.sendMessage(chatid, "/AccendiAllarme : Attiva Allarme");             // and send it
  myBot.sendMessage(chatid, "/SpegniAllarme : Attiva Allarme");             // and send it
  myBot.sendMessage(chatid, "/Stato: Stato Allarme e Stato Porta");             // and send it
  myBot.sendMessage(chatid, "/help: Comandi Bot");             // and send it

}

} // wait 500 milliseconds delay(500);

} //--------------Funzione Registra()---------------- void registra(void) { WiFiClient client; client.connect(host, 80);

String url = "/pagina1.php?azione=" + String(StatoPorta); Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); client.stop(); }

//--------Funzione leggidataultimo stato()---------------

void leggiUltimoStato(void) { WiFiClient client; client.connect(host, 80);

String url1 = "/pagina2.php";

Serial.println(host); Serial.println(url1);

client.print(String("GET ") + url1 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

if (client.find("##"))  {
   String datiporta = client.readStringUntil('\n');
   Serial.print("datiporta= ");Serial.println(datiporta);
   int pos=datiporta.indexOf('##');
   datastato=datiporta.substring(0,pos);

   Serial.print("stato= ");Serial.println(datastato);

   if (datastato=="PORTA_CHIUSA") {
    datastato="Porta Chiusa";
   }
   if (datastato=="PORTA_APERTA") {
    datastato="Porta Aperta";
   }

   datastato=datastato + " ; ";
   Serial.print("datiporta0= ");Serial.println(datastato);
   datiporta=datiporta.substring(pos+2);
   Serial.print("datiporta00= ");Serial.println(datiporta);

   int pos1=datiporta.indexOf('##');
   Serial.print("datiporta1= ");Serial.println(datiporta.substring(0,pos1));
   datastato=datastato + datiporta.substring(0,pos1);  
  }

client.stop(); }`

migreco commented 4 years ago

The problem is solved..... Recompiled with downloaded libraries

thanks

migreco commented 4 years ago

I have to reopen the post because the problem is not solved. in random way the card goes in block. in case of block: the card responds to ping if I restart the router will work again