diff --git a/include/controller.h b/include/controller.h index c5099a2..e1b8ad5 100644 --- a/include/controller.h +++ b/include/controller.h @@ -11,7 +11,9 @@ #ifndef FANLEDCTL_PINS #define FANLEDCTL_PINS -#define FAN_PIN 12 -#define SIGNAL_PIN 13 +#define FAN_PIN 12 /**< Output Pin, controlling FAN speed via PWM */ +#define SIGNAL_PIN 13 /**< Input Pin, reading current FANs RPM*/ + +#define GPIO_DS18B20 D7 /**< One-Wire used for Dallas temperature sensor */ #endif /* End FANLEDCTL_PINS */ \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index bd05b69..ef479a2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,3 +12,6 @@ platform = espressif8266 board = d1_wroom_02 framework = arduino + +lib_deps = OneWire + DallasTemperature \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f2554ae..9edc4b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,19 @@ #include +#include +#include #include "controller.h" -#define DELAY_TIME 10000 // time between measurements [ms] -#define MIN_FAN_SPEED_PERCENT 24 // minimum fan speed [%] -#define MIN_TEMP 25 // turn fan off below [deg C] -#define MAX_TEMP 40 // turn fan to full speed above [deg C] +#define DELAY_TIME 10000 /**< time between measurements [ms] */ +#define MIN_FAN_SPEED_PERCENT 24 /**< minimum fan speed [%] */ +#define MIN_TEMP 25 /**< turn fan off below [deg C] */ +#define MAX_TEMP 40 /**< turn fan to full speed above [deg C] */ +#define INVALID_TEMP (MAX_TEMP+MAX_TEMP) /**< Invalid temperature (on sensor failures)*/ + +#define TEMP_SENSOR_MEASURE_SERIES 5 /**< Maximum resets */ + +OneWire oneWire(GPIO_DS18B20); +DallasTemperature sensors(&oneWire); /** * @brief Called once at start @@ -14,9 +22,17 @@ void setup() { Serial.begin(115200); - + /* Setup FAN Control */ pinMode(FAN_PIN, OUTPUT); pinMode(SIGNAL_PIN, INPUT); + /* Initialize Temperature sensor */ + sensors.begin(); + for(int j=0; j < TEMP_SENSOR_MEASURE_SERIES && sensors.getDeviceCount() == 0; j++) + { + delay(100); + sensors.begin(); + Serial.println("Reset 1-Wire Bus"); + } } /** @@ -45,12 +61,28 @@ void setFanSpeedPercent(int p) { analogWrite(FAN_PIN, value); } +float readTemperature(void) +{ + int sensorCount = sensors.getDeviceCount(); + if (sensorCount > 0) + { + sensors.requestTemperatures(); + float temp1Raw = sensors.getTempCByIndex(0); + if (temp1Raw > 0) + { + return temp1Raw; + } + } + + return INVALID_TEMP; +} + /** * @brief Endless loop * (is called as fast as possible) */ void loop() { - float temp = /* FIXME bme.readTemperature(); */ + float temp = readTemperature(); Serial.print("Temperature is "); Serial.print(temp); Serial.println(" deg C");