diff --git a/ESPController/include/pylon_canbus.h b/ESPController/include/pylon_canbus.h index f7e6fd48..4b3a624c 100644 --- a/ESPController/include/pylon_canbus.h +++ b/ESPController/include/pylon_canbus.h @@ -17,12 +17,12 @@ extern uint8_t TotalNumberOfCells(); extern Rules rules; extern currentmonitoring_struct currentMonitor; extern diybms_eeprom_settings mysettings; -extern char hostname[16]; +extern std::string hostname; extern ControllerState _controller_state; extern uint32_t canbus_messages_failed_sent; extern uint32_t canbus_messages_sent; extern uint32_t canbus_messages_received; -extern void send_canbus_message(uint32_t identifier, uint8_t *buffer, uint8_t length); +extern void send_canbus_message(uint32_t identifier, const uint8_t *buffer,const uint8_t length); #endif \ No newline at end of file diff --git a/ESPController/include/tft.h b/ESPController/include/tft.h index a0ca1c09..23f67ad6 100644 --- a/ESPController/include/tft.h +++ b/ESPController/include/tft.h @@ -70,8 +70,8 @@ extern TaskHandle_t updatetftdisplay_task_handle; extern avrprogramsettings _avrsettings; extern currentmonitoring_struct currentMonitor; extern bool wifi_isconnected; -extern char hostname[16]; -extern char ip_string[16]; +extern std::string hostname; +extern std::string ip_string; extern PacketRequestGenerator prg; extern PacketReceiveProcessor receiveProc; extern uint32_t canbus_messages_received; diff --git a/ESPController/include/victron_canbus.h b/ESPController/include/victron_canbus.h index 571f2d38..a0422aaf 100644 --- a/ESPController/include/victron_canbus.h +++ b/ESPController/include/victron_canbus.h @@ -16,13 +16,13 @@ void victron_message_373(); void victron_message_35f(); void victron_message_374_375_376_377(); -extern void send_canbus_message(uint32_t identifier, uint8_t *buffer, uint8_t length); +extern void send_canbus_message(uint32_t identifier, const uint8_t *buffer,const uint8_t length); extern uint8_t TotalNumberOfCells(); extern Rules rules; extern currentmonitoring_struct currentMonitor; extern diybms_eeprom_settings mysettings; -extern char hostname[16]; +extern std::string hostname; extern ControllerState _controller_state; extern uint32_t canbus_messages_failed_sent; extern uint32_t canbus_messages_sent; diff --git a/ESPController/include/webserver_json_post.h b/ESPController/include/webserver_json_post.h index a6794b79..893ab672 100644 --- a/ESPController/include/webserver_json_post.h +++ b/ESPController/include/webserver_json_post.h @@ -24,6 +24,7 @@ extern Rules rules; extern CardAction card_action; extern avrprogramsettings _avrsettings; +extern wifi_eeprom_settings _wificonfig; extern void stopMqtt(); extern void ConfigureRS485(); @@ -34,9 +35,9 @@ extern void CurrentMonitorSetAdvancedSettings(currentmonitoring_struct newvalues extern void CurrentMonitorSetRelaySettingsInternal(currentmonitoring_struct newvalues); extern void CurrentMonitorSetRelaySettingsExternal(currentmonitoring_struct newvalues); extern void setCacheControl(httpd_req_t *req); -extern wifi_eeprom_settings _wificonfig; extern void configureSNTP(long gmtOffset_sec, int daylightOffset_sec, const char *server1); extern void DefaultConfiguration(diybms_eeprom_settings *_myset); +extern bool SaveWIFIJson(const wifi_eeprom_settings* setting); esp_err_t post_savebankconfig_json_handler(httpd_req_t *req, bool urlEncoded); esp_err_t post_saventp_json_handler(httpd_req_t *req, bool urlEncoded); @@ -67,6 +68,5 @@ esp_err_t post_savecurrentmon_json_handler(httpd_req_t *req, bool urlEncoded); esp_err_t post_saverules_json_handler(httpd_req_t *req, bool urlEncoded); esp_err_t post_restoreconfig_json_handler(httpd_req_t *req, bool urlEncoded); esp_err_t save_data_handler(httpd_req_t *req); -bool SaveWIFIJson(); #endif diff --git a/ESPController/include/webserver_json_requests.h b/ESPController/include/webserver_json_requests.h index c209d101..b6b79b8f 100644 --- a/ESPController/include/webserver_json_requests.h +++ b/ESPController/include/webserver_json_requests.h @@ -45,7 +45,7 @@ extern ControllerState _controller_state; extern void formatCurrentDateTime(char* buf, size_t buf_size); extern void setNoStoreCacheControl(httpd_req_t *req); extern char CookieValue[20 + 1]; -extern char hostname[16]; +extern std::string hostname; extern uint32_t time100; extern uint32_t time20; diff --git a/ESPController/src/main.cpp b/ESPController/src/main.cpp index cab2f00d..427a17c6 100644 --- a/ESPController/src/main.cpp +++ b/ESPController/src/main.cpp @@ -93,8 +93,8 @@ volatile bool emergencyStop = false; bool _sd_card_installed = false; // Used for WIFI hostname and also sent to Victron over CANBUS -char hostname[16]; -char ip_string[16]; // xxx.xxx.xxx.xxx +std::string hostname; +std::string ip_string; // xxx.xxx.xxx.xxx bool wifi_isconnected = false; @@ -1566,7 +1566,7 @@ static void startMDNS() } else { - mdns_hostname_set(hostname); + mdns_hostname_set(hostname.c_str()); mdns_instance_name_set("diybms"); mdns_service_add(nullptr, "_http", "_tcp", 80, nullptr, 0); } @@ -1648,11 +1648,14 @@ static void event_handler(void *, esp_event_base_t event_base, startMDNS(); - snprintf(ip_string, sizeof(ip_string), IPSTR, IP2STR(&event->ip_info.ip)); + char buffer[20]; + snprintf(buffer, sizeof(buffer), IPSTR, IP2STR(&event->ip_info.ip)); + ip_string.clear(); + ip_string.append(buffer); wake_up_tft(true); - ESP_LOGI(TAG, "You can access DIYBMS interface at http://%s.local or http://%s", hostname, ip_string); + ESP_LOGI(TAG, "You can access DIYBMS interface at http://%s.local or http://%s", hostname.c_str(), ip_string.c_str()); } } @@ -1666,6 +1669,57 @@ bool LoadWiFiConfig() return reply; } +bool SaveWIFIJson(const wifi_eeprom_settings* setting) +{ + if (!_sd_card_installed) + { + return false; + } + + if (_avrsettings.programmingModeEnabled) + { + return false; + } + + StaticJsonDocument<512> doc; + + JsonObject wifi = doc.createNestedObject("wifi"); + wifi["ssid"] = setting->wifi_ssid; + wifi["password"] = setting->wifi_passphrase; + + // Manual IP settings + wifi["ip"] = IPAddress(setting->wifi_ip).toString(); + wifi["gateway"] = IPAddress(setting->wifi_gateway).toString(); + wifi["netmask"] = IPAddress(setting->wifi_netmask).toString(); + wifi["dns1"] = IPAddress(setting->wifi_dns1).toString(); + wifi["dns2"] = IPAddress(setting->wifi_dns2).toString(); + wifi["manualconfig"] = setting->manualConfig; + + if (hal.GetVSPIMutex()) + { + ESP_LOGI(TAG, "Creating folder"); + SD.mkdir("/diybms"); + + // Get the file + ESP_LOGI(TAG, "Write SD file %s", wificonfigfilename); + + if (SD.exists(wificonfigfilename)) + { + ESP_LOGI(TAG, "Delete existing %s", wificonfigfilename); + SD.remove(wificonfigfilename); + } + + File file = SD.open(wificonfigfilename, "w"); + serializeJson(doc, file); + file.close(); + + hal.ReleaseVSPIMutex(); + return true; + } + + return false; +} + void BuildHostname() { uint32_t chipId = 0; @@ -1674,8 +1728,11 @@ void BuildHostname() chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; } // DIYBMS-00000000 - memset(&hostname, 0, sizeof(hostname)); - snprintf(hostname, sizeof(hostname), "DIYBMS-%08X", chipId); + char buffer[10]; + snprintf(buffer, sizeof(buffer), "%08X", chipId); + + hostname.clear(); + hostname.append("DIYBMS-").append(buffer); } void wifi_init_sta(void) @@ -1759,10 +1816,10 @@ void wifi_init_sta(void) ESP_ERROR_CHECK(esp_wifi_set_rssi_threshold(-80)); ESP_ERROR_CHECK(esp_wifi_start()); - ESP_ERROR_CHECK_WITHOUT_ABORT(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname)); - ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_set_hostname(netif, hostname)); + ESP_ERROR_CHECK_WITHOUT_ABORT(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname.c_str())); + ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_set_hostname(netif, hostname.c_str())); - ESP_LOGI(TAG, "Hostname: %s", hostname); + ESP_LOGI(TAG, "Hostname: %s", hostname.c_str()); ESP_LOGD(TAG, "wifi_init_sta finished"); } @@ -2570,7 +2627,7 @@ static const char *ESP32_TWAI_STATUS_STRINGS[] = { "RECOVERY UNDERWAY" // CAN_STATE_RECOVERING }; -void send_canbus_message(uint32_t identifier, uint8_t *buffer, uint8_t length) +void send_canbus_message(uint32_t identifier, const uint8_t *buffer, const uint8_t length) { twai_message_t message; message.identifier = identifier; @@ -3444,7 +3501,10 @@ bool AreWifiConfigurationsTheSame(const wifi_eeprom_settings *a, const wifi_eepr return true; } + /// @brief CHECK FOR THE PRESENCE OF A wifi.json CONFIG FILE ON THE SD CARD AND AUTOMATICALLY CONFIGURE WIFI +/// @param existingConfigValid true if the wifi settings in FLASH are valid/correctly stored +/// @return TRUE if the WIFI config on the card is different, false if identical bool LoadWiFiConfigFromSDCard(const bool existingConfigValid) { StaticJsonDocument<2048> json; @@ -3559,7 +3619,7 @@ const std::array log_levels = log_level_t{.tag = "*", .level = ESP_LOG_DEBUG}, {.tag = "wifi", .level = ESP_LOG_WARN}, {.tag = "dhcpc", .level = ESP_LOG_WARN}, - {.tag = "diybms", .level = ESP_LOG_VERBOSE}, + {.tag = "diybms", .level = ESP_LOG_DEBUG}, {.tag = "diybms-avrisp", .level = ESP_LOG_INFO}, {.tag = "diybms-hal", .level = ESP_LOG_INFO}, {.tag = "diybms-influxdb", .level = ESP_LOG_INFO}, diff --git a/ESPController/src/tft.cpp b/ESPController/src/tft.cpp index a81c52ce..62a58df7 100644 --- a/ESPController/src/tft.cpp +++ b/ESPController/src/tft.cpp @@ -106,9 +106,9 @@ void TFTDrawWifiDetails() if (wifi_isconnected) { - x += tft.drawString(hostname, x, y); + x += tft.drawString(hostname.c_str(), x, y); x += 10; - x += tft.drawString(ip_string, x, y); + x += tft.drawString(ip_string.c_str(), x, y); // Draw RSSI on bottom right corner // Received Signal Strength in dBm diff --git a/ESPController/src/victron_canbus.cpp b/ESPController/src/victron_canbus.cpp index c4e7c790..e5a5c5d1 100644 --- a/ESPController/src/victron_canbus.cpp +++ b/ESPController/src/victron_canbus.cpp @@ -19,13 +19,16 @@ static constexpr const char *const TAG = "diybms-victron"; // Transmit the DIYBMS hostname via two CAN Messages void victron_message_370_371() { - send_canbus_message(0x370, (uint8_t *)&hostname, 8); - send_canbus_message(0x371, (uint8_t *)&hostname[8], 8); + char buffer[16+1]; + strncpy(buffer,hostname.c_str(),sizeof(buffer)); + + send_canbus_message(0x370, (const uint8_t *)&buffer[0], 8); + send_canbus_message(0x371, (const uint8_t *)&buffer[8], 8); } void victron_message_35e() { - send_canbus_message(0x35e, (uint8_t *)&hostname, 6); + send_canbus_message(0x35e, (const uint8_t*)hostname.c_str(), 6); } void victron_message_35f() diff --git a/ESPController/src/webserver_json_post.cpp b/ESPController/src/webserver_json_post.cpp index ca56c705..063cae0f 100644 --- a/ESPController/src/webserver_json_post.cpp +++ b/ESPController/src/webserver_json_post.cpp @@ -217,62 +217,11 @@ esp_err_t post_saveconfigurationtoflash_json_handler(httpd_req_t *req, bool urlE return SendSuccess(req); } -bool SaveWIFIJson() -{ - const char *wificonfigfilename = "/diybms/wifi.json"; - - if (!_sd_card_installed) - { - return false; - } - - if (_avrsettings.programmingModeEnabled) - { - return false; - } - - StaticJsonDocument<512> doc; - - JsonObject wifi = doc.createNestedObject("wifi"); - wifi["ssid"] = _wificonfig.wifi_ssid; - wifi["password"] = _wificonfig.wifi_passphrase; - - // Manual IP settings - wifi["ip"] = IPAddress(_wificonfig.wifi_ip).toString(); - wifi["gateway"] = IPAddress(_wificonfig.wifi_gateway).toString(); - wifi["netmask"] = IPAddress(_wificonfig.wifi_netmask).toString(); - wifi["dns1"] = IPAddress(_wificonfig.wifi_dns1).toString(); - wifi["dns2"] = IPAddress(_wificonfig.wifi_dns2).toString(); - wifi["manualconfig"] = _wificonfig.manualConfig; - if (hal.GetVSPIMutex()) - { - ESP_LOGI(TAG, "Creating folder"); - SD.mkdir("/diybms"); - - // Get the file - ESP_LOGI(TAG, "Write SD file %s", wificonfigfilename); - - if (SD.exists(wificonfigfilename)) - { - ESP_LOGI(TAG, "Delete existing %s", wificonfigfilename); - SD.remove(wificonfigfilename); - } - - File file = SD.open(wificonfigfilename, "w"); - serializeJson(doc, file); - file.close(); - - hal.ReleaseVSPIMutex(); - return true; - } - - return false; -} esp_err_t post_savewificonfigtosdcard_json_handler(httpd_req_t *req, bool) { - if (SaveWIFIJson()) + if (SaveWIFIJson(&_wificonfig)) { return SendSuccess(req); } @@ -768,7 +717,7 @@ esp_err_t post_savenetconfig_json_handler(httpd_req_t *req, bool urlEncoded) SaveWIFI(&_wificonfig); // Attempt to save to SD card - but this may not be installed - SaveWIFIJson(); + SaveWIFIJson(&_wificonfig); return SendSuccess(req); } diff --git a/ESPController/src/webserver_json_requests.cpp b/ESPController/src/webserver_json_requests.cpp index 333c32cc..4b15a13e 100644 --- a/ESPController/src/webserver_json_requests.cpp +++ b/ESPController/src/webserver_json_requests.cpp @@ -733,7 +733,7 @@ esp_err_t content_handler_settings(httpd_req_t *req) settings["MinutesTimeZone"] = mysettings.minutesTimeZone; settings["DST"] = mysettings.daylight; - settings["HostName"] = hostname; + settings["HostName"] = hostname.c_str(); time_t now; if (time(&now))