Separate configuration file for Arduino sketch?

Hi all,

Is it possible to put configuration options for an Arduino sketch in a separate file, which could be excluded from the code repository?

For example, I have a 'secret' key which the Arduino uses to interface with a web service - I'd like that to be out of the main code, and only included at compile time. Then I could put the source in a public repository without worrying about revealing that key.

Cheers,

Tom

Tom, you can create a seperate header file that is included by the source file at compile time to hold your key.

In your source file:
#include "keyFileName.h" // this is the name (with optional path if not in the same directory) of the header file with the key

Your header file:
// keyFileName.h

#define key 12345678 // if your key is a string it needs to be in quotes. Note no semicolon after a hash define

Usage:

#include “keyFileName.h”

if (value == key){
do something if value equals key

if key is a string:

if (strcmp(value,key) == 0){
do something if value equals key

That's great, just what I'm looking for. Thanks a lot!

I'm having a bit of trouble implementing this. I have a file in the same directory as my PDE file, called configuration.h, which contains the following:

const char secret[] = "redacted";
const char hostname[] = "redacted";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 100, 160 };
byte gateway[] = { 192, 168, 100, 1 };
byte server[] = { 1, 2, 3, 4 };

Then I include this with

#include "configuration.h" on the 3rd line of my PDE file.

When I compile, I get the following error:

"In function 'void setup()':" (that's all)

If I copy those configuration lines into the main code and remove the include statement it compiles correctly. I've tried it with #include <configuration.h> (note the angle brackets instead of quotes), but this fails to find the file.

Any suggestions?

Its usually frowned on to declare variables in an include file, one reason for this is to prevent the kind of problems you are finding.

Your declarations reference variable types that are defined in other header files and these my be include after you header. Because of the Arduino build process it can be difficult to control the order of includes.

You can work around your problem in a number of ways. One is to only use #defines and have the actual declarations in the source. This was what I suggested above but it gets awkward when you have arrays

Another solution is to use types are that are predefined. 'byte' is an arduino defined type, try your code with a native type like this:

const char secret[] = "redacted";
const char hostname[] = "redacted";
unsigned char mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned char ip[] = { 192, 168, 100, 160 };
unsigned char gateway[] = { 192, 168, 100, 1 };
unsigned char server[] = { 1, 2, 3, 4 };

Thanks Mem, that compiles and I'll try and upload to the Arduino later today.

Is there a better way of doing this, rather than using #include? A native C++ configuration tool?

There are a number of other ways of doing it, but nothing comes to mind that is easy to implement as the above.

You should declare the variables in your main program and only #define their initial value in the .h-file. Something like this:

secret.h:

#define SECRET "redacted"
#define HOSTNAME "redacted"
#define MAC { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }
#define IP { 192, 168, 100, 160 }
#define GATEWAY { 192, 168, 100, 1 }
#define SERVER { 1, 2, 3, 4 }

In your sketch:

// Values defined in secret.h
const char secret[] = SECRET;
const char hostname[] = HOSTNAME;
byte mac[] = MAC;
byte ip[] = IP;
byte gateway[] = GATEWAY;
byte server[] = SERVER;

I think this is the easiest way of doing it with the Arduino IDE.

1 Like