Store data in an array on the Electron

Hi,

I’m using an electron and I would like to read 4 times a sensor per hour and store inside the Electron. After 4 times, send the 4 readings to the server.

Anybody knows how is possible to do that?

Thank’s in advance

The actual question is: “Why should it not work?”

What have you tried?

Hi ScruffR,

May be I did not explain correctly. My question is, if somebody did it and if somebody could show me an example…because I have not idea how to store data and how to extract to publish later.

Thank’s

What exactly are you intending to do with your devices?
Will you send it to deep sleep or only stop mode sleep between measurements?
Will there be a risk of battery loss?
How important will the last readings be in case of battery loss?

The answer to these fundamental questions greatly impact the way to go

Hi

Sorry about my poor explanation.

1º The electron wakes up
2º Read the sensors
3º Deep sleep till 15 minutes

Now I’m publishing after every read process, the problem what I have is the batt life. Maybe if I read, store and publish every hour not every quarter maybe the batt will be up more time

See attached the code

void setup() {


if ((Time.hour() >= 05) && (Time.hour()  <= 22) && (Time.weekday() != 1))
            {
            
            Serial.begin(9600);
            digitalWrite(D4, HIGH);
            
            //Reading Temperature Fresh
            sensor.read();
            double tempeatureFresh = sensor.celsius();
            digitalWrite(D4, LOW);
                
            delay(4000);
            
            //Reading Temperature Frozen
            digitalWrite(D5, HIGH);
            sensor2.read();
            double temperatureFrozen = sensor2.celsius();
            digitalWrite(D5, LOW);
            
            delay(2000);
            
            //Reading battery level
            FuelGauge fuel;
            float batt;
            batt = fuel.getVCell();
            
            
            //Publishing Json to Particle
            sprintf(publishString,"{\"tempeatureFresh\": %.2f, \"temperatureFrozen\": %.2f, \"batt\": %.2f}",tempeatureFresh,temperatureFrozen,batt);
            Particle.publish("UlaTracker",publishString);
            
            // Sleep for 15 minutes to save battery
            System.sleep(SLEEP_MODE_DEEP, 15 * 60);
              //*********************************
            }
    else 

    if ((Time.hour() >= 0) && (Time.hour()  <= 5) )    
    {
    int tWake = 5*3600 + 0*60 + 0;
    int tNow1 = Time.local() % 86400;
    int secondwakeup1= tWake - tNow1;
    //Serial.println("entre 00 y 05");
    System.sleep(SLEEP_MODE_DEEP, secondwakeup1);
    }
    else
    {
    int tend = 86400;
    int tNow2 = Time.local() % 86400;
    int secondwakeup2= (tend - tNow2)+18000;
    //Serial.println("entre 22 y 24");
    System.sleep(SLEEP_MODE_DEEP, secondwakeup2);
    }

OK.
First, you seem to be missing pinMode() for your pins.

And to store your values across deep sleeps, you’d most comfortably use retained variables

STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));

retained double values[4] = { 0.0, 0.0, 0.0, 0.0 };

After that you can just store the values in the array which will stay unchanged as long the device can keep the Backup RAM powered.

1 Like