[SOLVED] Need some advice on my project - ESP32 + Firebase

Hi all,
At this moment I'm using:

  • ESP32
  • BMP085
  • Photoresistor
  • HC-SR501

to send these data on firebase.
I would like to make the project more flexible and make it works with other users besides me.
I would like to implement a serial communication between Arduino UNO, that will receive the information to login (ex. User, pwd, WiFi credentials) via Bluetooth and also implement three buttons:

  1. To login the first time - to update the credentials
  2. To clear the data previously inserted
  3. To send or not send the data

This is the best idea it comes to my mind since as the program it's now:

#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>

//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"

//Libreries
#include <Adafruit_BMP085.h>
//#include "DHT.h"

/* 1. Define the WiFi credentials */
#define WIFI_SSID "***"
#define WIFI_PASSWORD "***"

/* 2. Define the API Key */
#define API_KEY "***"

/* 3. Define the RTDB URL */
#define DATABASE_URL "***" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app

/* 3. Define the project ID */
#define FIREBASE_PROJECT_ID "***"

/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "***"
#define USER_PASSWORD "***"

// Constants
#define DELAY 1000 // Delay between two measurements in ms
#define VIN 3.3 // V power voltage
#define R 10000 // Ohm resistance value
#define sensorPin 35 // Pin connected to photoresistor
//#define motionSensor 16

unsigned long sendDataPrevMillis = 0;

//Define Firebase Data object
FirebaseData fbdo;

FirebaseAuth auth;
FirebaseConfig config;

//Variables
int16_t sensorVal; // Analog value from the sensor
int16_t lux; //Lux value
int16_t count = 0;

//Objects
Adafruit_BMP085 bmp;
//DHT dht(DHT11_PIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  //pinMode(motionSensor, INPUT);
  //pinMode(DHT11_PIN, INPUT);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(300);
  }
  Serial.println();
  Serial.printf("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  config.api_key = API_KEY;

  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;

  config.database_url = DATABASE_URL;
  config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

#if defined(ESP8266)
  // In ESP8266 required for BearSSL rx/tx buffer for large data handle, increase Rx size as needed.
  fbdo.setBSSLBufferSize(2048 /* Rx buffer size in bytes from 512 - 16384 */, 2048 /* Tx buffer size in bytes from 512 - 16384 */);
#endif

  // Limit the size of response payload to be collected in FirebaseData
  fbdo.setResponseSize(2048);

  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);

  Firebase.setDoubleDigits(5);

  config.timeout.serverResponse = 10 * 1000;

  //dht.begin();
  bmp.begin();
}

void loop() {
  if (Firebase.ready() && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))// || digitalRead(motionSensor)== true
  {
    sendDataPrevMillis = millis();
    float temp = bmp.readTemperature();
    //float hum = dht.readHumidity();
    int pres = bmp.readPressure();
    float alt = bmp.readAltitude(101500);
    lux = sensorRawToPhys(analogRead(sensorPin));
    //bool motion = digitalRead(motionSensor);
    
    Firebase.RTDB.setFloat(&fbdo, "/Home/RealtimeDataStream/Temperature", temp);
    //Firebase.RTDB.setFloat(&fbdo, "/Home/RealtimeDataStream/Humidity", hum);
    Firebase.RTDB.setInt(&fbdo, "/Home/RealtimeDataStream/Pressure", pres);
    Firebase.RTDB.setFloat(&fbdo, "/Home/RealtimeDataStream/Altitude", alt);
    Firebase.RTDB.setInt(&fbdo, "/Home/RealtimeDataStream/Luminosity", lux);
    //Firebase.RTDB.setBool(&fbdo, "/Home/RealtimeDataStream/Motion", motion);
    
    Firebase.RTDB.setTimestamp(&fbdo, "/Home/timestamp");
    Firebase.RTDB.getDouble(&fbdo, "/Home/timestamp");
    /*Serial.println("Ho inserito temp:" + (String)temp);
    Serial.println("Ho inserito hum:" + (String)hum);
    Serial.println("Ho inserito lux:" + (String)lux);*/

    double timeStamp = fbdo.to<uint64_t>();
    //Serial.println("Nell'orario:" + (String)timeStamp);
    if (count == 10) //|| motion == true
    {
        FirebaseJson json;

        json.add("Temperature", temp);
        json.add("Pressure", pres);
        json.add("Altitude", alt);
        json.add("Luminosity", lux);
        json.add("timestamp", timeStamp);

        Serial.printf("Set json... %s\n", Firebase.RTDB.push(&fbdo, "/Home/DataCollection", &json) ? "ok" : fbdo.errorReason().c_str());
      }


      count = 0;
    }
    Serial.println("+" + (String)count);
    count++;
  }
}

int sensorRawToPhys(int raw)
{
  // Conversion rule
  float Vout = float(raw) * (VIN / float(4096));// Conversion analog to voltage
  float RLDR = (R * (VIN - Vout)) / Vout; // Conversion voltage to resistance
  int phys = 500 / (RLDR / 1000); // Conversion resitance to lumen
  return phys;
}

it already occupies about 80% of program storage space and I've no idea on how to optimize the memory usage on the ESP32 and this solution is the best I could think of.

I welcome any kind of advice: both software side and how to structure the project.
Thank you and best regards,
Federico

It might be interesting to use a board with a higher memory capacity, or some additional memory device. A Raspberry Pi maybe? Or maybe change the ESP32's FLASH memory?

Some libraries have a lot of unused code, it may be possible to comment out these parts of the libraries' code so that the compiler doesn't process these parts and leaves more resources free.

Thanks for the answer, changing the partition schema, something I didn't know could be done, everything changed. NOw with the same sketch I use only 33% of the program space. I had to give up on OTA updates but as it now it works fine for me

1 Like

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