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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // INFO coincell optimization
  10. // https://www.openhardware.io/view/398/NModule-Temperature-Humidity-Light-Door-sensor-shield#tabs-source
  11. #define MY_TRANSPORT_UPLINK_CHECK_DISABLED
  12. // Timeout for transport to be ready during initialization, after this duration, even if it didn't manage to enable transport the node will enter the loop() method
  13. // For battery powered nodes, I set this to 5000 so after 5 seconds it will stop eating the battery
  14. // 5s is long enough to connect to gateway so you should connect your node back to your computer and check the log if your node cannot connect
  15. #define MY_TRANSPORT_WAIT_READY_MS 5000 // no more than 5 seconds of waiting for transport to be ready
  16. // Reduce maximum delay before going to sleep, as we do not want to kill the battery if gateway is not available
  17. // Before going to sleep MySensors will make sure we don't have a connection problem to MySensors network
  18. // If this value is too high and you have connection problem, MySensors will never go to sleep and bye bye battery ...
  19. // If using AA/AAA batteries you can set a longer duration like 5000ms
  20. #define MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS 2000
  21. // #define MY_SIGNING_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
  22. // #define MY_SECURITY_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
  23. #include <SPI.h>
  24. #include <MySensors.h>
  25. // https://github.com/enjoyneering/AHTxx/blob/main/examples/AHT10_Serial/AHT10_Serial.ino
  26. #include <Wire.h>
  27. #include <AHTxx.h>
  28. static const uint64_t UPDATE_INTERVAL = 900000; // (15min * 60sec = 900 seconds * 1000 milliseconds)
  29. #define CHILD_ID_HUM 0
  30. #define CHILD_ID_TEMP 1
  31. #define CHILD_ID_BAT 2
  32. #define CHILD_ID_BATV 3
  33. bool metric = true;
  34. float ahtTemp; //to store T/RH result
  35. float last_ahtTemp; //to store T/RH result
  36. float ahtHumi;
  37. float last_ahtHumi;
  38. int oldBatteryPcnt = 0;
  39. #define FULL_BATTERY 3 // 3V for 2xAA alkaline. Adjust if you use a different battery setup.
  40. MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  41. MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  42. MyMessage msgBat(CHILD_ID_BAT, V_VAR1);
  43. MyMessage msgBatPrefix(CHILD_ID_BAT, V_UNIT_PREFIX); // Custom unit message.
  44. MyMessage msgBatV(CHILD_ID_BATV, V_VAR2);
  45. MyMessage msgBatVPrefix(CHILD_ID_BATV, V_UNIT_PREFIX); // Custom unit message.
  46. // AHT10
  47. // #define AHT1X_POWER_ON_DELAY 100 //wait for AHT1x to initialize after power-on, in milliseconds
  48. // #define AHTXX_I2C_SPEED_HZ 400000
  49. AHTxx aht10(0x38, AHT1x_SENSOR); //sensor address, sensor type
  50. // AHTxx aht10(0x00, 0x38);
  51. // Sleep between sendings to preserve coin cell
  52. void sleepForCoinCell() {
  53. sleep(400);
  54. }
  55. void presentation()
  56. {
  57. #ifdef MY_DEBUG
  58. Serial.println("- - - - - presentation - - - - -");
  59. #endif
  60. // Send the sketch version information to the gateway
  61. sendSketchInfo("aht10", "0.5");
  62. sleepForCoinCell();
  63. // Register all sensors to gw (they will be created as child devices)
  64. present(CHILD_ID_TEMP, S_TEMP, "Temperature");
  65. sleepForCoinCell();
  66. present(CHILD_ID_HUM, S_HUM, "Humidity");
  67. sleepForCoinCell();
  68. present(CHILD_ID_BAT, S_CUSTOM, "Battery Percent");
  69. sleepForCoinCell();
  70. present(CHILD_ID_BATV, S_CUSTOM, "Battery Voltage");
  71. sleepForCoinCell();
  72. metric = getControllerConfig().isMetric;
  73. sleepForCoinCell();
  74. }
  75. void setup()
  76. {
  77. #ifdef MY_DEBUG
  78. Serial.println("- - - - - setup - - - - -");
  79. // Serial.println(AHT1x_SENSOR);
  80. // aht10.setType(AHT1x_SENSOR);
  81. #endif
  82. while (aht10.begin() != true) //for ESP-01 use aht10.begin(0, 2);
  83. {
  84. Serial.println(F("AHT1x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
  85. delay(5000);
  86. }
  87. #ifdef MY_DEBUG
  88. Serial.println(F("AHT10 OK"));
  89. #endif
  90. }
  91. void loop()
  92. {
  93. #ifdef MY_DEBUG
  94. Serial.println("- - - - - loop - - - - -");
  95. Serial.println("getting humidity & temperature");
  96. #endif
  97. ahtTemp = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
  98. #ifdef MY_DEBUG
  99. Serial.print(F("Temperature: "));
  100. Serial.print(ahtTemp);
  101. Serial.println(F(" +-0.3C"));
  102. #endif
  103. // static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
  104. if (ahtTemp != last_ahtTemp)
  105. {
  106. send(msgTemp.set(ahtTemp, 1));
  107. last_ahtTemp = ahtTemp;
  108. }
  109. sleep(2000); //measurement with high frequency leads to heating of the sensor, see NOTE
  110. ahtHumi = aht10.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
  111. #ifdef MY_DEBUG
  112. Serial.print(F("Humidity: "));
  113. Serial.print(ahtHumi);
  114. Serial.println(F(" +-2%"));
  115. #endif
  116. // static MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  117. if (ahtHumi != last_ahtHumi)
  118. {
  119. send(msgHum.set(ahtHumi, 1));
  120. last_ahtHumi = ahtHumi;
  121. }
  122. sleep(2000);
  123. // get the battery Voltage
  124. long batteryMillivolts = hwCPUVoltage();
  125. float batteryVolts = batteryMillivolts / 1000.0;
  126. int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
  127. #ifdef MY_DEBUG
  128. Serial.print("Battery voltage: ");
  129. Serial.print(batteryVolts);
  130. Serial.println("V");
  131. Serial.print("Battery percent: ");
  132. Serial.print(batteryPcnt);
  133. Serial.println(" %");
  134. #endif
  135. if (oldBatteryPcnt != batteryPcnt) {
  136. sendBatteryLevel(batteryPcnt);
  137. sleepForCoinCell();
  138. oldBatteryPcnt = batteryPcnt;
  139. send(msgBatPrefix.set("%")); // Set custom unit.
  140. sleepForCoinCell();
  141. send(msgBat.set(batteryPcnt, 1));
  142. sleepForCoinCell();
  143. send(msgBatVPrefix.set("V")); // Set custom unit.
  144. sleepForCoinCell();
  145. send(msgBatV.set(batteryVolts, 1));
  146. }
  147. // Sleep for a while to save energy
  148. sleep(UPDATE_INTERVAL);
  149. }