Struggling with Blynk to get global var value

Hi folks,
I am trying to get the value of a Blynk menu stored in a global var. Blynk works fine, I can see I get some feedback when I press the menu, but the global var is not updated.
Please find my code below :

#define BLYNK_PRINT Serial



#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
const char* ssid = "xxx";
const char* password = "xxx";

int selected;


BLYNK_WRITE(V1){
  int selected = param.asInt();
  Serial.print("in blynk_write : ");
  Serial.println(selected);
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, password);
}

void loop()
{
  Blynk.run();
  delay(1000);
  Serial.print("in loop : ");
  Serial.println(selected);
}

And here is what I get in the serial monitor :

[10400] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP8266

[10480] Connecting to blynk-cloud.com:80
[10560] Ready (ping: 17ms).
in loop : 0
in loop : 0
in loop : 0
in loop : 0
in blynk_write : 3
in loop : 0

Any idea ?

Can you be more descriptive of what "blynk" does and what it is supposed to do?

What does this class do and where did it come from?

param.asInt();

Are you trying to get input from the serial monitor? If so, your loop code doesn't show any action, except for Blynk.run(); of which, without information about what "blynk" does, we won't be able to help you.

void loop()
{
  Blynk.run();
  delay(1000);
  Serial.print("in loop : ");
  Serial.println(selected);
}

If you haven't already, please take some time to read the post about requesting help so that we can better assist you! :slight_smile:

Ok, my bad, thought Blynk was something well known in the iot community !

Blynk is something you can use with your ESP8266 to interact with. Basically, I installed the Blynk app on my phone, and configured a drop-down menu where I can select a value. I am trying to make a phone-controlled strip led. I would like to use Blynk to do so.

So, I want to get the selected value of that menu. The BLYNK_WRITE(V1) function does this. It sends the selected value to the Arduino. The param.asInt() retrieves the selected item from the menu as an int.

My thought was : I create a global var ("selected"), and I update it via this Blynk menu. Seems to work fine according to the serial monitor. BUT, when I log it in the loop, it doesn't seem to be the case.

I hope I cleared things !

you have a local variable which shadows the global variable

@Supernovali, if you don't know what Blynk is, simply don't comment

Hmmm, well, it's a bit awkward :confused:

I found the problem : as I am a total noob in C++, I've put a "int" before the var in the BLYNK_WRITE(V1) function. So, I guess, it creates a new variable everytime. No wonder it doesn't get updated.

Thanks for your help and patience, and I am going to slap my face gently now.

zouzou21:
So, I guess, it creates a new variable everytime.

Right, do a Google search for "variable shadowing".

you have a local variable which shadows the global variable

@Supernovali, if you don’t know what Blynk is, simply don’t comment

Ok, dad. Trying to help someone here, but if you insist! Just kidding, I’m not afraid to ask for help or to try and help. I could learn something in the process. Hence, the creation of a forum.

Glad you were able to find the problem! And don’t be afraid to ask questions! We should be a positive community! We can all learn something… Like right now, I am working on a project with MQTT for an IoT device so I might just have to look into blynk some more! Thanks :slight_smile: