Skip to content

Commit

Permalink
gps baud change, and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
htotoo committed Apr 5, 2024
1 parent f1c6eaf commit 11199c9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Source/data/setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ <h1>ESP32 PP Setup</h1>
RGB Brightness %%: <input type="number" min="0" max="100" step="1" name="rgb_brightness" value="%d" /><br />
Mag declination angle: <input type="number" min="0" max="365" step="0.1" name="declinationAngle"
value="%0.1f" /><br />
<i>Find Declination angle for your location here: <a href="https://www.magnetic-declination.com/"
target="_blank">magnetic-declination.com</a></i>
<i>Find Declination angle for your location here: <a
href="https://www.ngdc.noaa.gov/geomag/calculators/magcalc.shtml"
target="_blank">noaa.gov</a></i><br />
GPS baud: <input type="number" step="1" name="gps_baud" value="%d" /><br />
</p>
<input type="submit" name="submit" value="Save" />&nbsp; &nbsp; <a href="/">Cancel</a>
</form>
Expand Down
28 changes: 24 additions & 4 deletions Source/main/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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
{
Expand All @@ -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);
Expand All @@ -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");
}
}

Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions Source/main/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions Source/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions Source/main/orientation.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <string.h>
#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;
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -68,8 +72,6 @@ void init_orientation()
}

init_gyro();
// load calibration data
load_config_orientation();
}

float fix_heading(float heading)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -150,4 +152,6 @@ void set_declination(float declination)
declinationAngle = declination;
save_config_orientation();
}
float get_declination() { return declinationAngle; }
float get_declination() { return declinationAngle; }

float get_tilt() { return m_tilt; }
2 changes: 2 additions & 0 deletions Source/main/orientation.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ void calibrate_orientation(uint8_t sec);
void set_declination(float declination);
float get_declination();

float get_tilt();

#endif
27 changes: 23 additions & 4 deletions Source/main/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -128,24 +128,43 @@ 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);
if (rgb_brightness > 100)
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);
Expand Down

0 comments on commit 11199c9

Please sign in to comment.