Flag for BLYNK_INVALID_TOKEN

Good morning at all!

I had already asked a question over an old post but it was closed because old, so I create this new topic.

Is there an “invalid token” flag?

In the monitor (over serial) I can see “[3277] Invalid auth token” but it would be very convenient for me to be able to visualize on an OLED, while entering (for example) the token via WiFi Manager, if the typed token is invalid.

PS: I try monitoring the “BLYNK_INVALID_TOKEN” but the result is every time “9”, so I understand che it’s not a flag :smile:!

1 Like

You can try looking into the BlynkProtocol.h and BlynkProtocolDefs.h library files and try to see how it is used.

1 Like

Yes, as I said, I tried to understand something before asking here (I don’t like spoon-fed).
Right in the BlynkProtocol.h I found the string “BLYNK_INVALID_TOKEN”

this->sendInfo();
BLYNK_RUN_YIELD();
BlynkOnConnected();
return true;
case BLYNK_INVALID_TOKEN:
BLYNK_LOG1(BLYNK_F("Invalid auth token"));
state = TOKEN_INVALID;
break;

-----

#ifdef BLYNK_USE_DIRECT_CONNECT
if (strncmp(authkey, (char*)inputBuffer, 32)) {
BLYNK_LOG1(BLYNK_F("Invalid token"));
sendCmd(BLYNK_CMD_RESPONSE, hdr.msg_id, NULL, BLYNK_INVALID_TOKEN);
break;

Do you mean to edit the BlynkProtocol.h file? I can add here a variable?

Ty!

No. You need to find where it is defined. Probably at the top of that file (without checking I am not sure) will be a define with Blynk invalid token.

Edit: it is defined in BlynkProtocolDefs.h

And you can see here that BlynkStatus will equal 9 when Blynk invalid token is true.

1 Like

thanks! Now I search how read the state of BlynkStatus enum, correct?

Maybe you could try to call

isTokenInvalid();

This should return true/false depending on the outcome. Then you can display what you want on the OLED dependant on the result.

bool tokenNotValid = Blynk.isTokenInvalid();
if (tokenNotValid){
    // do something if token is invalid
}

tokenNotValid is always “0” :frowning:

I haven’t tried that function and haven’t tried the following and may fail but can you check BlynkStatus using Blynk as class? Like so:

if (Blynk.BlynkStatus == BLYNK_INVLAID_TOKEN){
    // do something
}

error: ‘class BlynkWifi’ has no member named ‘BlynkStatus’

(thanks for help, of course :slight_smile: )

How are you displaying the result of
Blynk.isTokenInvalid();

Can you share you full code?

i try with correct first and then with a incorrect token, but nothing change

Also, some try and some strange result.
With a sketch with WiFi Manager, the sketch RUN with a invalid token too (of course not sync with the app but the sketch run, i can see temperature, etc etc).

This is the serial output:

[9167] Connecting to blynk-cloud.com:80
[9350] Invalid auth token
This is a invalid token
Check Run
Check Run

where i can see the print of INVALID TOKEN, but your:

  if (Blynk.isTokenInvalid()){
    Serial.println("This is a invalid token");
    } else {
    Serial.println("This is a valid token");
    }

is work only if I call it with a timer in a void (I try to put everywhere, with no result in Monitor).

With a correct token, this is the output:

[3114] Connecting to blynk-cloud.com:80
[4227] Ready (ping: 61ms).
[6969] Time sync: OK
This is a valid token

So, seems ok.

Now, with this simple cleaned sketch and correct token (NO WIFI MANAGER):

bool tokenNotValid = Blynk.isTokenInvalid();
BlynkTimer timer;

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
 
  if (Blynk.isTokenInvalid()){
    Serial.println("This is a invalid token");
    } else {
    Serial.println("This is a valid token");
    }
  
  while (Blynk.connect() == false) {
  }

 timer.setInterval(5012L, checkRun);
}

void checkRun() {
    Serial.println("Check Run");
}

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

The serial return me “This is a valid token” and the sketch RUN ok:

15:29:08.716 ->     ___  __          __
15:29:08.716 ->    / _ )/ /_ _____  / /__
15:29:08.716 ->   / _  / / // / _ \/  '_/
15:29:08.716 ->  /____/_/\_, /_//_/_/\_\
15:29:08.716 ->         /___/ v0.6.1 on NodeMCU
15:29:08.716 -> 
15:29:08.716 -> [10518] Connecting to blynk-cloud.com:80
15:29:08.889 -> [10689] Ready (ping: 47ms).
15:29:08.959 -> This is a valid token
15:29:13.872 -> [15690] Connecting to blynk-cloud.com:80
15:29:13.976 -> [15779] Ready (ping: 43ms).
15:29:19.058 -> Check Run
15:29:24.076 -> Check Run

…but with an incorrect token, the serial result is this:

15:22:08.215 -> [145523] Connecting to blynk-cloud.com:80
15:22:08.319 -> [145632] Invalid auth token
15:22:17.204 -> [154523] Connecting to blynk-cloud.com:80
15:22:17.305 -> [154629] Invalid auth token
15:22:26.198 -> [163523] Connecting to blynk-cloud.com:80
15:22:26.301 -> [163620] Invalid auth token
15:22:35.214 -> [172523] Connecting to blynk-cloud.com:80
15:22:35.315 -> [172621] Invalid auth token
15:22:44.204 -> [181523] Connecting to blynk-cloud.com:80
15:22:44.308 -> [181617] Invalid auth token

