[solved] Sensor freezes - Low memory available, stability problems may occur.
Hi,
I have a temp/hum/baro/voltage sensor (BME280) with signing (ATSHA204) and debug and I am running on a low memory.
Sketch uses 31158 bytes (96%) of program storage space. Maximum is 32256 bytes. Global variables use 1660 bytes (81%) of dynamic memory, leaving 388 bytes for local variables. Maximum is 2048 bytes. Low memory available, stability problems may occur.
This is causing the sensor to freeze after a few hours.
I have improved the sketch as much as I could. Can anyone help me to improve the sketch further please???
//ID of node #define MY_NODE_ID 6 //#define MY_PARENT_NODE_ID 0 // Enable debug prints to serial monitor #define MY_DEBUG #define MY_DEBUG_VERBOSE_SIGNING // Enable and select radio type attached #define MY_RADIO_NRF24 #define MY_SIGNING_ATSHA204 //#define MY_SIGNING_REQUEST_SIGNATURES #define MY_SIGNING_ATSHA204_PIN 17 #define MY_OTA_FIRMWARE_FEATURE #include <SPI.h> #include <MySensors.h> #include <Wire.h> #include <Adafruit_BME280.h> #include <Adafruit_Sensor.h> //#include <avr/power.h> #define BARO_CHILD 0 #define TEMP_CHILD 1 #define HUM_CHILD 2 #define BATT_SENSOR 3 #define RELEASE "1.1" const float ALTITUDE = 7; // <-- adapt this value to your own location's altitude. // Sleep time between reads (in seconds). Do not change this value as the forecast algorithm needs a sample every minute. const unsigned long SLEEP_TIME = 300000; const unsigned long FORCE_TRANSMIT_INTERVAL = SLEEP_TIME;ME280 bme; // I2C //Adafruit_BMP280 bme = Adafruit_BMP280(); // Digital Pressure Sensor // Pins #define LED_PIN 5 //8 float lastPressure = -1; float lastTemp = -1; int lastForecast = -1; float lastHum = ); MyMessage forecastMsg(BARO_CHILD, V_FORECAST); MyMessage msgBatt(BATT_SENSOR, V_VOLTAGE); // Global settings int measureCount = 0; int sendBattery = 0; boolean isMetric = true; boolean highfreq = true; // Storage of old measurements float lastTemperature = -100; int lastHumidity = -100; long lastBattery = -100; void setup() { pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); Serial.begin(115200); Serial.print(F("BME280 Pressure Sensor")); Serial.print(RELEASE); Serial.flush(); digitalWrite(LED_PIN, HIGH); if (!bme.begin()) { Serial.println(F("Could not find a valid BME280 sensor, check wiring!")); while (1) {} } digitalWrite(LED_PIN, LOW); metric = getControllerConfig().isMetric; Serial.print(F("isMetric: ")); Serial.println(isMetric); sendBattLevel(false); } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("High Precision Pressure Sensor", RELEASE); // Register sensors to gw (they will be created as child devices) present(BARO_CHILD, S_BARO); present(TEMP_CHILD, S_TEMP); present(HUM_CHILD, S_HUM); present(BATT_SENSOR, S_POWER); } void loop() { measureCount ++; sendBattery ++; bool forceTransmit = false; if ((measureCount == 5) && highfreq) { clock_prescale_set(clock_div_8); // Switch to 1Mhz for the reminder of the sketch, save power. highfreq = false; } if (measureCount > FORCE_TRANSMIT_INTERVAL) { // force a transmission forceTransmit = true; measureCount = 0; } float pressure = bme.readPressure() / 100.0F; float temperature = bme.readTemperature(); float humidity = bme.readHumidity(); if (!metric) { // Convert to fahrenheit temperature = temperature * 9.0 / 5.0 + 32.0; } int forecast = sample(pressure); Serial.print(F("Temperature = ")); Serial.print(temperature); Serial.println(metric ? " *C" : " *F"); Serial.print(F("Humidity = ")); Serial.print(humidity); Serial.println(metric ? " %" : " *F"); Serial.print(F("Pressure = ")); Serial.print(pressure); Serial.println(F(" hPa")); Serial.print(F("Forecast = ")); Serial.println(weather[forecast]); if (temperature != lastTemp) { send(tempMsg.set(temperature, 1)); lastTemp = temperature; } if (humidity != lastHum) { send(humMsg.set(humidity, 1)); lastHum = humidity; } if (pressure != lastPressure) { send(pressureMsg.set(pressure, 0)); lastPressure = pressure; } if (forecast != lastForecast) { send(forecastMsg.set(weather[forecast])); lastForecast = forecast; } digitalWrite(LED_PIN, HIGH); delay(50); digitalWrite(LED_PIN, LOW); if (sendBattery > 60) { sendBattLevel(forceTransmit); // Not needed to send battery info that often sendBattery = 0; };) * CONVERSION_FACTOR;) * CONVERSION_FACTOR;) * CONVERSION_FACTOR;) * CONVERSION_FACTOR;) * CONVERSION_FACTOR;. }; } /******************************************** Sends battery information (battery percentage) Parameters - force : Forces transmission of a value *******************************************/ void sendBattLevel(bool force) { if (force) lastBattery = -1; long vcc = readVcc(); if (vcc != lastBattery) { lastBattery = vcc; #ifdef BATT_SENSOR float siVolt = vcc / 1000.0; send(msgBatt.set(siVolt, 2)); #endif // Calculate percentage vcc = vcc - 1800; // subtract 1.9V from vcc, as this is the lowest voltage we will operate at long percent = vcc / 14.0; sendBatteryLevel(percent); } } /******************************************* Internal battery ADC measuring *******************************************/__) ADcdM }
Minor tweaks can be done, but nothing that should make a big difference. Cramming software signing and debug and externals libraries is probably just too much for your mcu. Maybe look at using STM32 or an ESP? Or disable debugging, but I guess you need that for something?
@mfalkvidd Disabling debugging does not help - I still get Low memory available, stability problems may occur.
@alexsh1 strange. It works for me.
Before disabling debug:
Sketch uses 34,854 bytes (113%) of program storage space. Maximum is 30,720 bytes. Global variables use 1,671 bytes (81%) of dynamic memory, leaving 377 bytes for local variables. Maximum is 2,048 bytes.
After disabling debug:
Sketch uses 25,638 bytes (83%) of program storage space. Maximum is 30,720 bytes. Global variables use 1,298 bytes (63%) of dynamic memory, leaving 750 bytes for local variables. Maximum is 2,048 bytes
I am using these versions of the libraries:
Using library SPI at version 1.0 Using library MySensors at version 2.1.1 Using library Wire at version 1.0 Using library Adafruit_BME280_Library at version 1.0.5 Using library Adafruit_Unified_Sensor at version 1.0.2
@mfalkvidd Thanks. I have the same libraries. I understand what the problem is now.
#define MY_DEBUG
and
#define MY_DEBUG_VERBOSE_SIGNING
are enabled in MyConfig.h
I disabled MY_DEBUG_VERBOSE_SIGNING in MyConfig.h and
put an extra line #define MY_DISABLED_SERIAL in the sketch - this undefines MY_DEBUG in MyConfig.h though this is not required as MY_DEBUG_VERBOSE_SIGNING would do it.
Sketch uses 29708 bytes (92%) of program storage space. Maximum is 32256 bytes. Global variables use 1360 bytes (66%) of dynamic memory, leaving 688 bytes for local variables. Maximum is 2048 bytes.```
A friend of mine told me that atmel studio is more efficient at compiling than the arduino ide, I haven't tested it yet but maybe somebody could confirm.
@gohan Well could be, but it is too late to teach an old dog a new trick in my case. I am comfortable with Arduino IDE - I can flash atmega, ESP, SAMD etc.
If that was the case, you could use the atmel studio just for sw upload. Myself I'm using visual studio preferably, it makes it faster to write code but for quick modification I still use the arduino ide
- scalz Hardware Contributor last edited by scalz
Another possible optimization, is to dig into the sensor libs you're using, and disable/remove/comment etc.. things you don't need. i'm pretty sure in BME280 there are stuff you don't need in some functions (just quick looked).
When using multiple libs and sensors, you can save a lot like this. Libs are there for providing us lot of features, but not necessarily needed in your final fw.
Shouldn't the compiler exclude all functions that aren't used in the code?
@gohan no, the compiler compiles whatever you ask to compile. It's your duty to "improve" your code by excluding stuff you are not using
Back in the days when I was studying, compilers used to have code optimizers built in... Oh well... Never mind
@scalz I'm with you, but just like you I just do not have time for it. There is Teensy 3.2 with excellent footprint and 10 times speed and memory. So I just feel like if I'm going to spend time, it has to be not yesterday's technology.
Just for info, that's what I found on arduino forum
Eighteen Hints to Reduce Code Size
- Compile with full size optimization.
- Use local variables whenever possible.
- Use the smallest applicable data type. Use unsigned if applicable.
- If a non-local variable is only referenced within one function, it should be declared static.
- Collect non-local data in structures whenever natural. This increases the possibility of indirect addressing without pointer reload.
- Use pointers with offset or declare structures to access memory mapped I/O.
- Use for(;;) { } for eternal loops.
- Use do { } while(expression) if applicable.
- Use descending loop counters and pre-decrement if applicable.
- Access I/O memory directly (i.e., do not use pointers).
- Declare main as C_task if not called from anywhere in the program.
- Use macros instead of functions for tasks that generates less than 2-3 lines assembly code.
- Reduce the size of the Interrupt Vector segment (INTVEC) to what is actually needed by the application. Alternatively, concatenate all the CODE segments into one declaration and it will be done automatically.
- Code reuse is intra-modular. Collect several functions in one module (i.e., in one file) to increase code reuse factor.
- In some cases, full speed optimization results in lower code size than full size optimization. Compile on a module by module basis to investigate what gives the best result.
- Optimize C_startup to not initialize unused segments (i.e., IDATA0 or IDATA1 if all variables are tiny or small).
- If possible, avoid calling functions from inside the interrupt routine.
- Use the smallest possible memory model.
A lot of debug code in this sketch has not been enclosed between #ifdef MYDEBUG/#endif blocks. Even if you disable MYDEBUG, most of the Serial.print() lines remain 'active'.
Try to comment most of the Serial.print() code or add the #ifdef statements to have the compiler ignore these Serial.print() lines.
Most memory in this sketch is eaten up by the strings ("xxx").
@ftw64 since the strings are wrapped in F() they actually do not use (global) ram. When using F(), the strings are only stored in flash.
So while adding #ifdef around the print statements will save flash, it will not save ram.
@mfalkvidd Oh, cool. I missed that (and I didn't know that, and I learned something today :-)). Yep, in that case it wouldn't help much.