From 328e43591270b1d3626e2edd48b2e95f1fb7a62c Mon Sep 17 00:00:00 2001 From: admar Date: Sat, 21 May 2022 10:28:59 +0200 Subject: [PATCH] Fix led order for proto v1 --- Claire.ino | 329 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 228 insertions(+), 101 deletions(-) diff --git a/Claire.ino b/Claire.ino index 30ef3d0..46d1e00 100644 --- a/Claire.ino +++ b/Claire.ino @@ -1,8 +1,6 @@ //#define DISABLE_WIFI #define HW_PROTO_PAPER 1 #define HW_PROTO_V1 2 -#define HW HW_PROTO_PAPER -//#define HW HW_PROTO_V1 #include // https://github.com/admarschoonen/WiFiManager #include @@ -13,60 +11,48 @@ #include -#if (HW == HW_PROTO_PAPER) - #define NUM_LEDS 93 - #define DATA_PIN 5 -#elif (HW == HW_PROTO_V1) - #define NUM_LEDS 29 - #define DATA_PIN 14 -#else - #error "HW not set" -#endif +#include +#include +#include "Adafruit_BME680.h" +//#include "bsec.h" + +#define NUM_LEDS_PROTO_PAPER 93 +#define DATA_PIN_PROTO_PAPER 5 +#define NUM_LEDS_PROTO_V1 29 +#define DATA_PIN_PROTO_V1 14 + +static int BUIENRADAR_START_LED = 0; +static int BUIENRADAR_SKIP_LED = 1; +static int BUIENRADAR_NUM_LEDS = 12; +static int PAQI_START_LED = 0; +static int PAQI_SKIP_LED = 0; +static int PAQI_NUM_LEDS = 0; +static int UVI_LED = 0; +static int AQI_LED = 0; +static int POLLEN_LED = 0; +static int IAQI_LED = 0; -#if (HW == HW_PROTO_PAPER) - #define BUIENRADAR_START_LED 32 - #define BUIENRADAR_SKIP_LED 1 - #define BUIENRADAR_NUM_LEDS 12 - - #define PAQI_START_LED 72 - #define PAQI_SKIP_LED 0 - #define PAQI_NUM_LEDS 12 - - // #define UVI_LED 92 // LED in center - #define UVI_LED 84 // LED above center - #define AQI_LED 86 // LED to the right - #define POLLEN_LED 90 // LED to the left - #define IAQI_LED 88 // LED below center -#elif (HW == HW_PROTO_V1) - #define BUIENRADAR_START_LED 1 - #define BUIENRADAR_SKIP_LED 0 - #define BUIENRADAR_NUM_LEDS 12 - - #define PAQI_START_LED 13 - #define PAQI_SKIP_LED 0 - #define PAQI_NUM_LEDS 12 - - #define UVI_LED 28 // LED above center - #define AQI_LED 25 // LED to the right - #define POLLEN_LED 27 // LED to the left - #define IAQI_LED 26 // LED below center -#endif #define SHOW_AQI_LED #define SHOW_POLLEN_LED #define SHOW_UVI_LED #define MIN(x, y) (((x) <= (y)) ? (x) : (y)) -#if (HW == HW_PROTO_PAPER) - Adafruit_NeoPixel leds(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800); -#elif (HW == HW_PROTO_V1) - Adafruit_NeoPixel leds(NUM_LEDS, DATA_PIN, NEO_RGBW + NEO_KHZ800); -#endif +Adafruit_NeoPixel leds_rgb_proto_paper(NUM_LEDS_PROTO_PAPER, DATA_PIN_PROTO_PAPER, NEO_GRB + NEO_KHZ800); +Adafruit_NeoPixel leds_rgbw_proto_v1(NUM_LEDS_PROTO_V1, DATA_PIN_PROTO_V1, NEO_RGBW + NEO_KHZ800); + +#define SEALEVELPRESSURE_HPA (1013.25) +#define BME_CS 5 +Adafruit_BME680 bme(BME_CS); // hardware SPI + +//SPIClass SPI1(HSPI); +//Bsec iaqSensor; static HTTPClient http; static const String baseUrlAQI = "http://target.luon.net:2356//forecast?&metrics=PAQI&metrics=AQI&metrics=pollen&metrics=UVI"; static const String baseUrlPrecipitation = "http://target.luon.net:2356//forecast?metrics=precipitation"; +static int hw_variant = 0; WiFiManager wifiManager; DESPatch dESPatch; @@ -197,6 +183,39 @@ void parseJson(String * payload) } } +static void setup_pins_proto_paper(void) +{ + BUIENRADAR_START_LED = 32; + BUIENRADAR_SKIP_LED = 1; + BUIENRADAR_NUM_LEDS = 12; + + PAQI_START_LED = 72; + PAQI_SKIP_LED = 0; + PAQI_NUM_LEDS = 12; + + // #define UVI_LED 92 // LED in center + UVI_LED = 84; // LED above center + AQI_LED = 86; // LED to the right + POLLEN_LED = 90; // LED to the left + IAQI_LED = 88; // LED below center +} + +static void setup_pins_proto_v1(void) +{ + BUIENRADAR_START_LED = 0; + BUIENRADAR_SKIP_LED = 0; + BUIENRADAR_NUM_LEDS = 12; + + PAQI_START_LED = 12; + PAQI_SKIP_LED = 0; + PAQI_NUM_LEDS = 12; + + UVI_LED = 27; // LED above center + AQI_LED = 24; // LED to the right + POLLEN_LED = 26; // LED to the left + IAQI_LED = 25; // LED below center +} + void setup() { const char url[] = "https://apikey:cqprlgiafadnidsgeqozcpldkaeqimqw@despatch.luon.net/files/4/despatch.json"; unsigned long interval = 60; // By default check for updates every 60 seconds @@ -204,12 +223,34 @@ void setup() { // sanity check delay - allows reprogramming if accidently blowing power w/leds delay(2000); - - leds.begin(); // INITIALIZE NeoPixel strip object - + Serial.begin(115200); Serial.println("buienradarklok starting"); + if (!bme.begin()) { + Serial.println(F("Could not find a valid BME680 sensor; assuming hw variant \"paper prototype\"!")); + hw_variant = HW_PROTO_PAPER; + setup_pins_proto_paper(); + leds_rgb_proto_paper.begin(); // INITIALIZE NeoPixel strip object + } else { + Serial.println(F("BME680 found; assuming hw variant \"proto v1\"")); + // Set up oversampling and filter initialization + bme.setTemperatureOversampling(BME680_OS_8X); + bme.setHumidityOversampling(BME680_OS_2X); + bme.setPressureOversampling(BME680_OS_4X); + bme.setIIRFilterSize(BME680_FILTER_SIZE_3); + bme.setGasHeater(320, 150); // 320*C for 150 ms + hw_variant = HW_PROTO_V1; + setup_pins_proto_v1(); + leds_rgbw_proto_v1.begin(); // INITIALIZE NeoPixel strip object + } + + //SPI1.begin(); + //iaqSensor.begin(BME_CS, SPI1); + + //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); + #ifndef DISABLE_WIFI //wifiManager.resetSettings(); wifiManager.configure("clairvoyance-", true, LED_BUILTIN, true, BUTTON_BUILTIN, false); @@ -227,9 +268,11 @@ void setup() { //if you get here you have connected to the WiFi Serial.print("connected with address: "); Serial.println(WiFi.localIP()); - - //keep LED on - digitalWrite(LED_BUILTIN, LED_ON_VALUE_DEFAULT); + + if (hw_variant == HW_PROTO_PAPER) { + //keep LED on + digitalWrite(LED_BUILTIN, LED_ON_VALUE_DEFAULT); + } x = dESPatch.configure(url, true, false, interval, false, root_ca); Serial.print("dESPatch.configure() returned with code "); @@ -398,11 +441,11 @@ static int colormap(float x) { y = (0 << 16) | (0 << 8) | 0; // black (good) } else if (x < 2.0) { // 1.0 <= x < 2.0 - #if (HW == HW_PROTO_PAPER) - y = (0 << 16) | (0 << 8) | 160; // blue (good) - #elif (HW == HW_PROTO_V1) - y = (0 << 16) | (0 << 8) | 255; // blue (good) - #endif + if (hw_variant == HW_PROTO_PAPER) { + y = (0 << 16) | (0 << 8) | 160; // blue (good) + } else if (hw_variant == HW_PROTO_V1) { + y = (0 << 16) | (0 << 8) | 255; // blue (good) + } } else if (x < 3.0) { // 2.0 <= x < 3.0 y = (0 << 16) | (255 << 8) | 255; // cyan (good) @@ -468,56 +511,49 @@ static float buienradarMap(float x) { return y; } -#if (HW == HW_PROTO_PAPER) -static void updateLedsNoWifi(void) { - uint32_t ledIdx; - uint32_t ledCount; - uint32_t color; - int r, g, b, n; - - // Start by setting all leds to value of the first datapoint - ledIdx = 11; - ledCount = 0; - for (ledCount = 0; ledCount < 11; ledCount++) { - color = colormap(ledCount); - - r = (color & 0xFF0000) >> 16; - g = (color & 0x00FF00) >> 8; - b = (color & 0x0000FF); - - leds.setPixelColor(ledIdx, leds.Color(r, g, b)); - ledIdx = ledIdx + PAQI_SKIP_LED + 1; - } -} -#elif (HW == HW_PROTO_V1) static void updateLedsNoWifi(void) { uint32_t ledIdx; uint32_t ledCount; uint32_t color; int r, g, b, w, n; - // Start by setting all leds to value of the first datapoint - ledIdx = 0; - ledCount = 0; - for (ledCount = 0; ledCount < 11; ledCount++) { - color = colormap(ledCount); - - r = (color & 0xFF0000) >> 16; - g = (color & 0x00FF00) >> 8; - b = (color & 0x0000FF); - w = (color & 0xFF000000) >> 24; - - leds.setPixelColor(ledIdx, leds.Color(g, r, b, w)); - ledIdx = ledIdx + PAQI_SKIP_LED + 1; - } + if (hw_variant == HW_PROTO_PAPER) { + // Start by setting all leds to value of the first datapoint + ledIdx = 11; + ledCount = 0; + for (ledCount = 0; ledCount < 11; ledCount++) { + color = colormap(ledCount); + + r = (color & 0xFF0000) >> 16; + g = (color & 0x00FF00) >> 8; + b = (color & 0x0000FF); + + leds_rgb_proto_paper.setPixelColor(ledIdx, leds_rgb_proto_paper.Color(r, g, b)); + ledIdx = ledIdx + PAQI_SKIP_LED + 1; + } + } else if (hw_variant == HW_PROTO_V1) { + // Start by setting all leds to value of the first datapoint + ledIdx = 0; + ledCount = 0; + for (ledCount = 0; ledCount < 11; ledCount++) { + color = colormap(ledCount); + + r = (color & 0xFF0000) >> 16; + g = (color & 0x00FF00) >> 8; + b = (color & 0x0000FF); + w = (color & 0xFF000000) >> 24; + + leds_rgbw_proto_v1.setPixelColor(ledIdx, leds_rgbw_proto_v1.Color(g, r, b, w)); + ledIdx = ledIdx + PAQI_SKIP_LED + 1; + } + } } -#endif static void updateLeds(void) { uint32_t ledIdx; uint32_t ledCount; uint32_t color; - int r, g, b, n; + int r, g, b, w, n; // Start by setting all leds to value of the first datapoint ledIdx = PAQI_START_LED; @@ -528,8 +564,13 @@ static void updateLeds(void) { r = (color & 0xFF0000) >> 16; g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); + w = (color & 0xFF000000) >> 24; - leds.setPixelColor(ledIdx, leds.Color(r, g, b)); + 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)); + } ledIdx = ledIdx + PAQI_SKIP_LED + 1; } @@ -549,8 +590,13 @@ static void updateLeds(void) { r = (color & 0xFF0000) >> 16; g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); + w = (color & 0xFF000000) >> 24; - leds.setPixelColor(ledIdx, leds.Color(r, g, b)); + 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)); + } } #ifdef SHOW_AQI_LED @@ -580,7 +626,11 @@ static void updateLeds(void) { g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - leds.setPixelColor(AQI_LED, leds.Color(r, g, b)); + 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)); + } #endif #ifdef SHOW_POLLEN_LED @@ -604,7 +654,11 @@ static void updateLeds(void) { g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - leds.setPixelColor(POLLEN_LED, leds.Color(r, g, b)); + 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)); + } #endif #ifdef SHOW_UVI_LED @@ -624,7 +678,11 @@ static void updateLeds(void) { g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - leds.setPixelColor(UVI_LED, leds.Color(r, g, b)); + 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)); + } #endif // Start by setting all leds to value of the first datapoint @@ -655,10 +713,73 @@ static void updateLeds(void) { r = (color & 0xFF0000) >> 16; g = (color & 0x00FF00) >> 8; b = (color & 0x0000FF); - leds.setPixelColor(ledIdx, leds.Color(r, g, b)); + 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)); + } } - leds.setPixelColor(IAQI_LED, leds.Color(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)); + } +} + +void readSensors(void) { + 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; + } + 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. + + // 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; + } + 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) { @@ -697,7 +818,13 @@ void loop(void) { updateLeds(); #endif - leds.show(); + if (hw_variant == HW_PROTO_PAPER) { + leds_rgb_proto_paper.show(); + } else if (hw_variant == HW_PROTO_V1) { + leds_rgbw_proto_v1.show(); + } + + readSensors(); delay(100); #ifndef DISABLE_WIFI