BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Virtal Pin string input

Hey Guys,

new here and wondering if someone could help. trying to take a sting as an input to turn some leds on and off, however when i try to use the web address with “…V3=on” or “V3 =off” if doesnt seem to work.
anyone able to assist?
thanks in advance

BLYNK_WRITE(V3) 
{
  Serial.println(param.asStr());
  if(param.asStr() == "on"){
    Serial.println("led on");
    for (int i=0; i<36; i++) {
      leds1[i] = CRGB::White;   
      FastLED.show();
      delay(25);
    }
  }
  else {
    for (int i=0; i<36; i++) {
      Serial.println("led off");
      leds1[i] = CRGB::Black;   
      FastLED.show();
      delay(25);
    }
  }
}

@taneesh Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

You’d need to clarify exactly what this means.

There is no ‘web address’ (url) handling code in the snippet you’ve posted.

If you’re meaning that neither of the if statements in your sketch evaluate as true then I’d suggest that you start by using a serial print command to visualise the data coming from your param.asStr() command.

Pete.

Hey Pete,

Thank you the reply. Below are the 2 web address I am using with the tokens removed

https://lon1.blynk.cloud/external/api/update?token=“…”&V3=on

https://lon1.blynk.cloud/external/api/update?token=“…”&V3=off

I use a serial print command in the v3 funciton to check what output I am getting in the serial monitor. I am alway getting the right string printed in the serial monitor e.g “on” is appearing but my if statements are not working when it gets the correct word input, it doesn’t seem to want to run what is within the if function when it receives the correct word

Thanks
Taneesh

I think it would be helpful if you explained in much more detail exactly what it is that you are trying to achieve, as well as sharing your updated code and serial output.

Pete.

Of course!

So essentially I’m running some stuff through siri shortcuts which when an nfc tag is scanned, it will do a bunch of stuff then run a url command to the blynk server virtual pins.
for example when the nfc tag is scanned it will get contents of URL:

https://lon1.blynk.cloud/external/api/update?token=“…”&V3=on ----- this should then turn virtual pin 3 on.

Next I want to be able to turn it off so I would run:
https://lon1.blynk.cloud/external/api/update?token=“…”&V3=off ----- turning virtual pin 3 off

The reason I want to use strings rather than integers for high and low is because i want to be able to control the colours of the LEDs, for example:
https://lon1.blynk.cloud/external/api/update?token=“…”&V3=blue ----- this would run the function to set the LED colours to blue.

Below is the code for where i have put my serial prints as well as the serial monitor.


BLYNK_WRITE(V3) 
{
  Serial.println(param.asStr()); //prints what value v3 is equal to in the url, this is correct all the time
  if(param.asStr() == "on"){ //this if statment doesn't run even if the previous serial print is correct and i did type 'on'
    Serial.println("led on");
    for (int i=0; i<36; i++) {
      leds1[i] = CRGB::White;   
      FastLED.show();
      delay(25);
    }
  }
  else {
    for (int i=0; i<36; i++) {
      Serial.println("led off");
      leds1[i] = CRGB::Black;   
      FastLED.show();
      delay(25);
    }
  }
}

When you post serial output please copy/paste the text from the serial monitor and post it between triple backticks.

Your updated code seems no different to your original code, and you certainly aren’t…

Pete.

sorry, will do that next time for the serial monitor,

as per your statement to visualise the data coming I though the

Serial.println(param.asStr());

is what is used to print out the values i am getting, if this is not right would you mind guiding me further with what you mean?

Taneesh

Sorry, I didn’t spot that line in the sketch, I’d expected you to save the incoming value to a variable and print that, as well as in the if statement.

There is a possibility that there are trailing characters at the end of the string, so assigning the value to a variable then doing a trim() on the result would solve that.

Pete.

ah brill that worked saving it to a variable.
a question on what you mean by using trim() though?

also, I’m curious to understand why i have to assign it to a variable? because when using integers I don’t have to do that

Taneesh

https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/trim/

I don’t know.

Pete.

Thanks for the help!

1 Like