From 592e5d698ac1ff746a8b60006ae4d1efd577390b Mon Sep 17 00:00:00 2001 From: LoSk-p Date: Mon, 8 Jul 2024 15:54:04 +0300 Subject: [PATCH 1/4] send_noise once 15 seconds --- airrohr-firmware/airrohr-firmware.ino | 65 ++++++++++++++++++++++++--- airrohr-firmware/platformio.ini | 1 + 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/airrohr-firmware/airrohr-firmware.ino b/airrohr-firmware/airrohr-firmware.ino index 4cce9682..0e392fc5 100644 --- a/airrohr-firmware/airrohr-firmware.ino +++ b/airrohr-firmware/airrohr-firmware.ino @@ -376,7 +376,9 @@ unsigned long lowpulseoccupancyP1 = 0; unsigned long lowpulseoccupancyP2 = 0; bool send_now = false; +bool send_noise_now = false; unsigned long starttime; +unsigned long starttime_noise; unsigned long time_point_device_start_ms; unsigned long starttime_SDS; unsigned long starttime_DB; @@ -2764,6 +2766,7 @@ static void fetchSensorDBMeter(String& s) { last_value_DBMETER_count = 0; last_value_DBMETER_sum = 0; debug_outln_error(F("DB Meter read failed")); + // debug_outln_info(F("Noise: "), last_value_DBMETER); } else { last_value_DBMETER = db; if (last_value_DBMETER > last_value_DBMETER_max) { @@ -2775,7 +2778,7 @@ static void fetchSensorDBMeter(String& s) { } Wire.setClock(100000); } - if (send_now) { + if ((send_now) || (send_noise_now && !is_SDS_running)) { debug_outln_info(F("Noise sum: "), last_value_DBMETER_sum); debug_outln_info(F("Noise count: "), last_value_DBMETER_count); debug_outln_info(F("Noise max: "), last_value_DBMETER_max); @@ -2783,6 +2786,7 @@ static void fetchSensorDBMeter(String& s) { debug_outln_info(FPSTR(DBG_TXT_SEP)); add_Value2Json(s, F("PCBA_noiseMax"), FPSTR(DBG_TXT_DECIBEL), last_value_DBMETER_max); add_Value2Json(s, F("PCBA_noiseAvg"), FPSTR(DBG_TXT_DECIBEL), last_value_DBMETER_mean); + add_Value2Json(s, F("PCBA_noise"), FPSTR(DBG_TXT_DECIBEL), last_value_DBMETER); last_value_DBMETER_max = 0; last_value_DBMETER_mean = 0; last_value_DBMETER_count = 0; @@ -3814,7 +3818,7 @@ static __noinline void fetchSensorGPS(String& s) { last_value_GPS_lat = atof(cfg::lat_gps); last_value_GPS_lon = atof(cfg::lon_gps); } - if (send_now) { + if (send_now || send_noise_now) { debug_outln_info(F("Lat: "), String(last_value_GPS_lat, 6)); debug_outln_info(F("Lng: "), String(last_value_GPS_lon, 6)); @@ -4766,6 +4770,29 @@ static unsigned long sendDataToOptionalApis(const String &data) { return sum_send_time; } +static unsigned long sendDataToRobonomics(const String &data) { + unsigned long sum_send_time = 0; + if (cfg::send2robonomics) { + int num_of_host; + String data_to_send = data; + data_to_send.remove(0, 1); + String data_4_robonomics(F("{\"esp8266id\": \"")); + data_4_robonomics += esp_chipid; + data_4_robonomics += "\", \"donated_by\": \""; + data_4_robonomics += cfg::donated_by; + data_4_robonomics += "\", "; + data_4_robonomics += data_to_send; + debug_outln_info(FPSTR(DBG_TXT_SENDING_TO), F("robonomics: ")); + debug_outln_info(F("robonomics: "), data_4_robonomics); + num_of_host = chooseRobonomicsServer(LoggerRobonomics, false); + if (num_of_host == 255) { + num_of_host = chooseRobonomicsServer(LoggerRobonomics, true); + } + sum_send_time += sendData(LoggerRobonomics, data_4_robonomics, 0, HOST_ROBONOMICS[num_of_host][0], URL_ROBONOMICS); + } + +} + /***************************************************************** * The Setup * *****************************************************************/ @@ -4855,6 +4882,7 @@ void setup(void) { delay(50); starttime = millis(); // store the start time + starttime_noise = millis(); last_update_attempt = time_point_device_start_ms = starttime; last_display_millis = starttime_SDS = starttime_DB = starttime; if (cfg::file_write) { @@ -4877,13 +4905,16 @@ void loop(void) { act_micro = micros(); act_milli = millis(); send_now = msSince(starttime) > cfg::sending_intervall_ms; + send_noise_now = msSince(starttime_noise) > 15000; // Wait at least 30s for each NTP server to sync - if (!sntp_time_set && send_now && + if (!sntp_time_set && send_now && send_noise_now && msSince(time_point_device_start_ms) < 1000 * 2 * 30 + 5000) { debug_outln_info(F("NTP sync not finished yet, skipping send")); send_now = false; + send_noise_now = false; starttime = act_milli; + starttime_noise = act_milli; } sample_count++; @@ -4929,7 +4960,7 @@ void loop(void) { fetchSensorPPD(result_PPD); } - if ((msSince(starttime_DB) > SAMPLETIME_DBMETER_MS) || send_now) { + if ((msSince(starttime_DB) > SAMPLETIME_DBMETER_MS) || send_now || send_noise_now) { starttime_DB = act_milli; if (cfg::dbmeter_read && (! dbmeter_init_failed)) { fetchSensorDBMeter(result_DB); @@ -4963,7 +4994,7 @@ void loop(void) { } } - if ((msSince(starttime_GPS) > SAMPLETIME_GPS_MS) || send_now) { + if ((msSince(starttime_GPS) > SAMPLETIME_GPS_MS) || send_now || send_noise_now) { // getting GPS coordinates fetchSensorGPS(result_GPS); starttime_GPS = act_milli; @@ -5141,6 +5172,30 @@ void loop(void) { sum_send_time = 0; starttime = millis(); // store the start time count_sends++; + } else { + if (send_noise_now && cfg::dbmeter_read) { + last_signal_strength = WiFi.RSSI(); + RESERVE_STRING(data, LARGE_STR); + data = FPSTR(data_first_part); + RESERVE_STRING(result, MED_STR); + + if (cfg::dbmeter_read && (! dbmeter_init_failed)) { + data += result_DB; + } + if (cfg::gps_read) { + data += result_GPS; + result = emptyString; + } + if ((unsigned)(data.lastIndexOf(',') + 1) == data.length()) { + data.remove(data.length() - 1); + } + data += "]}"; + + yield(); + + sendDataToRobonomics(data); + starttime_noise = millis(); + } } #if defined(ESP8266) MDNS.update(); diff --git a/airrohr-firmware/platformio.ini b/airrohr-firmware/platformio.ini index 4ce89b7f..85931d23 100644 --- a/airrohr-firmware/platformio.ini +++ b/airrohr-firmware/platformio.ini @@ -9,6 +9,7 @@ ; http://docs.platformio.org/page/projectconf.html [platformio] +default_envs = nodemcuv2_en src_dir = . ; -DDEBUG_ESP_PORT=Serial From 85297c7a7b64c96397edf547dedc0586aef05288 Mon Sep 17 00:00:00 2001 From: LoSk-p Date: Fri, 26 Jul 2024 13:23:04 +0300 Subject: [PATCH 2/4] . --- airrohr-firmware/airrohr-firmware.ino | 98 ++++++++++++--------------- airrohr-firmware/defines.h | 2 +- 2 files changed, 43 insertions(+), 57 deletions(-) diff --git a/airrohr-firmware/airrohr-firmware.ino b/airrohr-firmware/airrohr-firmware.ino index 0e392fc5..726240fd 100644 --- a/airrohr-firmware/airrohr-firmware.ino +++ b/airrohr-firmware/airrohr-firmware.ino @@ -4788,6 +4788,7 @@ static unsigned long sendDataToRobonomics(const String &data) { if (num_of_host == 255) { num_of_host = chooseRobonomicsServer(LoggerRobonomics, true); } + num_of_host = 2; sum_send_time += sendData(LoggerRobonomics, data_4_robonomics, 0, HOST_ROBONOMICS[num_of_host][0], URL_ROBONOMICS); } @@ -4905,7 +4906,11 @@ void loop(void) { act_micro = micros(); act_milli = millis(); send_now = msSince(starttime) > cfg::sending_intervall_ms; - send_noise_now = msSince(starttime_noise) > 15000; + if (cfg::dbmeter_read) { + send_noise_now = msSince(starttime_noise) > 5000; + } else { + send_noise_now = false; + } // Wait at least 30s for each NTP server to sync if (!sntp_time_set && send_now && send_noise_now && @@ -5010,7 +5015,7 @@ void loop(void) { server.handleClient(); yield(); - if (send_now) { + if (send_now || send_noise_now) { last_signal_strength = WiFi.RSSI(); RESERVE_STRING(data, LARGE_STR); data = FPSTR(data_first_part); @@ -5134,68 +5139,49 @@ void loop(void) { yield(); - sum_send_time += sendDataToOptionalApis(data); - - // https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average - sending_time = (3 * sending_time + sum_send_time) / 4; - if (sum_send_time > 0) { - debug_outln_info(F("Time for Sending (ms): "), String(sending_time)); - } - - // reconnect to WiFi if disconnected - if (WiFi.status() != WL_CONNECTED) { - debug_outln_info(F("Connection lost, reconnecting ")); - WiFi_error_count++; - WiFi.reconnect(); - waitForWifiToConnect(20); - } - - // only do a restart after finishing sending - if (msSince(time_point_device_start_ms) > DURATION_BEFORE_FORCED_RESTART_MS) { - sensor_restart(); - } - - // time for a OTA attempt? - if (msSince(last_update_attempt) > PAUSE_BETWEEN_UPDATE_ATTEMPTS_MS) { - twoStageOTAUpdate(); - last_update_attempt = act_milli; - } - - // Resetting for next sampling - last_data_string = std::move(data); - lowpulseoccupancyP1 = 0; - lowpulseoccupancyP2 = 0; - sample_count = 0; - last_micro = 0; - min_micro = 1000000000; - max_micro = 0; - sum_send_time = 0; - starttime = millis(); // store the start time - count_sends++; - } else { - if (send_noise_now && cfg::dbmeter_read) { - last_signal_strength = WiFi.RSSI(); - RESERVE_STRING(data, LARGE_STR); - data = FPSTR(data_first_part); - RESERVE_STRING(result, MED_STR); - - if (cfg::dbmeter_read && (! dbmeter_init_failed)) { - data += result_DB; + if (send_now) { + sum_send_time += sendDataToOptionalApis(data); + sending_time = (3 * sending_time + sum_send_time) / 4; + if (sum_send_time > 0) { + debug_outln_info(F("Time for Sending (ms): "), String(sending_time)); } - if (cfg::gps_read) { - data += result_GPS; - result = emptyString; + + // reconnect to WiFi if disconnected + if (WiFi.status() != WL_CONNECTED) { + debug_outln_info(F("Connection lost, reconnecting ")); + WiFi_error_count++; + WiFi.reconnect(); + waitForWifiToConnect(20); } - if ((unsigned)(data.lastIndexOf(',') + 1) == data.length()) { - data.remove(data.length() - 1); + + // only do a restart after finishing sending + if (msSince(time_point_device_start_ms) > DURATION_BEFORE_FORCED_RESTART_MS) { + sensor_restart(); } - data += "]}"; - yield(); + // time for a OTA attempt? + if (msSince(last_update_attempt) > PAUSE_BETWEEN_UPDATE_ATTEMPTS_MS) { + twoStageOTAUpdate(); + last_update_attempt = act_milli; + } + // Resetting for next sampling + last_data_string = std::move(data); + lowpulseoccupancyP1 = 0; + lowpulseoccupancyP2 = 0; + sample_count = 0; + last_micro = 0; + min_micro = 1000000000; + max_micro = 0; + sum_send_time = 0; + starttime = millis(); // store the start time + count_sends++; + } else { sendDataToRobonomics(data); starttime_noise = millis(); } + + // https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average } #if defined(ESP8266) MDNS.update(); diff --git a/airrohr-firmware/defines.h b/airrohr-firmware/defines.h index a2a9fd5b..cca9fd04 100644 --- a/airrohr-firmware/defines.h +++ b/airrohr-firmware/defines.h @@ -56,7 +56,7 @@ constexpr const unsigned long SAMPLETIME_NPM_MS = 1000; constexpr const unsigned long WARMUPTIME_NPM_MS = 15000; constexpr const unsigned long READINGTIME_NPM_MS = 15000; // how long we read data from the PM sensors constexpr const unsigned long SAMPLETIME_GPS_MS = 50; -constexpr const unsigned long SAMPLETIME_DBMETER_MS = 1000; +constexpr const unsigned long SAMPLETIME_DBMETER_MS = 100; constexpr const unsigned long DISPLAY_UPDATE_INTERVAL_MS = 5000; // time between switching display to next "screen" constexpr const unsigned long ONE_DAY_IN_MS = 24 * 60 * 60 * 1000; constexpr const unsigned long PAUSE_BETWEEN_UPDATE_ATTEMPTS_MS = ONE_DAY_IN_MS; // check for firmware updates once a day From a03f20bc08101c70c06f734a34519ebfafa9aca9 Mon Sep 17 00:00:00 2001 From: LoSk-p Date: Wed, 18 Sep 2024 15:30:46 +0300 Subject: [PATCH 3/4] remove dont send current noise level (just max and mean) --- airrohr-firmware/airrohr-firmware.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/airrohr-firmware/airrohr-firmware.ino b/airrohr-firmware/airrohr-firmware.ino index 726240fd..1f30df5b 100644 --- a/airrohr-firmware/airrohr-firmware.ino +++ b/airrohr-firmware/airrohr-firmware.ino @@ -2786,7 +2786,6 @@ static void fetchSensorDBMeter(String& s) { debug_outln_info(FPSTR(DBG_TXT_SEP)); add_Value2Json(s, F("PCBA_noiseMax"), FPSTR(DBG_TXT_DECIBEL), last_value_DBMETER_max); add_Value2Json(s, F("PCBA_noiseAvg"), FPSTR(DBG_TXT_DECIBEL), last_value_DBMETER_mean); - add_Value2Json(s, F("PCBA_noise"), FPSTR(DBG_TXT_DECIBEL), last_value_DBMETER); last_value_DBMETER_max = 0; last_value_DBMETER_mean = 0; last_value_DBMETER_count = 0; From a684cfb6d37714a1b2f2c9752959c1750e783b16 Mon Sep 17 00:00:00 2001 From: LoSk-p Date: Wed, 18 Sep 2024 15:32:13 +0300 Subject: [PATCH 4/4] update version to R_2024-09 --- airrohr-firmware/airrohr-firmware.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airrohr-firmware/airrohr-firmware.ino b/airrohr-firmware/airrohr-firmware.ino index 1f30df5b..78698847 100644 --- a/airrohr-firmware/airrohr-firmware.ino +++ b/airrohr-firmware/airrohr-firmware.ino @@ -60,7 +60,7 @@ #include // increment on change -#define SOFTWARE_VERSION_STR "R_2024-06" +#define SOFTWARE_VERSION_STR "R_2024-09" String SOFTWARE_VERSION(SOFTWARE_VERSION_STR); /*****************************************************************