mobizt / Firebase-ESP32

[DEPRECATED]🔥 Firebase RTDB Arduino Library for ESP32. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
413 stars 119 forks source link

Guru Meditation Error with version 3.10.0 or newer #173

Closed floatAsNeeded closed 3 years ago

floatAsNeeded commented 3 years ago

Hi!

I just found myself in this problem recently when updating to a version of the library from 3.10.0 or newer. It basically reset the board with the following error:

Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
19:46:57.839 -> Core 1 register dump:
19:46:57.839 -> PC      : 0x4000c2e4  PS      : 0x00060e30  A0      : 0x8018f5dc  A1      : 0x3ffb1e30  
19:46:57.839 -> A2      : 0x00000000  A3      : 0x3ffccc08  A4      : 0x00000024  A5      : 0x00000000  
19:46:57.839 -> A6      : 0x74616577  A7      : 0x2d726568  A8      : 0x00000000  A9      : 0x3ffb1e10  
19:46:57.839 -> A10     : 0x3ffccb1c  A11     : 0x400874d4  A12     : 0x00000000  A13     : 0x3f423a6f  
19:46:57.839 -> A14     : 0x00000024  A15     : 0x3ffccc08  SAR     : 0x0000000a  EXCCAUSE: 0x0000001d  
19:46:57.839 -> EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000001  
19:46:57.839 -> 
19:46:57.839 -> ELF file SHA256: 0000000000000000
19:46:57.839 -> 
19:46:57.839 -> Backtrace: 0x4000c2e4:0x3ffb1e30 0x4018f5d9:0x3ffb1e40 0x4018f668:0x3ffb1e60 0x4018f707:0x3ffb1e80 0x400d5602:0x3ffb1eb0 0x400d576d:0x3ffb1f50 0x400d2381:0x3ffb1f70 0x400f767a:0x3ffb1fb0 0x4008a652:0x3ffb1fd0
19:46:57.839 -> 
19:46:57.839 -> Rebooting...

This is my code I'm using


