RobTillaart / AM232X

Arduino library for AM2320 AM2321 and AM2323 I2C temperature and humidity sensor
MIT License
13 stars 5 forks source link

Demo for using second I2C Bus on ESP32 #24

Closed nkgiovannivl closed 2 years ago

nkgiovannivl commented 2 years ago

Hello all,

I'm trying to use this library on my ESP32, but in the examples i can not find the way to use it for my case, because i'm using another pins for I2C on my second bus:

#define pd_scl_dht 14
#define pd_sda_dht 27

TwoWire I2CIMU = TwoWire(0); //I2C1 bus
TwoWire I2CDHT = TwoWire(1); //I2C2 bus

AM232X AM2320(I2CDHT);

I2CDHT.begin(pd_sda_dht, pd_scl_dht, 100000ul); 
bool statusDHT = AM2320.begin();
Serial.println(statusDHT);
AM2320.wakeUp();
delay(2000);

So, i'm getting error at compile:

error: no matching function for call to 'AM232X::AM232X(TwoWire&)'
 AM232X AM2320(I2CDHT);

Thanks for your support!

RobTillaart commented 2 years ago

Thanks for the issue, I have no time to dive into the issue today. Will be later this week.

RobTillaart commented 2 years ago

ping!

try AM232X AM2320(&I2CDHT); // watch the &

RobTillaart commented 2 years ago

This compiles

//
//    FILE: AM2320_ESP32.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: AM2320 demo sketch for AM2320 I2C humidity & temperature sensor
//
//  AM232X PIN layout             AM2315 COLOR
//  ============================================
//   bottom view  DESCRIPTION     COLOR
//       +---+
//       |o  |       VDD          RED
//       |o  |       SDA          YELLOW
//       |o  |       GND          BLACK
//       |o  |       SCL          GREY
//       +---+
//
// do not forget pull up resistors between SDA, SCL and VDD..

#include "AM232X.h"

#define pd_scl_dht 14
#define pd_sda_dht 27

TwoWire I2CIMU = TwoWire(0);   //  I2C1 bus
TwoWire I2CDHT = TwoWire(1);   //  I2C2 bus

AM232X AM2320(&I2CDHT);

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("LIBRARY VERSION: ");
  Serial.println(AM232X_LIB_VERSION);
  Serial.println();

  I2CDHT.begin(pd_sda_dht, pd_scl_dht, 100000ul); 
  if (! AM2320.begin() )
  {
    Serial.println("Sensor not found");
    while (1);
  }
  AM2320.wakeUp();
  delay(2000);

  Serial.println("Type,\tStatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("AM2320, \t");
  int status = AM2320.read();
  switch (status)
  {
    case AM232X_OK:
      Serial.print("OK,\t");
      break;
    default:
      Serial.print(status);
      Serial.print("\t");
      break;
  }
  // DISPLAY DATA, sensor only returns one decimal.
  Serial.print(AM2320.getHumidity(), 1);
  Serial.print(",\t");
  Serial.println(AM2320.getTemperature(), 1);

  delay(2000);
}

// -- END OF FILE --
nkgiovannivl commented 2 years ago

Thanks @RobTillaart , It's works! sorry I'm novice so it is difficult for me find the correct way to do it!

RobTillaart commented 2 years ago

You're welcome, the example will be added to the library in the future.

RobTillaart commented 2 years ago

Note: the & means you want to pass the address of the TwoWIre object to the sensor class. It does not make a copy but uses a reference of the "one and only I2CDHT" object