Pressure sensor - arduino low memory (SOLVED)



  • When I compile pressure sensor sketch I get below error in Arduino IDE.

    pressure.jpg

    I can add it to Vera but it only works for few hours (3-4) and then stops responding. I tried disabling DEBUG but that didn't help (it did reduce storage space).

    Is there anything else to do in order to reduce dynamic memory usage?


  • Contest Winner

    you can start by putting all of your string constants into program space instead of SRAM:

    use the F() macro and PROGMEM

    instead of this:

    char *weather[] = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
    

    and this:

    Serial.print("Temperature = ");
    

    try this:

    char *weather[] PROGMEM = {"stable","sunny","cloudy","unstable","thunderstorm","unknown"};
    

    and this:

    Serial.print(F("Temperature = "));
    

    what board are you compiling it for?



  • @BulldogLowell I will try your suggestions. It's for Nano.



  • @BulldogLowell can I change any other line with PROGMEM? After changing one you suggested I got 1% more.

    F() macro doesn't compile.

    press1.jpg


  • Contest Winner

    the F() macro does work.... what's your sketch look like now? is it exactly as is in the examples?

    for me that compiles to

    Binary sketch size: 24,452 bytes (of a 30,720 byte maximum)
    

    selecting the Nano with ATmega 328



  • @BulldogLowell only things modified now are: disabled DEBUG in Myconfig.h and replaced PROGMEM line you suggested earlier. If I try to use F() I get compiling error. This is on IDE 1.5.8 and Nano 328.

    Sketch uses 18,002 bytes. Global variables use 1616 bytes (78%). I still get low memory warning.



  • @BulldogLowell just noticed that if I don't use PROGMEM then F() macro compiles ok.



  • @BulldogLowell I used F() on all Serialprints("") and I got below sketch size. What is your dynamic memory usage?

    pressure.jpg


  • Contest Winner

    When I upgraded to the 1.5.8 version of the IDE I had a lot of problems with some of the libraries I used that were bundled withe the IDE. Ethernet library have me troubles too.
    I also noticed the same thing with F() and PROGMEM.

    I reverted my IDE and all is well now.

    Sorry I can't be more helpful...



  • @BulldogLowell So which IDE are you using now? I can try with the same version. What is your dynamic memory usage?


  • Hardware Contributor

    I had some problems using F() in some variables like Serial.println(F(somevariable))
    A good start is to change all Serial.println("Mytext") with Serial.println(F("Mytext"))

    Andreas


  • Contest Winner

    @sundberg84 said:

    I had some problems using F() in some variables like Serial.println(F(somevariable))
    A good start is to change all Serial.println("Mytext") with Serial.println(F("Mytext"))

    Andreas

    you had those problems because you cannot store a variable in flash (program space) only constants.

    the compiler will automatically hold numeric constants in flash, but string constants have to be so identified for that to happen. otherwise the consume space in SRAM

    variables initialized as const are also saved in program space:

    const char *dayOfWeek[] = { "Null","Sunday ","Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "};


  • I tried IDE 1.0.5 and codebender with same results - sensor stops responding after 3-4 hours. Is anyone running this sensor successfully?
    Upon searching for arduino board with more SRAM I found arduino micro. It has 2.5 KB SRAM (0.5 KB more than Nano). Would it work with Mysensors?


  • Admin

    You could try with some other Arduino library for the pressure sensor perhaps.



  • @hek sure, I can try. Do you have anything specific in mind?


  • Admin

    Did a quick google but I couldn't find anything good.

    How do you power your pressure sensor? It could be the radio that hangs after a potential power-spike.


  • Hardware Contributor

    Why not start with a more simple sketch?
    #include <SPI.h>
    #include <MySensor.h>
    #include <Wire.h>
    #include <Adafruit_BMP085.h>

        #define BARO_CHILD 0
        #define TEMP_CHILD 1
        
        unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in seconds)
        
        Adafruit_BMP085 bmp = Adafruit_BMP085();      // Digital Pressure Sensor 
        MySensor gw;
        
        float lastPressure = -1;
        float lastTemp = -1;
        boolean metric; 
        MyMessage tempMsg(TEMP_CHILD, V_TEMP);
        MyMessage pressureMsg(BARO_CHILD, V_PRESSURE);
        
        void setup() {
          gw.begin();
        
          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Pressure Sensor", "1.0");
        
          if (!bmp.begin()) {
            Serial.println("Could not find a valid BMP085 sensor, check wiring!");
            while (1) { }
          }
        
          // Register sensors to gw (they will be created as child devices)
          gw.present(BARO_CHILD, S_BARO);
          gw.present(TEMP_CHILD, S_TEMP);
          metric =  gw.getConfig().isMetric;
        }
        
        void loop() {
          float pressure = bmp.readPressure()/100;
          float temperature = bmp.readTemperature();
          
          if (!metric) {
            // Convert to fahrenheit
            temperature = temperature * 9.0 / 5.0 + 32.0;
          }
          
        //  int forecast = sample(pressure);
        
          Serial.print("Temperature = ");
          Serial.print(temperature);
          Serial.println(metric?" *C":" *F");
          Serial.print("Pressure = ");
          Serial.print(pressure);
          Serial.println(" Pa");
        
        
          if (temperature != lastTemp) {
            gw.send(tempMsg.set(temperature,1));
            lastTemp = temperature;
          }
        
          if (pressure != lastPressure) {
            gw.send(pressureMsg.set(pressure, 0));
            lastPressure = pressure;
          }
        
          gw.sleep(SLEEP_TIME);
        }


  • pressure sensor and radio are powered off Nano. There is capacitor on radio vcc / gnd pins. I will try simple sketch and report back.



  • @niccodemi and @hek : unfortunately there is a bug in the sketch for the pressuresensor.
    I have to go now but I will look up how I fixed this and report here in the evening!



  • @niccodemi:

    Here I am again.

    The problem is in the sample() function. It saves the pressure every minute in the array pressureSamples[]. This one is declared for 180 values which means you can use pressureSamples[0] to pressureSamples[179]. Writing to pressureSamples[180] means you are writing outside the defined array and might be writing over some other stuff in your program.
    In my case it resulted in radio messages which got status 'fail' all the time. First I thought it was a problem with the radio but after debugging I found the problem to be the sample() function. It always happened exactly 180 minutes after I restarted the Arduino...

    You have to change the following lines of code:

    if (minuteCount > 180)
      minuteCount = 6;
    

    into:

    if (minuteCount == 180)
      minuteCount = 5;
    

    The problem with the original code was that it wrote a value to pressureSamples[180] before it restarted with 6 again (which should be 5 according to me).

    When looking at the code I also found that the wrong samples seem to be used for the first 5 minutes. If minuteCount has reached the value of 5 it means that samples 0 to 4 has been filled. The code is using 1 to 5 in stead.

    To fix this, change the code under if (minuteCount == 5) into:

    pressureAvg[0] = ((pressureSamples[0] + pressureSamples[1]
    		+ pressureSamples[2] + pressureSamples[3] + pressureSamples[4])
    		/ 5);
    

    To be clear: this bug is not something @hek introduced, he just used the code supplied by the link in the sample()-function.

    Edit: But maybe he can make sure the sketch is updated so no one else will get the same problems? 🙂


  • Admin

    Thanks @MagKas!

    I've pushed an update to github.



  • @hek: great, but not completely correct...

    It should be: if (minuteCount == 180)

    Could you also consider to update the sketch with my proposal in my other comment for the pressuresensor-sketch regarding changing

    float pressure = bmp.readPressure()/100;
    

    to

    float pressure = bmp.readSealevelPressure(205)/100; // 205 meters above sealevel
    

    I assume most people would like to have the same pressure values as the local wheather station or airport has.


  • Admin

    @MagKas

    Ok, done!
    Also had to update to the latest library-version to get the readSealevelPressure()-function.



  • thanks, pressure sensors works ok with updated code (7 hours now).



  • Still this code is working?


Log in to reply
 

Suggested Topics

18
Online

11.2k
Users

11.1k
Topics

112.5k
Posts