ms-atmega328p--e01c-ml01s--aht10.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // #define MY_DEBUG
  2. // Enable and select radio type attached
  3. #define MY_RADIO_RF24
  4. // #define MY_RF24_PA_LEVEL RF24_PA_MIN
  5. // #define MY_RF24_PA_LEVEL RF24_PA_LOW
  6. #define MY_RF24_PA_LEVEL RF24_PA_HIGH
  7. //#define MY_RF24_PA_LEVEL RF24_PA_MAX
  8. #define MY_RF24_CHANNEL 14
  9. // #define MY_SIGNING_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
  10. // #define MY_SECURITY_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
  11. #include <SPI.h>
  12. #include <MySensors.h>
  13. // https://github.com/enjoyneering/AHTxx/blob/main/examples/AHT10_Serial/AHT10_Serial.ino
  14. #include <Wire.h>
  15. #include <AHTxx.h>
  16. static const uint64_t UPDATE_INTERVAL = 900000; // (15min * 60sec = 900 seconds * 1000 milliseconds)
  17. #define CHILD_ID_HUM 0
  18. #define CHILD_ID_TEMP 1
  19. #define CHILD_ID_BAT 2
  20. #define CHILD_ID_BATV 3
  21. bool metric = true;
  22. float ahtTemp; //to store T/RH result
  23. float ahtHumi;
  24. int oldBatteryPcnt = 0;
  25. #define FULL_BATTERY 3 // 3V for 2xAA alkaline. Adjust if you use a different battery setup.
  26. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  27. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  28. MyMessage msgBat(CHILD_ID_BAT, V_VAR1);
  29. MyMessage msgBatPrefix(CHILD_ID_BAT, V_UNIT_PREFIX); // Custom unit message.
  30. MyMessage msgBatV(CHILD_ID_BATV, V_VAR2);
  31. MyMessage msgBatVPrefix(CHILD_ID_BATV, V_UNIT_PREFIX); // Custom unit message.
  32. // AHT10
  33. // #define AHT1X_POWER_ON_DELAY 100 //wait for AHT1x to initialize after power-on, in milliseconds
  34. // #define AHTXX_I2C_SPEED_HZ 400000
  35. AHTxx aht10(0x38, AHT1x_SENSOR); //sensor address, sensor type
  36. // AHTxx aht10(0x00, 0x38);
  37. void presentation()
  38. {
  39. Serial.println("- - - - - presentation - - - - -");
  40. // Send the sketch version information to the gateway
  41. sendSketchInfo("aht10", "0.4.2");
  42. // Register all sensors to gw (they will be created as child devices)
  43. present(CHILD_ID_TEMP, S_TEMP, "Temperature");
  44. present(CHILD_ID_HUM, S_HUM, "Humidity");
  45. present(CHILD_ID_BAT, S_CUSTOM, "Battery Percent");
  46. present(CHILD_ID_BATV, S_CUSTOM, "Battery Voltage");
  47. metric = getControllerConfig().isMetric;
  48. }
  49. void setup()
  50. {
  51. #ifdef MY_DEBUG
  52. Serial.println("- - - - - setup - - - - -");
  53. // Serial.println(AHT1x_SENSOR);
  54. // aht10.setType(AHT1x_SENSOR);
  55. #endif
  56. while (aht10.begin() != true) //for ESP-01 use aht10.begin(0, 2);
  57. {
  58. Serial.println(F("AHT1x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
  59. delay(5000);
  60. }
  61. #ifdef MY_DEBUG
  62. Serial.println(F("AHT10 OK"));
  63. #endif
  64. }
  65. void loop()
  66. {
  67. #ifdef MY_DEBUG
  68. Serial.println("- - - - - loop - - - - -");
  69. Serial.println("getting humidity & temperature");
  70. #endif
  71. ahtTemp = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
  72. #ifdef MY_DEBUG
  73. Serial.print(F("Temperature: "));
  74. Serial.print(ahtTemp);
  75. Serial.println(F(" +-0.3C"));
  76. #endif
  77. // static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  78. send(msgTemp.set(ahtTemp, 1));
  79. delay(2000); //measurement with high frequency leads to heating of the sensor, see NOTE
  80. ahtHumi = aht10.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
  81. #ifdef MY_DEBUG
  82. Serial.print(F("Humidity: "));
  83. Serial.print(ahtHumi);
  84. Serial.println(F(" +-2%"));
  85. #endif
  86. // static MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  87. send(msgHum.set(ahtHumi, 1));
  88. delay(2000);
  89. // get the battery Voltage
  90. long batteryMillivolts = hwCPUVoltage();
  91. float batteryVolts = batteryMillivolts / 1000.0;
  92. int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
  93. #ifdef MY_DEBUG
  94. Serial.print("Battery voltage: ");
  95. Serial.print(batteryVolts);
  96. Serial.println("V");
  97. Serial.print("Battery percent: ");
  98. Serial.print(batteryPcnt);
  99. Serial.println(" %");
  100. #endif
  101. if (oldBatteryPcnt != batteryPcnt) {
  102. sendBatteryLevel(batteryPcnt);
  103. oldBatteryPcnt = batteryPcnt;
  104. send(msgBatPrefix.set("%")); // Set custom unit.
  105. send(msgBat.set(batteryPcnt, 1));
  106. send(msgBatVPrefix.set("V")); // Set custom unit.
  107. send(msgBatV.set(batteryVolts, 1));
  108. }
  109. // Sleep for a while to save energy
  110. sleep(UPDATE_INTERVAL);
  111. }