Error reading EEPROM

I want to use EEPROM to store an INT that i can use after a reboot.

I have this code in place to store the value. That generates no errors.

int addr = 10; 
uint16_t tempSet;
EEPROM.put(addr, tempSet);

But when i want to retrieve the value with

int addr = 10;
uint16_t tempSet;
EEPROM.get(addr, tempSet);
if(tempSet == 0xFFFF) {
  tempSet = 10;  // EEPROM was empty -> initialize value
}

I get this error.

worst.ino:19:7: expected constructor, destructor, or type conversion before β€˜.’ token error
worst.ino:20:1: expected unqualified-id before β€˜if’

Anybody an idea what i’m doing wrong? I thought i followed the example well.
https://docs.particle.io/reference/device-os/firmware/photon/#eeprom

Can you also post the context of these instructions?

This error message may suggest that you are calling these functions in the declaration section and not from within a function.

Sure.



// Settings
int maxTempDiff = 1; // value should not be zero! worst should not get x degrees hotter or colder 
int fanSpeed = 100; //  value between 0% and 100%

int addr = 10;
uint16_t tempSet;
EEPROM.get(addr, tempSet);
if(tempSet == 0xFFFF) {
  tempSet = 10;  // EEPROM was empty -> initialize value
}

//int tempSet = 10; // temperature setting

const long valuesInterval = 1000; // interval for getting values // 1000 is ok
const long publishInterval = 30000; // interval for publishing values // 30000 for production and 5000 for debugging

// libs
#include <Adafruit_DHT.h> // DHT library
#define DHTPIN 7 // pin of DHT sensor for internal temp en humidity measurement
#define DHTTYPE DHT11 // sensor type
DHT dht(DHTPIN, DHTTYPE); 

#include <DS18B20.h>
const int      MAXRETRY          = 4;
DS18B20  ds18b20(D6, true); //Sets Pin D6 for external temp sensor

// Variables
int extTemp;
int intTemp; // to store the internal temperature
int intHumi; // to store the internal humidity
int worstHumi; // to store humidity of the worst
int relayCooling = D0; // relay for controling the cooling of the worstkast
int relayHeating = D1; // relay for controlling the heating the worstkast
int climaStatus; // to store the status of the climate controls, is the heating (1) or cooling (2) activated
int enviroStatus = 0; // to store the status of the enverimont, is the outside of the worstkast cooler (1) or hotter (2) then the desired temperature 
unsigned long previousMillisPublishInterval = 0; // remember the time 
unsigned long previousMillisValuesInterval = 0; // remember the time 

void setup() {
    Serial.begin(115200);
    dht.begin(); // Start DHT sensor
    pinMode(D0, OUTPUT); //relay1
    pinMode(D1, OUTPUT); //relay2
    pinMode(D6, INPUT); //external temperature sensor
    pinMode(D7, INPUT); //internal temperature / humidity sensor
    pinMode(A0, OUTPUT); //external fan
    pinMode(A1, INPUT); // humidity sensor
    Particle.function("setTemp",setTemp);
    Particle.function("setFan",setFan);
}

void loop() {
    unsigned long currentMillis = millis(); // what time is it?
   
    if (currentMillis - previousMillisValuesInterval >= valuesInterval) {
    previousMillisValuesInterval = currentMillis;
    getValues(); // get all values from the worstkast sensors
    }  
   
    if (currentMillis - previousMillisPublishInterval >= publishInterval) {
    previousMillisPublishInterval = currentMillis;
    publishValues(); // publish to console and dashboard
    }
    
    climaControl(); // regulate the climate in the worstkast
    delay(10);        // delay 10 ms in between reads for stability
}

int setFan (String value) { // function to call from Losant to control the fan
    fanSpeed = value.toInt();
    return 1;
}

int setTemp (String value) { // if a new temp is set from the dashboard 
    tempSet = value.toInt();
    
    // Write the tempSet to an EEPROM address
    int addr = 10; 
    uint16_t tempSet;
    EEPROM.put(addr, tempSet);
    
    return 1;
}

void getValues () { // read the values of the DHT sensor
    intTemp = dht.getTempCelcius(); // internal temperature measurement
    intHumi = dht.getHumidity(); // internal humidity measurement
    worstHumi = map (analogRead(A1),4096,500,0,100); // worst humidity measurement (4096 = 0% humi and 500 100% humi) and mapping to percentage
    getTemp();
}

void publishValues () { // publish the values to the losant dashboard https://app.losant.com/dashboards/5c45c1eac853d20008cd9f4d
  // sprintf(extTemp, "%2.0f", extTemp);
    Particle.publish("worst_closet_data", String(intTemp) + ":" + String(intHumi) + ":" + String(tempSet) + ":" + String(fanSpeed)
    + ":" + String(climaStatus) + ":" + String(enviroStatus) + ":" + String(worstHumi) + ":" + String(extTemp));
}

void climaControl () { // climaControl routine
    int fanWrite = map(fanSpeed, 0, 100, 0, 255); // transform from percentage (0-100) to value we can write to fan (0-255)
    analogWrite (A0, (fanSpeed)); // set fan to speed x
    
    int maxTemp = tempSet + maxTempDiff ; // calculate the minimum temperature based on the set temp. and the max allowed deviation 
    int minTemp = tempSet - maxTempDiff ; // calculate the maximum temperature based on the set temp. and the max allowed deviation 
    
    if (extTemp < minTemp) { // if enverimont is colder then minimum temp that worstkast has to be warmed to
        enviroStatus = 1; 
        if (intTemp >= maxTemp) { // if hot enough
        digitalWrite(relayHeating, LOW); // stop heating
        climaStatus = 0;
        }
        else if (intTemp <= minTemp) { // if to cold 
        digitalWrite(relayHeating, HIGH); //start heating
        climaStatus = 1;
        }
    } 
    
    else if (extTemp > maxTemp) { // if enverimont is hotter then maximum temp that the worstkast has to be cooled to
        enviroStatus = 2; 
        if (intTemp <= minTemp) { // if cold enough
        digitalWrite(relayCooling, LOW); // stop cooling
        climaStatus = 0;
        }
        else if (intTemp >= maxTemp) { // if to hot
        digitalWrite(relayCooling, HIGH); //start cooling
        climaStatus = 2;
        }
    }
    else if (extTemp = tempSet) {
           enviroStatus = 0; 
    }
}

void getTemp(){ // for reading external temp sensor
  float _temp;
  int   i = 0;

  do {
    _temp = ds18b20.getTemperature();
  } while (!ds18b20.crcCheck() && MAXRETRY > i++);

  if (i < MAXRETRY) {
    extTemp = _temp;
  }
  else {
    extTemp = NAN;
  }
}

As I expected.

That code can’t be placed outside of a function - only delcarations/definitions are allowed there.

When you put

  EEPROM.get(addr, tempSet);
  if(tempSet == 0xFFFF) {
    tempSet = 10;  // EEPROM was empty -> initialize value
  }

into setup() the error will go away.

6 Likes

That was an easy fix :slight_smile: Thx for the help!

1 Like