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);
}
}
}