Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Problema reset collegamento casuale

Progetti, interfacciamento, discussioni varie su questa piattaforma.

Moderatori: Foto UtenteMassimoB, Foto Utentexyz, Foto UtenteWALTERmwp

0
voti

[1] Problema reset collegamento casuale

Messaggioda Foto Utenteapollo17 » 22 set 2023, 15:16

Salve nello sketch, versione senza BME funziona benissimo da mesi con gestione PIR per LCD.
Ho aggiunto, ancora da definire le stampe alterne, il sensore BOSCH BME 680. Ho valori corretti sia per data e ora sia per BME,ovvio sempre 4 righe ho, quindi fatti più modifiche di spostamento.
Ma da questa unione è nato un problema, in modo aleatorio, con pir attivo e quindi lcd acceso,ho un reset casuale e un ritorno alla connessione Wait.

Chi procura tale anomalia è il primo IF nel loop della BME, importante per la visualizzazione dei dati.
if (! bme.performReading()) {
//Serial.println("Failed to perform reading :(");
MySerial.Stop_Session();
}

Ho problema a creare un secondo loop per tali poche righe, presumo vada fatto questo. Posso avere più indicazioni possibile? Grazie



Codice: Seleziona tutto
/*
   NTP PIR con BSE680  BME  su WEMOS D1R2  -

2023
settembre

  */



#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include <LibSerial.h>              // Libreria seriale
#include <ESP8266WiFi.h>            // per famiglia esp8266
#include <NTPClient.h>              // Libreria NTPCLIENT
#include <WiFiUdp.h>                // Libreria WiFiUdp
#include <LiquidCrystal_I2C.h>      // Libreria I2C LCD

#define Pin_Blink 2                   //   pin 4   di wemos
#define LED_SOLAR 1                  // -----> gestione ora legale
#define SEALEVELPRESSURE_HPA (1013.25)  // per b

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,20,4) for 20x4 LCD.


//     GESTIONE PIR     Prima parte-----------------------------------------
#define SegnalePIR D0               // PIN del Sensore PIR    out pir
#define BluLed D5                   // PIN del Led Blu presenza

bool flag_lcd        = false;       // Logica PIR
bool flag_presenza   = false;
uint32_t currentTime = 0x00L;
uint32_t lastMotionTime = 0;


//      TIMER  ==============================================
const uint32_t durata_display = 450L * 1000L; // più elegante 1800
String formattedDate;               // Data + tempo
String dayStamp;
String timeStamp;
String formattedTime;               // solo tempo
//Week Days
String weekDays[7]={"Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"};
//Month names
String months[12]={"Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago",
                    "Set", "Ott", "Nov", "Dic"};

// Impostazione diritti di accesso
const char* ssid       = "xxx";
const char* password    = "yyy";


Adafruit_BME680 bme;             // I2C
LiquidCrystal_I2C MyLcd(0x27, 20, 4);
LibSerial MySerial;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "time.inrim.it");

String Exit_Error(byte Codice_Errore) {
    switch (Codice_Errore) {
        case 0:  return "IDLE_STATUS" ;
        case 1:  return "NO_SSID_AVAILABLE";
        case 2:  return "SCAN_COMPLETED";
        case 3:  return "CONNECTED";
        case 4:  return "CONNECT_FAILED";
        case 5:  return "CONNECTION_LOST";
        case 6:  return "DISCONNECTED";
        default: return "CODE UNKNOWN";
    }
}

