hasenradball / AM2302-Sensor

Sensor Library for the AM2302 Sensor (aka DHT22) from ASAIR
MIT License
6 stars 1 forks source link

Question using multiple AM2302-Sensors #10

Closed larsfcfa closed 4 months ago

larsfcfa commented 4 months ago

Hello hasenradball,

thank you very much for providing this nice library which work awsome!

I'd like to read out 2 DHT22 sensors connceted to 2 different I/O pin. Thus, I did create a 2nd instance with AM2302::AM2302_Sensor am2302{SENSOR1_PIN}; AM2302::AM2302_Sensor am2302_2{SENSOR2_PIN};

and use either am2302.begin() am2302_2.begin()

am2302.read() am2302_2.read() ...

Each sendor alone work as well fine However, if I read out both sensors I get only garbage as sensor readings Is there an issue with 2 instances of the library or is there another way to read 2 sensors? (I'm not a C++ expert at all...)

hasenradball commented 4 months ago

Hi, thanks for your feedback. I wold have expected read 2 Sensors on different pins should work as you also expected. But I really did not test this explicitly.

First did you check the status? auto status = am2302.read();

I need more info from you.

1) Which microcontroller do you use (Arduino, ESP8266, etc...) 2) Maybee can you make a gist with your code and give a link?

Thanks.

larsfcfa commented 4 months ago

I did add some intermediate serial.print lines as "debugging" and it turned out that everything was caused be intermixed watchdog timer start/reset which did exceed the reset interval than.

in a nutshell I can confirm:

you excellent library works seamlessly with at least 2x DTH22 sensors on a Ardino Nano clone (ATmega328) board

hasenradball commented 4 months ago

thanks to hear. thank you for the flowers. 😊

hasenradball commented 4 months ago

@larsfcfa Hi I thought about your example.

normally it should also work if you make an array of am2302 sensor objects. And do all in a loop.

#include <Arduino.h>
#include <AM2302-Sensor.h>

constexpr int PIN1{4};
constexpr int PIN2{5};

// Create Sensor Object array
AM2302::AM2302_Sensor sensor_arr[2] = {
  AM2302::AM2302_Sensor{PIN1},
  AM2302::AM2302_Sensor{PIN2}
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  for (size_t i = 0; i < (sizeof(sensor_arr)/sizeof(AM2302::AM2302_Sensor)); ++i) {
    sensor_arr[i].begin();
  }
  delay(3000);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (size_t i = 0; i < (sizeof(sensor_arr)/sizeof(AM2302::AM2302_Sensor)); ++i) {
    sensor_arr[i].read();
    Serial.print("Sensor ");
    Serial.println(i);
    Serial.print("\tTemperature: ");
    Serial.print(sensor_arr[i].get_Temperature());
    Serial.println(" °C");
    Serial.print("\tHumidity:    ");
    Serial.print(sensor_arr[i].get_Humidity());
    Serial.println(" %\n\n");
  }
  delay(3000);
}
larsfcfa commented 4 months ago

Hi, Thank you for further supporting on this top, this works fine in principle!

I only approached a drawback I can't figure out:

for (size_t i = 0; i < sizeof(sensor_arr); ++i) { -> this for loop runs for 2 sensors 20 loops instead of 2

if I use for (size_t i = 0; i < 2; ++i) { instead everything ist fine

any idea?

hasenradball commented 4 months ago

@larsfcfa Thanks for the feedback. Yes I did an mistake.

sizeof(sensor_arr) gives a value you don‘ t estimate.

The fix would be:

a) using: sizeof(sensor_arr)/sizeof(AM2302::AM2302_Sensor)

or b) using a std::vector<AM2302::AM2302_Sensor> the you can use the .size() functionality of the std:: vector.

c) or using a ‚std::array<AM2302::AM2302_Sensor>

Sorry for the mistake.

hasenradball commented 4 months ago

@larsfcfa here the example with std:array

#include <Arduino.h>
#include <AM2302-Sensor.h>

constexpr int PIN1{4};
constexpr int PIN2{5};

// Create Sensor Object array with std:array
std::array<AM2302::AM2302_Sensor, 2> sensor_arr{
  AM2302::AM2302_Sensor{PIN1},
  AM2302::AM2302_Sensor{PIN2}
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  for (size_t i = 0; i < sensor_arr.size(); ++i) {
    sensor_arr[i].begin();
  }
  delay(3000);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (size_t i = 0; i < sensor_arr.size(); ++i) {
    Serial.print("Sensor ");
    Serial.println(i);
    Serial.print("\tSensor-Status: ");
    Serial.println(sensor_arr[i].read());
    Serial.print("\tTemperature: ");
    Serial.print(sensor_arr[i].get_Temperature());
    Serial.println(" °C");
    Serial.print("\tHumidity:    ");
    Serial.print(sensor_arr[i].get_Humidity());
    Serial.println(" %\n\n");
  }
  delay(3000);
}

