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

I found it in- https://ubidots.com/blog/speed-control-for-brushless-motors-with-an-esp8266-2/

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);


  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.


//Send the value (PWM) to the ESC


Please help me with the code!

  • What aspect of the code cannot you understand? Without being specific, it is hard to help you.
    – MichaelT
    May 16, 2019 at 20:18
  • The entire btof and callback function that have been created. Why have they been created, what is payload and length, why are we creating btof and callback function, what is the logic behind it and how are they related to speed control of brushless dc motor.
    – laks929
    May 17, 2019 at 11:18

1 Answer 1

  1. Callback function must be data send from server to the ESP.
  2. Payload is the data byte sent from server.
  3. Length is the size of data in bytes.
  4. BTOF is basically byte to float converter which converts all the data byte received into single floating variable and return that floating point value.
  5. ECS.write must be the function that write the value to the servo motor controller H-bridge IC. It belongs to servo library.
  6. Value range that can be mapped into this ECS.write is between 0-180 so map function will use that will convert the floating point data received from btof function.
  7. Basic definition of map() function:

Map function maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired.

Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers.


map(value, fromLow, fromHigh, toLow, toHigh)

Then ECS.write(value) will write that mapped value to servor motor controller H-Bridge.

  • Okay, thanks a lot for clearing most of it. Still I have few doubts- 1)char * demo = (char *) malloc(sizeof(char) * 10); Meaning of this line. 2) Is payload a defined function? Which library does it belong to? (3) Also I would want to know about the subscribe function and client there in the code- client.begin(callback); client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE);
    – laks929
    May 18, 2019 at 8:37
  • In that case you need share your full sketch here. 1.` char * demo = (char *) malloc(sizeof(char) * 10);` Meaning of this line. You should clear your basic C concepts. In this you are assigning some memory space to this char pointer of 10 bytes size;
    – Vaibhav
    May 18, 2019 at 9:07
  • The full sketch is here in the link- ubidots.com/blog/…
    – laks929
    May 18, 2019 at 9:13
  • Payload is a array pointer of byte type. It is declared in ESP8266 Library. Rest is features of TCP communication protocol. You should study about how TCp protocol works. Client Sever concept, what is publish and subscribe, and HTTP Post and Get request. Then you will know about these functions better.
    – Vaibhav
    May 18, 2019 at 9:21
  • Okay, thanks a lot!!
    – laks929
    May 18, 2019 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.