How to check if EEPROM value is null

I have a struct as follows:

struct GpsStatus{
    public:
        int Day;//4
        int Month;//4
        int Year;//4
        bool IsGpsCollected;//1
        int Satellites;//4
        String Latitude;//8
        String Longitude;//8
};

Now I am saving object to EEPROM

            GpsStatus* gStatus3;
            gStatus3 = new GpsStatus();
            gStatus3->Day = Time.day();
            gStatus3->Month = Time.month();
            gStatus3->Year = Time.year();
            gStatus3->IsGpsCollected = true;
            gStatus3->Latitude = latitude;
            gStatus3->Longitude = longitude;
            gStatus3->Satellites = satellitesCount;
            EEPROM.put(33, gStatus3);

after I retrieve the object like this:

GpsStatus gStatus;
EEPROM.get(33, gStatus);

how do I check if gStatus is null?

if I use if(gStatus == NULL){ I get error no match for 'operator==' (operand types are 'GpsStatus' and 'int')

How would you expect a struct to be NULL?
Or are you rather looking for a way to check whether all fields of that struct are zero?

If so, why would you expect all bytes to be zero?

BTW, you cannot store String objects in EEPROM.

3 Likes

Here is trimmed down version of my firmware

struct GpsStatus{
    public:
        int Day;//4
        int Month;//4
        int Year;//4
        bool IsGpsCollected;//1
        int Satellites;//4
        int Latitude;//4
        int Longitude;//4
};

void setup() {
    
}

void loop() {
    GpsStatus gStatus;
    EEPROM.get(25, gStatus);
    
    GpsStatus* myptr = &gStatus;
    if(gStatus.Day == -1 && gStatus.Month == -1)
    {
        Particle.publish("found","no gps in memory, writing new values");
        GpsStatus* gStatus2;
        gStatus2 = new GpsStatus();
        gStatus2->Day = Time.day();
        gStatus2->Month = Time.month();
        gStatus2->Year = Time.year();
        gStatus2->IsGpsCollected = false;
        EEPROM.put(25, gStatus2);
        EEPROM.get(25, gStatus);
    }
    
    Particle.publish("Day", String(gStatus.Day));
    Particle.publish("Month", String(gStatus.Month));
    Particle.publish("Year", String(gStatus.Year));
    Particle.publish("IsCollected", String(gStatus.IsGpsCollected));
    delay(1000);
}

I wonder why I am not getting stored values in particle console

What are these lines for?
Why not just use gStatus for the EEPROM.put() call too?

Just as the ones in my previous post, the following questions are not merely rhetorical. They are asked in order to be answered - each one of them.

  • Do you actually get the found event in console?
  • What do you get in console?
  • Have you tried checking the current state of your gStatus fields when the condition is not met?
  • What makes you think the EEPROM area you are reading from must be empty (filled with 0xFF bytes)?

Also, when using new you should also call delete when done with the object.
When your if() condition is met your fifth Particle.publish() call will violate the rate limit.

BTW, bool is not 1 byte (as your comment in the struct definition suggests) but 4 bytes (on a 32bit system that is).

2 Likes