Fixed one-wire library with an hammer

This commit is contained in:
Ollo 2020-12-28 14:57:17 +01:00
parent 46ab832624
commit 2fc835f396
4 changed files with 88 additions and 34 deletions

View File

@ -14,7 +14,8 @@
"streambuf": "cpp",
"functional": "cpp",
"string": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"cmath": "cpp"
}
}
}

View File

@ -93,6 +93,8 @@
#define PANIK_MODE_DEEPSLEEP (60 * 60 * 5U) /**< 5 hours in usecond */
#define PANIK_MODE_DEEPSLEEP_US (PANIK_MODE_DEEPSLEEP * 1000 * 1000)
#define LIPO_MAX_TEMPERATUR 85
#define LIPO_MAX_TEMPERATUR_DIFF 10
#define TEMPERATURE_DELTA_TRIGGER_IN_C 1.0f
#define MOIST_DELTA_TRIGGER_ADC 10

View File

@ -24,5 +24,4 @@
#define WAKEUP_REASON_MOIST_CHANGE 20 /**< <code>20-26</code> for plant0 to plant9 */
#define WAKEUP_REASON_PLANT_DRY 30 /**< <code>30-36</code> for plant0 to plant9 */
#endif

View File

@ -53,6 +53,7 @@ typedef struct
int determineNextPump();
void setLastActivationForPump(int pumpId, long time);
int readTemp();
/******************************************************************************
* NON VOLATILE VARIABLES in DEEP SLEEP
@ -301,30 +302,70 @@ void mode2MQTT()
rtcWaterTempIndex = waterSensorIndex.get();
float lipoTempCurrent = lipoTempSensor.getMedian();
float t2 = NAN;
if (! isnan(lipoTempCurrent))
{
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent));
Serial << "Lipo Temperatur " << lipoTempCurrent << " °C " << endl;
}
float t2 = waterTempSensor.getMedian();
t2 = waterTempSensor.getMedian();
if (! isnan(t2))
{
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2));
Serial << "Water Temperatur " << lipoTempCurrent << " °C " << endl;
}
//give mqtt time, use via publish callback instead?
delay(100);
} else {
int j=0;
/* Activate the Sensors and measure the temperature again */
/* activate all sensors */
pinMode(OUTPUT_SENSOR, OUTPUT);
digitalWrite(OUTPUT_SENSOR, HIGH);
bool lipoTempWarning = lipoTempCurrent != 85 && abs(lipoTempCurrent - t2) > 10;
delay(100);
sensors.begin();
for(j=0; j < TEMP_SENSOR_MEASURE_SERIES && sensors.getDeviceCount() == 0; j++) {
delay(100);
sensors.begin();
Serial << "Reset 1-Wire Bus" << endl;
}
for(j=0; j < TEMP_SENSOR_MEASURE_SERIES && isnan(lipoTempCurrent); j++) {
delay(200);
readTemp();
lipoTempCurrent = lipoTempSensor.getMedian();
t2 = waterTempSensor.getMedian();
Serial << "Temperatur Lipo:" << lipoTempCurrent << " °C Water : " << t2 << " °C" << endl;
}
if (! isnan(lipoTempCurrent))
{
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent));
Serial << "Lipo Temperatur " << lipoTempCurrent << " °C " << endl;
t2 = waterTempSensor.getMedian();
if (! isnan(t2))
{
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2));
Serial << "Water Temperatur " << lipoTempCurrent << " °C " << endl;
}
}
/* deactivate the sensors */
digitalWrite(OUTPUT_SENSOR, LOW);
}
if (! isnan(lipoTempCurrent) && ! isnan(t2)) {
bool lipoTempWarning = (lipoTempCurrent != LIPO_MAX_TEMPERATUR) && abs(lipoTempCurrent - t2) > LIPO_MAX_TEMPERATUR_DIFF;
if (lipoTempWarning)
{
Serial.println("Lipo temp incorrect, panic mode deepsleep TODO");
//espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
//return;
}
}
for (int i = 0; i < MAX_PLANTS; i++)
{
@ -443,8 +484,9 @@ void readDistance()
*/
int readTemp() {
int readAgain = TEMP_SENSOR_MEASURE_SERIES;
int sensorCount = 0;
int sensorCount = sensors.getDeviceCount();
int leaveMode1 = 0;
while (readAgain > 0)
{
sensors.requestTemperatures();
@ -463,7 +505,11 @@ int readTemp() {
readAgain = 0;
wakeUpReason = WAKEUP_REASON_RTC_MISSING;
}
} else {
Serial << "No Sensors detected" << endl;
return 1;
}
if (sensorCount > 1 && rtcWaterTempIndex != -1)
{
float temp2Raw = sensors.getTempCByIndex(rtcWaterTempIndex);
@ -545,6 +591,7 @@ bool readSensors()
Serial << "Waitloop: One wire count: " << sensorCount << endl;
delay(200);
}
Serial << "One wire count: " << sensorCount << endl;
/* Measure temperature */
if (sensorCount > 0)
@ -555,6 +602,10 @@ bool readSensors()
/* Read the distance and give the temperature sensors some time */
readDistance();
Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl;
/* Retreive temperatures */
if (sensorCount > 0)
{
leaveMode1 |= readTemp();
for (int i = 0; i < sensorCount; i++)
@ -572,6 +623,7 @@ bool readSensors()
wakeUpReason = WAKEUP_REASON_TEMP2_CHANGE;
leaveMode1 = true;
}
}
/* deactivate the sensors */
digitalWrite(OUTPUT_SENSOR, LOW);