From 11199c97c692aa865230ed9ce545a3154777823d Mon Sep 17 00:00:00 2001 From: HTotoo Date: Fri, 5 Apr 2024 14:04:30 +0200 Subject: [PATCH] gps baud change, and fixes --- Source/data/setup.html | 6 ++++-- Source/main/configuration.c | 28 ++++++++++++++++++++++++---- Source/main/configuration.h | 2 ++ Source/main/main.c | 7 ++++--- Source/main/orientation.c | 14 +++++++++----- Source/main/orientation.h | 2 ++ Source/main/webserver.h | 27 +++++++++++++++++++++++---- 7 files changed, 68 insertions(+), 18 deletions(-) diff --git a/Source/data/setup.html b/Source/data/setup.html index 080bdb6..96845f6 100644 --- a/Source/data/setup.html +++ b/Source/data/setup.html @@ -44,8 +44,10 @@

ESP32 PP Setup

RGB Brightness %%:
Mag declination angle:
- Find Declination angle for your location here: magnetic-declination.com + Find Declination angle for your location here: noaa.gov
+ GPS baud:

    Cancel diff --git a/Source/main/configuration.c b/Source/main/configuration.c index 7524f35..a37f37d 100644 --- a/Source/main/configuration.c +++ b/Source/main/configuration.c @@ -2,6 +2,7 @@ void load_config_wifi() { + ESP_LOGI("CONFIG", "load_config_wifi"); nvs_handle_t nvs_handle; esp_err_t err = nvs_open("wifi", NVS_READWRITE, &nvs_handle); // https://github.com/espressif/esp-idf/blob/v5.1.2/examples/storage/nvs_rw_value/main/nvs_value_example_main.c if (err != ESP_OK) @@ -40,11 +41,13 @@ void load_config_wifi() } nvs_commit(nvs_handle); nvs_close(nvs_handle); + ESP_LOGI("CONFIG", "load_config_wifi ok"); } } void save_config_wifi() { + ESP_LOGI("CONFIG", "save_config_wifi"); nvs_handle_t nvs_handle; esp_err_t err = nvs_open("wifi", NVS_READWRITE, &nvs_handle); if (err == ESP_OK) @@ -55,14 +58,16 @@ void save_config_wifi() nvs_set_str(nvs_handle, "wifiStaSSID", (const char *)wifiStaSSID); nvs_set_str(nvs_handle, "wifiStaPASS", (const char *)wifiStaPASS); nvs_close(nvs_handle); + ESP_LOGI("CONFIG", "save_config_wifi ok"); } } void load_config_orientation() { + ESP_LOGI("CONFIG", "load_config_orientation"); nvs_handle_t nvs_handle; esp_err_t err = nvs_open("orient", NVS_READWRITE, &nvs_handle); - if (err != ESP_OK) + if (err == ESP_OK) { nvs_get_i16(nvs_handle, "orientationXMin", &orientationXMin); nvs_get_i16(nvs_handle, "orientationYMin", &orientationYMin); @@ -74,6 +79,7 @@ void load_config_orientation() nvs_get_i16(nvs_handle, "declination", &tmp); declinationAngle = tmp / 100; // 2 decimal precision nvs_close(nvs_handle); + ESP_LOGI("CONFIG", "load_config_orientation ok"); } else { @@ -83,9 +89,10 @@ void load_config_orientation() void save_config_orientation() { + ESP_LOGI("CONFIG", "save_config_orientation"); nvs_handle_t nvs_handle; esp_err_t err = nvs_open("orient", NVS_READWRITE, &nvs_handle); - if (err != ESP_OK) + if (err == ESP_OK) { nvs_set_i16(nvs_handle, "orientationXMin", orientationXMin); nvs_set_i16(nvs_handle, "orientationYMin", orientationYMin); @@ -96,6 +103,7 @@ void save_config_orientation() nvs_set_i16(nvs_handle, "declination", (int16_t)(declinationAngle * 100)); nvs_commit(nvs_handle); nvs_close(nvs_handle); + ESP_LOGI("CONFIG", "save_config_orientation ok"); } } @@ -111,27 +119,39 @@ void reset_orientation_calibration() void load_config_misc() { + ESP_LOGI("CONFIG", "load_config_misc"); nvs_handle_t nvs_handle; esp_err_t err = nvs_open("misc", NVS_READWRITE, &nvs_handle); // https://github.com/espressif/esp-idf/blob/v5.1.2/examples/storage/nvs_rw_value/main/nvs_value_example_main.c + rgb_brightness = 50; + gps_baud = 9600; if (err != ESP_OK) { printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err)); - rgb_brightness = 50; } else { nvs_get_u8(nvs_handle, "rgb_brightness", &rgb_brightness); + nvs_get_u32(nvs_handle, "gps_baud", &gps_baud); nvs_close(nvs_handle); + ESP_LOGI("CONFIG", "load_config_misc ok"); } } + void save_config_misc() { + ESP_LOGI("CONFIG", "save_config_misc"); nvs_handle_t nvs_handle; esp_err_t err = nvs_open("misc", NVS_READWRITE, &nvs_handle); - if (err != ESP_OK) + if (err == ESP_OK) { nvs_set_u8(nvs_handle, "rgb_brightness", rgb_brightness); + nvs_set_u32(nvs_handle, "gps_baud", gps_baud); nvs_commit(nvs_handle); nvs_close(nvs_handle); + ESP_LOGI("CONFIG", "save_config_misc ok"); + } + else + { + ESP_LOGI("CONFIG", "save_config_misc err: %d", err); } } \ No newline at end of file diff --git a/Source/main/configuration.h b/Source/main/configuration.h index 35a5894..48fe64d 100644 --- a/Source/main/configuration.h +++ b/Source/main/configuration.h @@ -3,6 +3,7 @@ #include "nvs_flash.h" #include "string.h" +#include "esp_log.h" // no need to change, just connect to the AP, and change in the settings. #define DEFAULT_WIFI_HOSTNAME "ESP32PP" @@ -32,6 +33,7 @@ extern float declinationAngle; // misc extern uint8_t rgb_brightness; +extern uint32_t gps_baud; void load_config_wifi(); void save_config_wifi(); diff --git a/Source/main/main.c b/Source/main/main.c index 78b0143..acb012e 100644 --- a/Source/main/main.c +++ b/Source/main/main.c @@ -61,6 +61,7 @@ float pressure = 0.0; uint16_t light = 0; gps_t gpsdata; uint16_t lastReportedMxS = 0; // gps last reported gps time mix to see if it is changed. if not changes, it stuck (bad signal, no update), so won't update PP based on it +uint32_t gps_baud = 9600; #include "led.h" @@ -134,7 +135,7 @@ void app_main(void) wifi_apsta(); init_httpd(); nmea_parser_config_t nmeaconfig = NMEA_PARSER_CONFIG_DEFAULT(); - nmeaconfig.uart.baud_rate = 9600; // todo modify by config + nmeaconfig.uart.baud_rate = gps_baud; // todo modify by config nmea_parser_handle_t nmea_hdl = nmea_parser_init(&nmeaconfig); nmea_parser_add_handler(nmea_hdl, gps_event_handler, NULL); esp_task_wdt_deinit(); @@ -148,7 +149,7 @@ void app_main(void) init_rgb(); rgb_set(255, 255, 255); - init_orientation(); + init_orientation(); // it loads orientation data too init_environment(); uint32_t time_millis = 0; @@ -163,7 +164,7 @@ void app_main(void) // GPS IS AUTO ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &temperatureEsp)); // TEMPINT heading = get_heading_degrees(); // ORIENTATION - tilt = 400; // TILT //TODO + tilt = get_tilt(); // TILT get_environment_meas(&temperature, &pressure, &humidity); // env data get_environment_light(&light); // light (lux) data last_millis[TimerEntry_SENSORGET] = time_millis; diff --git a/Source/main/orientation.c b/Source/main/orientation.c index 4e4e651..b67f9e3 100644 --- a/Source/main/orientation.c +++ b/Source/main/orientation.c @@ -4,7 +4,7 @@ #include #include "sensordb.h" -float declinationAngle = 0; // todo setup to web interface http://www.magnetic-declination.com/ +float declinationAngle = 0; // todo setup to web interface https://www.ngdc.noaa.gov/geomag/calculators/magcalc.shtml hmc5883l_dev_t dev_hmc5883l; i2c_dev_t dev_adxl345; @@ -16,6 +16,8 @@ float accelo_x = 0; float accelo_y = 0; float accelo_z = 0; +float m_tilt = 400.0; + int16_t orientationXMin = INT16_MAX; int16_t orientationYMin = INT16_MAX; int16_t orientationZMin = INT16_MAX; @@ -42,6 +44,8 @@ void init_gyro() void init_orientation() { + // load calibration data + load_config_orientation(); // HCM5883l memset(&dev_hmc5883l, 0, sizeof(hmc5883l_dev_t)); hmc5883l_init_desc(&dev_hmc5883l, 0, CONFIG_IC2SDAPIN, CONFIG_IC2SCLPIN, getDevAddr(HMC5883L)); @@ -68,8 +72,6 @@ void init_orientation() } init_gyro(); - // load calibration data - load_config_orientation(); } float fix_heading(float heading) @@ -113,7 +115,7 @@ float get_heading() float accYnorm = accelo_y / sqrt(accelo_x * accelo_x + accelo_y * accelo_y + accelo_z * accelo_z); float pitch = asin(-accXnorm); float roll = asin(accYnorm / cos(pitch)); - + m_tilt = roll * 2 * M_PI; // todo use calibration float magXcomp = data.x; // (magX - calibration[0]) / (calibration[1] - calibration[0]) * 2 - 1; float magYcomp = data.y; //(magY - calibration[2]) / (calibration[3] - calibration[2]) * 2 - 1; @@ -150,4 +152,6 @@ void set_declination(float declination) declinationAngle = declination; save_config_orientation(); } -float get_declination() { return declinationAngle; } \ No newline at end of file +float get_declination() { return declinationAngle; } + +float get_tilt() { return m_tilt; } \ No newline at end of file diff --git a/Source/main/orientation.h b/Source/main/orientation.h index b97c988..f8b8cf9 100644 --- a/Source/main/orientation.h +++ b/Source/main/orientation.h @@ -37,4 +37,6 @@ void calibrate_orientation(uint8_t sec); void set_declination(float declination); float get_declination(); +float get_tilt(); + #endif \ No newline at end of file diff --git a/Source/main/webserver.h b/Source/main/webserver.h index 76878f9..aebf033 100644 --- a/Source/main/webserver.h +++ b/Source/main/webserver.h @@ -50,7 +50,7 @@ static esp_err_t get_req_handler(httpd_req_t *req) /// setup.html get handler static esp_err_t get_req_handler_setup(httpd_req_t *req) { - snprintf(setup_html_out, sizeof(setup_html_out), setup_start, wifiHostName, wifiAPSSID, wifiAPPASS, wifiStaSSID, wifiStaPASS, rgb_brightness, declinationAngle); + snprintf(setup_html_out, sizeof(setup_html_out), setup_start, wifiHostName, wifiAPSSID, wifiAPPASS, wifiStaSSID, wifiStaPASS, rgb_brightness, declinationAngle, gps_baud); int response = httpd_resp_send(req, setup_html_out, HTTPD_RESP_USE_STRLEN); return response; } @@ -128,6 +128,7 @@ static esp_err_t post_req_handler_setup(httpd_req_t *req) strcpy(wifiStaSSID, tmp); if (find_post_value("wifiStaPASS=", buf, tmp) > 0) strcpy(wifiStaPASS, tmp); + if (find_post_value("rgb_brightness=", buf, tmp) > 0) { rgb_brightness = (uint8_t)atoi(tmp); @@ -135,17 +136,35 @@ static esp_err_t post_req_handler_setup(httpd_req_t *req) rgb_brightness = 100; changeMask |= 2; } + if (find_post_value("gps_baud=", buf, tmp) > 0) + { + uint32_t gps_tmp = (uint32_t)atoi(tmp); // todo make it a select in html, with default selected. would take too much space + if (gps_tmp == 2400 || gps_tmp == 4800 || gps_tmp == 9600 || gps_tmp == 14400 || gps_tmp == 19200 || gps_tmp == 38400 || gps_tmp == 57600 || gps_tmp == 115200) + { + gps_baud = gps_tmp; + changeMask |= 2; + } + } if (find_post_value("declinationAngle=", buf, tmp) > 0) { + for (int i = 0; tmp[i] != '\0'; i++) // replace international stuff + { + if (i > 64) + break; + if (tmp[i] == ',') + { + tmp[i] = '.'; + } + } declinationAngle = atof(tmp); changeMask |= 4; } - if (changeMask && 1 == 1) + if ((changeMask & 1) == 1) save_config_wifi(); - if (changeMask && 2 == 2) + if ((changeMask & 2) == 2) save_config_misc(); - if (changeMask && 4 == 4) + if ((changeMask & 4) == 4) save_config_orientation(); free(buf);