EnvironmentSensorManager.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "EnvironmentSensorManager.h"
  2. #if ENV_INCLUDE_AHTX0
  3. #define TELEM_AHTX_ADDRESS 0x38 // AHT10, AHT20 temperature and humidity sensor I2C address
  4. #include <Adafruit_AHTX0.h>
  5. static Adafruit_AHTX0 AHTX0;
  6. #endif
  7. #if ENV_INCLUDE_BME280
  8. #ifndef TELEM_BME280_ADDRESS
  9. #define TELEM_BME280_ADDRESS 0x76 // BME280 environmental sensor I2C address
  10. #endif
  11. #define TELEM_BME280_SEALEVELPRESSURE_HPA (1013.25) // Athmospheric pressure at sea level
  12. #include <Adafruit_BME280.h>
  13. static Adafruit_BME280 BME280;
  14. #endif
  15. #if ENV_INCLUDE_INA3221
  16. #define TELEM_INA3221_ADDRESS 0x42 // INA3221 3 channel current sensor I2C address
  17. #define TELEM_INA3221_SHUNT_VALUE 0.100 // most variants will have a 0.1 ohm shunts
  18. #define TELEM_INA3221_NUM_CHANNELS 3
  19. #include <Adafruit_INA3221.h>
  20. static Adafruit_INA3221 INA3221;
  21. #endif
  22. #if ENV_INCLUDE_INA219
  23. #define TELEM_INA219_ADDRESS 0x40 // INA219 single channel current sensor I2C address
  24. #include <Adafruit_INA219.h>
  25. static Adafruit_INA219 INA219(TELEM_INA219_ADDRESS);
  26. #endif
  27. bool EnvironmentSensorManager::begin() {
  28. #if ENV_INCLUDE_GPS
  29. initBasicGPS();
  30. #endif
  31. #if ENV_INCLUDE_AHTX0
  32. if (AHTX0.begin(&Wire, 0, TELEM_AHTX_ADDRESS)) {
  33. MESH_DEBUG_PRINTLN("Found AHT10/AHT20 at address: %02X", TELEM_AHTX_ADDRESS);
  34. AHTX0_initialized = true;
  35. } else {
  36. AHTX0_initialized = false;
  37. MESH_DEBUG_PRINTLN("AHT10/AHT20 was not found at I2C address %02X", TELEM_AHTX_ADDRESS);
  38. }
  39. #endif
  40. #if ENV_INCLUDE_BME280
  41. if (BME280.begin(TELEM_BME280_ADDRESS, &Wire)) {
  42. MESH_DEBUG_PRINTLN("Found BME280 at address: %02X", TELEM_BME280_ADDRESS);
  43. MESH_DEBUG_PRINTLN("BME sensor ID: %02X", BME280.sensorID());
  44. BME280_initialized = true;
  45. } else {
  46. BME280_initialized = false;
  47. MESH_DEBUG_PRINTLN("BME280 was not found at I2C address %02X", TELEM_BME280_ADDRESS);
  48. }
  49. #endif
  50. #if ENV_INCLUDE_INA3221
  51. if (INA3221.begin(TELEM_INA3221_ADDRESS, &Wire)) {
  52. MESH_DEBUG_PRINTLN("Found INA3221 at address: %02X", TELEM_INA3221_ADDRESS);
  53. MESH_DEBUG_PRINTLN("%04X %04X", INA3221.getDieID(), INA3221.getManufacturerID());
  54. for(int i = 0; i < 3; i++) {
  55. INA3221.setShuntResistance(i, TELEM_INA3221_SHUNT_VALUE);
  56. }
  57. INA3221_initialized = true;
  58. } else {
  59. INA3221_initialized = false;
  60. MESH_DEBUG_PRINTLN("INA3221 was not found at I2C address %02X", TELEM_INA3221_ADDRESS);
  61. }
  62. #endif
  63. #if ENV_INCLUDE_INA219
  64. if (INA219.begin(&Wire)) {
  65. MESH_DEBUG_PRINTLN("Found INA219 at address: %02X", TELEM_INA219_ADDRESS);
  66. INA219_initialized = true;
  67. } else {
  68. INA219_initialized = false;
  69. MESH_DEBUG_PRINTLN("INA219 was not found at I2C address %02X", TELEM_INA219_ADDRESS);
  70. }
  71. #endif
  72. return true;
  73. }
  74. bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  75. next_available_channel = TELEM_CHANNEL_SELF + 1;
  76. if (requester_permissions & TELEM_PERM_LOCATION && gps_active) {
  77. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, 0.0f); // allow lat/lon via telemetry even if no GPS is detected
  78. }
  79. if (requester_permissions & TELEM_PERM_ENVIRONMENT) {
  80. #if ENV_INCLUDE_AHTX0
  81. if (AHTX0_initialized) {
  82. sensors_event_t humidity, temp;
  83. AHTX0.getEvent(&humidity, &temp);
  84. telemetry.addTemperature(TELEM_CHANNEL_SELF, temp.temperature);
  85. telemetry.addRelativeHumidity(TELEM_CHANNEL_SELF, humidity.relative_humidity);
  86. }
  87. #endif
  88. #if ENV_INCLUDE_BME280
  89. if (BME280_initialized) {
  90. telemetry.addTemperature(TELEM_CHANNEL_SELF, BME280.readTemperature());
  91. telemetry.addRelativeHumidity(TELEM_CHANNEL_SELF, BME280.readHumidity());
  92. telemetry.addBarometricPressure(TELEM_CHANNEL_SELF, BME280.readPressure());
  93. telemetry.addAltitude(TELEM_CHANNEL_SELF, BME280.readAltitude(TELEM_BME280_SEALEVELPRESSURE_HPA));
  94. }
  95. #endif
  96. #if ENV_INCLUDE_INA3221
  97. if (INA3221_initialized) {
  98. for(int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) {
  99. // add only enabled INA3221 channels to telemetry
  100. if (INA3221.isChannelEnabled(i)) {
  101. float voltage = INA3221.getBusVoltage(i);
  102. float current = INA3221.getCurrentAmps(i);
  103. telemetry.addVoltage(next_available_channel, voltage);
  104. telemetry.addCurrent(next_available_channel, current);
  105. telemetry.addPower(next_available_channel, voltage * current);
  106. next_available_channel++;
  107. }
  108. }
  109. }
  110. #endif
  111. #if ENV_INCLUDE_INA219
  112. if (INA219_initialized) {
  113. telemetry.addVoltage(next_available_channel, INA219.getBusVoltage_V());
  114. telemetry.addCurrent(next_available_channel, INA219.getCurrent_mA() / 1000);
  115. telemetry.addPower(next_available_channel, INA219.getPower_mW() / 1000);
  116. next_available_channel++;
  117. }
  118. #endif
  119. }
  120. return true;
  121. }
  122. int EnvironmentSensorManager::getNumSettings() const {
  123. #if ENV_INCLUDE_GPS
  124. return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
  125. #else
  126. return 0;
  127. #endif
  128. }
  129. const char* EnvironmentSensorManager::getSettingName(int i) const {
  130. #if ENV_INCLUDE_GPS
  131. return (gps_detected && i == 0) ? "gps" : NULL;
  132. #else
  133. return NULL;
  134. #endif
  135. }
  136. const char* EnvironmentSensorManager::getSettingValue(int i) const {
  137. #if ENV_INCLUDE_GPS
  138. if (gps_detected && i == 0) {
  139. return gps_active ? "1" : "0";
  140. }
  141. #endif
  142. return NULL;
  143. }
  144. bool EnvironmentSensorManager::setSettingValue(const char* name, const char* value) {
  145. #if ENV_INCLUDE_GPS
  146. if (gps_detected && strcmp(name, "gps") == 0) {
  147. if (strcmp(value, "0") == 0) {
  148. stop_gps();
  149. } else {
  150. start_gps();
  151. }
  152. return true;
  153. }
  154. #endif
  155. return false; // not supported
  156. }
  157. #if ENV_INCLUDE_GPS
  158. void EnvironmentSensorManager::initBasicGPS() {
  159. Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX);
  160. #ifdef GPS_BAUD_RATE
  161. Serial1.begin(GPS_BAUD_RATE);
  162. #else
  163. Serial1.begin(9600);
  164. #endif
  165. // Try to detect if GPS is physically connected to determine if we should expose the setting
  166. #ifdef PIN_GPS_EN
  167. pinMode(PIN_GPS_EN, OUTPUT);
  168. digitalWrite(PIN_GPS_EN, HIGH); // Power on GPS
  169. #endif
  170. #ifndef PIN_GPS_EN
  171. MESH_DEBUG_PRINTLN("No GPS wake/reset pin found for this board. Continuing on...");
  172. #endif
  173. // Give GPS a moment to power up and send data
  174. delay(1000);
  175. // We'll consider GPS detected if we see any data on Serial1
  176. gps_detected = (Serial1.available() > 0);
  177. if (gps_detected) {
  178. MESH_DEBUG_PRINTLN("GPS detected");
  179. #ifdef PERSISTANT_GPS
  180. gps_active = true;
  181. return;
  182. #endif
  183. } else {
  184. MESH_DEBUG_PRINTLN("No GPS detected");
  185. }
  186. #ifdef PIN_GPS_EN
  187. digitalWrite(PIN_GPS_EN, LOW); // Power off GPS until the setting is changed
  188. #endif
  189. gps_active = false; //Set GPS visibility off until setting is changed
  190. }
  191. void EnvironmentSensorManager::start_gps() {
  192. gps_active = true;
  193. #ifdef PIN_GPS_EN
  194. pinMode(PIN_GPS_EN, OUTPUT);
  195. digitalWrite(PIN_GPS_EN, HIGH);
  196. return;
  197. #endif
  198. MESH_DEBUG_PRINTLN("Start GPS is N/A on this board. Actual GPS state unchanged");
  199. }
  200. void EnvironmentSensorManager::stop_gps() {
  201. gps_active = false;
  202. #ifdef PIN_GPS_EN
  203. pinMode(PIN_GPS_EN, OUTPUT);
  204. digitalWrite(PIN_GPS_EN, LOW);
  205. return;
  206. #endif
  207. MESH_DEBUG_PRINTLN("Stop GPS is N/A on this board. Actual GPS state unchanged");
  208. }
  209. void EnvironmentSensorManager::loop() {
  210. static long next_gps_update = 0;
  211. _location->loop();
  212. if (millis() > next_gps_update) {
  213. if (gps_active && _location->isValid()) {
  214. node_lat = ((double)_location->getLatitude())/1000000.;
  215. node_lon = ((double)_location->getLongitude())/1000000.;
  216. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  217. }
  218. next_gps_update = millis() + 1000;
  219. }
  220. }
  221. #endif