Thanks for the reply. I started with your examples to write my code. I've already added that line of code, but I still can't get an iac accuracy other than 0. LoadState and UpdateState are necessary for iaq accuracy and iaq static values? Here the updated sketch: #include <Arduino.h> #include <ArduinoJson.h> #include <UniversalTelegramBot.h> #include "bsec.h" #include <PubSubClient.h> #include <WiFiClientSecure.h> const uint8_t bsec_config_iaq[] = { #include "config/generic_33v_3s_4d/bsec_iaq.txt" }; #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 600 /* Time ESP32 will go to sleep (in seconds) */ RTC_DATA_ATTR int bootCount = 0; const char *SSID = "XXXXX"; const char *PWD = "XXXXX"; const char* mqtt_server = "XXXXXX"; const char* mqtt_user = "XXXXXX"; const char* mqtt_pwd = "XXXXX"; WiFiClientSecure espclientSecure; WiFiClient espClient; PubSubClient client(espClient); unsigned long lastMsg = 0; #define MSG_BUFFER_SIZE (50) char msg[MSG_BUFFER_SIZE]; int value = 0; String output; StaticJsonDocument<800> jsonDocument; char buffer[800]; // Use @myidbot to find out the chat ID of an individual or a group // Also note that you need to click "start" on a bot before it can // message you #define CHAT_ID "XXXXX" // Initialize Telegram BOT #define BOTtoken "XXXXXX" // your Bot Token (Get from Botfather) UniversalTelegramBot bot(BOTtoken, espclientSecure); // Create an object of the class Bsec Bsec iaqSensor; // env variable String temperature; String humidity; String pressure; String CO2; String gasResistance; String iaq; int i; void connectToWiFi() { Serial.print("Connecting to "); Serial.println(SSID); WiFi.begin(SSID, PWD); 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 setup() { Serial.begin(9600); #ifdef ESP8266 espclientSecure.setInsecure(); #endif // Sensor setup Wire.begin(); iaqSensor.begin(BME680_I2C_ADDR_SECONDARY, Wire); 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(); connectToWiFi(); client.setServer(mqtt_server, XXXX); } void loop() { if (!client.connected()) { reconnect(); } setCpuFrequencyMhz(160); ++value; snprintf (msg, MSG_BUFFER_SIZE, "Nuovi dati letti, count: #%ld", value); Serial.print("Publish message: "); Serial.println(msg); if (iaqSensor.run()) { Serial.println("Umidità:"+(String)iaqSensor.humidity); Serial.println("Temperatura:"+(String)iaqSensor.temperature); Serial.println("IAQ:"+(String)iaqSensor.iaq); Serial.println("Pressure:"+(String)(iaqSensor.pressure/100)); Serial.println("Gas Resistance:"+(String)iaqSensor.gasResistance); Serial.print("IAQ Accuracy:" + String(iaqSensor.iaqAccuracy)); Serial.print("Static IAQ: " + String(iaqSensor.staticIaq)); Serial.print("CO2Equivalent: " + String(iaqSensor.co2Equivalent)); Serial.print("BreathVocEquivalent: " + String(iaqSensor.breathVocEquivalent)); //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 messageTemperature = "Temperatura:"+(String)iaqSensor.temperature; bot.sendMessage(CHAT_ID, messageTemperature, ""); delay(3000); client.disconnect(); startLightSleep(); }else{ checkIaqSensorStatus(); } } void startLightSleep(void){ esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); // ESP32 wakes up every 5 seconds Serial.println("Going to light-sleep now"); Serial.flush(); setCpuFrequencyMhz(80); WiFi.disconnect(true); WiFi.mode(WIFI_OFF); btStop(); esp_light_sleep_start(); } // 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); bot.sendMessage(CHAT_ID, output, ""); for (;;) errLeds(); /* Halt in case of failure */ Serial.println("Restarting in 10 seconds"); delay(10000); ESP.restart(); } else { output = "BSEC warning code : " + String(iaqSensor.status); Serial.println(output); bot.sendMessage(CHAT_ID, output, ""); } } if (iaqSensor.bme680Status != BME680_OK) { if (iaqSensor.bme680Status < BME680_OK) { output = "BME680 error code : " + String(iaqSensor.bme680Status); Serial.println(output); bot.sendMessage(CHAT_ID, output, ""); for (;;) errLeds(); /* Halt in case of failure */ Serial.println("Restarting in 10 seconds"); delay(10000); ESP.restart(); } else { output = "BME680 warning code : " + String(iaqSensor.bme680Status); Serial.println(output); bot.sendMessage(CHAT_ID, output, ""); } } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP32Cameretta-"; clientId += String(random(0xffff), HEX); if(WiFi.status() != WL_CONNECTED){ connectToWiFi(); } // Attempt to connect client.connect(clientId.c_str(), mqtt_user, mqtt_pwd); if (client.connected()) { Serial.println("connected"); bot.sendMessage(CHAT_ID, "Connected to MQTT Server", ""); // Once connected, publish an announcement... } 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"; bot.sendMessage(CHAT_ID, errorConnectMqtt, ""); // Wait 5 seconds before retrying delay(5000); } } } 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(); Serial.println("Get env"); 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(200); digitalWrite(LED_BUILTIN, LOW); delay(200); }
... View more