void setup() {
    // Setting time start
    uint32_t ST_TM_OUT= millis() / 1000;
    uint32_t END_TM_OUT = 0x00L;
    uint32_t Time_Out = 20;
    int Cod_Err;                    // errore status
    int  Cod_TM;                    // Time_out
    String MesError;
    digitalWrite(Pin_Blink,HIGH);
    pinMode( BluLed, OUTPUT );      // Inizializzo il pin del LED
    pinMode( SegnalePIR, INPUT );   // Inizializzo il pin del PIR Sensor
    digitalWrite( BluLed, LOW );    // Spengo inizialmente il LED
    pinMode (LED_SOLAR,INPUT_PULLUP);
    MySerial.Start_Serial(115200,"NTP CLOCK START",true);
    MyLcd.init();
    MyLcd.backlight();
    MyLcd.setCursor(4,0);
    MyLcd.print("WAIT");     // INIZIO CONNESSIONE
    MySerial.Write_Message("Connecting to --> ",true,false);
    MySerial.Write_Message(ssid,false,true);
    MySerial.Write_Message("Blink durante la connessione",false,true);
    WiFi.begin(ssid, password);        // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
          digitalWrite(Pin_Blink,HIGH);           // Blink for wait
          delay(500);                                            // time 500 ms
          digitalWrite(Pin_Blink,LOW);
          delay(500);
          END_TM_OUT = (millis() /1000)  - ST_TM_OUT;
          // Si connette entro Time_Out ??
          if (END_TM_OUT > Time_Out) {
              digitalWrite(Pin_Blink,HIGH);
              Cod_Err = WiFi.status();
              MesError = Exit_Error(Cod_Err);
              MySerial.Write_Other("WIFI status: ",Cod_Err,false,false,true);
              MySerial.Write_Message(MesError,false,true);
              MySerial.Write_Message("Or Time_out",false,true);
      //        MySerial.Stop_Session();
              while (1)
                      ESP.wdtFeed();
          }
    }

    // SE ARRIVA  QUESTO PUNTO LA CONNESSIONE E' OK
    MySerial.Write_Message("WIFI CONNECTED! IP --> ",true,false);
    String LP = WiFi.localIP().toString();
    MySerial.Write_Message(LP,false,true);
    MyLcd.setCursor(1,1);
    MyLcd.print(WiFi.localIP());
    delay(2000);                    // ritardo per visualizzazione
    MyLcd.setCursor(0,0);
    MyLcd.clear();
    timeClient.begin();             // start client
    if (digitalRead(LED_SOLAR) == HIGH)
        timeClient.setTimeOffset(3600);
    else                            // ora solare (7200 per legale)   
        timeClient.setTimeOffset(7200);
  // AGGIONAMENTO ORA ========================================
  // Setta intervallo di aggiornamento ntp a 5 min
  // timeClient.setUpdateInterval(300000);
  // Setta intervallo di aggiornamento ntp a 1 min
  //  ========================================================================
  //timeClient.setUpdateInterval(60000); //


 
  if (!bme.begin()) {
   Serial.println("Could not find a valid BME680 sensor, check wiring!");
   while (1);
  }

  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms
}


 



//  GESTIONE orologio -pir =============================================================

