|
| 1 | +#include "configuration.h" |
| 2 | +#if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && defined(RAK4630) |
| 3 | + |
| 4 | +#include "../mesh/generated/meshtastic/telemetry.pb.h" |
| 5 | +#include "RAK12035Sensor.h" |
| 6 | + |
| 7 | +RAK12035Sensor::RAK12035Sensor() : TelemetrySensor(meshtastic_TelemetrySensorType_RAK12035, "RAK12035") {} |
| 8 | + |
| 9 | +int32_t RAK12035Sensor::runOnce() |
| 10 | +{ |
| 11 | + LOG_INFO("Init sensor: %s", sensorName); |
| 12 | + if (!hasSensor()) { |
| 13 | + return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS; |
| 14 | + } |
| 15 | + |
| 16 | + sensor.set_sensor_addr(RAK120351_ADDR); |
| 17 | + |
| 18 | + sensor.begin(nodeTelemetrySensorsMap[sensorType].first); |
| 19 | + // Get sensor firmware version |
| 20 | + uint8_t data = 0; |
| 21 | + sensor.get_sensor_version(&data); |
| 22 | + LOG_INFO("Sensor Firmware version: %i", data); |
| 23 | + |
| 24 | + if (data != 0) { |
| 25 | + LOG_DEBUG("RAK12035Sensor Init Succeed"); |
| 26 | + status = true; |
| 27 | + } else { |
| 28 | + LOG_ERROR("RAK12035Sensor Init Failed"); |
| 29 | + status = false; |
| 30 | + } |
| 31 | + sensor.sensor_sleep(); |
| 32 | + return initI2CSensor(); |
| 33 | +} |
| 34 | + |
| 35 | +void RAK12035Sensor::setup() |
| 36 | +{ |
| 37 | + // Set the calibration values |
| 38 | + // Reading the saved calibration values from the sensor. |
| 39 | + uint16_t zero_val = 0; |
| 40 | + uint16_t hundred_val = 0; |
| 41 | + uint16_t default_zero_val = 550; |
| 42 | + uint16_t default_hundred_val = 420; |
| 43 | + sensor.sensor_on(); |
| 44 | + delay(200); |
| 45 | + sensor.get_dry_cal(&zero_val); |
| 46 | + sensor.get_wet_cal(&hundred_val); |
| 47 | + delay(200); |
| 48 | + if (zero_val == 0 || zero_val <= hundred_val) { |
| 49 | + LOG_ERROR("Dry calibration value is %d", zero_val); |
| 50 | + LOG_ERROR("Wet calibration value is %d", hundred_val); |
| 51 | + LOG_ERROR("This does not make sense. Youc can recalibrate this sensor using the calibration sketch included here: " |
| 52 | + "https://github.com/RAKWireless/RAK12035_SoilMoisture."); |
| 53 | + LOG_ERROR("For now, setting default calibration value for Dry Calibration: %d", default_zero_val); |
| 54 | + sensor.set_dry_cal(default_zero_val); |
| 55 | + sensor.get_dry_cal(&zero_val); |
| 56 | + LOG_ERROR("Dry calibration reset complete. New value is %d", zero_val); |
| 57 | + } |
| 58 | + if (hundred_val == 0 || hundred_val >= zero_val) { |
| 59 | + LOG_ERROR("Dry calibration value is %d", zero_val); |
| 60 | + LOG_ERROR("Wet calibration value is %d", hundred_val); |
| 61 | + LOG_ERROR("This does not make sense. Youc can recalibrate this sensor using the calibration sketch included here: " |
| 62 | + "https://github.com/RAKWireless/RAK12035_SoilMoisture."); |
| 63 | + LOG_ERROR("For now, setting default calibration value for Wet Calibration: %d", default_hundred_val); |
| 64 | + sensor.set_wet_cal(default_hundred_val); |
| 65 | + sensor.get_wet_cal(&hundred_val); |
| 66 | + LOG_ERROR("Wet calibration reset complete. New value is %d", hundred_val); |
| 67 | + } |
| 68 | + sensor.sensor_sleep(); |
| 69 | + delay(200); |
| 70 | + LOG_INFO("Dry calibration value is %d", zero_val); |
| 71 | + LOG_INFO("Wet calibration value is %d", hundred_val); |
| 72 | +} |
| 73 | + |
| 74 | +bool RAK12035Sensor::getMetrics(meshtastic_Telemetry *measurement) |
| 75 | +{ |
| 76 | + measurement->variant.environment_metrics.has_soil_temperature = true; |
| 77 | + measurement->variant.environment_metrics.has_soil_moisture = true; |
| 78 | + |
| 79 | + uint8_t moisture = 0; |
| 80 | + uint16_t temp = 0; |
| 81 | + bool success = false; |
| 82 | + |
| 83 | + sensor.sensor_on(); |
| 84 | + delay(200); |
| 85 | + success = sensor.get_sensor_moisture(&moisture); |
| 86 | + delay(200); |
| 87 | + success = sensor.get_sensor_temperature(&temp); |
| 88 | + delay(200); |
| 89 | + sensor.sensor_sleep(); |
| 90 | + |
| 91 | + if (success == false) { |
| 92 | + LOG_ERROR("Failed to read sensor data"); |
| 93 | + return false; |
| 94 | + } |
| 95 | + LOG_INFO("Successful read from sensor Temperature: %.2f, Moisture: %ld%", (double)(temp / 10), moisture); |
| 96 | + measurement->variant.environment_metrics.soil_temperature = (float)(temp / 10); |
| 97 | + measurement->variant.environment_metrics.soil_moisture = moisture; |
| 98 | + |
| 99 | + LOG_INFO("Check if the original temperature and moisture (relative_humidity) are being used.. if not just use them for the " |
| 100 | + "soil monitoring."); |
| 101 | + |
| 102 | + if (!measurement->variant.environment_metrics.has_temperature) { |
| 103 | + LOG_INFO("Overwrite the temp metrics (not being set right now and this will allow the soil temp value to be used in the " |
| 104 | + "client interface)."); |
| 105 | + measurement->variant.environment_metrics.has_temperature = true; |
| 106 | + measurement->variant.environment_metrics.temperature = (float)(temp / 10); |
| 107 | + } |
| 108 | + |
| 109 | + if (!measurement->variant.environment_metrics.has_relative_humidity) { |
| 110 | + LOG_INFO("Overwrite the moisture metrics (not being used for air humidity and this will allow the soil humidity to " |
| 111 | + "appear in the client interfaces without adjustments)."); |
| 112 | + measurement->variant.environment_metrics.has_relative_humidity = true; |
| 113 | + measurement->variant.environment_metrics.relative_humidity = (float)moisture; |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | +} |
| 118 | +#endif |
0 commit comments