diff --git a/Claire.ino b/Claire.ino index 46d1e00..1226b2f 100644 --- a/Claire.ino +++ b/Claire.ino @@ -18,8 +18,10 @@ #define NUM_LEDS_PROTO_PAPER 93 #define DATA_PIN_PROTO_PAPER 5 +#define CONNECT_SW_PIN_PROTO_PAPER BUTTON_BUILTIN #define NUM_LEDS_PROTO_V1 29 #define DATA_PIN_PROTO_V1 14 +#define CONNECT_SW_PIN_PROTO_V1 21 static int BUIENRADAR_START_LED = 0; static int BUIENRADAR_SKIP_LED = 1; @@ -31,6 +33,7 @@ static int UVI_LED = 0; static int AQI_LED = 0; static int POLLEN_LED = 0; static int IAQI_LED = 0; +static int CONNECT_SW_PIN = 0; #define SHOW_AQI_LED #define SHOW_POLLEN_LED @@ -198,6 +201,8 @@ static void setup_pins_proto_paper(void) AQI_LED = 86; // LED to the right POLLEN_LED = 90; // LED to the left IAQI_LED = 88; // LED below center + + CONNECT_SW_PIN = CONNECT_SW_PIN_PROTO_PAPER; } static void setup_pins_proto_v1(void) @@ -214,6 +219,18 @@ static void setup_pins_proto_v1(void) AQI_LED = 24; // LED to the right POLLEN_LED = 26; // LED to the left IAQI_LED = 25; // LED below center + + CONNECT_SW_PIN = CONNECT_SW_PIN_PROTO_V1; +} + +void leds_clear(void) { + if (hw_variant == HW_PROTO_PAPER) { + leds_rgb_proto_paper.clear(); + leds_rgb_proto_paper.show(); + } else if (hw_variant == HW_PROTO_V1) { + leds_rgbw_proto_v1.clear(); + leds_rgbw_proto_v1.show(); + } } void setup() { @@ -251,9 +268,11 @@ void setup() { //String output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix); //Serial.println(output); + leds_clear(); + #ifndef DISABLE_WIFI //wifiManager.resetSettings(); - wifiManager.configure("clairvoyance-", true, LED_BUILTIN, true, BUTTON_BUILTIN, false); + wifiManager.configure("Claire-", true, LED_BUILTIN, true, CONNECT_SW_PIN, false); //fetches ssid and pass and tries to connect //if it does not connect it starts an access point @@ -511,6 +530,18 @@ static float buienradarMap(float x) { return y; } +static void ledsSetPixelColor(uint32_t ledIdx, int r, int g, int b, int w) { + if (hw_variant == HW_PROTO_PAPER) { + leds_rgb_proto_paper.setPixelColor(ledIdx, leds_rgb_proto_paper.Color(r, g, b)); + } else if (hw_variant == HW_PROTO_V1) { + leds_rgbw_proto_v1.setPixelColor(ledIdx, leds_rgbw_proto_v1.Color(g, r, b, w)); + } +} + +static void ledsSetPixelColor(uint32_t ledIdx, int r, int g, int b) { + ledsSetPixelColor(ledIdx, r, g, b, 0); +} + static void updateLedsNoWifi(void) { uint32_t ledIdx; uint32_t ledCount; @@ -528,7 +559,7 @@ static void updateLedsNoWifi(void) { g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - leds_rgb_proto_paper.setPixelColor(ledIdx, leds_rgb_proto_paper.Color(r, g, b)); + ledsSetPixelColor(ledIdx, r, g, b); ledIdx = ledIdx + PAQI_SKIP_LED + 1; } } else if (hw_variant == HW_PROTO_V1) { @@ -543,7 +574,7 @@ static void updateLedsNoWifi(void) { b = (color & 0x0000FF); w = (color & 0xFF000000) >> 24; - leds_rgbw_proto_v1.setPixelColor(ledIdx, leds_rgbw_proto_v1.Color(g, r, b, w)); + ledsSetPixelColor(ledIdx, r, g, b, w); ledIdx = ledIdx + PAQI_SKIP_LED + 1; } } @@ -566,11 +597,7 @@ static void updateLeds(void) { b = (color & 0x0000FF); w = (color & 0xFF000000) >> 24; - if (hw_variant == HW_PROTO_PAPER) { - leds_rgb_proto_paper.setPixelColor(ledIdx, leds_rgb_proto_paper.Color(r, g, b)); - } else if (hw_variant == HW_PROTO_V1) { - leds_rgbw_proto_v1.setPixelColor(ledIdx, leds_rgbw_proto_v1.Color(g, r, b, w)); - } + ledsSetPixelColor(ledIdx, r, g, b); ledIdx = ledIdx + PAQI_SKIP_LED + 1; } @@ -592,11 +619,7 @@ static void updateLeds(void) { b = (color & 0x0000FF); w = (color & 0xFF000000) >> 24; - if (hw_variant == HW_PROTO_PAPER) { - leds_rgb_proto_paper.setPixelColor(ledIdx, leds_rgb_proto_paper.Color(r, g, b)); - } else if (hw_variant == HW_PROTO_V1) { - leds_rgbw_proto_v1.setPixelColor(ledIdx, leds_rgbw_proto_v1.Color(g, r, b, w)); - } + ledsSetPixelColor(ledIdx, r, g, b); } #ifdef SHOW_AQI_LED @@ -626,11 +649,7 @@ static void updateLeds(void) { g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - if (hw_variant == HW_PROTO_PAPER) { - leds_rgb_proto_paper.setPixelColor(AQI_LED, leds_rgb_proto_paper.Color(r, g, b)); - } else if (hw_variant == HW_PROTO_V1) { - leds_rgbw_proto_v1.setPixelColor(AQI_LED, leds_rgbw_proto_v1.Color(g, r, b, w)); - } + ledsSetPixelColor(AQI_LED, r, g, b); #endif #ifdef SHOW_POLLEN_LED @@ -654,11 +673,7 @@ static void updateLeds(void) { g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - if (hw_variant == HW_PROTO_PAPER) { - leds_rgb_proto_paper.setPixelColor(POLLEN_LED, leds_rgb_proto_paper.Color(r, g, b)); - } else if (hw_variant == HW_PROTO_V1) { - leds_rgbw_proto_v1.setPixelColor(POLLEN_LED, leds_rgbw_proto_v1.Color(g, r, b, w)); - } + ledsSetPixelColor(POLLEN_LED, r, g, b); #endif #ifdef SHOW_UVI_LED @@ -678,11 +693,7 @@ static void updateLeds(void) { g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - if (hw_variant == HW_PROTO_PAPER) { - leds_rgb_proto_paper.setPixelColor(UVI_LED, leds_rgb_proto_paper.Color(r, g, b)); - } else if (hw_variant == HW_PROTO_V1) { - leds_rgbw_proto_v1.setPixelColor(UVI_LED, leds_rgbw_proto_v1.Color(g, r, b, w)); - } + ledsSetPixelColor(UVI_LED, r, g, b); #endif // Start by setting all leds to value of the first datapoint @@ -713,73 +724,104 @@ static void updateLeds(void) { r = (color & 0xFF0000) >> 16; g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - if (hw_variant == HW_PROTO_PAPER) { - leds_rgb_proto_paper.setPixelColor(ledIdx, leds_rgb_proto_paper.Color(r, g, b)); - } else if (hw_variant == HW_PROTO_V1) { - leds_rgbw_proto_v1.setPixelColor(ledIdx, leds_rgbw_proto_v1.Color(g, r, b, w)); - } + ledsSetPixelColor(ledIdx, r, g, b); } - if (hw_variant == HW_PROTO_PAPER) { - leds_rgb_proto_paper.setPixelColor(IAQI_LED, leds_rgb_proto_paper.Color(r, g, b)); - } else if (hw_variant == HW_PROTO_V1) { - leds_rgbw_proto_v1.setPixelColor(IAQI_LED, leds_rgbw_proto_v1.Color(g, r, b, w)); - } + ledsSetPixelColor(IAQI_LED, r, g, b); } void readSensors(void) { + static bool readingInProgress = false; + static unsigned long endTime = 0; + if (hw_variant == HW_PROTO_PAPER) { return; } - - // Tell BME680 to begin measurement. - unsigned long endTime = bme.beginReading(); - if (endTime == 0) { - Serial.println(F("Failed to begin reading :(")); - return; + + if (readingInProgress == false) { + Serial.println("Starting new measurement"); + unsigned long endTime = bme.beginReading(); + if (endTime == 0) { + Serial.println(F("Failed to begin reading :(")); + return; + } + readingInProgress = true; + + Serial.print(F("Reading started at ")); + Serial.print(millis()); + Serial.print(F(" and will finish at ")); + Serial.println(endTime); + } else { + if (millis() - endTime > 0) { + // Obtain measurement results from BME680. Note that this operation isn't + // instantaneous even if milli() >= endTime due to I2C/SPI latency. + // + // This call takes about 162 ms + if (!bme.endReading()) { + Serial.println(F("Failed to complete reading :(")); + return; + } + readingInProgress = false; + + Serial.print(F("Reading completed at ")); + Serial.println(millis()); + + Serial.print(F("Temperature = ")); + Serial.print(bme.temperature); + Serial.println(F(" *C")); + + Serial.print(F("Pressure = ")); + Serial.print(bme.pressure / 100.0); + Serial.println(F(" hPa")); + + Serial.print(F("Humidity = ")); + Serial.print(bme.humidity); + Serial.println(F(" %")); + + Serial.print(F("Gas = ")); + Serial.print(bme.gas_resistance / 1000.0); + Serial.println(F(" KOhms")); + + Serial.print(F("Approx. Altitude = ")); + Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.println(F(" m")); + + Serial.println(); + } } - Serial.print(F("Reading started at ")); - Serial.print(millis()); - Serial.print(F(" and will finish at ")); - Serial.println(endTime); +} - Serial.println(F("You can do other work during BME680 measurement.")); - delay(50); // This represents parallel work. - // There's no need to delay() until millis() >= endTime: bme.endReading() - // takes care of that. It's okay for parallel work to take longer than - // BME680's measurement time. +void readConnectButton(void) { + static int reset_wifi_timer = millis(); + static bool reset_blocked = true; - // Obtain measurement results from BME680. Note that this operation isn't - // instantaneous even if milli() >= endTime due to I2C/SPI latency. - if (!bme.endReading()) { - Serial.println(F("Failed to complete reading :(")); - return; + if (digitalRead(CONNECT_SW_PIN) == HIGH) { + // button released + reset_wifi_timer = millis(); + reset_blocked = false; + digitalWrite(LED_BUILTIN, LOW); + } else { + // button pressed + if (reset_blocked == false) { + int delta = millis() - reset_wifi_timer; + if (delta < 10000) { + if (delta % 1000 < 500) { + digitalWrite(LED_BUILTIN, HIGH); + } else { + digitalWrite(LED_BUILTIN, LOW); + } + } else { + leds_clear(); + + wifiManager.resetSettings(); + + // block reset so user must release button before we can reset again + reset_blocked = true; + digitalWrite(LED_BUILTIN, LOW); + ESP.restart(); + } + } } - Serial.print(F("Reading completed at ")); - Serial.println(millis()); - - Serial.print(F("Temperature = ")); - Serial.print(bme.temperature); - Serial.println(F(" *C")); - - Serial.print(F("Pressure = ")); - Serial.print(bme.pressure / 100.0); - Serial.println(F(" hPa")); - - Serial.print(F("Humidity = ")); - Serial.print(bme.humidity); - Serial.println(F(" %")); - - Serial.print(F("Gas = ")); - Serial.print(bme.gas_resistance / 1000.0); - Serial.println(F(" KOhms")); - - Serial.print(F("Approx. Altitude = ")); - Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); - Serial.println(F(" m")); - - Serial.println(); - delay(2000); } void loop(void) { @@ -824,8 +866,10 @@ void loop(void) { leds_rgbw_proto_v1.show(); } - readSensors(); - + // readSensors(); + + readConnectButton(); + delay(100); #ifndef DISABLE_WIFI dESPatch.checkForUpdate(true);