diff --git a/include/HomieSettings.h b/include/HomieSettings.h index 84c7270..69b47e7 100644 --- a/include/HomieSettings.h +++ b/include/HomieSettings.h @@ -13,6 +13,6 @@ #define HOMIE_SETTINGS #define HOMIE_FIRMWARE_NAME "RoomSensor" -#define HOMIE_FIRMWARE_VERSION "2.0.0" +#define HOMIE_FIRMWARE_VERSION "2.1.0" #endif diff --git a/platformio.ini b/platformio.ini index 6580e7c..dcb8698 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,7 @@ platform = espressif8266 board = d1_mini framework = arduino -build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY -D BME680 +build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY -D BMP280 ; build_flag needs define the Bosch sensor... ; -D BMP280 ;or @@ -25,4 +25,4 @@ lib_deps = https://github.com/homieiot/homie-esp8266.git#develop adafruit/Adafruit BMP280 Library @ ^2.4.2 adafruit/Adafruit BME680 Library @ ^2.0.1 -upload_port = /dev/ttyUSB1 \ No newline at end of file +upload_port = /dev/ttyUSB0 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4faab6a..c6ce8aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,6 +50,10 @@ #define SEALEVELPRESSURE_HPA (1013.25) +#define BUTTON_MAX_CYCLE 10000U /**< Action: Reset configuration */ +#define BUTTON_MIN_ACTION_CYCLE 55U /**< Minimum cycle to react on the button (e.g. 5 second) */ +#define BUTTON_CHECK_INTERVALL 100U /**< Check every 100 ms the button state */ + #define LOG_TOPIC "log\0" #define MQTT_LEVEL_ERROR 1 #define MQTT_LEVEL_WARNING 10 @@ -80,6 +84,7 @@ #define NODE_AMBIENT "ambient" #define MQTT_DUMMYHOST "localhost" +#define NODE_BUTTON "button" /****************************************************************************** * TYPE DEFS ******************************************************************************/ @@ -98,6 +103,7 @@ bool mConfigured = false; bool mConnected = false; bool mFailedI2Cinitialization = false; AsyncWebServer* mHttp = NULL; +long mLastButtonAction = 0; /******************************* Sensor data **************************/ HomieNode particle(NODE_PARTICLE, "particle", "number"); /**< Measuret in micro gram per quibik meter air volume */ @@ -108,6 +114,7 @@ HomieNode altitudeNode(NODE_ALTITUDE, "Altitude", "number"); HomieNode gasNode(NODE_GAS, "Gas", "number"); HomieNode humidityNode(NODE_HUMIDITY, "Humidity", "number"); #endif +HomieNode buttonNode(NODE_BUTTON, "Button", "number"); /****************************** Output control ***********************/ HomieNode ledStripNode /* to rule them all */("led", "RGB led", "color"); @@ -252,7 +259,9 @@ void updateLEDs() { } strip.show(); } else { +#ifdef BME680 bmx.performReading(); +#endif } } @@ -287,15 +296,15 @@ String sensorAsJSON(void) { String buffer; StaticJsonDocument<500> doc; - doc["temp"] = String(bmx.temperature); + doc["temp"] = String(bmx.readTemperature()); #ifdef BME680 doc["gas"] = String((bmx.gas_resistance / 1000.0)); doc["humidity"] = String(bmx.humidity); #endif - float atmospheric = bmx.pressure / 100.0F; + float atmospheric = bmx.readPressure() / 100.0F; float altitude = 44330.0 * (1.0 - pow(atmospheric / SEALEVELPRESSURE_HPA, 0.1903)); doc["altitude"] = String(altitude); - doc["pressure"] = String(bmx.pressure / 100.0F); + doc["pressure"] = String(atmospheric); doc["particle"] = String(mParticle_pM25); serializeJson(doc, buffer); return buffer; @@ -340,9 +349,19 @@ void loopHandler() bmpPublishValues(); } } - + + /* Clean cycles buttons */ + if (mButtonPressed <= BUTTON_MIN_ACTION_CYCLE) { + buttonNode.setProperty(NODE_BUTTON).send("0"); + } lastRead = millis(); } + + /* if the user sees something via the LEDs, inform MQTT, too */ + if (mButtonPressed > BUTTON_MIN_ACTION_CYCLE) { + buttonNode.setProperty(NODE_BUTTON).send(String(mButtonPressed)); + } + // Feed the dog -> ESP stay alive ESP.wdtFeed(); } @@ -427,7 +446,8 @@ void setup() ledStripNode.advertise(NODE_AMBIENT).setName("All Leds") .setDatatype("color").setFormat("rgb") .settable(ledHandler); - + buttonNode.advertise(NODE_BUTTON).setName("Button pressed") + .setDatatype("integer"); strip.begin(); /* activate I2C for BOSCH sensor */ @@ -512,19 +532,37 @@ void loop() } /* use the pin, receiving the soft serial additionally as button */ if (digitalRead(GPIO_BUTTON) == LOW) { - mButtonPressed++; + if ((millis() - mLastButtonAction) > BUTTON_CHECK_INTERVALL) { + mButtonPressed++; + } + if (mButtonPressed > BUTTON_MIN_ACTION_CYCLE) { + digitalWrite(WITTY_RGB_R, HIGH); + digitalWrite(WITTY_RGB_G, LOW); + digitalWrite(WITTY_RGB_B, LOW); + strip.fill(strip.Color(0,0,0)); + strip.setPixelColor(0, strip.Color((mButtonPressed % 100),0,0)); + strip.setPixelColor(1, strip.Color((mButtonPressed / 100),0,0)); + strip.setPixelColor(2, strip.Color((mButtonPressed / 100),0,0)); + strip.show(); + } } else { mButtonPressed=0U; } - if (mButtonPressed > 10000U) { - mButtonPressed=0U; + if (mButtonPressed > BUTTON_MAX_CYCLE) { + mButtonPressed = (BUTTON_MAX_CYCLE -1); if (SPIFFS.exists("/homie/config.json")) { + strip.fill(strip.Color(0,128,0)); + strip.show(); printf("Resetting config\r\n"); SPIFFS.remove("/homie/config.json"); SPIFFS.end(); + delay(50); + Homie.reboot(); } else { printf("No config present\r\n"); + strip.fill(strip.Color(0,0,128)); + strip.show(); } } }