Search code examples
esp32arduino-ide

Interfacing ESP32-C3-MINI-1 using Arduino IDE


I am doing a project wherein I need to store device states. Essentially I have a momentary button (that will act as a toggle) to control which LED will illuminate. However, I also need to send the state of the device to an app. I was thinking that the best way to do this is using a counter variable and the preferences.h library. The counter will count how many times the button has been pressed and depending on that number (will either be a 1 or 2), it will tell either LED1 or LED2 to turn on. Then with the preferences library, I would store the counter and communicate the number with the app (both states need to be communicated).

I am having trouble with the code for this. Any help is greatly appreciated. Thanks!

I have gotten the button to act as a toggle, but I don't know how to implement the counter with the preferences library with this. I also do have a base code that I am able to send pictures of if needed.


Solution

  • It's pretty simple, here is a simple explanation of how to use the preferences.h on an ESP32

    //include the library first
    #include <Preferences.h>
    Preferences myPrefs;
    
    //in the setup
    void setup(){
        .....
        
        //start the preferences within a "status" namespace, if the namespace doesn't exists it will be created
       // it doesn't have to be called "status", can be anything "myApp", "log","settings"....
       // once the namespace is created you can add keys with values to it
    
        myPrefs.begin("status", false);
    
    }
    

    anywhere else you can read the keys and change them, you can use a ton of different types of values, here I have only used an integer type so the commands are:

    int something =  myPrefs.getInt("keyname",0) //read integer value and if it doesn't exists set the value to 0 (zero)
    
    myPrefs.putInt("keyname", 1) //save this key to have value 1 it can be 2, 1000, 476529 etc... 
    

    nad Herę is a simple code that "remembers" the button clicks , on the first click it will light up led 1, on the second it will turn off led 1 and light up led 2 and on the third click it will turn both legs off. It can be written more efficiently of course but should give you an idea.

    #include <Arduino.h>
    
    #include <Preferences.h>
    Preferences myPrefs;
    
    //change the pins to whatever you use
    const int button_pin = 5;
    const int led_1_pin = 6;
    const int led_2_pin = 7;
    
    int led_state = 0;
    
    void setup(){
    
        Serial.begin(115200);
    
        myPrefs.begin("status", false); //start the preferences and if don't exists create one
    
        pinMode(button_pin,INPUT_PULLUP);
        pinMode(led_1_pin,OUTPUT);
        pinMode(led_2_pin,OUTPUT);
    
         led_state = myPrefs.getInt("led_state", 0); //read the saved state if there is one or set it to zero
    
        //set the led to the saved status
        if(led_state == 0){
            digitalWrite(led_1_pin,LOW);
            digitalWrite(led_2_pin,LOW);
        }else if(led_state == 1){
            digitalWrite(led_1_pin,HIGH);
        }else if(led_state == 2){
            digitalWrite(led_1_pin,HIGH);
        }
    
    }
    
    
    void loop(){
    
    
        if(digitalRead(button_pin) == LOW){ //button is pressed
            led_state++;
    
            //when the button is pressed after the Led 2 was on both leds will go off
            //change that to whatever logic you want
            if(led_state == 3){
                led_state = 0;
            }
    
            
            if(led_state == 0){
                digitalWrite(led_1_pin,LOW);
                digitalWrite(led_2_pin,LOW);
               
            }else if(led_state == 1){
                digitalWrite(led_1_pin,HIGH);
                digitalWrite(led_2_pin,LOW);
               
                
            }else if(led_state == 2){
                digitalWrite(led_1_pin,LOW);
                digitalWrite(led_2_pin,HIGH);
               
            }
    
            myPrefs.putInt("led_state", led_state); //save the current state to preferences
    
        }
    
    
    }
    

    Hope this helps