diff options
author | 2018-04-27 10:04:26 +0300 | |
---|---|---|
committer | 2018-04-27 10:04:26 +0300 | |
commit | 23d06a17f8a4033be57ca46079e5c3e65fa9e845 (patch) | |
tree | 7bfec5a9deedcc1e7de7c9ecd54ecf36020212c8 /src/tempmodule | |
parent | 884013b71ba9abd9ab0c9d0a8f90a769aff9b6a9 (diff) | |
download | coffeetemp-23d06a17f8a4033be57ca46079e5c3e65fa9e845.tar.gz coffeetemp-23d06a17f8a4033be57ca46079e5c3e65fa9e845.tar.bz2 coffeetemp-23d06a17f8a4033be57ca46079e5c3e65fa9e845.zip |
various minor improvements.
Diffstat (limited to 'src/tempmodule')
-rw-r--r-- | src/tempmodule/indicator.c | 9 | ||||
-rw-r--r-- | src/tempmodule/indicator.h | 2 | ||||
-rw-r--r-- | src/tempmodule/main.ino | 65 | ||||
-rw-r--r-- | src/tempmodule/temperature.c | 2 | ||||
-rw-r--r-- | src/tempmodule/temperature.h | 2 |
5 files changed, 60 insertions, 20 deletions
diff --git a/src/tempmodule/indicator.c b/src/tempmodule/indicator.c index 774c801..4c1b958 100644 --- a/src/tempmodule/indicator.c +++ b/src/tempmodule/indicator.c @@ -57,8 +57,13 @@ void indicator_init(unsigned int temp, unsigned int const * const calibration) } -void indicator_update(unsigned int temp) +void indicator_update(const unsigned int temp, const unsigned int on) { + if (!on) { + indicator_set_state(0, 0, 0); + return; + } + if (temp > state.calibration[0]) { indicator_set_state(1, 0, 0); } else if (temp > state.calibration[1]) { @@ -75,7 +80,7 @@ void indicator_update(unsigned int temp) void indicator_calibrate(unsigned int const * const calibration) { memcpy(state.calibration, calibration, sizeof(state.calibration)); - indicator_update(state.current_temp); + indicator_update(state.current_temp, 1); } static void indicator_set_state(unsigned int red, unsigned int green, unsigned int blue) diff --git a/src/tempmodule/indicator.h b/src/tempmodule/indicator.h index db7bcce..d44b7fe 100644 --- a/src/tempmodule/indicator.h +++ b/src/tempmodule/indicator.h @@ -26,7 +26,7 @@ extern "C" { #endif void indicator_init(unsigned int temp, unsigned int const * const calibration); -void indicator_update(unsigned int temp); +void indicator_update(const unsigned int temp, const unsigned int on); void indicator_calibrate(unsigned int const * const calibration); #ifdef __cplusplus diff --git a/src/tempmodule/main.ino b/src/tempmodule/main.ino index 6057b66..58e3dfc 100644 --- a/src/tempmodule/main.ino +++ b/src/tempmodule/main.ino @@ -26,15 +26,16 @@ #include "indicator.h" #include "temperature.h" -static const unsigned int iled = 2; /* D4 */ -static const unsigned int wifiled = 12; /* D6 */ -static const unsigned int wifibutton = 13; /* D7 */ +static const unsigned int iled = 2; /* D4 */ +static const unsigned int wifiled = 12; /* D6 */ +static const unsigned int wifibutton = 15; /* D8 */ +static const unsigned int thermistorvcc = 13; /* D7 */ static const unsigned int analog = A0; static volatile unsigned int wifidesired = 0; static unsigned int wifistate = 0; static unsigned int wifiledstate = 0; static const int port = 2191; -static const int splitter_res = 9900; +static const int splitter_res = 4680; static char udppacketbuffer[32] = {0}; static char *udppacketcursor = NULL; IPAddress ip; @@ -47,6 +48,7 @@ static int wifi_connect(const char * const ssid, const char * const password, co static void wifi_disconnect(void); static void blink_led(const int pin, const int ontime, const int offtime); static void discover_client(void); +static void get_data(short int * const data); void toggle_wifi(void); void wifiled_toggle(void); @@ -54,10 +56,28 @@ void setup(void) { pinMode(iled, OUTPUT); digitalWrite(iled, HIGH); + pinMode(wifiled, OUTPUT); + digitalWrite(wifiled, LOW); + pinMode(wifibutton, INPUT); + + pinMode(thermistorvcc, OUTPUT); + digitalWrite(thermistorvcc, LOW); + indicator_init(0, NULL); + WiFi.forceSleepBegin(); + indicator_update(3450, 1); + delay(1000); + indicator_update(3350, 1); + delay(1000); + indicator_update(3250, 1); + delay(1000); + indicator_update(3150, 1); + delay(1000); + indicator_update(3050, 1); + delay(1000); attachInterrupt(digitalPinToInterrupt(wifibutton), toggle_wifi, RISING); } @@ -66,7 +86,7 @@ void loop(void) static const char * const ssid = "SSID"; static const char * const password = "password"; static unsigned int ticker = 0; - short int data; + short int data[3]; size_t i; udp_init_packet(ip, port); @@ -78,30 +98,28 @@ void loop(void) } } - if (!wifidesired && wifistate) { + if (!wifidesired) { wifi_disconnect(); } if (wifistate) { - data = 0; - udp_push(&data, sizeof(data)); + data[0] = 0; + udp_push(data, sizeof(data[0])); udp_push(&ticker, sizeof(ticker)); } - data = analogRead(analog); - data = get_temperature(get_resistance(data, splitter_res)); + get_data(data); if (wifistate) { - udp_push(&data, sizeof(data)); - + udp_push(data, sizeof(data)); /* if flushing fails, disconnect WiFi */ if (!(udp_flush())) { wifidesired = 0; } } - indicator_update(data); - delay(1000); + indicator_update(data[0], 1); + delay(245); ++ticker; } @@ -126,6 +144,9 @@ static int udp_flush(void) static int wifi_connect(const char * const ssid, const char * const password, const char doblink, const int ledpin) { + if (wifistate) { + return 1; + } wifiled_toggle(); WiFi.forceSleepWake(); @@ -150,6 +171,10 @@ static int wifi_connect(const char * const ssid, const char * const password, co static void wifi_disconnect(void) { + if (!wifistate) { + return; + } + wifiled_toggle(); Udp.stop(); WiFi.disconnect(1); @@ -191,9 +216,19 @@ static void discover_client(void) } while (!done); } +static void get_data(short int * const data) +{ + digitalWrite(thermistorvcc, HIGH); + delay(5); /* make sure the voltage settles down */ + data[2] = analogRead(analog); + digitalWrite(thermistorvcc, LOW); + data[1] = get_resistance(data[2], splitter_res); + data[0] = get_temperature(data[1]); +} + void toggle_wifi(void) { - static const unsigned int timeout = 2000000; /* 2 seconds */ + static const unsigned int timeout = 2 * 1000 * 1000; /* 2 seconds */ static volatile unsigned int lastswitch; volatile unsigned int switchtime; diff --git a/src/tempmodule/temperature.c b/src/tempmodule/temperature.c index 684f654..c95807b 100644 --- a/src/tempmodule/temperature.c +++ b/src/tempmodule/temperature.c @@ -81,7 +81,7 @@ int get_temperature(int res) } /* Returns the last 0-10 part of the temperature, in 0.1°K. */ -int get_temp_subrange(short a, short b, int res) +int get_temp_subrange(int a, int b, int res) { return (res - a) * 100 / (b - a); } diff --git a/src/tempmodule/temperature.h b/src/tempmodule/temperature.h index 2882514..1f7e33e 100644 --- a/src/tempmodule/temperature.h +++ b/src/tempmodule/temperature.h @@ -27,7 +27,7 @@ extern "C" { int get_resistance(int vt, int ra); int get_temperature(int res); -int get_temp_subrange(short a, short b, int res); +int get_temp_subrange(int a, int b, int res); #ifdef __cplusplus } |