Get BLYNK_WRITE param type

Is there a way to know the type of param or the type of the datastream when read from the BLYNK_WRITE on an ESP32?

And what happens if you read the wrong type, for example .asInt when it’s a non-integer string?

Look at the data type you’ve specified in the datastream.

Generally it doesn’t matter too much whether you use param,asInt() param.asFloat() or param.asString, although obviously you’ll lose data when you read a floating point value into an integer. You’ll also be unable to do maths operations on strings, so it makes sense to choose the correct type.

Pete.

I have a general purpose server that receives data on any the virtual pins. If my code expects an integer and instead it receives a string “Dingle Arm”, what happens if I read it as integer? If I read a float as integer, does it truncate or round or corrupt the data? I really would expect these details to be documented.

I need a way of programmatically knowing the type of the data so I’m not passing on invalid data.

Well, the C++ variable type rules apply, so that’s where your answer lies.

Why? You’re not programmatically creating your datastreams, so you already know what type you’ve created.

Maybe I’m missing something here?

Pete.

I’m using BLYNK_WRITE_DEFAULT, which receives a pin number and param. Depending on the pin and datastreams, param can by any type. I encode the pin and the data and send that to another device. As a general purpose server, it doesn’t know how the datastreams are configured and they would be different for different connections. Even if I know the types, it would be bad practice to rely on no human error.

You can use this code to check the data type.

BLYNK_WRITE_DEFAULT()

{

int pin = request.pin;

String str = param.asStr();

bool isNumeric = true;

bool isDecimal = false;

for (int i = 0; i < str.length(); i++) {

    if (!isdigit(str[i])) {

        if (str[i] == '.') {

            if (isDecimal) {

                Serial.println("The string is not a valid number.");

                return;

            }

            isDecimal = true;

        } else {

            isNumeric = false;

            break;

        }

    }

}

if (isNumeric) {

    if (isDecimal) {

        Serial.println(pin);

        Serial.println("Float type");

    } else {

        Serial.println(pin);

        Serial.println("Int type");

    }

} else {

    Serial.println(pin);

    Serial.println("String type");

}

}

Mentioning that earlier, like in the title maybe, would have been useful!

Presumably you’re using the request.pin functionality to do some switch/case logic handling? If so then you could either do an initial triage of the pin numbers using a case 0 ... 50: for example, putting one type of virtual pin in the 0-50 range.

Or, you could use specific BLYNK_WRITE(vPin) entries for the datastreams that are say the String or Float types, and using BLYNK_WRITE_DEFAULT() to pick-up the rest.

Pete.

Thanks for the help, but I am concluding it’s not possible to know the intended type programmatically.

Not as far as I know.

Pete.

Please tell me what is the difference between the record type:

BLYNK_WRITE (V10)

and the record type:

BLYNK_WRITE (10)

Since I specify just a number in the function, without “V”, and everything seems to work…

V10 is just an alias.

If you take a look at BlynkHandlers.h you’ll see that it contains…

// Helper macro

#define V0  0
#define V1  1
#define V2  2
#define V3  3
#define V4  4
#define V5  5
#define V6  6
#define V7  7
#define V8  8
#define V9  9
#define V10 10
#define V11 11
#define V12 12
#define V13 13

etc. etc, etc

Pete.

1 Like

Thanks, Pete!