How to store common (secret) values across programs?

I need to store various secrets (same for all my sketches), and having to build a secrets file for each sketch is fairly inconvenient (let alone reliable).

Is there a way I can have a "shared secrets" file, that I can reuse for all my various sketches?
e.g. info I'd want to store:
WIFI credentials
Home assistant IP
MQTT credentials

Hi @noegros. You can create a custom library to share this data between your sketches.

Since many libraries are very complex, this might sound like a daunting task, but it is actually possible to make libraries that are as minimal as a single header file. I'll provide instructions:

  1. Select File > Preferences from the Arduino IDE menus.
    The "Preferences" dialog will open.
  2. Take note of the path shown in the "Sketchbook location" field of the "Preferences" dialog.
  3. Click the "CANCEL" button.
  4. Create a new folder under the libraries subfolder of the path from the "Sketchbook location" preference. The folder name should not contain any spaces or other special characters.
  5. Create a file with the .h file extension under the new folder you created. The filename should not contain any spaces or other special characters.
  6. Open the .h file you created in any text editor.
  7. Add the code to define the shared secret values, just as you would do with a secrets file in a sketch.
  8. Save the file.
  9. Add #include directives for the .h file you created to the sketches that need access to the shared secret values.

Here is an example of how the library installation structure might look:

<sketchbook location>
├── libraries/
│   └── MySharedSecrets/
│       └── MySharedSecrets.h
...

(where <sketchbook location> is the path from the "Sketchbook location" preference.

You would then add this to the sketches:

#include <MySharedSecrets.h>
2 Likes

Thanks!!

You are welcome. I'm glad if I was able to be of assistance.

Regards,
Per

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.