Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Firmware/RTK_Everywhere/RTK_Everywhere.ino
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ bool firstButtonThrownOut = false;
// Because the incoming string is longer than max len, there are multiple callbacks so we
// use a global to combine the incoming
#define AP_CONFIG_SETTING_SIZE 20000 // 10000 isn't enough if the SD card contains many files
char *settingsCSV; // Push large array onto heap
#define AP_FIRMWARE_VERSION_SIZE 256

char *incomingSettings;
int incomingSettingsSpot;
unsigned long timeSinceLastIncomingSetting;
Expand Down
22 changes: 0 additions & 22 deletions Firmware/RTK_Everywhere/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,6 @@ void stopWebServer()
webServer = nullptr;
}

if (settingsCSV != nullptr)
{
rtkFree(settingsCSV, "Settings buffer (settingsCSV)");
settingsCSV = nullptr;
}

if (incomingSettings != nullptr)
{
rtkFree(incomingSettings, "Settings buffer (incomingSettings)");
Expand Down Expand Up @@ -684,16 +678,6 @@ bool webServerAssignResources(int httpPort = 80)
}
memset(incomingSettings, 0, AP_CONFIG_SETTING_SIZE);

// Pre-load settings CSV
// Freed by webServerStop
settingsCSV = (char *)rtkMalloc(AP_CONFIG_SETTING_SIZE, "Settings buffer (settingsCSV)");
if (!settingsCSV)
{
systemPrintln("ERROR: Web server failed to allocate settingsCSV");
break;
}
createSettingsString(settingsCSV);

/* https://github.com/espressif/arduino-esp32/blob/master/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino
*/

Expand Down Expand Up @@ -907,12 +891,6 @@ void webServerReleaseResources()
webServer = nullptr;
}

if (settingsCSV != nullptr)
{
rtkFree(settingsCSV, "Settings buffer (settingsCSV)");
settingsCSV = nullptr;
}

if (incomingSettings != nullptr)
{
rtkFree(incomingSettings, "Settings buffer (incomingSettings)");
Expand Down
87 changes: 56 additions & 31 deletions Firmware/RTK_Everywhere/WebSockets.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WebSockets.ino
// Constants
//----------------------------------------

static const int webSocketsStackSize = 1024 * 20; // Needs to be large enough to hold the full settingsCSV
static const int webSocketsStackSize = 1024 * 20;

//----------------------------------------
// New types
Expand All @@ -37,29 +37,29 @@ static SemaphoreHandle_t webSocketsMutex;
// Create a csv string with the dynamic data to update (current coordinates,
// battery level, etc)
//----------------------------------------
void webSocketsCreateDynamicDataString(char *settingsCSV)
void webSocketsCreateDynamicDataString(char *csvList)
{
settingsCSV[0] = '\0'; // Erase current settings string
csvList[0] = '\0'; // Erase current settings string

// Current coordinates come from HPPOSLLH call back
stringRecord(settingsCSV, "geodeticLat", gnss->getLatitude(), haeNumberOfDecimals);
stringRecord(settingsCSV, "geodeticLon", gnss->getLongitude(), haeNumberOfDecimals);
stringRecord(settingsCSV, "geodeticAlt", gnss->getAltitude(), 3);
stringRecord(csvList, "geodeticLat", gnss->getLatitude(), haeNumberOfDecimals);
stringRecord(csvList, "geodeticLon", gnss->getLongitude(), haeNumberOfDecimals);
stringRecord(csvList, "geodeticAlt", gnss->getAltitude(), 3);

double ecefX = 0;
double ecefY = 0;
double ecefZ = 0;

geodeticToEcef(gnss->getLatitude(), gnss->getLongitude(), gnss->getAltitude(), &ecefX, &ecefY, &ecefZ);

stringRecord(settingsCSV, "ecefX", ecefX, 3);
stringRecord(settingsCSV, "ecefY", ecefY, 3);
stringRecord(settingsCSV, "ecefZ", ecefZ, 3);
stringRecord(csvList, "ecefX", ecefX, 3);
stringRecord(csvList, "ecefY", ecefY, 3);
stringRecord(csvList, "ecefZ", ecefZ, 3);

if (online.batteryFuelGauge == false) // Product has no battery
{
stringRecord(settingsCSV, "batteryIconFileName", (char *)"src/BatteryBlank.png");
stringRecord(settingsCSV, "batteryPercent", (char *)" ");
stringRecord(csvList, "batteryIconFileName", (char *)"src/BatteryBlank.png");
stringRecord(csvList, "batteryPercent", (char *)" ");
}
else
{
Expand All @@ -81,7 +81,7 @@ void webSocketsCreateDynamicDataString(char *settingsCSV)
else
snprintf(batteryIconFileName, sizeof(batteryIconFileName), "src/Battery%d.png", iconLevel);

stringRecord(settingsCSV, "batteryIconFileName", batteryIconFileName);
stringRecord(csvList, "batteryIconFileName", batteryIconFileName);

// Limit batteryLevelPercent to sane levels
if (batteryLevelPercent > 100)
Expand All @@ -93,21 +93,21 @@ void webSocketsCreateDynamicDataString(char *settingsCSV)
snprintf(batteryPercent, sizeof(batteryPercent), "+%d%%", batteryLevelPercent);
else
snprintf(batteryPercent, sizeof(batteryPercent), "%d%%", batteryLevelPercent);
stringRecord(settingsCSV, "batteryPercent", batteryPercent);
stringRecord(csvList, "batteryPercent", batteryPercent);
}

strcat(settingsCSV, "\0");
strcat(csvList, "\0");
}

//----------------------------------------
// Report back to the web config page with a CSV that contains the either CURRENT or
// the latest version as obtained by the OTA state machine
//----------------------------------------
void webSocketsCreateFirmwareVersionString(char *settingsCSV)
void webSocketsCreateFirmwareVersionString(char *firmwareString)
{
char newVersionCSV[100];

settingsCSV[0] = '\0'; // Erase current settings string
firmwareString[0] = '\0'; // Erase current settings string

// Create a string of the unit's current firmware version
char currentVersion[21];
Expand All @@ -127,9 +127,9 @@ void webSocketsCreateFirmwareVersionString(char *settingsCSV)
snprintf(newVersionCSV, sizeof(newVersionCSV), "CURRENT,");
}

stringRecord(settingsCSV, "newFirmwareVersion", newVersionCSV);
stringRecord(firmwareString, "newFirmwareVersion", newVersionCSV);

strcat(settingsCSV, "\0");
strcat(firmwareString, "\0");
}

