prevent infinite loop on waiting for hall if not connected, fixed formating

This commit is contained in:
Empire 2020-12-12 00:01:19 +01:00
parent 2b7840abf3
commit 3bd77e0b57

View File

@ -115,33 +115,39 @@ long getLastMoisture(int plantId)
} }
} }
long getDistance() { long getDistance()
{
byte startByte, h_data, l_data, sum; byte startByte, h_data, l_data, sum;
byte buf[3]; byte buf[3];
startByte = (byte)Serial.read(); startByte = (byte)Serial.read();
if(startByte == 255) { if (startByte == 255)
{
unsigned int distance; unsigned int distance;
Serial.readBytes(buf, 3); Serial.readBytes(buf, 3);
h_data = buf[0]; h_data = buf[0];
l_data = buf[1]; l_data = buf[1];
sum = buf[2]; sum = buf[2];
distance = (h_data << 8) + l_data; distance = (h_data << 8) + l_data;
if(((startByte + h_data + l_data)&0xFF) != sum){ if (((startByte + h_data + l_data) & 0xFF) != sum)
{
return -1; return -1;
} }
else{ else
{
return distance; return distance;
} }
} else { }
else
{
return -2; return -2;
} }
} }
void readSystemSensors() void readSystemSensors()
{ {
for (int i=0; i < 5; i++) { for (int i = 0; i < 5; i++)
{
lipoRawSensor.add(analogRead(SENSOR_LIPO)); lipoRawSensor.add(analogRead(SENSOR_LIPO));
solarRawSensor.add(analogRead(SENSOR_SOLAR)); solarRawSensor.add(analogRead(SENSOR_SOLAR));
} }
@ -364,6 +370,27 @@ long getLastActivationForPump(int plantId)
} }
} }
/* Use the Ultrasonic sensor to measure waterLevel */
void readDistance()
{
for (int i = 0; i < 5; i++)
{
long start = millis();
while (!Serial.available())
{
if (start + 200 < millis())
{
return;
}
}
unsigned int distance = getDistance();
if (distance > 0)
{
waterRawSensor.add(distance);
}
}
}
/** /**
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI. * @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
* These sensors (ADC2) can only be read when no Wifi is used. * These sensors (ADC2) can only be read when no Wifi is used.
@ -409,7 +436,8 @@ bool readSensors()
/* Read the temperature sensors once, as first time 85 degree is returned */ /* Read the temperature sensors once, as first time 85 degree is returned */
Serial << "DS18B20" << String(dallas.readDevices()) << endl; Serial << "DS18B20" << String(dallas.readDevices()) << endl;
delay(200); delay(200);
if (dallas.readDevices() > 0)
{
/* Required to read the temperature once */ /* Required to read the temperature once */
int readAgain = 5; int readAgain = 5;
while (readAgain > 0) while (readAgain > 0)
@ -426,35 +454,43 @@ bool readSensors()
temp2.add(temp[1]); temp2.add(temp[1]);
} }
if ((temp1.getAverage() - rtcLastTemp1 > TEMPERATURE_DELTA_TRIGGER_IN_C) || if ((temp1.getAverage() - rtcLastTemp1 > TEMPERATURE_DELTA_TRIGGER_IN_C) ||
(rtcLastTemp1 - temp1.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C)) { (rtcLastTemp1 - temp1.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C))
{
leaveMode1 = true; leaveMode1 = true;
} }
if ((temp2.getAverage() - rtcLastTemp2 > TEMPERATURE_DELTA_TRIGGER_IN_C) || if ((temp2.getAverage() - rtcLastTemp2 > TEMPERATURE_DELTA_TRIGGER_IN_C) ||
(rtcLastTemp2 - temp2.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C)) { (rtcLastTemp2 - temp2.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C))
{
leaveMode1 = true; leaveMode1 = true;
} }
if(!leaveMode1){ if (!leaveMode1)
{
readAgain = 0; readAgain = 0;
} }
readAgain--; readAgain--;
delay(50); delay(50);
} }
}
if (abs(temp1.getAverage() - rtcLastTemp1) > TEMPERATURE_DELTA_TRIGGER_IN_C) { if (abs(temp1.getAverage() - rtcLastTemp1) > TEMPERATURE_DELTA_TRIGGER_IN_C)
{
leaveMode1 = true; leaveMode1 = true;
wakeUpReason = WAKEUP_REASON_TEMP1_CHANGE; wakeUpReason = WAKEUP_REASON_TEMP1_CHANGE;
} }
if (abs(temp2.getAverage() - rtcLastTemp2) > TEMPERATURE_DELTA_TRIGGER_IN_C) { if (abs(temp2.getAverage() - rtcLastTemp2) > TEMPERATURE_DELTA_TRIGGER_IN_C)
{
wakeUpReason = WAKEUP_REASON_TEMP2_CHANGE; wakeUpReason = WAKEUP_REASON_TEMP2_CHANGE;
leaveMode1 = true; leaveMode1 = true;
} }
if (abs(getBatteryVoltage() - rtcLastBatteryVoltage) > LIPO_DELTA_VOLT_ADC) { if (abs(getBatteryVoltage() - rtcLastBatteryVoltage) > LIPO_DELTA_VOLT_ADC)
{
wakeUpReason = WAKEUP_REASON_BATTERY_CHANGE; wakeUpReason = WAKEUP_REASON_BATTERY_CHANGE;
leaveMode1 = true; leaveMode1 = true;
} }
if (abs(getSolarVoltage() - rtcLastSolarVoltage) > SOLAR_DELTA_VOLT_ADC) { if (abs(getSolarVoltage() - rtcLastSolarVoltage) > SOLAR_DELTA_VOLT_ADC)
{
wakeUpReason = WAKEUP_REASON_SOLAR_CHANGE; wakeUpReason = WAKEUP_REASON_SOLAR_CHANGE;
leaveMode1 = true; leaveMode1 = true;
} }
@ -464,15 +500,7 @@ bool readSensors()
rtcLastBatteryVoltage = getBatteryVoltage(); rtcLastBatteryVoltage = getBatteryVoltage();
rtcLastSolarVoltage = getSolarVoltage(); rtcLastSolarVoltage = getSolarVoltage();
/* Use the Ultrasonic sensor to measure waterLevel */ readDistance();
for (int i = 0; i < 5; i++)
{
while(!Serial.available()){}
unsigned int distance = getDistance();
if(distance > 0){
waterRawSensor.add(distance);
}
}
Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl; Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl;
/* deactivate the sensors */ /* deactivate the sensors */
digitalWrite(OUTPUT_SENSOR, LOW); digitalWrite(OUTPUT_SENSOR, LOW);