control works again (fixed with wifi and fallback)
This commit is contained in:
parent
ad0ae88c3c
commit
92005c1c33
@ -44,8 +44,14 @@ void Plant::init(void)
|
|||||||
});
|
});
|
||||||
|
|
||||||
/* Initialize Hardware */
|
/* Initialize Hardware */
|
||||||
|
Serial.println("Set GPIO mode " + String(mPinPump) + "=" + String(OUTPUT));
|
||||||
|
Serial.flush();
|
||||||
pinMode(this->mPinPump, OUTPUT);
|
pinMode(this->mPinPump, OUTPUT);
|
||||||
|
Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG));
|
||||||
|
Serial.flush();
|
||||||
pinMode(this->mPinSensor, ANALOG);
|
pinMode(this->mPinSensor, ANALOG);
|
||||||
|
Serial.println("Set GPIO " + String(mPinPump) + "=" + String(LOW));
|
||||||
|
Serial.flush();
|
||||||
digitalWrite(this->mPinPump, LOW);
|
digitalWrite(this->mPinPump, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,8 +38,8 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
|
||||||
int determineNextPump();
|
int determineNextPump();
|
||||||
int readTemp();
|
|
||||||
void plantcontrol();
|
void plantcontrol();
|
||||||
|
void readPowerSwitchedSensors();
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* NON VOLATILE VARIABLES in DEEP SLEEP
|
* NON VOLATILE VARIABLES in DEEP SLEEP
|
||||||
@ -62,6 +62,8 @@ long nextBlink = 0; /**< Time needed in main loop to support expected blink code
|
|||||||
|
|
||||||
RunningMedian waterRawSensor = RunningMedian(5);
|
RunningMedian waterRawSensor = RunningMedian(5);
|
||||||
float mSolarVoltage = 0.0f; /**< Voltage from solar panels */
|
float mSolarVoltage = 0.0f; /**< Voltage from solar panels */
|
||||||
|
unsigned long setupFinishedTimestamp;
|
||||||
|
|
||||||
|
|
||||||
/*************************** Hardware abstraction *****************************/
|
/*************************** Hardware abstraction *****************************/
|
||||||
|
|
||||||
@ -150,45 +152,12 @@ void espDeepSleepFor(long seconds, bool activatePump = false)
|
|||||||
mDeepsleep = true;
|
mDeepsleep = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Read ultra sensor JSN-SR04T-2.0
|
|
||||||
* Read the distance of the water level.
|
|
||||||
*/
|
|
||||||
void readDistance()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < AMOUNT_SENOR_QUERYS; i++)
|
|
||||||
{
|
|
||||||
unsigned long duration = 0;
|
|
||||||
|
|
||||||
digitalWrite(SENSOR_TANK_TRG, HIGH);
|
//requires homie being started
|
||||||
delayMicroseconds(20);
|
void readOneWireSensors(boolean withMQTT){
|
||||||
cli();
|
|
||||||
digitalWrite(SENSOR_TANK_TRG, LOW);
|
|
||||||
duration = pulseIn(SENSOR_TANK_ECHO, HIGH);
|
|
||||||
sei();
|
|
||||||
|
|
||||||
int mmDis = duration * 0.3432 / 2;
|
|
||||||
if (mmDis > MAX_TANK_DEPTH)
|
|
||||||
{
|
|
||||||
waterRawSensor.add(0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
waterRawSensor.add(mmDis);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
|
|
||||||
* These sensors (ADC2) can only be read when no Wifi is used.
|
|
||||||
*/
|
|
||||||
void readSensors()
|
|
||||||
{
|
|
||||||
int sensorCount = sensors.getDS18Count();
|
int sensorCount = sensors.getDS18Count();
|
||||||
Serial << "Read Sensors" << endl;
|
Serial << "Read OneWire" << endl;
|
||||||
/* activate all sensors */
|
Serial.flush();
|
||||||
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < sensorCount; i++)
|
for (uint8_t i = 0; i < sensorCount; i++)
|
||||||
{
|
{
|
||||||
@ -208,21 +177,36 @@ void readSensors()
|
|||||||
ds18b20Address[7]);
|
ds18b20Address[7]);
|
||||||
|
|
||||||
if (String(lipoSensorAddr.get()).compareTo(String(buf))) {
|
if (String(lipoSensorAddr.get()).compareTo(String(buf))) {
|
||||||
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(temp));
|
if(withMQTT){
|
||||||
|
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(temp));
|
||||||
|
}
|
||||||
Serial << "Lipo Temperatur " << temp << " °C " << endl;
|
Serial << "Lipo Temperatur " << temp << " °C " << endl;
|
||||||
} else if (String(waterSensorAddr.get()).compareTo(String(buf))) {
|
} else if (String(waterSensorAddr.get()).compareTo(String(buf))) {
|
||||||
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(temp));
|
if(withMQTT){
|
||||||
|
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(temp));
|
||||||
|
}
|
||||||
Serial << "Water Temperatur " << temp << " °C " << endl;
|
Serial << "Water Temperatur " << temp << " °C " << endl;
|
||||||
}
|
}
|
||||||
/* Always send the sensor address with the temperatur value */
|
/* Always send the sensor address with the temperatur value */
|
||||||
sensorTemp.setProperty(String(buf)).send(String(temp));
|
if(withMQTT){
|
||||||
|
sensorTemp.setProperty(String(buf)).send(String(temp));
|
||||||
|
}
|
||||||
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
|
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update battery chip data
|
|
||||||
battery.update();
|
battery.update();
|
||||||
mSolarVoltage = battery.getVoltage(BATTSENSOR_INDEX_SOLAR) * SOLAR_VOLT_FACTOR;
|
mSolarVoltage = battery.getVoltage(BATTSENSOR_INDEX_SOLAR) * SOLAR_VOLT_FACTOR;
|
||||||
|
|
||||||
|
Serial.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
|
||||||
|
* These sensors (ADC2) can only be read when no Wifi is used.
|
||||||
|
*/
|
||||||
|
void readPowerSwitchedSensors()
|
||||||
|
{
|
||||||
|
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
|
||||||
for (int readCnt = 0; readCnt < AMOUNT_SENOR_QUERYS; readCnt++)
|
for (int readCnt = 0; readCnt < AMOUNT_SENOR_QUERYS; readCnt++)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < MAX_PLANTS; i++)
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
@ -233,7 +217,31 @@ void readSensors()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Read the distance and give the temperature sensors some time */
|
/* Read the distance and give the temperature sensors some time */
|
||||||
readDistance();
|
{
|
||||||
|
for (int i = 0; i < AMOUNT_SENOR_QUERYS; i++)
|
||||||
|
{
|
||||||
|
unsigned long duration = 0;
|
||||||
|
|
||||||
|
digitalWrite(SENSOR_TANK_TRG, HIGH);
|
||||||
|
delayMicroseconds(20);
|
||||||
|
cli();
|
||||||
|
digitalWrite(SENSOR_TANK_TRG, LOW);
|
||||||
|
//10ms is > 2m tank depth
|
||||||
|
duration = pulseIn(SENSOR_TANK_ECHO, HIGH, 10);
|
||||||
|
sei();
|
||||||
|
|
||||||
|
int mmDis = duration * 0.3432 / 2;
|
||||||
|
if (mmDis > MAX_TANK_DEPTH)
|
||||||
|
{
|
||||||
|
waterRawSensor.add(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
waterRawSensor.add(mmDis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl;
|
Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl;
|
||||||
|
|
||||||
/* deactivate the sensors */
|
/* deactivate the sensors */
|
||||||
@ -266,7 +274,6 @@ void onHomieEvent(const HomieEvent &event)
|
|||||||
break;
|
break;
|
||||||
case HomieEventType::OTA_STARTED:
|
case HomieEventType::OTA_STARTED:
|
||||||
Homie.getLogger() << "OTA started" << endl;
|
Homie.getLogger() << "OTA started" << endl;
|
||||||
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
|
|
||||||
digitalWrite(OUTPUT_ENABLE_PUMP, HIGH);
|
digitalWrite(OUTPUT_ENABLE_PUMP, HIGH);
|
||||||
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
||||||
gpio_deep_sleep_hold_dis();
|
gpio_deep_sleep_hold_dis();
|
||||||
@ -373,33 +380,40 @@ void setup()
|
|||||||
setCpuFrequencyMhz(80);
|
setCpuFrequencyMhz(80);
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial << endl
|
|
||||||
<< endl;
|
Serial <<"Wifi mode set to " << WIFI_OFF << " to allow analog2 useage " << endl;
|
||||||
|
WiFi.mode(WIFI_OFF);
|
||||||
|
Serial.flush();
|
||||||
|
|
||||||
/* Intialize Plant */
|
/* Intialize Plant */
|
||||||
for (int i = 0; i < MAX_PLANTS; i++)
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
{
|
{
|
||||||
mPlants[i].init();
|
mPlants[i].init();
|
||||||
}
|
}
|
||||||
|
Serial.println("plants init");
|
||||||
|
Serial.flush();
|
||||||
// read button
|
// read button
|
||||||
pinMode(BUTTON, INPUT);
|
pinMode(BUTTON, INPUT);
|
||||||
|
|
||||||
// Power pins
|
// Power pins
|
||||||
pinMode(OUTPUT_ENABLE_PUMP, OUTPUT);
|
pinMode(OUTPUT_ENABLE_PUMP, OUTPUT);
|
||||||
|
digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
|
||||||
pinMode(OUTPUT_ENABLE_SENSOR, OUTPUT);
|
pinMode(OUTPUT_ENABLE_SENSOR, OUTPUT);
|
||||||
|
|
||||||
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS)
|
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS)
|
||||||
{
|
{
|
||||||
//increase the config settings to 50 and the json to 3000
|
//increase the config settings to 50 and the json to 3000
|
||||||
Serial << "Limits.hpp" << endl;
|
Serial << "Limits.hpp is not adjusted, please search for this string and increase" << endl;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/************************* Start One-Wire bus ***************/
|
/************************* Start One-Wire bus ***************/
|
||||||
int timeoutTemp = millis() + TEMPERATUR_TIMEOUT;
|
int tempInitStartTime = millis();
|
||||||
uint8_t sensorCount = 0U;
|
uint8_t sensorCount = 0U;
|
||||||
|
|
||||||
/* Required to read the temperature at least once */
|
/* Required to read the temperature at least once */
|
||||||
while ((sensorCount == 0 || !battery.isFound()) && millis() < timeoutTemp)
|
while ((sensorCount == 0 || !battery.isFound()) && millis() < tempInitStartTime + TEMPERATUR_TIMEOUT)
|
||||||
{
|
{
|
||||||
sensors.begin();
|
sensors.begin();
|
||||||
battery.begin();
|
battery.begin();
|
||||||
@ -407,17 +421,20 @@ void setup()
|
|||||||
delay(50);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial << "One wire count: " << sensorCount << " found in " << (millis() - timeoutTemp) << "ms" << endl;
|
Serial << "One wire count: " << sensorCount << " found in " << (millis() - tempInitStartTime) << "ms" << endl;
|
||||||
|
Serial.flush();
|
||||||
/* Measure temperature TODO idea: move this into setup */
|
/* Measure temperature TODO idea: move this into setup */
|
||||||
if (sensorCount > 0)
|
if (sensorCount > 0)
|
||||||
{
|
{
|
||||||
sensors.setResolution(DS18B20_RESOLUTION);
|
sensors.setResolution(DS18B20_RESOLUTION);
|
||||||
sensors.requestTemperatures();
|
sensors.requestTemperatures();
|
||||||
}
|
}
|
||||||
|
Serial << "Reading sensors start" << endl;
|
||||||
|
Serial.flush();
|
||||||
|
readPowerSwitchedSensors();
|
||||||
|
Serial << "Reading sensors end" << endl;
|
||||||
|
Serial.flush();
|
||||||
/************************* Start Homie Framework ***************/
|
/************************* Start Homie Framework ***************/
|
||||||
WiFi.mode(WIFI_STA);
|
|
||||||
|
|
||||||
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
||||||
|
|
||||||
// Set default values
|
// Set default values
|
||||||
@ -439,11 +456,14 @@ void setup()
|
|||||||
Homie.setLoopFunction(homieLoop);
|
Homie.setLoopFunction(homieLoop);
|
||||||
Homie.onEvent(onHomieEvent);
|
Homie.onEvent(onHomieEvent);
|
||||||
//Homie.disableLogging();
|
//Homie.disableLogging();
|
||||||
|
|
||||||
Homie.setup();
|
Homie.setup();
|
||||||
|
|
||||||
mConfigured = Homie.isConfigured();
|
mConfigured = Homie.isConfigured();
|
||||||
if (mConfigured)
|
if (mConfigured)
|
||||||
{
|
{
|
||||||
|
Serial <<"Wifi mode set to " << WIFI_STA << endl;
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
for (int i = 0; i < MAX_PLANTS; i++)
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
{
|
{
|
||||||
mPlants[i].advertise();
|
mPlants[i].advertise();
|
||||||
@ -484,6 +504,7 @@ void setup()
|
|||||||
mDownloadMode = true;
|
mDownloadMode = true;
|
||||||
}
|
}
|
||||||
stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler);
|
stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler);
|
||||||
|
setupFinishedTimestamp = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -492,6 +513,7 @@ void setup()
|
|||||||
*/
|
*/
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
Homie.loop();
|
||||||
/* Toggel Senor LED to visualize mode 3 */
|
/* Toggel Senor LED to visualize mode 3 */
|
||||||
if (mDownloadMode)
|
if (mDownloadMode)
|
||||||
{
|
{
|
||||||
@ -503,12 +525,14 @@ void loop()
|
|||||||
}
|
}
|
||||||
else if (!mDeepsleep)
|
else if (!mDeepsleep)
|
||||||
{
|
{
|
||||||
Homie.loop();
|
|
||||||
if ((millis() > MQTT_TIMEOUT) && (!mSensorsRead)) {
|
unsigned long timeSinceSetup = millis() - setupFinishedTimestamp;
|
||||||
|
if ((timeSinceSetup > MQTT_TIMEOUT) && (!mSensorsRead)) {
|
||||||
mSensorsRead = true;
|
mSensorsRead = true;
|
||||||
/* Disable Wifi and put modem into sleep mode */
|
/* Disable Wifi and put modem into sleep mode */
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
Serial << (millis() / 1000) << "s passed, read sensors manually" << endl;
|
Serial <<"Wifi mode set to " << WIFI_OFF << " mqqt was no reached within " << timeSinceSetup << "ms , fallback to offline mode " << endl;
|
||||||
|
Serial.flush();
|
||||||
plantcontrol();
|
plantcontrol();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -535,14 +559,6 @@ void loop()
|
|||||||
*/
|
*/
|
||||||
void plantcontrol()
|
void plantcontrol()
|
||||||
{
|
{
|
||||||
digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
|
|
||||||
for (int i = 0; i < MAX_PLANTS; i++)
|
|
||||||
{
|
|
||||||
mPlants[i].deactivatePump();
|
|
||||||
}
|
|
||||||
|
|
||||||
readSensors();
|
|
||||||
|
|
||||||
if (lastPumpRunning != -1)
|
if (lastPumpRunning != -1)
|
||||||
{
|
{
|
||||||
long waterDiff = waterRawSensor.getAverage() - lastWaterValue;
|
long waterDiff = waterRawSensor.getAverage() - lastWaterValue;
|
||||||
@ -551,6 +567,8 @@ void plantcontrol()
|
|||||||
Serial << "Plant" << lastPumpRunning << ": Water diff " << waterDiff << " mm" << endl;
|
Serial << "Plant" << lastPumpRunning << ": Water diff " << waterDiff << " mm" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
readOneWireSensors(true);
|
||||||
|
|
||||||
for (int i = 0; i < MAX_PLANTS; i++)
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
{
|
{
|
||||||
long raw = mPlants[i].getCurrentMoisture();
|
long raw = mPlants[i].getCurrentMoisture();
|
||||||
|
@ -14,6 +14,4 @@ board = esp32doit-devkit-v1
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
|
build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
|
||||||
lib_deps = OneWire
|
lib_deps = OneWire
|
||||||
DallasTemperature
|
DallasTemperature
|
||||||
|
|
||||||
upload_port = /dev/ttyUSB1
|
|
Loading…
Reference in New Issue
Block a user