Redefine variables from secrets.h

I recently tried to use a secrets.h file for my wifi passwords, APIs and login data.
The project works so that I can choose between various wifi networks and thingaverse channels at the beginning by defining, for instance
#define BalkonRegal or any other category.
If this is defined, all of that block gets taken into the code

#ifdef BalkonRegal
#define HomeNetwork
#define LiFePo4
#define EMail
String textMsg = "Batterie leer: Balkon Regal";
RTC_DATA_ATTR unsigned long waittime = 28800;
unsigned int NumberOfPlants = 4;
RTC_DATA_ATTR unsigned int watertimeM[4] = { 15, 15, 20, 25 };
const int dry[4] = { 2750, 2800, 2800, 3500 };
const int wet[4] = { 1100, 1150, 1190, 1150 };
RTC_DATA_ATTR int moistureThreshold[4] = { 70, 70, 70, 70 };
float in_calibrationOffset = 0.066;
#define AHT
//Thingspeak. Fields for the 4 moisture sensors and thingspeak field for the plants.
#define Thingspeak
unsigned long myChannelNumber = #;
const char *myWriteAPIKey = "#";
int tsfield1 = 1; //??
int tsfield2 = 2; //??
int tsfield3 = 3; //??
int tsfield4 = 4; //??
int tsfield5 = 5; //Temp
int tsfield6 = 6; //Humidity
#endif

while only the lines

unsigned long myChannelNumber = #;
const char *myWriteAPIKey = "#";

are an issue at the moment.

The file secrets.h, which is placed in the same folder as the poject since I want to work on it form different computers, looks like this:

//Thingaverse
#define BalkonChannelNumber "123"
#define BalkonmyWriteAPIKey "abc123"
#define BalkonRegalChannelNumber "456"
#define BalkonRegalmyWriteAPIKey "abc456"

In the code further down, the variables myChannelNumber and myWriteAPIKey are used to write to thingaverse and their content should be taken from the secrets.h depending on the defined variable for the #ifdef blocks.

However if I want to take the data from the secrets.h by
unsigned long myChannelNumber = BalkonRegalChannelNumber;
this results in an error:

Compilation error: invalid conversion from 'const char*' to 'long unsigned int' [-fpermissive]

So my question is:

  1. Is there any way I can fix this?
  2. What does it mean if there is a star (*) in front of the const char *myWriteAPIKey = "#"; but not at the unsigned long myChannelNumber = #;

same

const char myWriteAPIKey[] = "#";

That's not a valid line of code. What do you think it does?

You REALLY need to learn the basics.

char myChannelNumber[] = BalkonRegalChannelNumber;

because BalkonRegalChannelNumber is a literal, look here:

That's not a number, it's a character string literal. If you want it to be a number, get rid of the double quotes:

#define BalkonRegalChannelNumber 456

Can you give me a keyword at least so I know what I have to search for?

It now gives me the error:

c:\Users\lenov\Documents\Arduino\libraries\ThingSpeak\src/ThingSpeak.h:555:39: note: initializing argument 1 of 'int ThingSpeakClass::writeFields(long unsigned int, const char*)'
int writeFields(unsigned long channelNumber, const char * writeAPIKey)

and highlights the first line of this block of code:

        int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
        if (x == 200) {
          Serial.println("Channel update successful.");
        } else {
          Serial.println("Problem updating channel. HTTP error code " + String(x));
        }

this must be long int, so your try to assign a string to it was wrong.
change BalkonRegalChannelNumber so it is a number, not text.
read post#6

I started by reading and learning from the book "The C Programming Language" By Kernighan & Ritchie. Once you master that, try to learn C++ if you plan on writing libraries. I never needed to learn C++ but I get by. Been doing this since 1959.

it worked, I just had to remove the "" in the secrets.h
thank you! :slight_smile:

Do you understand why it worked ?

I think my first error was that I just wanted the data to be in a variable somehow. The library for thingaverse however expected a specific data type (just a number, not a string or letters).
The second was that the general function of redefining the data from one variable to another. The secrets contained a string because I used the "" and I tried to reassign it to an unsigned long, which did not work.
So first I should have researched what data type is expected in the library
then I should have checked what variable type needs to be definied for assigning that type, and then I had to make sure that the data taken out of the secrets.h is in that data type that needs to be assigned to an unsigned long.
... I guess that was my issue.
About the star (*) I believe that this makes the variable to contain an array, so
const char *myWriteAPIKey makes it possible to assign multiple letters to it instead of just one, basically the same (?) as const char[] or const char[10] if the API had 10 letters.

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