So, I think that in this last case there are no possibilities to insert some verification code, because the blynk.run is in its loop. Or, if it can be done, in some other way.

Thanks

I’m not quite sure which sketch you want the verification to work in.

If it is in the last code then you need to put the invalid token code after the Blynk.connect(); while statement.

I want in the sketch with WiFi Manager, and now is ok with your code!! :slight_smile:

But i try in this simple sketch to put everywhere, without successfull!

Anyway, thanks for the help, I have what I need im ny WiFi Manager+OLED sketch :slight_smile:

1 Like

If I put IN while Blynk.connect():

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
 
  while (Blynk.connect() == false) {
  if (Blynk.isTokenInvalid()){
    Serial.println("This is a invalid token");
    } else {
    Serial.println("This is a valid token");
    }
  }
timer.setInterval(5012L, checkRun); //for check if the sketch is running
}

in monitor I don’t see nothing, but:

WITH INCORRECT TOKEN:

18:02:55.175 -> [10515] IP: 192.168.1.110
18:02:55.175 -> [10515] 
18:02:55.175 ->     ___  __          __
18:02:55.175 ->    / _ )/ /_ _____  / /__
18:02:55.175 ->   / _  / / // / _ \/  '_/
18:02:55.209 ->  /____/_/\_, /_//_/_/\_\
18:02:55.209 ->         /___/ v0.6.1 on NodeMCU
18:02:55.209 -> 
18:02:55.209 -> [10523] Connecting to blynk-cloud.com:80
18:02:56.515 -> [11854] Invalid auth token
18:03:04.203 -> [19521] Connecting to blynk-cloud.com:80
18:03:04.272 -> [19620] Invalid auth token
18:03:13.176 -> [28521] Connecting to blynk-cloud.com:80
18:03:13.587 -> [28926] Invalid auth token
18:03:22.205 -> [37521] Connecting to blynk-cloud.com:80
18:03:22.307 -> [37626] Invalid auth token
18:03:31.175 -> [46521] Connecting to blynk-cloud.com:80
18:03:31.312 -> [46628] Invalid auth token

WITH CORRECT TOKEN:

18:05:15.381 -> [10535] IP: 192.168.1.110
18:05:15.381 -> [10535] 
18:05:15.381 ->     ___  __          __
18:05:15.381 ->    / _ )/ /_ _____  / /__
18:05:15.381 ->   / _  / / // / _ \/  '_/
18:05:15.381 ->  /____/_/\_, /_//_/_/\_\
18:05:15.415 ->         /___/ v0.6.1 on NodeMCU
18:05:15.415 -> 
18:05:15.415 -> [10542] Connecting to blynk-cloud.com:80
18:05:16.477 -> [11613] Ready (ping: 53ms).
18:05:21.455 -> [16614] Connecting to blynk-cloud.com:80
18:05:21.592 -> [16737] Ready (ping: 58ms).
18:05:26.678 -> Check Run
18:05:31.676 -> Check Run
18:05:36.698 -> Check Run

If I put AFTER while Blynk.connect():

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
 
  while (Blynk.connect() == false) {
  }

if (Blynk.isTokenInvalid()){
    Serial.println("This is a invalid token");
    } else {
    Serial.println("This is a valid token");
    }

 timer.setInterval(5012L, checkRun);
}

I can see the VALID TOKEN only if is valid, but if it is invalid:

18:08:24.156 -> [10468] IP: 192.168.1.110
18:08:24.156 -> [10468] 
18:08:24.156 ->     ___  __          __
18:08:24.156 ->    / _ )/ /_ _____  / /__
18:08:24.156 ->   / _  / / // / _ \/  '_/
18:08:24.156 ->  /____/_/\_, /_//_/_/\_\
18:08:24.191 ->         /___/ v0.6.1 on NodeMCU
18:08:24.191 -> 
18:08:24.191 -> [10475] Connecting to blynk-cloud.com:80
18:08:25.382 -> [11674] Invalid auth token
18:08:33.151 -> [19474] Connecting to blynk-cloud.com:80
18:08:33.287 -> [19589] Invalid auth token
18:08:42.179 -> [28474] Connecting to blynk-cloud.com:80
18:08:42.279 -> [28574] Invalid auth token
18:08:51.169 -> [37474] Connecting to blynk-cloud.com:80
18:08:51.272 -> [37566] Invalid auth token
18:09:00.174 -> [46474] Connecting to blynk-cloud.com:80
18:09:00.243 -> [46566] Invalid auth token
18:09:09.166 -> [55474] Connecting to blynk-cloud.com:80
18:09:09.265 -> [55579] Invalid auth token
18:09:18.163 -> [64474] Connecting to blynk-cloud.com:80
18:09:18.268 -> [64576] Invalid auth token
18:09:27.153 -> [73474] Connecting to blynk-cloud.com:80
18:09:27.290 -> [73609] Invalid auth token

:slight_smile:

1 Like

Ah maybe we cannot use it using Blynk.connect but we must use Blynk.begin (as we would when using WiFi manager).

I have OTA setup on another device so one day I will check on there. (That uses Blynk.begin instead of connect)

I am glad that you sorted it out for your WiFi manager sketch though :slight_smile:

1 Like