I'm a newbie in arduino, after a code review the value of iaq accuracy goes to 1 after 100 reads (I had to disable the light sleep). The problem is that after more than 12 hours of operation the value remains stationary at 1 and the value of the iaq is very high (more than 200) while the value of the static iaq is still at 65. How many hours does it take for the value of iaq accuracy to reach 3? Thank you This is my actual sketch: #include <Arduino.h> #include <ArduinoJson.h> #include <UniversalTelegramBot.h> #include "bsec.h" #include <PubSubClient.h> #include <WiFiClientSecure.h> #include <WiFiManager.h> const uint8_t bsec_config_iaq[] = { #include "config/generic_33v_3s_4d/bsec_iaq.txt" }; #define uS_TO_S_FACTOR 10000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 20 /* Time ESP32 will go to sleep (in seconds) */ #define MSG_BUFFER_SIZE (50) //define your default values here, if there are different values in config.json, they are overwritten. char mqtt_server[40]; char mqtt_port[6]; char bot_token[150]; char telegram_chat_id[40]; char send_telegram_notification[5]; char read_value_delay_in_minutes[3]; char mqtt_user[30]; char mqtt_pwd[50]; char *mqttUser; char *mqttPwd; WiFiClientSecure espclientSecure; WiFiClient espClient; PubSubClient client(espClient); unsigned long lastMsg = 0; char msg[MSG_BUFFER_SIZE]; int value = 0; String output; StaticJsonDocument<800> jsonDocument; char buffer[800]; UniversalTelegramBot bot(bot_token, espclientSecure); // Helper functions declarations void checkIaqSensorStatus(void); void errLeds(void); // Create an object of the class Bsec Bsec iaqSensor; int i; void connectToWiFi() { Serial.print("Connecting to WIFI "); //Serial.println(SSID); WiFi.begin(); while (WiFi.status() != WL_CONNECTED) { if ( i == 15 ){ WiFi.reconnect();} if (i > 30){ break; } delay(500); i++; } Serial.print("Connected. IP: "); Serial.println(WiFi.localIP()); } void setupWifiManagerAP(){ // The extra parameters to be configured (can be either global or just in the setup) // After connecting, parameter.getValue() will get you the configured value // id/name placeholder/prompt default length WiFiManagerParameter custom_mqtt_server("server", "MQTT SERVER IP", mqtt_server, 40); WiFiManagerParameter custom_mqtt_port("port", "MQTT SERVER PORT", mqtt_port, 6); WiFiManagerParameter custom_mqtt_user("mqtt_user", "MQTT USER", mqtt_user, 30); WiFiManagerParameter custom_mqtt_pwd("mqtt_pwd", "MQTT PWD", mqtt_pwd, 50); WiFiManagerParameter custom_bot_token("telegram_bot_token", "Telegram Bot Token", bot_token, 150); WiFiManagerParameter custom_telegram_chat_id("telegram_chat_id", "Telegram Chat ID", telegram_chat_id, 40); WiFiManagerParameter custom_send_telegram_notification("send_telegram_notification", "Send telegram notification (true/false)", send_telegram_notification, 5); WiFiManagerParameter custom_read_value_delay_in_minutes("read_value_delay_in_minutes", "Read Value Delay in Minutes", read_value_delay_in_minutes, 3); WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP // WiFi.mode(WiFi_STA); // it is a good practice to make sure your code sets wifi mode how you want it. //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around WiFiManager wm; //reset settings - wipe credentials for testing wm.resetSettings(); //add all your parameters here wm.addParameter(&custom_mqtt_server); wm.addParameter(&custom_mqtt_port); wm.addParameter(&custom_mqtt_user); wm.addParameter(&custom_mqtt_pwd); wm.addParameter(&custom_bot_token); wm.addParameter(&custom_telegram_chat_id); wm.addParameter(&custom_send_telegram_notification); wm.addParameter(&custom_read_value_delay_in_minutes); // Automatically connect using saved credentials, // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"), // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect()) // then goes into a blocking loop awaiting configuration and will return success result bool res; res = wm.autoConnect("TemperaturinoAPConfig","XXXXXX"); // password protected ap if(!res) { Serial.println("Failed to connect"); // ESP.restart(); }else { //if you get here you have connected to the WiFi Serial.println("connected...:)"); //read updated parameters strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(mqtt_user, custom_mqtt_user.getValue()); strcpy(mqtt_pwd, custom_mqtt_pwd.getValue()); strcpy(bot_token, custom_bot_token.getValue()); strcpy(telegram_chat_id, custom_telegram_chat_id.getValue()); strcpy(send_telegram_notification, custom_send_telegram_notification.getValue()); strcpy(read_value_delay_in_minutes, custom_read_value_delay_in_minutes.getValue()); Serial.print(F("server --> ")); Serial.println(mqtt_server); Serial.print(F("port --> ")); Serial.println(mqtt_port); Serial.print(F("mqtt_user --> ")); Serial.println(mqtt_user); Serial.print(F("mqtt_pwd --> ")); Serial.println(mqtt_pwd); Serial.print(F("bot_token --> ")); Serial.println(bot_token); Serial.print(F("telegram_chat_id --> ")); Serial.println(telegram_chat_id); Serial.print(F("send_telegram_notification --> ")); Serial.println(send_telegram_notification); Serial.print(F("read_value_delay_in_minutes --> ")); Serial.println(read_value_delay_in_minutes); } } void setupBME680(){ // Sensor setup Wire.begin(); iaqSensor.begin(BME680_I2C_ADDR_SECONDARY, Wire); checkIaqSensorStatus(); iaqSensor.setConfig(bsec_config_iaq); bsec_virtual_sensor_t sensorList[10] = { BSEC_OUTPUT_RAW_TEMPERATURE, BSEC_OUTPUT_RAW_PRESSURE, BSEC_OUTPUT_RAW_HUMIDITY, BSEC_OUTPUT_RAW_GAS, BSEC_OUTPUT_IAQ, BSEC_OUTPUT_STATIC_IAQ, BSEC_OUTPUT_CO2_EQUIVALENT, BSEC_OUTPUT_BREATH_VOC_EQUIVALENT, BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE, BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY, }; iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP); checkIaqSensorStatus(); } void setup() { Serial.begin(115200); #ifdef ESP8266 espclientSecure.setInsecure(); #endif setupWifiManagerAP(); setupBME680(); //connectToWiFi(); bot.updateToken(bot_token); mqttUser = mqtt_user; mqttPwd = mqtt_pwd; Serial.println("MqttUser:"+String(mqttUser)); Serial.println("MqttPwd:"+String(mqttPwd)); client.setServer(mqtt_server, atoi(mqtt_port)); } void loop() { unsigned long now = millis(); String delayString = String(read_value_delay_in_minutes); int delayValue = delayString.toInt(); if (now - lastMsg >(delayValue * uS_TO_S_FACTOR)) { Serial.print("Delay:"); Serial.println(delayValue*uS_TO_S_FACTOR); lastMsg = now; Serial.print("LastMsg:"); Serial.println(lastMsg); ++value; snprintf (msg, MSG_BUFFER_SIZE, "Nuovi dati letti, count: #%ld", value); Serial.print("Publish message: "); Serial.println(msg); if (!client.connected()) { reconnect(); } if (iaqSensor.run()) { /*output = String(now); output += ", " + String(iaqSensor.rawTemperature); output += ", " + String(iaqSensor.pressure); output += ", " + String(iaqSensor.rawHumidity); output += ", " + String(iaqSensor.gasResistance); output += ", " + String(iaqSensor.iaq); output += ", " + String(iaqSensor.iaqAccuracy); output += ", " + String(iaqSensor.temperature); output += ", " + String(iaqSensor.humidity); output += ", " + String(iaqSensor.staticIaq); output += ", " + String(iaqSensor.co2Equivalent); output += ", " + String(iaqSensor.breathVocEquivalent); Serial.println(output);*/ //PubSubClient::setBufferSize(500); setJsonValues(iaqSensor.temperature, iaqSensor.humidity, iaqSensor.gasResistance, iaqSensor.pressure, iaqSensor.iaq,iaqSensor.iaqAccuracy, iaqSensor.staticIaq, iaqSensor.co2Equivalent, iaqSensor.breathVocEquivalent); size_t n = serializeJson(jsonDocument, buffer); client.publish("esp32/cameretta/sensor", buffer, n); String messageIaqAccuracy = "IaqAccuracy:"+(String)iaqSensor.iaqAccuracy; sendTelegramMessage(messageIaqAccuracy); Serial.print(messageIaqAccuracy); delay(3000); client.disconnect(); }else{ checkIaqSensorStatus(); } } } // Helper function definitions void checkIaqSensorStatus(void) { if (iaqSensor.status != BSEC_OK) { if (iaqSensor.status < BSEC_OK) { output = "BSEC error code : " + String(iaqSensor.status); Serial.println(output); sendTelegramMessage(output); for (;;) errLeds(); /* Halt in case of failure */ } else { output = "BSEC warning code : " + String(iaqSensor.status); Serial.println(output); sendTelegramMessage(output); } } if (iaqSensor.bme680Status != BME680_OK) { if (iaqSensor.bme680Status < BME680_OK) { output = "BME680 error code : " + String(iaqSensor.bme680Status); Serial.println(output); sendTelegramMessage(output); for (;;) errLeds(); /* Halt in case of failure */ } else { output = "BME680 warning code : " + String(iaqSensor.bme680Status); Serial.println(output); sendTelegramMessage(output); } } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP32-"; clientId += String(random(0xffff), HEX); if(WiFi.status() != WL_CONNECTED){ connectToWiFi(); delay(2000); } // Attempt to connect client.connect(clientId.c_str(),mqttUser, mqttPwd); if (client.connected()) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); String errorConnectMqtt = "failed to connect to MQTT Server try again in 5 seconds"; sendTelegramMessage(errorConnectMqtt); // Wait 5 seconds before retrying delay(5000); } } } void sendTelegramMessage(String message){ if (String(send_telegram_notification) =="true"){ bot.sendMessage(telegram_chat_id, message, ""); } } void add_json_object(char *tag, float value, char *unit) { JsonObject obj = jsonDocument.createNestedObject(); obj["type"] = tag; obj["value"] = value; obj["unit"] = unit; } void setJsonValues(float temperature, float humidity, float gasResistance, float pressure, float iaq, int iaqAccuracy, int staticIaq, float co2Equivalent, float breathVocEquivalent ) { jsonDocument.clear(); add_json_object("temperature", temperature, "°C"); add_json_object("humidity", humidity, "%"); add_json_object("gas", gasResistance, "ohm"); add_json_object("pressure", pressure, "mBar"); add_json_object("iaq", iaq, "IAQ"); add_json_object("iaqAccuracy", iaqAccuracy, "IAQ"); add_json_object("staticIaq", staticIaq, "IAQ"); add_json_object("co2Equivalent", co2Equivalent, "ppm"); add_json_object("breathVocEquivalent", breathVocEquivalent, "ppm"); } void errLeds(void) { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); delay(100); digitalWrite(LED_BUILTIN, LOW); delay(100); }
... View more