Compiles with ESP8266, not sure std::array works for Arduino...

hasenradball commented 4 months ago

@larsfcfa sizeof() gives you the amount of Bytes the array holds. But sizeof itself do not know that the Bytes are caused by two objects.

hasenradball commented 4 months ago

@larsfcfa,

my final Program looks like:

 #include <Arduino.h>
#include <AM2302-Sensor.h>

constexpr int PIN1{4};
constexpr int PIN2{5};

// Create Sensor Object array with std:array
std::array<AM2302::AM2302_Sensor, 2> sensor_arr{
  AM2302::AM2302_Sensor{PIN1},
  AM2302::AM2302_Sensor{PIN2}
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while(!Serial) {
    yield();
  }
  Serial.println();

  for (size_t i = 0; i < sensor_arr.size(); ++i) {
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(" available - ");
    Serial.println(sensor_arr[i].begin());
  }
  delay(3000);
}

void loop() {
  // put your main code here, to run repeatedly:

  for (size_t i = 0; i < sensor_arr.size(); ++i) {
    Serial.print("Sensor ");
    Serial.println(i);
    Serial.print("\tSensor-Status: ");
    Serial.println(sensor_arr[i].read());
    Serial.print("\tTemperature: ");
    Serial.print(sensor_arr[i].get_Temperature());
    Serial.println(" °C");
    Serial.print("\tHumidity:    ");
    Serial.print(sensor_arr[i].get_Humidity());
    Serial.println(" %\n\n");
  }
  delay(3000);
}

And the output:

rll��|�l�|�l�b|����r�b�b��nn�lnn���bp�lrlrlp�n��l��bn�|���b��nn�l��l`�nnl`nr���nbl�lp�n�r������bn�|�b��nn�l`�nnl`nr���nb��`r��nb��`���l`��n�l����n�r��n|�l�l`b��|r�l�n��n�l`��r�l�l��
Sensor 0 available - 1
Sensor 1 available - 1
Sensor 0
    Sensor-Status: 0
    Temperature: 23.90 °C
    Humidity:    48.90 %

Sensor 1
    Sensor-Status: 0
    Temperature: 23.90 °C
    Humidity:    46.10 %
hasenradball commented 4 months ago

@larsfcfa I checked for the Anduino Nano...

would look like this....

#include <Arduino.h>
#include <AM2302-Sensor.h>

constexpr int SIZE {3};

constexpr int PIN_1{4};
constexpr int PIN_2{5};
constexpr int PIN_3{0};

// Create Sensor Object array with std:array
AM2302::AM2302_Sensor sensor_arr[SIZE] = {
  AM2302::AM2302_Sensor{PIN_1},
  AM2302::AM2302_Sensor{PIN_2},
  AM2302::AM2302_Sensor{PIN_3}
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while(!Serial) {
    yield();
  }
  Serial.println();

  for (size_t i = 0; i < SIZE; ++i) {
    Serial.print("Sensor ");
    Serial.print(i);
    Serial.print(" available - ");
    Serial.println(sensor_arr[i].begin());
  }
  delay(3000);
}

void loop() {
  // put your main code here, to run repeatedly:

  for (size_t i = 0; i < SIZE; ++i) {
    Serial.print("Sensor ");
    Serial.println(i);
    Serial.print("\tSensor-Status: ");
    Serial.println(sensor_arr[i].read());
    //Serial.print("\tTemperature: ");
    //Serial.print(sensor_arr[i].get_Temperature());
    //Serial.println(" °C");
    //Serial.print("\tHumidity:    ");
    //Serial.print(sensor_arr[i].get_Humidity());
    //Serial.println(" %\n\n");
  }
  Serial.print("\n\tTemperature: ");
  for (size_t i = 0; i < SIZE; ++i) {
    Serial.print(sensor_arr[i].get_Temperature());
    Serial.print("\t");
  }
  Serial.print("\n\tHumidity:    ");
  for (size_t i = 0; i < SIZE; ++i) {
    Serial.print(sensor_arr[i].get_Humidity());
    Serial.print("\t");
  }
  Serial.print("\n\n");
  delay(10000);
}
hasenradball commented 4 months ago

@larsfcfa I prepared a update in the branch improve error handling. Also updated the examples. If you can I would appreciate if you can take a short test. Especially I prepared a new Example for you with Arduino nano and Sensor Array.