`///////////////////////////////LIBRARY DECLARATION////////////////////////////////////////
#include <WiFi.h>
#include <FirebaseESP32.h>
#include "ClosedCube_SHT31D.h"
#include "Adafruit_VEML6075.h"
#include "DFRobot_SHT20.h"
#include "LC709203F.h"
#include "Max44009.h"
#include <WiFiManager.h>         // https://github.com/tzapu/WiFiManager
#include <Wire.h>
WiFiManager wifiManager;

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

///BATTERY OBJECT//
LC709203F gg;  // create a gas gauge object.

//DECLARATION SENSORS//
ClosedCube_SHT31D sht3xd;
DFRobot_SHT20    sht20;

//LIGHT SENSOR DEFINE//
Adafruit_VEML6075 uv = Adafruit_VEML6075();
Max44009 myLux(0x4A);

//I2C COMUNICATION ADDRESS//
const int I2CSlaveAddress = 8;      // I2C Address.

////////////////////*********FIREBASE DETAILS************///////////////////////////////////
#define FIREBASE_HOST ""                 // the project name address from firebase id
#define FIREBASE_AUTH ""            // the secret key generated from firebase
FirebaseData Weather;

//WIND DIRECTION//
#define WindDirectionDavis 32 // anemometer
#define WindDirectionAnemometer 33 // anemometer

////////////RAIN////////////////
float mmGoccia; // tipping bucket count in mm
float mmPioggia = 0.0; // daily rain
RTC_DATA_ATTR float mmPioggia24H = 0;
RTC_DATA_ATTR float rainrate = 0; // real-time rainrate
RTC_DATA_ATTR float rainrateMax = 0; // daily rainrateMax
byte rainratedetect = 0;
int rainrateMaxIntensity = 0;
byte PluvioFlag = 0; // detect interrupt of rain
byte rainrateI2C = 0;
byte rainrateMaxI2C = 0;
byte rainrateMaxI2CIntensity = 0;

////////////WIND/////////////////
byte Rotations;
byte GustRotations;
unsigned int average = 3000; // sample time for wind speed
float constant; // formula calculation
float WindSpeed; // speed km/h
float GustNow = 0;
int VaneValue; // raw analog value from wind vane
int Direction; // translated 0 - 360 direction
int CalDirection; // converted value with offset applied
byte AnemometerType;

/////TIMING VARIALABLES//////
unsigned long timeout; // waiting for WiFi connection
byte CurrentDay; // current day to reset max and min values
RTC_DATA_ATTR byte PreviousDay; // previous day to reset max and min values
bool res; // WiFi Status
unsigned int sampletime;
RTC_DATA_ATTR byte INITDATA = 0;
byte ReadByte = 0;
RTC_DATA_ATTR byte Alternate = 0;
RTC_DATA_ATTR boolean justrestart = true;

//////////TEMPERATURE///////////
//SHT2X//
float tempSHT2x; // temperature in centigrade
float temp_f; // temperature in fahrenheit
int humiditySHT2x; // humidity variable

//SHT3X//
float temperatureSHT3x; // temperature in centigrade
int humiditySHT3x; // humidity variable

//////////////UV/////////////////
float UVindex; // variable UVindex

void InitSensors()
{
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);
  sht20.checkSHT20();
  sht3xd.begin(0x44);
  sht3xd.readSerialNumber();
  if (!gg.begin()) {
    while (1) delay(10);
  }
  gg.setCellCapacity(LC709203F_APA_3000MAH);
  //gg.setAlarmVoltage(3.4);
  gg.setCellProfile( LC709203_NOM3p7_Charge4p2 ) ;
  uv.begin();
  uv.setIntegrationTime(VEML6075_100MS);
  uv.setHighDynamic(false);
  uv.setForcedMode(false);
  uv.setCoefficients(2.22, 1.17,  // UVA_A and UVA_B coefficients
                     2.95, 1.58,  // UVB_C and UVB_D coefficients
                     0.004770, 0.006135); // UVA and UVB responses

  myLux.setAutomaticMode();
}

void initData()
{
  if (INITDATA == 0)
  {
    if (Firebase.getFloat(Weather, "/Rain/Rain24H"))
    {
      mmPioggia24H = Weather.floatData();
    }
    else
    {
      //Serial.println(Weather.errorReason());
    }
    if (Firebase.getFloat(Weather, "/Rain/RainRateMax"))
    {
      rainrateMax = Weather.floatData();
    }
    if (Firebase.getInt(Weather, "Time/PreviousDay"))
    {
      PreviousDay = Weather.intData();
    }
    INITDATA = 1;
  }
  if (Firebase.getInt(Weather, "Time/SampleTime"))
  {
    sampletime = Weather.intData();
  }
  if (sampletime == 0)
  {
    sampletime = 90;
  }
  if (Firebase.getInt(Weather, "Wind/Anemometer"))
  {
    AnemometerType = Weather.intData();
  }
  if (Firebase.getFloat(Weather, "/Rain/mmGoccia"))
  {
    mmGoccia = Weather.floatData();
  }
  PluvioFlag = readTiny(I2CSlaveAddress);
  Rotations = readTiny(I2CSlaveAddress);
  GustRotations = readTiny(I2CSlaveAddress);
  rainrateI2C = readTiny(I2CSlaveAddress);
  rainrateMaxI2C = readTiny(I2CSlaveAddress);
  rainrateMaxI2CIntensity = readTiny(I2CSlaveAddress);
}

void setup()
{
  Wire.begin(); //SDA , SCL
  //Serial.begin(115200);
  InitSensors();
  WiFi.mode(WIFI_STA);
  wifiManager.setConfigPortalTimeout(300);
  WiFi.begin();
  timeout = millis();
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    //Serial.print(".");
    if (millis() - timeout > 25000)
    {
      res = wifiManager.autoConnect("LineaMeteoStazioneS", "LaMeteo2005");

      if (!res) {
        esp_sleep_enable_timer_wakeup(60 * 1000000LL);
        esp_deep_sleep_start();
      }
    }
  }
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);   // connect to firebase
  Firebase.setMaxRetry(Weather, 2);
  initData();
}

void loop()
{
  readRain ();
  if (AnemometerType == 1)
  {
    readDavisAnemometer ();
  }
  else if (AnemometerType == 2)
  {
    readAnemometer ();
  }
  ReadSensors();
  WriteData();
  CheckDay();
  uv.setForcedMode(true);
  esp_sleep_enable_timer_wakeup(sampletime * 1000000LL);
  esp_deep_sleep_start();
}

byte readTiny(int address) {
  byte rawRead;
  switch (ReadByte) {
    case 0:
      Wire.requestFrom(address, 1);                  // The TinyWire library only allows for one byte to be requested at a time
      while (Wire.available() == 0)  Serial.print("W");
      rawRead = Wire.read();
      return rawRead;
      ReadByte = 1;
      break;
    case 1:
      Wire.requestFrom(address, 1);                  // The TinyWire library only allows for one byte to be requested at a time
      while (Wire.available() == 0)  Serial.print("W");
      rawRead = Wire.read();
      return rawRead;
      ReadByte = 2;
      break;
    case 2:
      Wire.requestFrom(address, 1);                  // The TinyWire library only allows for one byte to be requested at a time
      while (Wire.available() == 0)  Serial.print("W");
      rawRead = Wire.read();
      return rawRead;
      ReadByte = 3;
      break;
    case 3:
      Wire.requestFrom(address, 1);                  // The TinyWire library only allows for one byte to be requested at a time
      while (Wire.available() == 0)  Serial.print("W");
      rawRead = Wire.read();
      return rawRead;
      ReadByte = 4;
      break;
    case 4:
      Wire.requestFrom(address, 1);                  // The TinyWire library only allows for one byte to be requested at a time
      while (Wire.available() == 0)  Serial.print("W");
      rawRead = Wire.read();
      return rawRead;
      ReadByte = 5;
      break;
    case 5:
      Wire.requestFrom(address, 1);                  // The TinyWire library only allows for one byte to be requested at a time
      while (Wire.available() == 0)  Serial.print("W");
      rawRead = Wire.read();
      return rawRead;
      ReadByte = 0;
      break;
  }
}

//RAIN//
void readRain () {
  mmPioggia = (mmPioggia + mmGoccia) * PluvioFlag;
  mmPioggia24H = mmPioggia24H + mmPioggia;
  rainrate = (rainrateI2C / 0.3) * mmGoccia;
  rainrateMaxIntensity = rainrateMaxI2CIntensity * 10;

  if (rainrateMaxI2C > (rainrateMax / mmGoccia) * 0.3)
  {
    rainrateMax = (rainrateMaxI2C / 0.3) * mmGoccia;
    rainratedetect = 1;
  }
  if (rainrateMaxIntensity > (rainrateMax / mmGoccia) * 0.3)
  {
    rainrateMax = (rainrateMaxIntensity / 0.3) * mmGoccia;
    rainratedetect = 1;
  }
}

//WIND//
void readDavisAnemometer () {
  //FORMULA V=P(Rotations)(2.25/T)--->constant
  constant = 2.25 / (average / 1000);
  WindSpeed = (Rotations * constant) * 1.60934;
  GustNow = (GustRotations * constant) * 1.60934;

  VaneValue = analogRead(WindDirectionDavis);
  Direction = map(VaneValue, 0, 4095, 0, 360);
  CalDirection = Direction;
}

//WIND//
void readAnemometer () {
  WindSpeed = (Rotations * 2448.0) / average;
  GustNow = (GustRotations * 2448.0) / average;

  VaneValue = analogRead(WindDirectionAnemometer);

  if (VaneValue < 250)
  {
    CalDirection = 90;
  }
  if (VaneValue > 250 && VaneValue < 600)
  {
    CalDirection = 135;
  }
  if (VaneValue > 600 && VaneValue < 1100)
  {
    CalDirection = 180;
  }
  if (VaneValue > 1100 && VaneValue < 2000)
  {
    CalDirection = 45;
  }
  if (VaneValue > 2000 && VaneValue < 2500 )
  {
    CalDirection = 225;
  }
  if (VaneValue > 2500 && VaneValue < 3100)
  {
    CalDirection = 0;
  }
  if (VaneValue > 3100 && VaneValue < 3650)
  {
    CalDirection = 315;
  }
  if (VaneValue > 3650 && VaneValue < 4095)
  {
    CalDirection = 270;
  }
}

//READ SHT3X//
void printResult(String text, SHT31D result) {
  if (result.error == SHT3XD_NO_ERROR) {
    temperatureSHT3x = result.t;
    humiditySHT3x = result.rh;
  } else {
    //Serial.print(text);
    //Serial.print(": [ERROR] Code #");
    //Serial.println(result.error);
  }
}

void ReadSensors()
{
  humiditySHT2x = sht20.readHumidity();              // Read Humidity
  tempSHT2x = sht20.readTemperature();               // Read Temperature
  printResult("Pooling Mode", sht3xd.readTempAndHumidity(SHT3XD_REPEATABILITY_HIGH, SHT3XD_MODE_POLLING, 50));
}

void WriteData()
{
  Alternate++;
  if (justrestart == true)
  {
    justrestart = false;
    Firebase.setIntAsync(Weather, "Connection/WiFiSignaldb",   WiFi.RSSI());
    Firebase.setFloatAsync(Weather, "Battery/CellVoltage", gg.cellVoltage_mV() / 1000.0);
    Firebase.setIntAsync(Weather, "Battery/CellPercentage", gg.cellRemainingPercent10() / 10);
  }
  if (Alternate == 5)
  {
    justrestart = true;
    Alternate = 0;
  }
  Firebase.setFloatAsync(Weather, "SHT2x/Temperature/Temperature", tempSHT2x);
  Firebase.setIntAsync(Weather, "SHT2x/Humidity/Humidity", humiditySHT2x);
  Firebase.setFloatAsync(Weather, "SHT3x/Temperature/Temperature", temperatureSHT3x);
  Firebase.setIntAsync(Weather, "SHT3x/Humidity/Humidity", humiditySHT3x);
  Firebase.setFloatAsync(Weather, "Rain/RainRate", rainrate);
  Firebase.setFloatAsync(Weather, "Wind/WindSpeed", WindSpeed);
  Firebase.setIntAsync(Weather, "Wind/WindDirection", CalDirection);
  Firebase.setFloatAsync(Weather, "Wind/Gust", GustNow);
  UVindex = uv.readUVI();
  if (UVindex < 0)
  {
    Firebase.setFloatAsync(Weather, "Light/UVindex", 0);
  }
  else
  {
    Firebase.setFloatAsync(Weather, "Light/UVindex", UVindex);
  }
  Firebase.setFloatAsync(Weather, "Light/Lux", myLux.getLux());

  if (PluvioFlag > 0)
  {
    Firebase.setFloatAsync(Weather, "Rain/Rain24H", mmPioggia24H);
  }
  if (rainratedetect == 1)
  {
    Firebase.setFloatAsync(Weather, "Rain/RainRateMax", rainrateMax);
    rainratedetect = 0;
  }
}

void CheckDay()
{
  if (Firebase.getInt(Weather, "Time/CurrentDay"))
  {
    CurrentDay = Weather.intData();
  }

  if (CurrentDay != PreviousDay)
  {
    PreviousDay = CurrentDay;
    mmPioggia24H = 0;
    rainrateMax = 0;
    Firebase.setIntAsync(Weather, "Time/PreviousDay",  PreviousDay);
    Firebase.setFloatAsync(Weather, "Rain/Rain24H", mmPioggia24H);
    Firebase.setFloatAsync(Weather, "Rain/RainRateMax", rainrateMax);
  }
}

I Know I use third party library as well, but I don't think that is the issue because it always worked with previous versions of the library. Deleted firebase secret for privacy.

IDE and its version:

ESP32 Arduino Core SDK version

mobizt commented 3 years ago

Your code can't reproduce.

You should remove all third party libraries from the code.

Share your clean, clear and minimal code instead of integration code.

Otherwise you should debug your code first and address where in the code that causes the error and what's your expected result and what's the error you get instead of crash that possibilities of crash are unclear and not related directly to the library.

mobizt commented 3 years ago

You need to update the library to the latest version, post your clean code without third party libs which everyone can test and reproduce the error.

floatAsNeeded commented 3 years ago

Hi

Thanks for the respond! I tried the latest version already, the issue came after the 3.10.0 as stated, I didn't try to debug because everything was working always, but just after updating from 3.9.7 the issue came up, that's why I thought it was related to the library, but it could be a mix of things. I will do the test without any library and let you know!

Also, another thing, the reboot happens just immediately after the board restarts

mobizt commented 3 years ago

Now I'm testing something that possible causes the crash with legacy initialize function (begin using Firebase host and database secret).

I will inform you soon.

mobizt commented 3 years ago

Now the update, v3.10.4 is available with this bug fixed.

Please update the library.

floatAsNeeded commented 3 years ago

Thank you very much! It is now working well! The only thing I noticed is that sometimes if I use the setAsync it doens't actually update the data if I update many datas at once. Why you think that is happening? Is it okay to use it on float values?

mobizt commented 3 years ago

The async in this library used is for store only and is not actually async request unless the response was ignored (no wait) and continue to send other requests which Firebase supports concurrent request like this.

The session reused and closed by server or by library (every 3 minutes and when get was called). Sometimes server closed the connection by itself, and due to no response was checked then your database was not changed.

mobizt commented 3 years ago

You can check the connection by calling httpConnected() from Firebase Data object to determine the request of async function was sent or not.

floatAsNeeded commented 3 years ago

Sorry for late reply! Thank you very much for the info