can't get aht10 working sidebyside mysensors



  • Hello,

    i have a sketch for aht10 wich is working perfectly alone
    the library is https://github.com/enjoyneering/AHTxx

    // AHT10
    
    // https://github.com/enjoyneering/AHTxx/blob/main/examples/AHT10_Serial/AHT10_Serial.ino
    #include <Wire.h>
    #include <AHTxx.h>
    
    float ahtTemp;                               //to store T/RH result
    float ahtHumi;
    
    AHTxx aht10(AHTXX_ADDRESS_X38, AHT1x_SENSOR); //sensor address, sensor type
    
    
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println("- - - - - setup - - - - -");
      Wire.begin();
      
      while (aht10.begin() != true) //for ESP-01 use aht10.begin(0, 2);
      {
        Serial.println(F("AHT1x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
        delay(5000);
      }
    
      Serial.println(F("AHT10 OK"));
    }
    
    
    void loop()      
    {  
      Serial.println("getting humidity & temperature");
    
      ahtTemp = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
      Serial.print(F("Temperature: "));
      Serial.print(ahtTemp);
      Serial.println(F(" +-0.3C"));
      
      delay(2000); //measurement with high frequency leads to heating of the sensor, see NOTE
    
      ahtHumi = aht10.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
      Serial.print(F("Humidity......: "));
      Serial.print(ahtHumi);
      Serial.println(F(" +-2%"));
    
      delay(10000);
    }
    

    but when i try to incorporate it in a mysensor sketch, it stays stuck at aht10.begin()

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_RF24
    
    #define MY_RF24_PA_LEVEL RF24_PA_HIGH
    
    #define MY_SECURITY_SIMPLE_PASSWD "xxxxxxxxxxxxxxxxxxxxxxx"
    
    #define MY_RF24_CHANNEL 14
    
    #include <SPI.h>
    #include <MySensors.h>
    // https://github.com/enjoyneering/AHTxx/blob/main/examples/AHT10_Serial/AHT10_Serial.ino
    #include <Wire.h>
    #include <AHTxx.h>
    
    static const uint64_t UPDATE_INTERVAL = 60*1000;
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    // AHT10
    float ahtTemp;                               //to store T/RH result
    float ahtHumi;
    
    AHTxx aht10(AHTXX_ADDRESS_X38, AHT1x_SENSOR); //sensor address, sensor type
    
    
    void presentation()  
    { 
      // Send the sketch version information to the gateway
      sendSketchInfo("aht10", "0.3");
      
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_TEMP, S_TEMP, "Temperature");
      present(CHILD_ID_HUM, S_HUM, "Humidity");
      
      metric = getControllerConfig().isMetric;
    }
    
    
    void setup()
    {
      Serial.println("- - - - - setup - - - - -");
      Wire.begin();
      
      while (aht10.begin() != true) //for ESP-01 use aht10.begin(0, 2);
      {
        Serial.println(F("AHT1x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
    
        delay(5000);
      }
    
      Serial.println(F("AHT10 OK"));
    }
    
    
    void loop()      
    {  
      #ifdef MY_DEBUG
        Serial.println("getting humidity & temperature");
      #endif
    
      ahtTemp = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
      #ifdef MY_DEBUG
        Serial.print(F("Temperature: "));
        Serial.print(ahtTemp);
        Serial.println(F(" +-0.3C"));
      #endif
      
      delay(2000); //measurement with high frequency leads to heating of the sensor, see NOTE
    
      ahtHumi = aht10.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
      #ifdef MY_DEBUG
        Serial.print(F("Humidity......: "));
        Serial.print(ahtHumi);
        Serial.println(F(" +-2%"));
      #endif
    
    
      static MyMessage msgHum(CHILD_ID_HUM,  V_HUM);
      static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
      send(msgTemp.set(ahtTemp, 1));
      send(msgHum.set(ahtHumi, 1));
    
      // Sleep for a while to save energy
      sleep(UPDATE_INTERVAL);
    }
    

    has you see concerning the ath10 code it is exactly the same
    both are running with the same arduino nanos
    i triple checked my wirering
    and a same breakout board is working alone but not with mysensors code included

    any idea ?
    thanks



  • ok, so i figured out what is going wrong, but i don't know how to solve this 😞
    more info at https://github.com/enjoyneering/AHTxx/issues/15
    if someone can help from here ...



  • Hi bach,

    yes, _address is a global used by mysensors. Just replace _address by something else (like _myAddress or __address) in all 8 occurances in AHTxx.cpp and you're fine.

    Regards, Edi



  • thanks eiten,

    I arrived at the same conclusion, indeed i replaced _address by _i2cAHTAddress in AHTxx library and it works.
    I am completely new to cpp (and arduino) but not to developpement, and what bothers me it's that libraries, like mysensors and ahtxx, for example, could compete on variables. Comming from javascript where global variables are avoided i am realy suprised about that. Is this normal behaviour ? what i don't understand is that _address is a private property of AHTxx class, so it should be encapsulated, how could mysensors overwrite it ?



  • i had my answer on ahtxx issue

    Hi,

    ...figured out that the _address is messed up (i guess by mysensors).

    It is impossible, because _address is private variable. The private data members cannot be accessed from outside the class (mysensors can not see it).

    Сan you try my original library but with class initialization line

    AHTxx aht10(0x38, AHT1x_SENSOR); //sensor address, sensor type


Log in to reply
 

Suggested Topics

  • 1
  • 2
  • 5
  • 2
  • 3
  • 1

26
Online

11.2k
Users

11.1k
Topics

112.5k
Posts