weatherstation/firmware/sensors.ino

76 lines
1.9 KiB
Arduino
Raw Normal View History

2019-01-27 18:49:28 +01:00
#include "config_user.h"
2017-11-12 21:37:55 +01:00
int anemometerRotations = 0;
unsigned long currentTime = 0;
2017-11-07 22:20:14 +01:00
float fetchTemperature() {
2017-11-20 20:42:02 +01:00
return bme.readTemperature();
}
float fetchPressure() {
return bme.readPressure() / 100.0F;
2017-11-07 22:20:14 +01:00
}
float fetchHumidity() {
return bme.readHumidity() * HUMIDITY_FACTOR;
2017-11-07 22:20:14 +01:00
}
float fetchLight() {
2019-01-27 18:49:28 +01:00
//TODO read values
uint16_t red, green, blue, white, lux;
while(!apds.colorDataReady()) {
2017-11-20 20:42:02 +01:00
delay(5);
}
2019-03-31 17:01:04 +02:00
2017-11-20 20:42:02 +01:00
apds.getColorData(&red, &green, &blue, &white);
2019-01-27 18:49:28 +01:00
//calculate lux
lux = apds.calculateLux(red, green, blue);
return lux * LIGHT_FACTOR;
2017-11-12 21:37:55 +01:00
}
2019-08-03 20:34:17 +02:00
ICACHE_RAM_ATTR void _anemometerInterrupt() {
2017-11-12 21:37:55 +01:00
anemometerRotations++;
#ifdef DEBUG
Serial.print("*");
#endif
2017-11-12 21:37:55 +01:00
}
float fetchWindspeed() {
anemometerRotations = 0;
currentTime = millis();
int interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN);
attachInterrupt(interruptNumber, _anemometerInterrupt, RISING);
2017-11-12 21:37:55 +01:00
delay(1000 * 5);
detachInterrupt(interruptNumber);
return (float)anemometerRotations / 5.0 * 2.4;
2017-11-07 22:20:14 +01:00
}
2018-07-11 11:12:53 +02:00
// Copied from https://arduinodiy.wordpress.com/2016/12/25/monitoring-lipo-battery-voltage-with-wemos-d1-minibattery-shield-and-thingspeak/
2019-01-27 18:49:28 +01:00
#ifdef BATTERY_POWERED
2018-07-11 11:12:53 +02:00
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.
2018-07-11 11:12:53 +02:00
uint16_t raw = analogRead(A0);
float volt = raw / 1023.0;
2019-01-06 15:23:48 +01:00
return volt * 4.2;
2018-07-11 11:12:53 +02:00
}
2019-02-02 10:09:00 +01:00
2019-02-02 11:15:27 +01:00
float isBatCharging() {
2019-02-09 09:52:49 +01:00
if (LOW == digitalRead(BAT_CHARGING_PIN))
2019-02-02 10:09:00 +01:00
{
return BAT_CHARGE_STATE_CHARGING;
2019-02-09 09:52:49 +01:00
} else if (LOW == digitalRead(BAT_CHARGED_PIN))
{
return BAT_CHARGE_STATE_CHARGED;
2019-02-02 10:09:00 +01:00
}
return BAT_CHARGE_STATE_NOTCHARGING;
}
#endif