//----------------------------------------
Expand Down Expand Up @@ -182,9 +182,19 @@ static esp_err_t webSocketsHandler(httpd_req_t *req)
client->_request, client->_socketFD);

lastDynamicDataUpdate = millis();
webSocketsSendString(settingsCSV);

return ESP_OK;
// Send new settings to browser.
char * settingsCsvList = (char *)rtkMalloc(AP_CONFIG_SETTING_SIZE, "Command CSV settings list");
if (settingsCsvList)
{
createSettingsString(settingsCsvList);
webSocketsSendString(settingsCsvList);
rtkFree(settingsCsvList, "Command CSV settings list");
return ESP_OK;
}
if (settings.debugWebServer == true)
systemPrintf("ERROR: Failed to allocate settings CSV list!\r\n");
return ESP_FAIL;
}

httpd_ws_frame_t ws_pkt;
Expand Down Expand Up @@ -277,8 +287,6 @@ static esp_err_t webSocketsHandler(httpd_req_t *req)
{
if (settings.debugWebServer == true)
systemPrintln("WebSockets: Client closed or refreshed the web page");

createSettingsString(settingsCSV);
}

rtkFree(buf, "Payload buffer (buf)");
Expand All @@ -298,21 +306,40 @@ bool webSocketsIsConnected()
//----------------------------------------
void webSocketsSendFirmwareVersion(void)
{
webSocketsCreateFirmwareVersionString(settingsCSV);
char * firmwareVersion;

if (settings.debugWebServer)
systemPrintf("WebSockets: Firmware version requested. Sending: %s\r\n", settingsCSV);
firmwareVersion = (char *)rtkMalloc(AP_FIRMWARE_VERSION_SIZE, "WebSockets firmware description");
if (firmwareVersion == nullptr)
systemPrintf("ERROR: WebSockets failed to allocate firmware description\r\n");
else
{
webSocketsCreateFirmwareVersionString(firmwareVersion);

webSocketsSendString(settingsCSV);
if (settings.debugWebServer)
systemPrintf("WebSockets: Firmware version requested. Sending: %s\r\n",
firmwareVersion);

webSocketsSendString(firmwareVersion);
rtkFree(firmwareVersion, "WebSockets firmware description");
}
}

//----------------------------------------
// Send the current settings via web sockets
//----------------------------------------
void webSocketsSendSettings(void)
{
webSocketsCreateDynamicDataString(settingsCSV);
webSocketsSendString(settingsCSV);
char * settingsCsvList;

settingsCsvList = (char *)rtkMalloc(AP_CONFIG_SETTING_SIZE, "WebSockets CSV settings list");
if (settingsCsvList == nullptr)
systemPrintf("ERROR: WebSockets failed to allocate settings CSV list\r\n");
else
{
webSocketsCreateDynamicDataString(settingsCsvList);
webSocketsSendString(settingsCsvList);
rtkFree(settingsCsvList, "WebSockets CSV settings list");
}
}

//----------------------------------------
Expand Down Expand Up @@ -373,7 +400,7 @@ void webSocketsSendString(const char *stringToSend)

// Successfully sent the message
else if (settings.debugWebServer == true)
systemPrintf("webSocketsSendString: %s\r\n", stringToSend);
systemPrintf("webSocketsSendString: %s\r\n", stringToSend);

// Get the next client
client = nextClient;
Expand Down Expand Up @@ -406,8 +433,6 @@ bool webSocketsStart(void)

// Use different ports for websocket and webServer - use port 81 for the websocket - also defined in main.js
config.server_port = 81;

// Increase the stack size from 4K to handle page processing (settingsCSV)
config.stack_size = webSocketsStackSize;

// Start the httpd server
Expand Down
Loading