CORE using EEPROM with DEEP SLEEP (RESOLVED)

I am quite new to Particle. I have 2 Core units. First time writing to community.particle.io
I am using Web IDE and ATO (Over The Air) to send my code to my unit.

I have developed a unit to monitor the soil moisture, working as I wished, however I want to change it to work on battery and used the DEEP SLEEP function to reduce power comsumption.

I need to store date between the DEEP SLEEP cycles so that I can publish only once a day.

I used EEPROM.write in one cycle (before going in deep sleep), but I cannot read the content on the second cycle when the unit wake up.

I would like to insert my code, but I don’t know how.

Hi Gaston!

You can paste your code into the editor window, and highlight it and click the </> button to format it as code. Alternatively, you could create a gist, and share that: https://gist.github.com

Thanks!
David

Here is my code

// Plant moisture monitoring
// by Gaston Paradis July 5, 2015
 
// This program will monitor the soil moisture of a plant and send an email when it need waterning
// Will use DEEP SLEEP to minimized  the usage of battery

#define MOISTPIN_1 A0                   // Pin where the soil moisture sensor is connected
#define READING_FREQUENCY  (1*10*1000)  // Frequency of reading the soil moisture
#define FLAG 1                          // EEPROM memory location to indicate if the minimum have been reached
#define DAY 2                           // EEPROM memory location containning the day the error message have been send

 struct  Content{                       // Create a structure to contain data for each sensor
     char name[15];
     char plant[15];
     int pin;                           // Pin where the soil moisture sensor is connected
     int reading;                       // Will store the reading of the soil moisture for sensor
     int minimum;                       // Level below what an alarm should be send for sensor
     int alarm_from;                    // Publishing should be done only after this hour
     int alarm_to;                      // Publishing should be done only until this hour

 };
 struct Content sensor = {"Sensor #2","Cactus",MOISTPIN_1,0,25,8,23};// Loading variable values
 unsigned long lastloop = 0;            // Use to control the next reading 
 char publishString [40];               // Use to publish to the register
 char publish_event[10];


void setup() {
    Time.zone(-4); // Set time zone to Eastern USA daylight saving time
    // DST Daily Saving Time Second Sunday March until First Sunday vember at 02:00
    //Time.zone(-4); // Set time zone to Eastern USA daylight saving time
    // DST Daily Saving Time Second Sunday March until First Sunday vember at 02:00
    sensor.reading = map(analogRead(sensor.pin), 0, 4100, 100, 0); // Initial reading
    delay(2000);        // temporaly to slow down things
    sprintf(publish_event,"Moisture");
    sprintf(publishString,"%u:%u:%u|||%i|||%i",Time.hour(),Time.minute(),Time.second(),sensor.reading,sensor.minimum);
    Spark.publish(publish_event,publishString);
    delay(2000);          // temporaly to slow down things for publication
    
    if(sensor.reading >= sensor.minimum){  // Flag to indicate that minimum has been reached
        EEPROM.write(FLAG, FALSE);  // The reading is above minimum  set the falg FALSE
        EEPROM.write(DAY,99);       // Make sure there is no date in the memory 
    }
    
    //Publish need water only once day 
    if ((sensor.reading < sensor.minimum)   // the moisture reading is below the minimum?
                                            // I wish to publish an alarm only within a determine time (not in the midle of the night)
    and ( Time.hour() >sensor.alarm_from)   // the current hour is more than the minimum hour to publish alarm
    and (Time.hour() <sensor.alarm_to)      // the current hour is less that the maximum hour to publish an alarm

        ) {
            Spark.publish("DEBUG below minimum"); 
               if(EEPROM.read(FLAG) == FALSE){ // The flag not being set: first time around MSG
                    Spark.publish("Need Water");
                    delay(2000);          // temporaly to slow down things for publication
                    Spark.publish("DEBUG Flag not set");
                    delay(2000);          // temporaly to slow down things for publication
                    EEPROM.write(FLAG, TRUE);   // Set the flag because the minimum has been reached
                    EEPROM.write(DAY, Time.day()); // Remember the date the msg was published
                 }   
                if(EEPROM.read(FLAG == TRUE)) { // We have been here before
                    if(EEPROM.read(DAY) != Time.day() ){ // Stored date is different than current day
                        Spark.publish("Need Water");
                        delay(2000);          // temporaly to slow down things for publication
                        //Spark.publish("DEBUG Flag set not same date");
                        EEPROM.write(DAY, Time.day()); // Remember the date the msg was published   
                    }
                    else{
                        Spark.publish("DEBUG Flag set, same date");
                        delay(2000);          // temporaly to slow down things for publication
                    }
                 } 
    }
    delay(20000);  // wait 20 second doing nothing to allow update OTA (Over The Air)
    Spark.sleep(SLEEP_MODE_DEEP,2); // Goto Deep Sleep for 2 seconds
}  
void loop() {
    // The main loop does nothing
    }

Did not work out as intended.
How do I remove my message?

I fixed it for you, edit to see how to apply that formatting :smile:

1 Like

On startup, if the sensor value is greater than the minimum, then your code will clear the eeprom value.

 if(sensor.reading >= sensor.minimum){  // Flag to indicate that minimum has been reached
        EEPROM.write(FLAG, FALSE);  // The reading is above minimum  set the falg FALSE
        EEPROM.write(DAY,99);       // Make sure there is no date in the memory 
    }

I don’t know the values your sensor is reporting, so I can only guess that’s why you’re not seeing any stored data.

The sensor reading being read is about 1 or 2, while my minimum is set to 25.
I can see the reading in the Particle Dashboar.

Ok. For debugging code like this, one trick is connecting the device to your computer and adding, Serial.println() statements in strategic locations, such as when writing to EEPROM. That will help you debug what’s going on.

(Add Serial.begin(9600); at the very start of your setup() method.)

Thank you mdma.
you helped me to find my mistake. I did not initialized the EEPROM memory, so it was something different than False or True.

Your suggestion to use Serial.println(), will it work from Web IDE?

1 Like

Yes, you can add Serial commands when compiling in the WebIDE too.