TTGO LoRa32 SX1276 OLED

Buonasera, sto realizzando un piccolo progetto con la scheda TTGO LoRa32 SX1276 OLED. Vi spiego brevemente cosa vorrei fare, tramite il protocollo Modbus leggo i dati del mio impianto fotovoltaico (corrente,tensione,potenza,...) una volta letti questi dati mandando un comando tramite Telegram vorrei visualizzarli. Il programma che ho scritto è funzionante e fa quello che dovrebbe, l'unico problema è che dopo un po' di tempo che non viene mandato un comando tramite Telegram, la scheda smette di rispondere ai vari comandi che invio. Ho controllato piu volte il codice per vedere se per caso c'era una temporizzazione che se vedeva che non veniva mandato niente per un certo tempo si metteva in sleep mode o qualcosa del genere ma non ho trovato niente. Metto il codice cosi se qualcuno ha tempo e voglia può darmi una mano

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>   
#include <ArduinoJson.h>
/*
 * Libreria per dati
 */
#include <ModbusMaster.h>
#define MAX485_DE      3
#define MAX485_RE_NEG  1

ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}
float TensioneBattterie = 0;

// Replace with your network credentials
const char* ssid = "Ciao";
const char* password = "Ciao";

// Initialize Telegram BOT
#define BOTtoken "......................................"  

#define CHAT_ID ".................."

#ifdef ESP8266
  X509List cert(TELEGRAM_CERTIFICATE_ROOT);
#endif

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Checks for new messages every 1 second.
int botRequestDelay = 500;
unsigned long lastTimeBotRan;

const int ledPin = 2;
bool ledState = LOW;

// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages,float PVvolt,float PVCurr,float PVPow,float Vbatt,float Abatt,float Pbatt,
float MaxVbatt,float MinVbatt,float GenEnTod,float GenEnMon,float GenEnYea,float GenEnTot) {
  Serial.println("handleNewMessages");
  Serial.println(String(numNewMessages));

  for (int i=0; i<numNewMessages; i++) {
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != CHAT_ID){
      bot.sendMessage(chat_id, "Unauthorized user", "");
      continue;
    }
    
    // Print the received message
    String text = bot.messages[i].text;
    Serial.println(text);

    String from_name = bot.messages[i].from_name;

    if (text == "/start") {
      String welcome = "Welcome, " + from_name + ".\n";
      welcome += "Con i seguenti comandi puoi comandare la cascina\n\n";
      welcome += "/Inverter_acceso \n";
      welcome += "/Inverter_spento \n";
      welcome += "/Stato_inverter \n";
      welcome += "/Dati_impianto \n";
      welcome += "/Produzione_pannelli \n";
      welcome += "/Batterie \n";
      bot.sendMessage(chat_id, welcome, "");
    }

    if (text == "/Inverter_acceso") {
      bot.sendMessage(chat_id, "LED state set to ON", "");
      ledState = HIGH;
      digitalWrite(ledPin, ledState);
    }
    
    if (text == "/Inverter_spento") {
      bot.sendMessage(chat_id, "LED state set to OFF", "");
      ledState = LOW;
      digitalWrite(ledPin, ledState);
    }
    if (text == "/Dati_impianto") {
    String messaggio;
    messaggio += "Batterie: \n";
    messaggio += "Tensione massima: ";
    messaggio += MaxVbatt;
    messaggio += " V \n";
    messaggio += "Tensione minima: ";
    messaggio += MinVbatt;
    messaggio += " V \n";
    messaggio += "\n";
    messaggio += "Produzione impianto: \n";
    messaggio += "Energia generata oggi: ";
    messaggio += GenEnTod;
    messaggio += " kWh \n";
    messaggio += "Energia generata mese: ";
    messaggio += GenEnMon;
    messaggio += " kWh \n";
    messaggio += "Energia generata anno: ";
    messaggio += GenEnYea;
    messaggio += " kWh \n";
    messaggio += "Energia generata totale: ";
    messaggio += GenEnTot;
    messaggio += " kWh \n";
    bot.sendMessage(chat_id, messaggio, "");
    }
    if (text == "/Produzione_pannelli") {
    String messaggio;
    messaggio += "Pannelli: \n";
    messaggio += "Tensione: ";
    messaggio += PVvolt;
    messaggio += " V \n";
    messaggio += "Corrente: ";
    messaggio += PVCurr;
    messaggio += " A \n";
    messaggio += "Potenza: ";
    messaggio += PVPow;
    messaggio += " W \n";
    bot.sendMessage(chat_id, messaggio, "");
    }
    if (text == "/Batterie") {
    String messaggio;
    messaggio += "Batterie: \n";
    messaggio += "Tensione: ";
    messaggio += Vbatt;
    messaggio += " V \n";
    messaggio += "Corrente: ";
    messaggio += Abatt;
    messaggio += " A \n";
    messaggio += "Potenza: ";
    messaggio += Pbatt;
    messaggio += " W \n"; 
    bot.sendMessage(chat_id, messaggio, "");
    }
    if (text == "/Stato_inverter") {
      if (digitalRead(ledPin)){
        bot.sendMessage(chat_id, "LED is ON", "");
      }
      else{
        bot.sendMessage(chat_id, "LED is OFF", "");
      }
    }
  }
}
float Vbatt;
float Abatt;
float Pbatt;
float MaxVbatt;
float MinVbatt;
float GenEnTod;
float GenEnMon;
float GenEnYea;
float GenEnTot;
float PVvolt;
float PVCurr;
float PVPow;
float Dati(){
  uint8_t resultMain;
  resultMain = node.readInputRegisters(0x3300,19);
  if (resultMain == node.ku8MBSuccess)
  {
  Serial.print("Maximum battery volt today:");
  Serial.println(node.getResponseBuffer(0x02)/100.0f);
  MaxVbatt = node.getResponseBuffer(0x02)/100.0f;
  Serial.print("Minimum battery volt today:");
  Serial.println(node.getResponseBuffer(0x03)/100.0f);
  MinVbatt = node.getResponseBuffer(0x03)/100.0f;
  Serial.print("Generated energy today:");
  Serial.println(node.getResponseBuffer(0x0C)/100.0f);
  GenEnTod = node.getResponseBuffer(0x0C)/100.0f;
  Serial.print("Generated energy month:");
  Serial.println(node.getResponseBuffer(0x0E)/100.0f);
  GenEnMon = node.getResponseBuffer(0x0E)/100.0f;
  Serial.print("Generated energy year:");
  Serial.println(node.getResponseBuffer(0x10)/100.0f);
  GenEnYea = node.getResponseBuffer(0x10)/100.0f;
  Serial.print("Generated energy total:");
  Serial.println(node.getResponseBuffer(0x12)/100.0f);
  GenEnTot = node.getResponseBuffer(0x12)/100.0f;
  }
  delay(1000);
  return MaxVbatt,MinVbatt,GenEnTod,GenEnMon,GenEnYea,GenEnTot;
}
float DatiPannelli(){
  uint8_t result;
  result = node.readInputRegisters(0x3100,16);
  if (result == node.ku8MBSuccess)
  {
  Serial.print("PV voltage:");
  Serial.println(node.getResponseBuffer(0x00)/100.0f);
  PVvolt = node.getResponseBuffer(0x00)/100.0f;
  Serial.print("PV current:");
  Serial.println(node.getResponseBuffer(0x01)/100.0f);
  PVCurr = node.getResponseBuffer(0x01)/100.0f;
  Serial.print("PV power:");
  Serial.println(node.getResponseBuffer(0x02)/100.0f);
  PVPow = node.getResponseBuffer(0x02)/100.0f;
  Serial.print("Battery volt:");
  Serial.println(node.getResponseBuffer(0x04)/100.0f);
  Vbatt = node.getResponseBuffer(0x04)/100.0f;
  Serial.print("Battery current:");
  Serial.println(node.getResponseBuffer(0x05)/100.0f);
  Abatt = node.getResponseBuffer(0x05)/100.0f;
  Serial.print("Battery power:");
  Serial.println(node.getResponseBuffer(0x06)/100.0f);
  Pbatt = node.getResponseBuffer(0x06)/100.0f;
  delay(1000);
  return PVvolt,PVCurr,PVPow,Vbatt,Abatt,Pbatt;
}
}
void setup() {
  Serial.begin(115200);
  /*
   * Definizione per comunicazione
   */
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 115200 baud
  Serial.begin(115200);

  // Modbus slave ID 1
  node.begin(1, Serial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
  
  #ifdef ESP8266
    configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
    client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
  #endif

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
  
  // Connect to Wi-Fi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  #ifdef ESP32
    client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  #endif
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());
}

void loop() {
  Dati();
  DatiPannelli();
  if (millis() > lastTimeBotRan + botRequestDelay)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages,PVvolt,PVCurr,PVPow,Vbatt,Abatt,Pbatt,MaxVbatt,MinVbatt,GenEnTod,GenEnMon,GenEnYea,GenEnTot);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }
    lastTimeBotRan = millis();
  }
}

So che le credenziali per Telegram sono sbagliate ma l'ho fatto apposta per non dare i codici a tutti...

Grazie mille a tutti per la disponibilità

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