sht20 support

This commit is contained in:
Empire
2022-03-05 09:31:43 +00:00
parent a9c4c32a22
commit defbcca8ae
8 changed files with 121 additions and 74 deletions

View File

@@ -1,26 +1,29 @@
/**
* @file PlantCtrl.cpp
* @author your name (you@domain.com)
* @brief
* @brief
* @version 0.1
* @date 2020-05-27
*
*
* @copyright Copyright (c) 2020
*
*
*/
#include "PlantCtrl.h"
#include "ControllerConfiguration.h"
#include "TimeUtils.h"
#include "driver/pcnt.h"
#include "MQTTUtils.h"
Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting)
Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting, SENSOR_MODE mode)
{
this->mPinSensor = pinSensor;
this->mPinPump = pinPump;
this->mPlant = plant;
this->mSetting = setting;
this->mPlantId = plantId;
this->mSensorMode = mode;
this->sht20 = SHT2x();
}
void Plant::init(void)
@@ -30,10 +33,6 @@ void Plant::init(void)
this->mSetting->pSensorDry->setValidator([](long candidate)
{ return (((candidate >= 0.0) && (candidate <= 100.0)) || equalish(candidate, DEACTIVATED_PLANT) || equalish(candidate, HYDROPONIC_MODE)); });
this->mSetting->pSensorMode->setDefaultValue(NONE);
this->mSetting->pSensorMode->setValidator([](long candidate)
{ return candidate == NONE || candidate == CAPACITIVE_FREQUENCY || candidate == ANALOG_RESISTANCE_PROBE; });
this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00
this->mSetting->pPumpAllowedHourRangeStart->setValidator([](long candidate)
{ return ((candidate >= 0) && (candidate <= 23)); });
@@ -56,18 +55,14 @@ void Plant::init(void)
{ return ((candidate >= 0) && (candidate <= 100)); });
/* Initialize Hardware */
Serial.println("Init PWM controller for pump " + String(mPinPump) + "=" + String(OUTPUT));
Serial.flush();
ledcSetup(this->mPlantId, PWM_FREQ, PWM_BITS);
ledcAttachPin(mPinPump, this->mPlantId);
ledcWrite(this->mPlantId, 0);
Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG));
Serial.flush();
pinMode(this->mPinSensor, INPUT);
}
void Plant::initSensors(void){
void Plant::initSensors(void)
{
switch (getSensorMode())
{
case CAPACITIVE_FREQUENCY:
@@ -89,19 +84,17 @@ void Plant::initSensors(void){
pcnt_counter_pause(unit); // Pausa o contador PCNT
pcnt_counter_clear(unit); // Zera o contador PCNT
Serial.println("Setup Counter " + String(mPinPump) + "=" + String(LOW));
break;
}
case ANALOG_RESISTANCE_PROBE:
{
adcAttachPin(this->mPinSensor);
break;
}
case SHT20:
case NONE:
{
//do nothing
// do nothing
break;
}
}
@@ -117,13 +110,41 @@ void Plant::blockingMoistureMeasurement(void)
{
this->mMoisture_raw.add(analogReadMilliVolts(this->mPinSensor));
delay(5);
break;
}
break;
}
case SHT20:
{
//do not assume valid i2c state, reinit
TwoWire sensorWire = TwoWire(0);
sensorWire.setPins(SENSOR_TANK_SDA, SHARED_SCL);
sht20.begin(&sensorWire);
sht20.reset();
delay(100);
if(!sht20.isConnected()){
Serial.println("SHT20 connection error");
}
bool success = sht20.read();
int error = sht20.getError();
if (error)
{
log(LOG_LEVEL_ERROR, "Failure reading SHT20 " + String(error), LOG_SENSOR_MISSING);
}
if (!success || error)
{
this->mMoisture_raw.clear();
this->mMoisture_raw.add(MISSING_SENSOR);
return;
}
mMoisture_raw.add(sht20.getHumidity());
mTemperature_degree.add(sht20.getTemperature());
break;
}
case CAPACITIVE_FREQUENCY:
case NONE:
{
//nothing to do here
// nothing to do here
break;
}
}
@@ -139,10 +160,11 @@ void Plant::startMoistureMeasurement(void)
pcnt_counter_resume(unit);
break;
}
case SHT20:
case ANALOG_RESISTANCE_PROBE:
case NONE:
{
//do nothing here
// do nothing here
}
}
}
@@ -170,11 +192,8 @@ void Plant::stopMoistureMeasurement(void)
}
break;
}
case SHT20:
case ANALOG_RESISTANCE_PROBE:
{
//nothing to do here
break;
}
case NONE:
{
break;
@@ -280,4 +299,5 @@ void Plant::advertise(void)
this->mPlant->advertise("moist").setName("Percent").setDatatype("Float").setUnit("%");
this->mPlant->advertise("moistraw").setName("adc").setDatatype("Float").setUnit("3.3/4096V");
this->mPlant->advertise("state").setName("state").setDatatype("String");
}

