Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Saving to EEPROM

Saving to EEPROM

Scheduled Pinned Locked Moved Development
11 Posts 4 Posters 2.4k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    esawyja
    wrote on last edited by esawyja
    #1

    Hi all,
    Could someone please explain how to save a state to EEPROM, I have a hot water heater that I have to "dumb up", my problem is the saving off the last state, IE when there is a power interruption and the sensor reboots, how do I read the previous state and the previous LOW and HIGH temperature settings. For example I use these statements
    #define LowTempState 0
    #define HighTempState 0
    #define HeaterONOFF 0

    So the heater will not be switched on when you install it the 1st time, but then once the sensor is up and running, you would set the state and temperature from the controller and save that, but I want to prevent the heater from being ON and heating and causing damage when you install it for the 1st time. If I can explain more.. If this is a new installation, run the
    #define LowTempState 10
    #define HighTempState 20
    #define HeaterONOFF 0
    But once it is connected and running and the temperature and state is set from the controller, save those values and when there is a power failure and the sensor reboots, use the saved values to come up with.. I hope this makes sense?

    Is there some example code somewhere that I might check please...

    Regards

    mfalkviddM 1 Reply Last reply
    0
    • E esawyja

      Hi all,
      Could someone please explain how to save a state to EEPROM, I have a hot water heater that I have to "dumb up", my problem is the saving off the last state, IE when there is a power interruption and the sensor reboots, how do I read the previous state and the previous LOW and HIGH temperature settings. For example I use these statements
      #define LowTempState 0
      #define HighTempState 0
      #define HeaterONOFF 0

      So the heater will not be switched on when you install it the 1st time, but then once the sensor is up and running, you would set the state and temperature from the controller and save that, but I want to prevent the heater from being ON and heating and causing damage when you install it for the 1st time. If I can explain more.. If this is a new installation, run the
      #define LowTempState 10
      #define HighTempState 20
      #define HeaterONOFF 0
      But once it is connected and running and the temperature and state is set from the controller, save those values and when there is a power failure and the sensor reboots, use the saved values to come up with.. I hope this makes sense?

      Is there some example code somewhere that I might check please...

      Regards

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      @esawyja the relay example saves state. See if you can use that as template.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        esawyja
        wrote on last edited by
        #3

        Hi
        I think the relay example get the previous state from the EEPROM, I need to get the state from the controller after it was set for the 1st time from the controller?

        void before()
        {
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);
        // Set relay to last known state (using eeprom storage)
        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
        }

        An example with lots of comments that is being done will help a lot please

        Regards

        BartEB 1 Reply Last reply
        0
        • E esawyja

          Hi
          I think the relay example get the previous state from the EEPROM, I need to get the state from the controller after it was set for the 1st time from the controller?

          void before()
          {
          for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
          // Then set relay pins in output mode
          pinMode(pin, OUTPUT);
          // Set relay to last known state (using eeprom storage)
          digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
          }
          }

          An example with lots of comments that is being done will help a lot please

          Regards

          BartEB Offline
          BartEB Offline
          BartE
          Contest Winner
          wrote on last edited by
          #4

          @esawyja for getting a value from the controller one can use "request()", i.e. in the relay example like this:

          request(CHILD_ID,V_LIGHT);
          

          The answer should be handled by the receive function

          1 Reply Last reply
          0
          • E Offline
            E Offline
            esawyja
            wrote on last edited by
            #5

            Thanks all, I'll try to get this to work

            1 Reply Last reply
            0
            • E Offline
              E Offline
              esawyja
              wrote on last edited by
              #6

              Hi
              I'm struggling to get my head around this, when I do this

              #define LowTempState 0
              #define HighTempState 0
              #define HeaterONOFF 0

              Does this define the position in EEPROM, as per the documentation, so what I'm doing here is completely incorrect? What I need to do it to give it a value where to save with the #define statements?

              The documentation states
              void saveState(uint8_t pos, uint8_t value);

              pos - The position to store value in (0-255)
              value - Value to store in position

              So if I do this
              #define LowTempState 10 //Naming the position LowTempState at position 10 in EEPROM
              #define HighTempState 20
              #define HeaterONOFF 30
              I have to save the values to the EEPROM addresses with
              saveState(HighTempState, 40);
              saveState(LowTempState, 30);
              saveState(HeaterONOFF, 0);
              So the above with save the values 40, 30 and 0 to their positions 10, 20 and 30 in EEPROM?

              I have to read the state with
              int temHighTrigger=loadState(HighTempState);
              int temLowTrigger=loadState(LowTempState);
              int HeaterStatus=loadState(HeaterONOFF);
              So temHighTrigger will now contain 40? temLowTrigger will now contain 30 and the HeaterStatus will be 0?

              Retrieving a state (from local EEPROM).
              uint8_t loadState(uint8_t pos);

              Sorry for all the questions, my head is spinning....

              AWIA 1 Reply Last reply
              0
              • E esawyja

                Hi
                I'm struggling to get my head around this, when I do this

                #define LowTempState 0
                #define HighTempState 0
                #define HeaterONOFF 0

                Does this define the position in EEPROM, as per the documentation, so what I'm doing here is completely incorrect? What I need to do it to give it a value where to save with the #define statements?

                The documentation states
                void saveState(uint8_t pos, uint8_t value);

                pos - The position to store value in (0-255)
                value - Value to store in position

                So if I do this
                #define LowTempState 10 //Naming the position LowTempState at position 10 in EEPROM
                #define HighTempState 20
                #define HeaterONOFF 30
                I have to save the values to the EEPROM addresses with
                saveState(HighTempState, 40);
                saveState(LowTempState, 30);
                saveState(HeaterONOFF, 0);
                So the above with save the values 40, 30 and 0 to their positions 10, 20 and 30 in EEPROM?

                I have to read the state with
                int temHighTrigger=loadState(HighTempState);
                int temLowTrigger=loadState(LowTempState);
                int HeaterStatus=loadState(HeaterONOFF);
                So temHighTrigger will now contain 40? temLowTrigger will now contain 30 and the HeaterStatus will be 0?

                Retrieving a state (from local EEPROM).
                uint8_t loadState(uint8_t pos);

                Sorry for all the questions, my head is spinning....

                AWIA Offline
                AWIA Offline
                AWI
                Hero Member
                wrote on last edited by
                #7

                @esawyja You are correct with the your explanation. The EEPROM consists of 256 "byte" (8 bit) positions , best defined by the type uint_8_t. Be aware that an "int" (int8_t) type in the Arduino C++ compilers is 16 bits. So if you want to store a full int you need two EEPROM positions.

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  esawyja
                  wrote on last edited by
                  #8

                  Thanks @AWI
                  So for
                  #define LowTempState 10 //Naming the position LowTempState at position 10 in EEPROM
                  #define HighTempState 20
                  #define HeaterONOFF 30

                  I need to do something like this if it is possible?
                  #define LowTempState 10, 11 //Naming the position LowTempState at position 10 in EEPROM
                  #define HighTempState 20, 21
                  #define HeaterONOFF 30

                  Or do I just declare the trigger as 2 bytes with
                  uint_8_t temHighTrigger=loadState(HighTempState); //So this will read from position 10 for 16 bits?
                  uint_8_t temLowTrigger=loadState(LowTempState); //This one will start at 20 and read 21 as well?
                  int HeaterStatus=loadState(HeaterONOFF); //This one will read only position 30?

                  Regards

                  AWIA 1 Reply Last reply
                  0
                  • E esawyja

                    Thanks @AWI
                    So for
                    #define LowTempState 10 //Naming the position LowTempState at position 10 in EEPROM
                    #define HighTempState 20
                    #define HeaterONOFF 30

                    I need to do something like this if it is possible?
                    #define LowTempState 10, 11 //Naming the position LowTempState at position 10 in EEPROM
                    #define HighTempState 20, 21
                    #define HeaterONOFF 30

                    Or do I just declare the trigger as 2 bytes with
                    uint_8_t temHighTrigger=loadState(HighTempState); //So this will read from position 10 for 16 bits?
                    uint_8_t temLowTrigger=loadState(LowTempState); //This one will start at 20 and read 21 as well?
                    int HeaterStatus=loadState(HeaterONOFF); //This one will read only position 30?

                    Regards

                    AWIA Offline
                    AWIA Offline
                    AWI
                    Hero Member
                    wrote on last edited by
                    #9

                    @esawyja you are getting a little too creative here. I suggest you stick to using uint8_t (byte) types for all of your variables unless you need more range than 0..255. If you need more you need to split these types (int, long, float,... ) into bytes yourself and handle them separately.
                    If you need suggestions there are a few easy ways to accomplish this by means of using 'union'. Just let us know here.

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      esawyja
                      wrote on last edited by
                      #10

                      Ok now I'm more confused, but thanks, I will figure it out

                      AWIA 1 Reply Last reply
                      0
                      • E esawyja

                        Ok now I'm more confused, but thanks, I will figure it out

                        AWIA Offline
                        AWIA Offline
                        AWI
                        Hero Member
                        wrote on last edited by
                        #11

                        @esawyja sorry... Also for the first sentence? Use byte (uint8_t) for all variables you need to store..

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        7

                        Online

                        11.7k

                        Users

                        11.2k

                        Topics

                        113.1k

                        Posts


                        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • MySensors
                        • OpenHardware.io
                        • Categories
                        • Recent
                        • Tags
                        • Popular