ESP8266 library has maxed out SRAM and is causing instabilit

Hey There!

So my sketch (see below) takes data from a pressure sensor and flowmeter and uploads that data to Thingspeak using an ESP8266 wifi client. Unfortunately, when I completed the code, the dynamic memory usage was at 83%, creating the warning “Low memory available, stability problems may occur.” Indeed it has created instability in the form of improperly uploading data to Thingspeak. From commented out bits and pieces of the code, I found that 25% of the memory usage comes from calling the library (“#include <SparkFunESP8266WiFi.h>”), and another quarter of the memory goes to calling the line “ESP8266Client client;”

Is there any way I can reduce the amount of memory used by that library or just get the code to function normally? I know very little about C++ coding so I’m not sure if there’s anything in the library code I can change/delete.

Because of a looming deadline, an urgent answer would be greatly appreciated! Thank you very much!

#include <SparkFun_MS5803_I2C.h>
#include <Time.h>
#include <ThingSpeak.h>

// The SparkFunESP8266WiFi library uses SoftwareSerial
// to communicate with the ESP8266 module. Include that
// library first:
#include <SoftwareSerial.h>
// Include the ESP8266 AT library:
#include <SparkFunESP8266WiFi.h>

MS5803 sensor(ADDRESS_HIGH);


//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "IFH Loft";
const char myPSK[] = "ifhJanuary";

ESP8266Client client;

//////////////////////////
// Thingspeak Constants //
//////////////////////////
// Phant detsination server:
unsigned long myChannelNumber = 64341;
const char * myWriteAPIKey = "4JRIMA3GJF3PIQKE";

///////////////////
//Time Retrieval //
///////////////////

     
////////////////////////
//Variable Definitions//
////////////////////////
int Calc;
volatile int NbTopsFan;
unsigned long copyCount = 0;
unsigned long lastRead = 0;
unsigned long interval = 1000;
unsigned long thingspeakInterval = 16000;
unsigned long lastThingspeak = 0;
int hallsensor = 3;
unsigned long lastThingspeak2 = 0;
unsigned long thingspeakInterval2 = 40000;

float temperature_c, temperature_f;
double pressure_abs, pressure_relative, altitude_delta, pressure_baseline;
float atmPressure = 101300;
float depth; 
float rho = 1000.0; // units: kg/m^3
float g = 9.8; // units: m/s^2
float currentPressure;
float depthft;

double base_altitude = 1655.0; // Altitude of SparkFun's HQ in Boulder, CO. in (m)


const int redPin=5;
const int greenPin=10;
const int bluePin=6;

const int max_red=255;
const int max_green=255;
const int max_blue=255;

byte colors[3]={0, 0, 0};
byte lineEnding = 0x0A;
byte redVal=0;
byte greenVal=0;
byte blueVal=0;

void rpm(){
  NbTopsFan++;
}

void setup() 
{
  pinMode (hallsensor, INPUT);
  pinMode (redPin, OUTPUT);
  pinMode (greenPin, OUTPUT);
  pinMode (bluePin, OUTPUT);
  attachInterrupt(1, rpm, RISING);
  
  int status;
  Serial.begin(9600);
  
  // To turn the MG2639 shield on, and verify communication
  // always begin a sketch by calling cell.begin().
  status = esp8266.begin();
  if (status <= 0)
  {
    Serial.println(F("Unable to communicate with shield. Looping"));
    while(1) ;
  }
  
  esp8266.setMode(ESP8266_MODE_STA); // Set WiFi mode to station
  if (esp8266.status() <= 0) // If we're not already connected
  {
    if (esp8266.connect(mySSID, myPSK) < 0)
    {
      Serial.println(F("Error connecting"));
      while (1) ;
    }    
  }
  
  // Get our assigned IP address and print it:
  Serial.print(F("My IP address is: "));
  Serial.println(esp8266.localIP());

  sensor.reset();
  sensor.begin();

  pressure_baseline = sensor.getPressure(ADC_4096);

  //ThingSpeak.begin(client);
}

void loop()
{

  if (millis() - lastRead >= interval){
    lastRead +=interval;
    //disable interrupts, make copy of count, reenable interrupts
    noInterrupts();
    copyCount = NbTopsFan;
    NbTopsFan = 0;
    interrupts();
    Calc = (copyCount * 60 / 12);
    //Serial.println(Calc);
    //Serial.println(hour());
  }

  pressure_abs = sensor.getPressure(ADC_4096);
  currentPressure = pressure_abs * 100;
  depth = (currentPressure - atmPressure)/(rho * g); //this is in meters!
  depthft = depth * 3.28;
  
  if (millis() - lastThingspeak >= thingspeakInterval){
    ThingSpeak.writeField(myChannelNumber, 2, depthft, myWriteAPIKey);
    lastThingspeak = millis();
  }
  
  Serial.print("Flow: ");
  Serial.println(Calc);
  //LEDlights();  
  Serial.print("Depth: ");
  Serial.println(depthft);
  Serial.print("Pressure: ");
  Serial.println(pressure_abs);
  delay(1000);
  lights();
}

void lights() {
  if (depthft <= 1){
    if (Calc == 0){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 128);
      analogWrite(bluePin, 0);
    }
    if (Calc > 0 && Calc <=8){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 153);
      analogWrite(bluePin, 51); 
    }
    if (Calc > 8 && Calc <= 16){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 178);
      analogWrite(bluePin, 102);
    }
    if (Calc > 16){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 204);
      analogWrite(bluePin, 153);
    }
  }
  if (depthft > 1 && depthft <= 2){
    if (Calc == 0){
      analogWrite(redPin, 0);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 0);
    }
    if (Calc > 0 && Calc <=8){
      analogWrite(redPin, 51);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 51); 
    }
    if (Calc > 8 && Calc <= 16){
      analogWrite(redPin, 102);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 102);
    }
    if (Calc > 16){
      analogWrite(redPin, 153);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 153);
    }
  }
  if (depthft > 2 && depthft <= 3){
    if (Calc == 0){
      analogWrite(redPin, 0);
      analogWrite(greenPin, 0);
      analogWrite(bluePin, 255);
    }
    if (Calc > 0 && Calc <=8){
      analogWrite(redPin, 51);
      analogWrite(greenPin, 51);
      analogWrite(bluePin, 255); 
    }
    if (Calc > 8 && Calc <= 16){
      analogWrite(redPin, 102);
      analogWrite(greenPin, 102);
      analogWrite(bluePin, 255);
    }
    if (Calc > 16){
      analogWrite(redPin, 153);
      analogWrite(greenPin, 153);
      analogWrite(bluePin, 255);
    }
  }
  if (depthft > 3 && depthft <= 4){
    if (Calc == 0){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 0);
      analogWrite(bluePin, 127);
    }
    if (Calc > 0 && Calc <=8){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 51);
      analogWrite(bluePin, 153); 
    }
    if (Calc > 8 && Calc <= 16){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 102);
      analogWrite(bluePin, 178);
    }
    if (Calc > 16){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 153);
      analogWrite(bluePin, 204);
    }
  }
  if (depthft > 4){
    if (Calc == 0){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 0);
    }
    if (Calc > 0 && Calc <=8){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 51); 
    }
    if (Calc > 8 && Calc <= 16){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 102);
    }
    if (Calc > 16){
      analogWrite(redPin, 255);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 153);
    }
  }
}

https://learn.adafruit.com/memories-of- … izing-sram

https://www.arduino.cc/en/Tutorial/Memory

You can also try use another library besides Sparkfun’s.

You could have also have solved this issue if you used Google.

Thanks for the reply. I did see those pages but I didn’t really understand some of it. According to Adafruit, one thing to do is reduce the buffer sizes so that it’s no bigger than it needs to be. How do I know how big it needs to be? Apart from that, I’m not sure I understand how to use PROGMEM, but from what I understand, it doesn’t seem like it will improve the capacity that significantly in my case because I don’t have large blocks of data.

Also, I couldn’t find any other libraries for the shield…Do you have any suggestions?

Thank you!

If you avoid using floating point number calculations then you can prevent that library from being incorporated into your code. You may not have ordered it to be included, but the compiler does it automatically if it is needed. Since your sensors likely deliver integer values anyway you could keep intermediate calculations and the end result that way by doing multiplication before divisions and using proper multiplication factors to simulate binary fractions.

can you make the size (in bytes) of your datum smaller? Usually you can.

Or save a list of averages not every data point.