Can a device be programmed to switch accounts by storing two auth codes?

My devices will move around and connect on different wifi networks. A different wifi network may belong to a different user with a different Blynk account and therefore a different auth code for the device. I want the device firmware to be universal, not unique to each device or associated account. When the device finds itself at a particular wifi access point, I would like the device automatically to assume the identity, i.e., adopt the auth code associated with the wifi location it finds itself at.
The device code knows in advance all the wifi credentials it could encounter and it tries them all. Once connected to wifi it can look up the auth code associated with that location ssid and connect to the Blynk server under the appropriate auth code.

this requires device code that has two or more auth codes listed, where the device picks one.

#define BLYNK_TEMPLATE_ID           "Template ID"
#define BLYNK_DEVICE_NAME          "My Device Template"
#define BLYNK_AUTH_TOKEN_0            "xxxxxxxxxxxxxxxxxxxxx0"
#define BLYNK_AUTH_TOKEN_1            "xxxxxxxxxxxxxxxxxxxxx1"

void setup() {
int i = 0;  // used to count while loop executions/
while (status != WL_CONNECTED) { // loop exits once connected
locIndex = i % NUMBER_OF_LOCATIONS;  // cycles thorugh all known locations
// ... scan the neighborhood for all known wifi networks
// ... attempting to connect to each one using each stored ssid and password
i++; 
} // end of while loop

// locIndex - 1 now contains the number of the location at which we successfully connected to wifi
// for this example assume there are only two wifi locations each on different Blynk accounts and
// two associated auth tokens.

if(locIndex == 1) auth = BLYNK_AUTH_TOKEN_0;  else auth = BLYNK_AUTH_TOKEN_1;
Blynk.config ( auth,  "blynk.cloud", 80);  //
Blynk.connect();  // If I'm doing this wrong, please comment.  I get v0.6 connected.  Should it be 1.0?

void loop()  {
  Blynk.run();
  timer.run();
} // end void loop

Well, your Template ID and Template Name (was Device Name) would also need to change.

I don’t see how your current code stores the WiFi credentials or selects which WiFi network to connect to. It would then need to use this to identify which set of Blynk credentials to use.

Pete.

The code that stores wifi credentials and selects which network to connect, works and is not relevant to the question so I left that out. The eventual connected network is identified by the variable locIndex and the if statement choses which auth code to use based on locIndex. But you noted that both Template ID and Template Name would also need to change. I was thinking I could keep those the same. Only the auth credential is used to connect to Blynk. The Template Name is not unique as the user makes it up. But the Template ID is assigned at the time of device creation in Blynk.console. It is required up front in the code but it’s not used as a sign-in credential.

However, I think what you’re saying is that the Template_ID is keyed to an account. If that is the case reusing a Template_ID in a different account would cause Blynk console to not be able to locate that Template_ID.

So lets say a Alice creates her own account and creates a device with the same Template_Name as the Template_Name in my account. The device is created in Blynk.console and it receives it’s own unique auth code and Template_ID. Now I want my device to work in Alice’s network under her account as well as in my own network under my account.

I can list in the code both sets of:

#define BLYNK_TEMPLATE_ID "TMPLxxxxxxaliceTempIDxxxxx"
#define BLYNK_TEMPLATE_NAME "Our Template"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxalice'sAuthToken"

#define BLYNK_TEMPLATE_ID "TMPLxxxxxxxxmyTempIDxxxx"
#define BLYNK_TEMPLATE_NAME "Our Template"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxmyAuthToken"

and then select the AUTH_TOKEN mapped to the network wifi ssid.

Other than as a reminder to the developer what the TEMPLATE_ID and the TEMPLATE_NAME are, I don’t see the point of placing these in the code, as neither of them is part of the connect sequence. Only the AUTH_TOKEN parameter is used to connect to the Blynk server.

So I guess the question boils down to: Will choosing the right AUTH_TOKEN be sufficient to connect my device to the correct account.

Thanks.
Dana

Yes. It will work even if the template ID and template name are left blank.

It depends what you want to do though.
Things like Blynk.Air won’t work, and I suspect that there are other things that will cause issues along the way.

Pete.

I didn’t make mention of the Edgent example. I suppose that the template ID and template name are required when we use the Edgent example only. That’s just my personal opinion.

You don’t need to use Edgent to use Blynk.Air…

Pete.

@Dana if I understood your use case correctly - it’s possible. Your steps would be the next:

  • Manually create device for user X (via web), manually create device for user Y, etc
  • Copy tokens from the created devices above
  • When Wi-Fi switches, find the required Auth Token and replace the previous Auth Token with a new one based on the Wi-fi creds

Should work without any issues.

It’s a pretty complex use-case, but it looks doable with Blynk. You would need to write logic on device. Also you would need a PRO account to have access to sub orgs.

Let’s say you have 3 devices and 3 users

  1. Create one template
  2. Create 3 sub-organizations, each containing 1 user. Add template for each organization
  3. Manually create 9 devices - get 9 AuthTokens
  4. Transfer 3 devices to each organization.
  5. write a logic where based on wifi device chooses the token. Users will see 2 devices offline, one online

Not ideal solution, but better than nothing )