View File

@@ -93,13 +93,13 @@ DS2438 battery(&oneWire, 0.0333333f, AMOUNT_SENOR_QUERYS);
VL53L0X tankSensor;
Plant mPlants[MAX_PLANTS] = {
Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0),
Plant(SENSOR_PLANT1, OUTPUT_PUMP1, 1, &plant1, &mSetting1),
Plant(SENSOR_PLANT2, OUTPUT_PUMP2, 2, &plant2, &mSetting2),
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3, &plant3, &mSetting3),
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4, &plant4, &mSetting4),
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5, &plant5, &mSetting5),
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6)};
Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0, SHT20),
Plant(SENSOR_PLANT1, OUTPUT_PUMP1, 1, &plant1, &mSetting1, ANALOG_RESISTANCE_PROBE),
Plant(SENSOR_PLANT2, OUTPUT_PUMP2, 2, &plant2, &mSetting2, CAPACITIVE_FREQUENCY),
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3, &plant3, &mSetting3, CAPACITIVE_FREQUENCY),
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4, &plant4, &mSetting4, CAPACITIVE_FREQUENCY),
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5, &plant5, &mSetting5, CAPACITIVE_FREQUENCY),
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6, CAPACITIVE_FREQUENCY)};
/******************************************************************************
* LOCAL FUNCTIONS
@@ -264,7 +264,6 @@ void readPowerSwitchedSensors()
{
mPlants[i].startMoistureMeasurement();
}
delay(MOISTURE_MEASUREMENT_DURATION);
for (int i = 0; i < MAX_PLANTS; i++)
{
@@ -286,18 +285,26 @@ void readPowerSwitchedSensors()
break;
}
case ANALOG_RESISTANCE_PROBE : {
Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " mV" << mPlants[i].getCurrentMoisturePCT() << "%" << endl;
Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " mV " << mPlants[i].getCurrentMoisturePCT() << "%" << endl;
break;
}
case SHT20:{
Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentTemperature() << "°C " << mPlants[i].getCurrentMoisturePCT() << "rH%" << endl;
break;
}
case NONE : {
}
}
}
waterRawSensor.clear();
Wire.setPins(SENSOR_TANK_TRG, SENSOR_TANK_ECHO);
//do not assume valid i2c state, force release of hardware
Wire = TwoWire(0);
Wire.setPins(SENSOR_TANK_SDA, SHARED_SCL);
Wire.begin();
waterRawSensor.clear();
tankSensor.setTimeout(500);
long start = millis();
bool distanceReady = false;
@@ -761,13 +768,17 @@ void safeSetup()
Homie.setLoopFunction(homieLoop);
Homie.onEvent(onHomieEvent);
Homie.setup();
/* Intialize Plant */
/* Intialize Plant */
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].initSensors();
}
readPowerSwitchedSensors();
Homie.setup();
/************************* Start One-Wire bus ***************/
int tempInitStartTime = millis();
@@ -794,14 +805,13 @@ void safeSetup()
mConfigured = Homie.isConfigured();
if (mConfigured)
{
Serial << "Reading sensors start" << endl;
Serial.flush();
readPowerSwitchedSensors();
Serial << "Reading sensors end" << endl;
Serial.flush();
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].advertise();
//write to temperature node instead
if(!equalish(mPlants[i].getCurrentTemperature(),PLANT_WITHOUT_TEMPSENSOR)){
mqttWrite(&sensorTemp, "Plant" + String(i), String(mPlants[i].getCurrentTemperature()));
}
}
mPlants[0].setSwitchHandler(switch1);
mPlants[1].setSwitchHandler(switch2);