0

I'm trying to call the following function which should fill an array with sensors' data and return the number of detected sensors:

uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result) 

How am I supposed to call it without knowing the number of sensors? I thought about:

ds_sensor_t *result = (ds_sensor_t *) malloc(NUM_OF_SENSORS * sizeof(ds_sensor_t));
uint8_t count = ds18b20_read_all(pin, result);

But again I don't know NUM_OF_SENSORS. Should I just choose a large enough number?

EDIT: The source code of the function: https://github.com/SuperHouse/esp-open-rtos/blob/master/extras/ds18b20/ds18b20.c

12
  • 1
    Read the description of the function. Commented Oct 14, 2017 at 16:44
  • I think you can use realloc function every time you read data of new sensor. en.cppreference.com/w/c/memory/realloc It means that you can addyour sensors one by one to result Commented Oct 14, 2017 at 16:45
  • I think there should be a constant that says you max available mumber of sensors (something like MAX_NUM_OF_SENSORS). Or something like this... Do you have source code of function ds18b20_read_all ? Commented Oct 14, 2017 at 16:48
  • An example is here. Commented Oct 14, 2017 at 16:55
  • The source code is available at github.com/SuperHouse/esp-open-rtos/blob/master/extras/ds18b20/… The documentation says nothing about how to call // Scan all ds18b20 sensors on bus and return its amount. // Result are saved in array of ds_sensor_t structure. Commented Oct 14, 2017 at 16:56

2 Answers 2

3

Max sensors on 1-wire is 75. So you can have up to 75 ds18b20s attached. You can also query the wire to find out how many sensors are attached using int ds18b20_scan_devices(int pin, ds18b20_addr_t *addr_list, int addr_count); set addr_count = 75.

Sign up to request clarification or add additional context in comments.

Comments

2

The header says, that the function you are asking about is an obsolete API. That usually means you should not be using it if replacement is provided:

// The following are obsolete/deprecated APIs ... uint8_t ds18b20_read_all(uint8_t pin, ds_sensor_t *result);

I suspect it's deprecated for the very reason you have questions about:. It's unclear how to use it.

New APIs (below is one of them) has addr_count as a parameter:

bool ds18b20_read_temp_multi(int pin, ds18b20_addr_t *addr_list, int addr_count, float *result_list);

1 Comment

Ok, I missed that somehow. It makes sense. I thought I missed something in the C language.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.