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++)
{
mPlants[i].addSenseValue();
}
delay(20);
mPlants[i].stopMoistureMeasurement();
}
waterRawSensor.clear();

View File

@ -1,174 +1,52 @@
#include <Arduino.h>
#include "esp_sleep.h"
#include "DallasTemperature.h"
#include "DS2438.h"
#include "driver/pcnt.h"
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
#define OUTPUT_SENSOR 14 /**< GPIO 16 - Enable Sensors */
#define SENSOR_PLANT5 39 /**< SENSOR vn */
#define SENSOR_PLANT6 36 /**< SENSOR VP */
#define SENSOR_DS18B20 2 /**< GPIO 2 */
#define OUTPUT_PUMP0 17 /**< GPIO 23 */
#define OUTPUT_PUMP1 05 /**< GPIO 22 */
#define OUTPUT_PUMP2 18 /**< GPIO 21 */
#define OUTPUT_PUMP3 19 /**< GPIO 19 */
#define OUTPUT_PUMP4 21 /**< GPIO 18 */
#define OUTPUT_PUMP5 15 /**< GPIO 5 */
#define OUTPUT_PUMP6 23 /**< GPIO 15 */
#define OUTPUT_SENSOR 16 /**< GPIO 16 - Enable Sensors */
#define OUTPUT_PUMP 13 /**< GPIO 13 - Enable Pumps */
#define SENSOR_PLANT0 32 /**< GPIO 32 (ADC1) */
#define ADC_TO_VOLT(adc) ((adc) * 3.3 ) / 4095)
#define ADC_TO_VOLT_WITH_MULTI(adc, multi) (((adc) * 3.3 * (multi)) / 4095)
#define SOLAR_VOLT(adc) ADC_TO_VOLT_WITH_MULTI(adc, 4.0306) /**< 100k and 33k voltage dividor */
#define ADC_5V_TO_3V3(adc) ADC_TO_VOLT_WITH_MULTI(adc, 1.7) /**< 33k and 47k8 voltage dividor */
RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR int pumpActive = 0;
int secondBootCount = 0;
OneWire oneWire(SENSOR_DS18B20);
DallasTemperature temp(&oneWire);
DS2438 battery(&oneWire,0.1f);
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason){
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\r\n",wakeup_reason); break;
}
}
bool whatever = true;
void setAll2to(int state) {
Serial.println("Set GPIO" + String(OUTPUT_PUMP0) + "=" + String(state));
digitalWrite(OUTPUT_PUMP0, state);
Serial.println("Set GPIO" + String(OUTPUT_PUMP1) + "=" + String(state));
digitalWrite(OUTPUT_PUMP1, state);
Serial.println("Set GPIO" + String(OUTPUT_PUMP2) + "=" + String(state));
digitalWrite(OUTPUT_PUMP2, state);
Serial.println("Set GPIO" + String(OUTPUT_PUMP3) + "=" + String(state));
digitalWrite(OUTPUT_PUMP3, state);
Serial.println("Set GPIO" + String(OUTPUT_PUMP4) + "=" + String(state));
digitalWrite(OUTPUT_PUMP4, state);
Serial.println("Set GPIO" + String(OUTPUT_PUMP5) + "=" + String(state));
digitalWrite(OUTPUT_PUMP5, state);
Serial.println("Set GPIO" + String(OUTPUT_PUMP6) + "=" + String(state));
digitalWrite(OUTPUT_PUMP6, state);
Serial.println("Set GPIO" + String(OUTPUT_SENSOR) + "=" + String(state));
digitalWrite(OUTPUT_SENSOR, state);
}
int16_t pulses = 0;
int16_t pulses2 = 0;
void setup() {
Serial.begin(115200);
pinMode(OUTPUT_PUMP0, OUTPUT);
pinMode(OUTPUT_PUMP1, OUTPUT);
pinMode(OUTPUT_PUMP2, OUTPUT);
pinMode(OUTPUT_PUMP3, OUTPUT);
pinMode(OUTPUT_PUMP4, OUTPUT);
pinMode(OUTPUT_PUMP5, OUTPUT);
pinMode(OUTPUT_PUMP6, OUTPUT);
pinMode(OUTPUT_SENSOR, OUTPUT);
pinMode(OUTPUT_PUMP, OUTPUT);
pinMode(SENSOR_PLANT0, ANALOG);
pinMode(SENSOR_PLANT5, INPUT);
//Increment boot number and print it every reboot
++bootCount;
++secondBootCount;
Serial.println("Boot number: " + String(bootCount) + " " + String(secondBootCount));
pcnt_config_t pcnt_config = { }; // Instancia PCNT config
//Print the wakeup reason for ESP32
print_wakeup_reason();
Serial.println("------- build from " + String(__DATE__) + "=" + String(__TIME__) + " @ " + String(millis()) + "ms");
Serial.println("Set GPIO" + String(OUTPUT_PUMP) + "=" + String(LOW));
digitalWrite(OUTPUT_PUMP, LOW);
pcnt_config.pulse_gpio_num = SENSOR_PLANT5; // Configura GPIO para entrada dos pulsos
pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem
pcnt_config.unit = PCNT_UNIT_0; // 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
setAll2to(HIGH);
delay(1000);
Serial.println("--------------------------" + String(" @ ") + String(millis()) + "ms");
setAll2to(LOW);
delay(1000);
Serial.println("--------------------------" + String(" @ ") + String(millis()) + "ms");
/* activate power pump and pump 0 */
pcnt_counter_pause(PCNT_UNIT_0); // Pausa o contador PCNT
pcnt_counter_clear(PCNT_UNIT_0); // Zera o contador PCNT
digitalWrite(OUTPUT_SENSOR, HIGH);
delay(1);
temp.begin();
battery.begin();
Serial.print("Battery");
Serial.print("\t");
Serial.print("Solar");
Serial.print("\t");
Serial.print("Bat I");
Serial.print("\t");
Serial.println("Temp/10");
Serial.println("Start done");
}
void loop() {
static int loop=1;
pulses2 = pulseIn(SENSOR_PLANT5,HIGH);
pcnt_counter_resume(PCNT_UNIT_0);
DeviceAddress t;
for(int i=0; i < sizeof(t); i++) {
t[i] = loop + i*2;
}
char buf[sizeof(DeviceAddress)*2];
snprintf(buf, sizeof(buf), "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X", t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
/*
%X -> linksbündige hexzahl
%2X -> 2Stellige hexzahl ... dynamisch erweitert
%0.2X -> 2stellige hexzahl mit führerder "0"
*/
printf("Print: %s\n", buf);
loop++;
delay(500);
return;
whatever = !whatever;
digitalWrite(OUTPUT_PUMP, HIGH);
delay(500);
digitalWrite(OUTPUT_PUMP6, HIGH);
pcnt_counter_pause(PCNT_UNIT_0);
pcnt_get_counter_value(PCNT_UNIT_0, &pulses);
pcnt_counter_clear(PCNT_UNIT_0);
for(int j=0; j < 5 && temp.getDeviceCount() == 0; j++) {
delay(10);
// Serial.println("Reset 1wire temp");
temp.begin();
}
for(int j=0; j < 5 && (0 == battery.isFound()); j++) {
delay(10);
Serial.println("Reset 1wire bat");
battery.begin();
battery.update();
}
battery.update();
Serial.print(battery.getVoltage(0)); //use define here, solar
Serial.print("\t");
Serial.print(battery.getVoltage(1)); //use define here, battery
Serial.print("\t");
Serial.print(battery.getCurrent());
Serial.print("\t");
Serial.println(battery.getTemperature()/10);
Serial.println(pulses2*2);
Serial.println(pulses*2);
}