void loop () {
{
  if (! bme.performReading()) {
  //Serial.println("Failed to perform reading :(");
  MySerial.Stop_Session();
  }
 
    while(!timeClient.update())
            timeClient.forceUpdate();
    time_t epochTime = timeClient.getEpochTime();
    formattedTime = timeClient.getFormattedTime();
    //formattedDate = timeClient.getFormattedDate();
    int splitT = formattedDate.indexOf("T");
    dayStamp = formattedDate.substring(0, splitT);
    MySerial.Write_Message(formattedTime,false,true);
    MySerial.Write_Message(formattedDate,false,true);
    MySerial.Write_Message(dayStamp,false,true);
    String weekDay = weekDays[timeClient.getDay()];
    MySerial.Write_Message("Week Day: ",false,false);
    MySerial.Write_Message(weekDay,false,true);
    //Get a time structure per ricostruire la data
    struct tm *ptm = gmtime ((time_t *)&epochTime);
    // Giorno
    int monthDay = ptm->tm_mday;
    // Mese
    int currentMonth = ptm->tm_mon+1;
    String currentMonthName = months[currentMonth-1];
    // Anno
    int Anno = ptm->tm_year+1900;
    MySerial.Write_Other("Month day: ",monthDay,false,false,true);
    MySerial.Write_Other("Month: ",currentMonth,false,false,true);
    MySerial.Write_Message("Month name: ",false,false);
    MySerial.Write_Message(currentMonthName,false,true);

//                                                       GRUPPO PRIMA STAMPA    durata tre minuti  ==============================


    MyLcd.setCursor(3,0);           // CURSORE   DATA
   
    MyLcd.print(weekDay);
    MyLcd.print(" ");
    MyLcd.print(monthDay);
   MyLcd.print(" ");
    MyLcd.print(currentMonthName);
   MyLcd.print(" ");
   MyLcd.print(Anno);
    MyLcd.setCursor(6,2);           // cursor a c3/r1
   MyLcd.print(formattedTime);     // Orario
//                                                       GRUPPO SECONDA  STAMPA    durata 1  minuto  ==============================

   // Temperatura °C
// MyLcd.setCursor(0, 3); // Set the cursor on the third column and first row.
//MyLcd.print ("Temp");
// MyLcd.setCursor(6, 3);
  //MyLcd.print((char)223);
// MyLcd.setCursor(7, 3);
  //MyLcd.print ("C");
// MyLcd.setCursor(10, 3);
// MyLcd.print(bme.temperature - 0.88 );
// Umididità %
  //MyLcd.setCursor(0, 1);
  //MyLcd.print ("Umid   %");   
  //MyLcd.setCursor(10, 1);
  //MyLcd.print(bme.humidity);

  // Pressione hpa
MyLcd.setCursor(0, 1); // Set the cursor on the third column and first row.
  MyLcd.print ("P");
  MyLcd.setCursor(2,1);
MyLcd.print(bme.pressure / 100.0);
// Altezza slm
  //MyLcd.setCursor(10, 2);
  //MyLcd.print ("Alt");   
  //MyLcd.setCursor(14, 2);
  //MyLcd.print(bme.readAltitude(SEALEVELPRESSURE_HPA));  // ogni unità = 7,5 m

  //7 resistenza aria
  MyLcd.setCursor(0, 3);
  MyLcd.print ("Aria Q KOhms");   
  MyLcd.setCursor(14, 3);
  MyLcd.print (bme.gas_resistance / 1000.0);



    // Leggo lo stato del sensore
    bool PIR_Output = digitalRead(SegnalePIR);
    // Accendo o spengo il LED a seconda
    digitalWrite ( BluLed, PIR_Output );
    // se rileva o NON rileva movimenti
    // Se display spento lo accende e si segna il momento che lo ha fatto
    if (PIR_Output)
        flag_presenza   = true;
    if (flag_presenza) {
        if (!flag_lcd) {
            flag_lcd = true;
            lastMotionTime = millis();
            MyLcd.display();        // Accendi il display
            MyLcd.setBacklight(HIGH);
        }
    }
    currentTime = millis();
    if (currentTime - lastMotionTime >= durata_display) {
        // Passati 15 minuti spengo il display se acceso
        if (flag_lcd) {
            MyLcd.noDisplay();
            flag_lcd = false;
            flag_presenza = false;
            // spengo retroill
            MyLcd.setBacklight(LOW);
            MyLcd.noDisplay();      // spengo display
        }
    }
    else
       // Tempo non scaduto aggiorno il display se acceso
       if (flag_lcd) MyLcd.setBacklight(HIGH);
       }
       }

// ==========================================================================

Avatar utente
Foto Utenteapollo17
81 1 2 6
Stabilizzato
Stabilizzato
 
Messaggi: 303
Iscritto il: 30 lug 2017, 11:59

0
voti

[2] Re: Problema reset collegamento casuale

Messaggioda Foto Utenteapollo17 » 25 set 2023, 16:07

Risolto ma non riesco a capire bene il perché. Mi spiego : ho semplicemente sdoppiato sda e scl. , prima sia LCD che BME 680 in parallelo entrambi collegato su unico sda e scl standard della scheda. Adesso ho staccato bme e collegato il suo sda a sda/d2 del wemos e idem per scl spostato su Scl/d1. Domando : 1) quindi pin sda e scl sono fisicamente e logicamente diversi da sda /d1 e scl/ d2? Quale è la spiegazione che adesso va bene? Grazie
Avatar utente
Foto Utenteapollo17
81 1 2 6
Stabilizzato
Stabilizzato
 
Messaggi: 303
Iscritto il: 30 lug 2017, 11:59


Torna a Arduino

Chi c’è in linea

Visitano il forum: Nessuno e 4 ospiti