From 65e4dc58c29f4f2d82caad8190f5e8ca9402b834 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Thu, 15 Feb 2018 10:33:05 +0200 Subject: tempmodule: make the WiFi off by default and togglable with a button interrupt. --- src/tempmodule/main.ino | 103 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 86 insertions(+), 17 deletions(-) (limited to 'src/tempmodule') diff --git a/src/tempmodule/main.ino b/src/tempmodule/main.ino index e15cd93..fd7d5d7 100644 --- a/src/tempmodule/main.ino +++ b/src/tempmodule/main.ino @@ -22,11 +22,17 @@ #include #include #include +#include #include "indicator.h" #include "temperature.h" -static const unsigned int iled = 2; +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 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 = 4699; static char udppacketbuffer[32] = {0}; @@ -39,43 +45,58 @@ static void udp_init_packet(IPAddress ip, const int port); static void udp_push(const void * const data, const size_t size); 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 wifi_disconnect(void); static void blink_led(const int pin, const int ontime, const int offtime); static void discover_client(void); +void toggle_wifi(void); +void wifiled_toggle(void); void setup(void) { - static const char * const ssid = "SSID"; - static const char * const password = "password"; - pinMode(iled, OUTPUT); + pinMode(wifiled, OUTPUT); + pinMode(wifibutton, INPUT); indicator_init(0, NULL); - wifi_connect(ssid, password, 1, iled); - Udp.begin(port); - discover_client(); + WiFi.forceSleepBegin(); + attachInterrupt(digitalPinToInterrupt(wifibutton), toggle_wifi, RISING); } void loop(void) { + static const char * const ssid = "SSID"; + static const char * const password = "password"; static unsigned int ticker = 0; short int data; - char *dataptr; size_t i; - udp_init_packet(ip, port); - data = 0; - dataptr = (char*) &data; - udp_push(&data, sizeof(data)); - udp_push(&ticker, sizeof(ticker)); + if (wifidesired && !wifistate) { + wifi_connect(ssid, password, 1, iled); + Udp.begin(port); + discover_client(); + } + + if (!wifidesired && wifistate) { + wifi_disconnect(); + } + + if (wifistate) { + data = 0; + udp_push(&data, sizeof(data)); + udp_push(&ticker, sizeof(ticker)); + } data = analogRead(analog); data = get_temperature(get_resistance(data, splitter_res)); - udp_push(&data, sizeof(data)); - if (!(udp_flush())) { - Udp.stop(); - sleep(); + if (wifistate) { + udp_push(&data, sizeof(data)); + + if (!(udp_flush())) { + Udp.stop(); + sleep(); + } } indicator_update(data); @@ -111,6 +132,9 @@ static int udp_flush(void) static void wifi_connect(const char * const ssid, const char * const password, const char doblink, const int ledpin) { + + wifiled_toggle(); + WiFi.forceSleepWake(); WiFi.begin(ssid, password); do { @@ -120,6 +144,16 @@ static void wifi_connect(const char * const ssid, const char * const password, c delay (500); } } while (WiFi.status() != WL_CONNECTED); + + wifistate = 1; +} + +static void wifi_disconnect(void) +{ + wifiled_toggle(); + WiFi.disconnect(1); + WiFi.forceSleepBegin(); + wifistate = 0; } static void blink_led(const int pin, const int ontime, const int offtime) @@ -155,3 +189,38 @@ static void discover_client(void) delay(95); } while (!done); } + +void toggle_wifi(void) +{ + static const unsigned int timeout = 2000000; /* 2 seconds */ + static volatile unsigned int lastswitch; + volatile unsigned int switchtime; + + switchtime = micros(); + + /* if the value overflowed */ + if (switchtime < lastswitch) { + if ((UINT_MAX - lastswitch + timeout) > switchtime) { + return; + } + /* if the value overflows if we add 2 seconds */ + } else if ((lastswitch + timeout) < lastswitch) { + if (lastswitch + timeout > switchtime || lastswitch < switchtime) { + return; + } + } + + lastswitch = switchtime; + wifidesired = !wifidesired; +} + +void wifiled_toggle(void) +{ + if (wifiledstate) { + digitalWrite(wifiled, LOW); + } else { + digitalWrite(wifiled, HIGH); + } + + wifiledstate = !wifiledstate; +} -- cgit v1.2.3