Passing variable to Auth Code?

Hey all-

I have similar code that I have running on multiple esp’s in a few different rooms (agriculture related) but every time I make an update, I have to go through and change my tokens (among a few other items). It would be nice to just change one variable at the top of my code (in this case, the room number) and upload. I’m trying to do something like:

int roomNumber = 2

char auth[] = “authCode”

//room 1 token
rm1token = “12312f23234”

//room 2 token
rm2token = “123123ljksdfj”

void setup() {
Switch(roomNumber) {
case 1:
authCode = rm1token;
case 2:
authCode = rm2token;

Blynk.begin(auth, ssid, pass, “x.x.x.x”);

}

Any thoughts? And I’m always up for a simpler solution (perhaps some python script to upload code :slight_smile: )

Are you looking for assistance as based on your “Need help with my project”, or is this an “Idea” topic?

This is easy.

Duplicate your code for all your rooms… but add the following right at the top first.
Then just change the ROOM value and it will pick a difference AUTH code which you can just keep in your code all the time.

#define ROOM 1 // change your room here and your auth code will update during compile

#if (ROOM == 1)
  #define AUTH "12312f2234" // auth for room 1 
#elif (ROOM == 2)
  #define AUTH "1232335443" // auth for room 2 
#elif (ROOM == 3)
  #define AUTH "1212121211" // auth for room 3 
#endif

Then in setup(), change your connection line to

Blynk.begin(AUTH, ssid, pass, "x.x.x.x");
1 Like

Darn it… @Jamin, stop being so smrtypnts (kidding, you da man :+1: )… I had just found the #if reference I was looking for, and was trying to figure out how it would apply here :stuck_out_tongue:

Ahh, well… I have my strengths… unfortunately coding isn’t quite one of them… yet…

PS - wouldn’t the AUTH = still require a string? as in #define char auth[] = "12312f2234" // auth for room 1 ? Or are you responding from your phone, while in traffic, or on the beach again, and just providing pseudocode :wink:

2 Likes

Either is fine but just less compile/declare issues doing it my way.

2 Likes

Very very cool, @Jamin . Ill give this a whirl.

1 Like

This worked for me! Thank you so much!

–ja