Reading sensors by frequency

This commit is contained in:
c3ma
2021-07-21 21:23:58 +02:00
parent 2d167c7393
commit d043d873cc
5 changed files with 82 additions and 183 deletions

View File

@@ -80,8 +80,8 @@
*/
#define FIRMWARE_VERSION "sw 1.3 hw 0.10"
#define MOIST_SENSOR_MAX_ADC 2800 //swamp earth - 50 margin
#define MOIST_SENSOR_MIN_ADC 1200 //dry earth + 1500 margin
#define MOIST_SENSOR_MAX_FRQ 60000 // 60kHz (500Hz margin)
#define MOIST_SENSOR_MIN_FRQ 1000 // 1kHz (500Hz margin)
#define SOLAR_VOLT_FACTOR 11
#define BATTSENSOR_INDEX_SOLAR 0

View File

@@ -18,13 +18,16 @@
#include "RunningMedian.h"
#include "MathUtils.h"
#define MOISTURE_MEASUREMENT_DURATION 500 /** ms */
class Plant
{
private:
RunningMedian moistureRaw = RunningMedian(5);
HomieNode *mPlant = NULL;
HomieInternals::PropertyInterface mPump;
int32_t mMoisture_freq = 0;
int mPinSensor = 0; /**< Pin of the moist sensor */
int mPinPump = 0; /**< Pin of the pump */
bool mConnected = false;
@@ -51,8 +54,8 @@ public:
* @brief Measure a new analog moister value
*
*/
void addSenseValue(void);
void clearMoisture(void);
void startMoistureMeasurement(void);
void stopMoistureMeasurement(void);
void deactivatePump(void);
@@ -79,10 +82,7 @@ public:
float getCurrentMoisture()
{
if(moistureRaw.getCount()==0){
return MISSING_SENSOR;
}
return this->moistureRaw.getMedian();
return mMoisture_freq;
}
long getSetting2Moisture()
@@ -90,7 +90,7 @@ public:
if (this->mSetting->pSensorDry != NULL)
{
float percent = (this->mSetting->pSensorDry->get());
return MOIST_SENSOR_MIN_ADC + ((MOIST_SENSOR_MAX_ADC - MOIST_SENSOR_MIN_ADC) * percent);
return (((MOIST_SENSOR_MAX_FRQ - MOIST_SENSOR_MIN_FRQ) * percent) + MOIST_SENSOR_MIN_FRQ);
}
else
{

View File

@@ -14,6 +14,7 @@
#include "ControllerConfiguration.h"
#include "TimeUtils.h"
#include "MathUtils.h"
#include "driver/pcnt.h"
double mapf(double x, double in_min, double in_max, double out_min, double out_max)
{
@@ -56,22 +57,44 @@ void Plant::init(void)
pinMode(this->mPinPump, OUTPUT);
Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG));
Serial.flush();
pinMode(this->mPinSensor, ANALOG);
pinMode(this->mPinSensor, INPUT);
Serial.println("Set GPIO " + String(mPinPump) + "=" + String(LOW));
Serial.flush();
digitalWrite(this->mPinPump, LOW);
pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId);
pcnt_config_t pcnt_config = { }; // Instancia PCNT config
pcnt_config.pulse_gpio_num = this->mPinSensor; // Configura GPIO para entrada dos pulsos
pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem
pcnt_config.unit = unit; // Unidade de contagem PCNT - 0
pcnt_config.channel = PCNT_CHANNEL_0; // Canal de contagem PCNT - 0
pcnt_config.counter_h_lim = INT16_MAX; // Limite maximo de contagem - 20000
pcnt_config.pos_mode = PCNT_COUNT_INC; // Incrementa contagem na subida do pulso
pcnt_config.neg_mode = PCNT_COUNT_DIS; // Incrementa contagem na descida do pulso
pcnt_config.lctrl_mode = PCNT_MODE_KEEP; // PCNT - modo lctrl desabilitado
pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT - modo hctrl - se HIGH conta incrementando
pcnt_unit_config(&pcnt_config); // Configura o contador PCNT
pcnt_counter_pause(unit); // Pausa o contador PCNT
pcnt_counter_clear(unit); // Zera o contador PCNT
Serial.println("Setup Counter " + String(mPinPump) + "=" + String(LOW));
}
void Plant::clearMoisture(void){
this->moistureRaw.clear();
void Plant::startMoistureMeasurement(void) {
pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId);
pcnt_counter_resume(unit);
}
void Plant::addSenseValue(void)
{
int raw = analogRead(this->mPinSensor);
if(raw < MOIST_SENSOR_MAX_ADC && raw > MOIST_SENSOR_MIN_ADC){
this->moistureRaw.add(raw);
}
void Plant::stopMoistureMeasurement(void) {
int16_t pulses;
pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId);
pcnt_counter_pause(unit);
pcnt_get_counter_value(unit, &pulses);
pcnt_counter_clear(unit);
this->mMoisture_freq = pulses * (1000 / MOISTURE_MEASUREMENT_DURATION);
}
void Plant::postMQTTconnection(void)
@@ -81,7 +104,7 @@ void Plant::postMQTTconnection(void)
this->mPlant->setProperty("switch").send(OFF);
long raw = getCurrentMoisture();
double pct = 100 - mapf(raw, MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100);
double pct = mapf(raw, MOIST_SENSOR_MIN_FRQ, MOIST_SENSOR_MAX_FRQ, 100, 0);
if (equalish(raw, MISSING_SENSOR))
{
pct = 0;

View File

@@ -34,6 +34,7 @@
#include <Wire.h>
#include <VL53L0X.h>
/******************************************************************************
* DEFINES
******************************************************************************/
@@ -260,19 +261,16 @@ void readOneWireSensors()
void readPowerSwitchedSensors()
{
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
delay(500);
delay(50);
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].clearMoisture();
mPlants[i].startMoistureMeasurement();
}
for (int readCnt = 0; readCnt < AMOUNT_SENOR_QUERYS; readCnt++)
delay(MOISTURE_MEASUREMENT_DURATION);
for (int i = 0; i < MAX_PLANTS; i++)
{
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].addSenseValue();
}
delay(20);
mPlants[i].stopMoistureMeasurement();
}
waterRawSensor.clear();