weatherstation/firmware/sensors.ino
2019-08-04 12:27:37 +02:00

77 lines
1.9 KiB
C++

#include "config_user.h"
int anemometerRotations = 0;
unsigned long currentTime = 0;
float fetchTemperature() {
//return 10;
return bme.readTemperature();
}
float fetchPressure() {
//return 10;
return bme.readPressure() / 100.0F;
}
float fetchHumidity() {
return bme.readHumidity() * HUMIDITY_FACTOR;
}
float fetchLight() {
//TODO read values
uint16_t red, green, blue, white, lux;
while(!apds.colorDataReady()) {
delay(5);
}
apds.getColorData(&red, &green, &blue, &white);
//calculate lux
lux = apds.calculateLux(red, green, blue);
return lux * LIGHT_FACTOR;
}
ICACHE_RAM_ATTR void _anemometerInterrupt() {
anemometerRotations++;
#ifdef DEBUG
Serial.print("*");
#endif
}
float fetchWindspeed() {
anemometerRotations = 0;
currentTime = millis();
int interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN);
attachInterrupt(interruptNumber, _anemometerInterrupt, RISING);
delay(1000 * 5);
detachInterrupt(interruptNumber);
return (float)anemometerRotations / 5.0 * 2.4;
}
// Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/
#ifdef BATTERY_POWERED
float getBatteryVoltage() {
// ESP8266 ADC pin input voltage range ist 0V .. 1V
// The Wemos D1 mini does already contain a voltage divider circuit: A0(Wemos PCB) -- [220kOhm] -- ADC (ESP8266)-- [100kOhm] -- GND
// The (+) pole of the battery is connected to the A0 pin of the Wemos board through a additional 100kOhm resistance.
// The battery voltage of 4.2V max is measured as 1.0V on ESP8266 ADC pin.
uint16_t raw = analogRead(A0);
float volt = raw / 1023.0;
return volt * 4.2;
}
float isBatCharging() {
if (LOW == digitalRead(BAT_CHARGING_PIN))
{
return BAT_CHARGE_STATE_CHARGING;
} else if (LOW == digitalRead(BAT_CHARGED_PIN))
{
return BAT_CHARGE_STATE_CHARGED;
}
return BAT_CHARGE_STATE_NOTCHARGING;
}
#endif