diff options
-rw-r--r-- | src/tempmodule/main.ino | 70 | ||||
-rw-r--r-- | src/tempmodule/temperature.c | 68 | ||||
-rw-r--r-- | src/tempmodule/temperature.h | 13 |
3 files changed, 82 insertions, 69 deletions
diff --git a/src/tempmodule/main.ino b/src/tempmodule/main.ino index b07365d..e15cd93 100644 --- a/src/tempmodule/main.ino +++ b/src/tempmodule/main.ino @@ -22,13 +22,8 @@ #include <WiFiUdp.h> #include <stddef.h> #include <string.h> -#include <inttypes.h> #include "indicator.h" - -struct temptuple { - short res; /* Resistance in ohms. */ - short temp; /* Temperatures in 0.1°K. */ -}; +#include "temperature.h" static const unsigned int iled = 2; static const unsigned int analog = A0; @@ -46,9 +41,6 @@ static int udp_flush(void); static void wifi_connect(const char * const ssid, const char * const password, const char doblink, const int ledpin); static void blink_led(const int pin, const int ontime, const int offtime); static void discover_client(void); -static int get_resistance(int64_t vt, int64_t ra); -static int get_temperature(int res); -static int get_temp_subrange(short a, short b, int res); void setup(void) { @@ -163,63 +155,3 @@ static void discover_client(void) delay(95); } while (!done); } - -/* - * formula by applying Ohms law: - * - * Rt: Thermistor resistance - * Ra: Voltage divider resistor - * Vt: Tap voltage - * Vcc: Input Voltage (3.31V) - * - * Rt = Ra * Vcc / Vt - Ra - * - * Using 64-bit arithmetic as 32-bit would overflow. - * On WeMOS D1 Mini the A0 reads 3.2V as 1023; our Vcc is 3.31V - * The correct value of the reference volume obtained by measuring - * resistances with a multimeter, taking the ADC reading and applying - * Ohm's law. - * We shift the range by one to avoid division by zero. - */ -static int get_resistance(int64_t vt, int64_t ra) -{ - ++vt; - return ra * 1081 / vt - ra; -} - -static int get_temperature(int res) -{ - int ret; - size_t i; - static const struct temptuple lt[] = { - {1495, 2630}, {1630, 2730}, {1772, 2830}, {1922, 2930}, - {2080, 3030}, {2245, 3130}, {2417, 3230}, {2597, 3330}, - {2785, 3430}, {2980, 3530}, {3182, 3630}, {3392, 3730}, - {3607, 3830}}; - - ret = -1; - - for (i = 0; i < 12; ++i) { - /* If we have a matching resistance, nothing to calculate. */ - if (res == lt[i].res) { - ret = lt[i].temp; - break; - } else if (res == lt[i + 1].res) { - ret = lt[i + 1].temp; - break; - /* If no matching resistance is found, calculate temp from subrange. */ - } else if (res > lt[i].res && res < lt[i + 1].res) { - ret = lt[i].temp + get_temp_subrange(lt[i].res, lt[i + 1].res, res); - break; - } - } - - return ret; - -} - -/* Returns the last 0-10 part of the temperature, in 0.1°K. */ -static int get_temp_subrange(short a, short b, int res) -{ - return (res - a) * 100 / (b - a); -} diff --git a/src/tempmodule/temperature.c b/src/tempmodule/temperature.c new file mode 100644 index 0000000..7986585 --- /dev/null +++ b/src/tempmodule/temperature.c @@ -0,0 +1,68 @@ +#include <stddef.h> +#include "temperature.h" + +/* + * formula by applying Ohms law: + * + * Rt: Thermistor resistance + * Ra: Voltage divider resistor + * Vt: Tap voltage + * Vcc: Input Voltage (3.31V) + * + * Rt = Ra * Vcc / Vt - Ra + * + * Using 64-bit arithmetic as 32-bit would overflow. + * On WeMOS D1 Mini the A0 reads 3.2V as 1023; our Vcc is 3.31V + * The correct value of the reference volume obtained by measuring + * resistances with a multimeter, taking the ADC reading and applying + * Ohm's law. + * We shift the range by one to avoid division by zero. + */ + +struct temptuple { + short res; /* Resistance in ohms. */ + short temp; /* Temperatures in 0.1°K. */ +}; + +int get_resistance(int64_t vt, int64_t ra) +{ + ++vt; + return ra * 1081 / vt - ra; +} + +int get_temperature(int res) +{ + int ret; + size_t i; + static const struct temptuple lt[] = { + {1495, 2630}, {1630, 2730}, {1772, 2830}, {1922, 2930}, + {2080, 3030}, {2245, 3130}, {2417, 3230}, {2597, 3330}, + {2785, 3430}, {2980, 3530}, {3182, 3630}, {3392, 3730}, + {3607, 3830}}; + + ret = -1; + + for (i = 0; i < 12; ++i) { + /* If we have a matching resistance, nothing to calculate. */ + if (res == lt[i].res) { + ret = lt[i].temp; + break; + } else if (res == lt[i + 1].res) { + ret = lt[i + 1].temp; + break; + /* If no matching resistance is found, calculate temp from subrange. */ + } else if (res > lt[i].res && res < lt[i + 1].res) { + ret = lt[i].temp + get_temp_subrange(lt[i].res, lt[i + 1].res, res); + break; + } + } + + return ret; + +} + +/* Returns the last 0-10 part of the temperature, in 0.1°K. */ +int get_temp_subrange(short a, short b, int res) +{ + return (res - a) * 100 / (b - a); +} diff --git a/src/tempmodule/temperature.h b/src/tempmodule/temperature.h new file mode 100644 index 0000000..2dfac73 --- /dev/null +++ b/src/tempmodule/temperature.h @@ -0,0 +1,13 @@ +#include <inttypes.h> + +#ifdef __cplusplus +extern "C" { +#endif + +int get_resistance(int64_t vt, int64_t ra); +int get_temperature(int res); +int get_temp_subrange(short a, short b, int res); + +#ifdef __cplusplus +} +#endif |