implement anemometer

This commit is contained in:
interfisch 2020-10-30 17:51:03 +01:00
parent 913a586eeb
commit 691c77ee98
2 changed files with 110 additions and 6 deletions

View File

@ -34,7 +34,7 @@ build_flags =
-D dataBMP180_pressure_minchange=0.5
-D SENSOR_TCS34725
-D dataTCS34725_lux_minchange=20
-D dataTCS34725_lux_minchange=10
-D dataTCS34725_colortemp_minchange=100
-D SENSOR_HS1101
@ -45,6 +45,13 @@ build_flags =
-D ML8511PIN=A0
-D dataML8511_minchange=0.05
-D SENSOR_ANEMOMETER
-D ANEMOMETERPIN=D6 #Light Blue thicker cable (in distribution box)
-D dataAnemometer_minchange=0.5
-D dataAnemometer_readdelay=1000*10
-D dataAnemometer_senddelaymax=1000*60*2
# Cable from Anemometer: Black=GND, Blue=3v3, Brown=Signal (needs pullup (internal))
lib_deps =
Adafruit BMP085 Library@1.1.0
https://github.com/adafruit/Adafruit_TCS34725

View File

@ -77,7 +77,7 @@ struct sensordata
sensordata dataML8511;
float getUV_ML8511(int pin);
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max);
float value_uvML8511=0;
float value_uvML8511=0; //uvIntensity (mW/cm^2)
#endif
@ -179,17 +179,35 @@ struct sensordata
struct sensordata dataTCS34725_lux;
struct sensordata dataTCS34725_colortemp;
uint16_t value_colortemp, value_tcs_lux, value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c;
unsigned long lastread_tcs34725=0;
#endif
#ifdef SENSOR_ANEMOMETER
// ## ANEMOMETER ##
sensordata dataAnemometer;
//unsigned long anemometer_timeLastReset=0;
unsigned long anemometer_lasttimereset=0;
uint16_t anemometer_pulsecounter=0; //counted pulses since last reset
//boolean anemometer_update_flag=false;
//unsigned long anemometer_lastPulse=0; //time for max calculation
//uint16_t anemometer_mintime=65535; //minimum time for this minute
//#define ANEMOMETER_MINTIMES_SIZE 4
//uint8_t anemometer_mintimepos=0;
//uint16_t anemometer_mintimes[ANEMOMETER_MINTIMES_SIZE];
#define ANEMOMETER_PPMtoMPS 0.0208640462;
float value_anemometer=0; // [m/s]
void ICACHE_RAM_ATTR interrupt_anemometer();
void updateAnemometer();
#endif
// data/homie/config.json hochladen mit platformio run --target uploadfs
// config contains homie device name, mqtt ip and wifi credentials
HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type
char tempstring[16]; //for dtostrf
@ -339,6 +357,22 @@ void setup() {
#endif
#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
#endif
//Homie_setFirmware(FW_NAME, FW_VERSION);
//Homie_setBrand(FW_NAME);
@ -406,6 +440,10 @@ void setup() {
#endif
sensorNode.advertise("colortemp");
#endif
#ifdef SENSOR_ANEMOMETER
sensorNode.advertise("windspeed");
#endif
Serial.println("connecting..");
@ -866,6 +904,54 @@ void loop_TCS34725_colortemp()
}
#endif
#ifdef SENSOR_ANEMOMETER
void loop_anemometer()
{
/*
if (anemometer_update_flag) {
anemometer_update_flag=false;
//anemometer_mintime=0;
for (int i=0;i<ANEMOMETER_MINTIMES_SIZE;i++){
if (anemometer_mintime<anemometer_mintimes[i] && anemometer_mintimes[i]!=0){ //use longest time (=slowest speed) in array as max speed, reduces false readings. =0 -> initial value, ignore (not enough roations counted)
anemometer_mintime=anemometer_mintimes[i];
}
}
}*/
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
void loopHandler() {
@ -919,7 +1005,9 @@ void loopHandler() {
}
#endif
#ifdef SENSOR_ANEMOMETER
loop_anemometer();
#endif
}
@ -1080,6 +1168,15 @@ float getUV_ML8511(int pin) {
#endif
#ifdef SENSOR_ANEMOMETER
void ICACHE_RAM_ATTR interrupt_anemometer()
{
anemometer_pulsecounter++;
}
#endif
/*##################################
* ######## HELPER FUNCTIONS ########
*/