Thermal sensor AOSONG AM2302 project


#1

Hi there, I have a project, which want to sample the temperature using two AM2302 sensors.
In the Loop function I write it below:

void loop()
{
    unsigned long _us = millis();
    int _delta = (_us - _lastTimeInLoop);

    if (_delta > (1.05 * LOOP_DELAY))
        _spark_error_count++;

    // Launch the acquisition on the two sensors
    DHTA.acquire();
    DHTB.acquire();

    // Print information for Sensor A
    Serial.print("\n");
    Serial.print(n);
    Serial.print(" : ");
    Serial.print((float) (_delta / 1000.0));
    Serial.print("s");
    if (_sensorA_error_count > 0 || _spark_error_count > 0) {
        Serial.print(" : E=");
        Serial.print(_sensorA_error_count);
        Serial.print("/");
        Serial.print(_spark_error_count);
    }
    Serial.print(", Retrieving information from sensor: ");
    Serial.print("Read sensor A: ");

    while (DHTA.acquiring()) ;
    printSensorData(&DHTA);

    // Print information for Sensor B
    Serial.print("\n");
    Serial.print(n);
    Serial.print(" : ");
    Serial.print((float) (_delta / 1000.0));
    Serial.print("s");
    if (_sensorB_error_count > 0 || _spark_error_count > 0) {
        Serial.print(" : E=");
        Serial.print(_sensorB_error_count);
        Serial.print("/");
        Serial.print(_spark_error_count);
    }
    Serial.print(", Retrieving information from sensor: ");
    Serial.print("Read sensor B: ");

    while (DHTB.acquiring()) ;
    printSensorData(&DHTB);

    n++;
    _lastTimeInLoop = _us;
    
    delay(LOOP_DELAY);
}

The point is when there is a bad senor in DHTB hardware. Serial port will be stop at the value as shown in the picture. I have changed the codes into one sensor condition, it works fine. Can anybody help to fix the code for jumping sensor B, if the hardware has problem and automatic jump to scan sensor A.


I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy


#2

Is this on a Core?
What pin? are you using to read the sensors? I didn’t see that in the script (I may have missed it).
This is NOT using Dallas one wire. Do you have a library for the Core?

Thanks for the correction @ScruffR


#3

Even more interesting is what library are you using?

And I would strongly advise against the use of potentially infinitly blocking loops like

while (DHTB.acquiring()) ;

Please also provide the setup code including initialisation of DHTx


To avoid the infinit loop when DHTx.acquiring() never turns false again try to flip your logic round

void setup()
{
  // all your setup code
  ...
  // Launch the acquisition for both sensors
  DHTA.acquire(); 
  DHTB.acquire();
}

void loop()
{
  if (!DHTA.acquiring())
  {
    // we're done reading, so proceed
    // and do all your printing and stuff
    ...
    // retrigger 
    DHTA.acquire(); 
  }   

  if (!DHTB.acquiring())
  {
    // we're done reading, so proceed
    // and do all your printing and stuff
    ...
    // retrigger 
    DHTB.acquire(); 
  }   
}

And since you’re only doing the same thing for both sensors, you could even push all that into one function that only takes the DHTx object as parameter.


#4

You can see my solution to that problem here :


#5

I use D2 and D3 as a port for reading the value of sensors.


#6

Sorry, but this seems to be not true :wink:
e.g. http://www.adafruit.com/products/393 states:
“Although it uses a single-wire to send data it is not Dallas One Wire compatible!”

Hence the use of a DHT library.


#7

@ScruffR, to add to what you said, Adafruit clearly states:

The AM2302 is a wired version of the DHT22

So using the PietteTech DHT library is dead on!


#8

True, but since I can’t see if @Tony_Blur is actually using PietteTech DHT, I could only guess.

The use of acquiring() does suggest so, but I’m not sure if there is no other lib (prone to blocking) also using such a function :wink:
If I had an answer, I would have suggested PietteTech, tho’