From 23d06a17f8a4033be57ca46079e5c3e65fa9e845 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Fri, 27 Apr 2018 10:04:26 +0300 Subject: various minor improvements. --- src/tempmodule/main.ino | 65 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 15 deletions(-) (limited to 'src/tempmodule/main.ino') 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; -- cgit v1.2.3