move anemometer to class
This commit is contained in:
parent
da46dd1a77
commit
4a4faf68d4
|
@ -0,0 +1,80 @@
|
|||
|
||||
#include "sensor_anemometer.h"
|
||||
|
||||
//uses ATS177 Latched hall sensor for rotation sensing
|
||||
|
||||
/*
|
||||
Sensor_Anemometer* Sensor_Anemometer_Instance;
|
||||
|
||||
|
||||
void assignAnemometerObject(Sensor_Anemometer* p) {
|
||||
Sensor_Anemometer_Instance = p;
|
||||
}*/
|
||||
/*
|
||||
void sensor_anemometer_interrupt() {
|
||||
Sensor_Anemometer_Instance->interrupt_anemometer();
|
||||
}*/
|
||||
|
||||
Sensor_Anemometer::Sensor_Anemometer(int p)
|
||||
{
|
||||
pin=p;
|
||||
}
|
||||
|
||||
void Sensor_Anemometer::init() //Things to be done during setup()
|
||||
{
|
||||
pinMode(pin,INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(pin),interrupt_anemometer,CHANGE); //anemometer interrupt
|
||||
init_ok=true;
|
||||
}
|
||||
|
||||
//Also called during setup()
|
||||
void Sensor_Anemometer::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||
{
|
||||
data.minchange=minchange;
|
||||
data.senddelaymax=senddelaymax;
|
||||
data.readdelay=readdelay;
|
||||
}
|
||||
|
||||
//Called during setup
|
||||
void Sensor_Anemometer::advertise(HomieNode& p_sensorNode)
|
||||
{
|
||||
sensorNode = &p_sensorNode;
|
||||
sensorNode->advertise("windspeed");
|
||||
}
|
||||
|
||||
void Sensor_Anemometer::sensorloop()
|
||||
{
|
||||
if (init_ok) {
|
||||
sensordata &d=data;
|
||||
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
uint16_t _anepulsesPerMinute=anemometer_pulsecounter/((millis()-anemometer_lasttimereset)/60000.0);
|
||||
d.value = _anepulsesPerMinute*SENSOR_ANEMOMETER_PPMtoMPS;
|
||||
|
||||
if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending windspeed. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
|
||||
Homie.getLogger() << "windspeed tcs " << ": " << d.value << endl;
|
||||
sensorNode->setProperty("windspeed").send(String(d.value));
|
||||
|
||||
//reset when sent. makes it more accurate but keeps fast response
|
||||
anemometer_pulsecounter=0; //reset counter
|
||||
anemometer_lasttimereset=millis();
|
||||
d.lastreadtime=millis(); //also set lastread time to avoid having 1 count with a low time = high windspeed
|
||||
|
||||
d.lastsentvalue=d.value;
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#ifndef SENSOR_Anemometer_H
|
||||
#define SENSOR_Anemometer_H
|
||||
|
||||
#include "sensordata.h"
|
||||
#include <Homie.h>
|
||||
|
||||
void ICACHE_RAM_ATTR interrupt_anemometer();
|
||||
|
||||
class Sensor_Anemometer
|
||||
{
|
||||
|
||||
private:
|
||||
HomieNode *sensorNode; //reference to HomieNode
|
||||
|
||||
int pin;
|
||||
|
||||
struct sensordata data; //struct values are changed in setup()
|
||||
//value in [m/s]
|
||||
|
||||
bool init_ok;
|
||||
|
||||
unsigned long anemometer_lasttimereset=0;
|
||||
|
||||
|
||||
#define ANEMOMETER_DEBOUNCETIME 15 //15ms between pulses is approx 85m/s windspeed
|
||||
|
||||
|
||||
public:
|
||||
Sensor_Anemometer(int p);
|
||||
|
||||
|
||||
void init();
|
||||
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||
void advertise(HomieNode& p_sensorNode);
|
||||
void sensorloop();
|
||||
//void ICACHE_RAM_ATTR interrupt_anemometer();
|
||||
|
||||
uint16_t anemometer_pulsecounter=0; //counted pulses since last reset
|
||||
unsigned long anemometer_lastpulse_fordebounce=0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -51,11 +51,11 @@ build_flags =
|
|||
-D SENSOR_ML8511_minchange=0.2
|
||||
|
||||
-D SENSOR_ANEMOMETER
|
||||
-D ANEMOMETERPIN=D6 #Light Blue thicker cable (in distribution box)
|
||||
-D dataAnemometer_minchange=0.25
|
||||
-D dataAnemometer_readdelay=1000*30
|
||||
-D dataAnemometer_senddelaymax=1000*60*5
|
||||
-D ANEMOMETER_PPMtoMPS=0.0208640462
|
||||
-D SENSOR_Anemometer_PIN=D6 #Light Blue thicker cable (in distribution box)
|
||||
-D SENSOR_Anemometer_minchange=0.25
|
||||
-D SENSOR_Anemometer_readdelay=1000*30
|
||||
-D SENSOR_Anemometer_senddelaymax=1000*60*5
|
||||
-D SENSOR_ANEMOMETER_PPMtoMPS=0.0208640462
|
||||
# Cable from Anemometer: Black=GND, Blue=3v3, Brown=Signal (needs pullup (internal))
|
||||
|
||||
-D SENSOR_RAINGAUGE
|
||||
|
|
95
src/main.cpp
95
src/main.cpp
|
@ -265,18 +265,27 @@
|
|||
#endif
|
||||
|
||||
#ifdef SENSOR_ANEMOMETER
|
||||
//uses ATS177 Latched hall sensor for rotation sensing
|
||||
sensordata dataAnemometer;
|
||||
unsigned long anemometer_lasttimereset=0;
|
||||
uint16_t anemometer_pulsecounter=0; //counted pulses since last reset
|
||||
#include "sensor_anemometer.cpp"
|
||||
//Sensor_Anemometer_Instance = new Sensor_Anemometer(SENSOR_Anemometer_PIN);
|
||||
Sensor_Anemometer sensor_anemometer(SENSOR_Anemometer_PIN);
|
||||
|
||||
#define ANEMOMETER_DEBOUNCETIME 15 //15ms between pulses is approx 85m/s windspeed
|
||||
unsigned long anemometer_lastpulse_fordebounce=0;
|
||||
void ICACHE_RAM_ATTR interrupt_anemometer()
|
||||
{
|
||||
if (millis() - sensor_anemometer.anemometer_lastpulse_fordebounce >= ANEMOMETER_DEBOUNCETIME) { //ignore if pulse came too fast
|
||||
sensor_anemometer.anemometer_pulsecounter++;
|
||||
sensor_anemometer.anemometer_lastpulse_fordebounce=millis();
|
||||
}
|
||||
}
|
||||
|
||||
float value_anemometer=0; // [m/s]
|
||||
|
||||
void ICACHE_RAM_ATTR interrupt_anemometer();
|
||||
void updateAnemometer();
|
||||
#ifndef SENSOR_Anemometer_minchange
|
||||
#define SENSOR_Anemometer_minchange 0.25
|
||||
#endif
|
||||
#ifndef SENSOR_Anemometer_senddelaymax
|
||||
#define SENSOR_Anemometer_senddelaymax 1000*60*5
|
||||
#endif
|
||||
#ifndef SENSOR_Anemometer_readdelay
|
||||
#define SENSOR_Anemometer_readdelay 1000*30
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_RAINGAUGE
|
||||
|
@ -401,17 +410,8 @@ void setup() {
|
|||
#endif
|
||||
|
||||
#ifdef SENSOR_ANEMOMETER
|
||||
pinMode(ANEMOMETERPIN,INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(ANEMOMETERPIN),interrupt_anemometer,CHANGE); //anemometer interrupt
|
||||
#ifdef dataAnemometer_minchange
|
||||
dataAnemometer.minchange=dataAnemometer_minchange;
|
||||
#endif
|
||||
#ifdef dataAnemometer_readdelay
|
||||
dataAnemometer.readdelay=dataAnemometer_readdelay;
|
||||
#endif
|
||||
#ifdef dataAnemometer_senddelaymax
|
||||
dataAnemometer.senddelaymax=dataAnemometer_senddelaymax;
|
||||
#endif
|
||||
sensor_anemometer.init();
|
||||
sensor_anemometer.setSettings(SENSOR_Anemometer_minchange,SENSOR_Anemometer_senddelaymax,SENSOR_Anemometer_readdelay);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_RAINGAUGE
|
||||
|
@ -493,7 +493,7 @@ void setup() {
|
|||
#endif
|
||||
|
||||
#ifdef SENSOR_ANEMOMETER
|
||||
sensorNode.advertise("windspeed");
|
||||
sensor_anemometer.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_RAINGAUGE
|
||||
|
@ -512,43 +512,6 @@ void loop() {
|
|||
}
|
||||
|
||||
|
||||
#ifdef SENSOR_ANEMOMETER
|
||||
void loop_anemometer()
|
||||
{
|
||||
sensordata &d=dataAnemometer;
|
||||
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
uint16_t _anepulsesPerMinute=anemometer_pulsecounter/((millis()-anemometer_lasttimereset)/60000.0);
|
||||
value_anemometer = _anepulsesPerMinute*ANEMOMETER_PPMtoMPS;
|
||||
|
||||
if (abs((int)d.lastsentvalue-value_anemometer)>=d.minchange){ //int abs
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending windspeed. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
checkESPStatus();
|
||||
|
||||
Homie.getLogger() << "windspeed tcs " << ": " << value_anemometer << endl;
|
||||
sensorNode.setProperty("windspeed").send(String(value_anemometer));
|
||||
|
||||
//reset when sent. makes it more accurate but keeps fast response
|
||||
anemometer_pulsecounter=0; //reset counter
|
||||
anemometer_lasttimereset=millis();
|
||||
d.lastreadtime=millis(); //also set lastread time to avoid having 1 count with a low time = high windspeed
|
||||
|
||||
d.lastsentvalue=value_anemometer;
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_RAINGAUGE
|
||||
void loop_raingauge()
|
||||
{
|
||||
|
@ -654,7 +617,7 @@ void loopHandler() {
|
|||
#endif
|
||||
|
||||
#ifdef SENSOR_ANEMOMETER
|
||||
loop_anemometer();
|
||||
sensor_anemometer.sensorloop();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_RAINGAUGE
|
||||
|
@ -675,16 +638,6 @@ void checkESPStatus()
|
|||
|
||||
|
||||
|
||||
#ifdef SENSOR_ANEMOMETER
|
||||
void ICACHE_RAM_ATTR interrupt_anemometer()
|
||||
{
|
||||
if (millis() - anemometer_lastpulse_fordebounce >= ANEMOMETER_DEBOUNCETIME) { //ignore if pulse came too fast
|
||||
anemometer_pulsecounter++;
|
||||
anemometer_lastpulse_fordebounce=millis();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_RAINGAUGE
|
||||
void ICACHE_RAM_ATTR interrupt_raingauge()
|
||||
{
|
||||
|
@ -727,4 +680,4 @@ int get_mapped(const unsigned int* _in, const unsigned int* _out, byte size,int
|
|||
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
|
||||
{
|
||||
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue