Problem understanding the code, please help quickly!

I cannot understand the code of controlling the speed of brushless dc motor using ESP8266 WiFi Module.

I found it in- Speed control for brushless motors with an ESP8266

The part that I cannot understand is-

float btof(byte * payload, unsigned int length) {

  char * demo = (char *) malloc(sizeof(char) * 10);

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

    demo[i] = payload[i];

  }

float value = atof(demo);

  free(demo);

  return value;
}

// Callback to handle subscription

void callback(char* topic, byte* payload, unsigned int length) 

{

    value = btof(payload, length);

      value = map(value, 0, 1023, 0, 180); 

//Map the 0-100 values from the slider to the 0-180 to use the servo lib.

 ESC.write(value); 

//Send the value (PWM) to the ESC

}

I cannot understand what is payload and length, why have we used in in the code of speed control of brushless dc motor, what are these functions btof and callback, what is the logic behind them.

The payload is a buffer containing an ASCII representation of a decimal value, length tells you how many characters are in it.

The code is flawed, and may fail unexpectedly - check the documentation for malloc (I don't know why you'd bother to use malloc in such a case.

It's indeed hard to understand without any context. I can only guess that the code receives as an input the value of a slider (maybe from a smartphone, via bluetooth) representing the speed of the motor. The value seems to arrive as an array of bytes. This piece of code is intended to decode this value, put it into a float variable and map it to [0 - 180] to feed a PWM.

I don't understand the purpose of this :

 char * demo = (char *) malloc(sizeof(char) * 10);
 for (int i = 0; i < length; i++) demo[i] = payload[i];

as this could have been sufficient :

float value = atof(payload);

'atof' converts a string to a floating-point number.

'length' is required if you send an array as a parameter of a function, to know how many values you have to process.

The code, as presented, is crap:

  1. Dynamically allocate a 10 byte array.
    -- Stupid, a local (stack) variable would work just as well.
    -- Don't check if malloc() succeeds. Stupid, asking for grief.

  2. Copy "length" bytes into the new array.
    -- Don't bother checking if you overwrite it. Stupid, asking for grief.

  3. Call atof() on the new arrary. Stupid, you could have done that with the array passed in.

  4. Free the dynamic memory. Correct and important. But, see #1 above.

2a) Not terminating the string, before 3.

gfvalvo:
Stupid, you could have done that with the array passed in.

Obviously, it's not his code, so the only stupid thing he did here was to copy a code he didn't understand

@LeSlept I didn't know how to write the code for it so obviously I'll search on the net for it. I tested the code, the speed of the motor could be controlled using the slider so obviously I'll look for the logic behind the code to modify it and make changes to make it more efficient. Didn't expect people to be this less understanding. And please instead of pointing out me as fool, help me solve my query.

The criticism was misdirected. My comments were regarding the quality of the code in the snippet you posted, understand that you're just starting to learn. I'm not familiar with the library you're trying to use. But, based on the part you posted, I'd be suspect of the author's coding skills.

To answer your questions:

btof() is a function that's supposed to turn an ASCII character representation of a floating point number into a variable of type float -- you need to understand those are two different things. The function is poorly implemented for the reasons pointed out (not a criticism of you). The only useful thing it does is call the atof() function, which the calling code could have more easily done directly.

callback() is a (rather unhelpfully named) example of a Callback Function. Its identity (pointer) is passed as an argument to another function and saved internally. At a later time (in this case, determined by the UbidotsESPMQTT.h library) it is called in response to some condition. This allows the user to insert custom code that is run in response to events within the library.

Update:

I now see that the author of the code you copied is not the same as the author of the UbidotsESPMQTT.h library. So, it may only be the ubidots guy who doesn't know what he's doing.

Okay, thank you for the answer.