diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 6d75b183..2a3a752f 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -24,16 +24,19 @@ A clear description of what you expected to happen. If applicable, add screenshots to help explain your problem. **Hardware information:** + - Country: [e.g. Norway] - Meter: [e.g. Aidon] + - Encryption enabled [yes/no] - AMS reader: [e.g. Pow-U, ESP32 etc] - M-bus adapter (if applicable): **Relevant firmware information:** - - Version: [e.g. 1.5.0] + - Version: [e.g. 2.1.0] - MQTT: [yes/no] - HAN GPIO: [e.g. GPIO5] - HAN baud and parity: [e.g. 2400 8E1] - Temperature sensors [e.g. 3xDS18B20] + - ENTSO-E API enabled: [yes/no] **Additional context** Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/support.md b/.github/ISSUE_TEMPLATE/support.md index 981cfa37..13121413 100644 --- a/.github/ISSUE_TEMPLATE/support.md +++ b/.github/ISSUE_TEMPLATE/support.md @@ -11,12 +11,16 @@ assignees: '' A clear and concise description of what the problem is. **Hardware information:** + - Country: [e.g. Norway] - Meter: [e.g. Aidon] + - Encryption enabled [yes/no] - AMS reader: [e.g. Pow-U, ESP32 etc] - M-bus adapter (if applicable): **Relevant firmware information:** - - Version: [e.g. 1.5.0] + - Version: [e.g. 2.1.0] - MQTT: [yes/no] - HAN GPIO: [e.g. GPIO5] + - HAN baud and parity: [e.g. 2400 8E1] - Temperature sensors [e.g. 3xDS18B20] + - ENTSO-E API enabled: [yes/no] diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 8cfc0242..12f53adf 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -86,7 +86,7 @@ Timezone* tz; AmsWebServer ws(commonBuffer, &Debug, &hw); MQTTClient *mqtt = NULL; -WiFiClient *mqttClient = new WiFiClient(); +WiFiClient *mqttClient = NULL; WiFiClientSecure *mqttSecureClient = NULL; AmsMqttHandler* mqttHandler = NULL; @@ -1284,24 +1284,19 @@ void MQTT_connect() { } if(mqttConfig.ssl) { - debugI("MQTT SSL is configured"); + debugI("MQTT SSL is configured (%dkb free heap)", ESP.getFreeHeap()); if(mqttSecureClient == NULL) { mqttSecureClient = new WiFiClientSecure(); } #if defined(ESP8266) mqttSecureClient->setBufferSizes(512, 512); - debugE("ESP8266 does not have enough RAM to SSL"); - return; #endif - + if(LittleFS.begin()) { - char *ca = NULL; - char *cert = NULL; - char *key = NULL; File file; if(LittleFS.exists(FILE_MQTT_CA)) { - debugI("Found MQTT CA file"); + debugI("Found MQTT CA file (%dkb free heap)", ESP.getFreeHeap()); file = LittleFS.open(FILE_MQTT_CA, "r"); #if defined(ESP8266) BearSSL::X509List *serverTrustedCA = new BearSSL::X509List(file); @@ -1309,32 +1304,37 @@ void MQTT_connect() { #elif defined(ESP32) mqttSecureClient->loadCACert(file, file.size()); #endif + file.close(); } if(LittleFS.exists(FILE_MQTT_CERT) && LittleFS.exists(FILE_MQTT_KEY)) { #if defined(ESP8266) - debugI("Found MQTT certificate file"); + debugI("Found MQTT certificate file (%dkb free heap)", ESP.getFreeHeap()); file = LittleFS.open(FILE_MQTT_CERT, "r"); BearSSL::X509List *serverCertList = new BearSSL::X509List(file); + file.close(); - debugI("Found MQTT key file"); + debugI("Found MQTT key file (%dkb free heap)", ESP.getFreeHeap()); file = LittleFS.open(FILE_MQTT_KEY, "r"); BearSSL::PrivateKey *serverPrivKey = new BearSSL::PrivateKey(file); + file.close(); - debugD("Setting client certificates"); + debugD("Setting client certificates (%dkb free heap)", ESP.getFreeHeap()); mqttSecureClient->setClientRSACert(serverCertList, serverPrivKey); #elif defined(ESP32) - debugI("Found MQTT certificate file"); + debugI("Found MQTT certificate file (%dkb free heap)", ESP.getFreeHeap()); file = LittleFS.open(FILE_MQTT_CERT, "r"); mqttSecureClient->loadCertificate(file, file.size()); + file.close(); - debugI("Found MQTT key file"); + debugI("Found MQTT key file (%dkb free heap)", ESP.getFreeHeap()); file = LittleFS.open(FILE_MQTT_KEY, "r"); mqttSecureClient->loadPrivateKey(file, file.size()); + file.close(); #endif } LittleFS.end(); - debugD("MQTT SSL setup complete"); + debugD("MQTT SSL setup complete (%dkb free heap)", ESP.getFreeHeap()); } mqttClient = mqttSecureClient; } else if(mqttClient == NULL) { diff --git a/src/HwTools.cpp b/src/HwTools.cpp index 220eb476..007c06a4 100644 --- a/src/HwTools.cpp +++ b/src/HwTools.cpp @@ -14,12 +14,30 @@ void HwTools::setup(GpioConfig* config, AmsConfiguration* amsConf) { config->tempSensorPin = 0xFF; } - #if defined(ESP32) + #if defined(CONFIG_IDF_TARGET_ESP32S2) + analogReadResolution(13); + analogRange = 8192; + analogSetAttenuation(ADC_11db); + #elif defined(ESP32) analogReadResolution(12); analogRange = 4096; + analogSetAttenuation(ADC_6db); #endif if(config->vccPin > 0 && config->vccPin < 40) { - #if defined(CONFIG_IDF_TARGET_ESP32) + #if defined(CONFIG_IDF_TARGET_ESP32S2) + getAdcChannel(config->vccPin, voltAdc); + if(voltAdc.unit != 0xFF) { + if(voltAdc.unit == ADC_UNIT_1) { + voltAdcChar = (esp_adc_cal_characteristics_t*) calloc(1, sizeof(esp_adc_cal_characteristics_t)); + esp_adc_cal_value_t adcVal = esp_adc_cal_characterize((adc_unit_t) voltAdc.unit, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_13, 1100, voltAdcChar); + adc1_config_channel_atten((adc1_channel_t) voltAdc.channel, ADC_ATTEN_DB_11); + } else if(voltAdc.unit == ADC_UNIT_2) { + voltAdcChar = (esp_adc_cal_characteristics_t*) calloc(1, sizeof(esp_adc_cal_characteristics_t)); + esp_adc_cal_value_t adcVal = esp_adc_cal_characterize((adc_unit_t) voltAdc.unit, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_13, 1100, voltAdcChar); + adc2_config_channel_atten((adc2_channel_t) voltAdc.channel, ADC_ATTEN_DB_11); + } + } + #elif defined(ESP32) getAdcChannel(config->vccPin, voltAdc); if(voltAdc.unit != 0xFF) { if(voltAdc.unit == ADC_UNIT_1) { @@ -113,6 +131,16 @@ void HwTools::getAdcChannel(uint8_t pin, AdcConfig& config) { config.unit = ADC_UNIT_1; config.channel = ADC1_CHANNEL_7; break; + #if defined(CONFIG_IDF_TARGET_ESP32S2) + case ADC1_CHANNEL_8_GPIO_NUM: + config.unit = ADC_UNIT_1; + config.channel = ADC1_CHANNEL_8; + break; + case ADC1_CHANNEL_9_GPIO_NUM: + config.unit = ADC_UNIT_1; + config.channel = ADC1_CHANNEL_9; + break; + #endif case ADC2_CHANNEL_0_GPIO_NUM: config.unit = ADC_UNIT_2; config.channel = ADC2_CHANNEL_0; @@ -160,7 +188,7 @@ void HwTools::getAdcChannel(uint8_t pin, AdcConfig& config) { double HwTools::getVcc() { double volts = 0.0; if(config->vccPin != 0xFF) { - #if defined(CONFIG_IDF_TARGET_ESP32) + #if defined(ESP32) if(voltAdc.unit != 0xFF) { uint32_t x = 0; for (int i = 0; i < 10; i++) { @@ -168,7 +196,11 @@ double HwTools::getVcc() { x += adc1_get_raw((adc1_channel_t) voltAdc.channel); } else if(voltAdc.unit == ADC_UNIT_2) { int v = 0; + #if defined(CONFIG_IDF_TARGET_ESP32S2) + adc2_get_raw((adc2_channel_t) voltAdc.channel, ADC_WIDTH_BIT_13, &v); + #elif defined(CONFIG_IDF_TARGET_ESP32) adc2_get_raw((adc2_channel_t) voltAdc.channel, ADC_WIDTH_BIT_12, &v); + #endif x += v; } } diff --git a/src/mqtt/HomeAssistantMqttHandler.cpp b/src/mqtt/HomeAssistantMqttHandler.cpp index 485e0905..798dc8f9 100644 --- a/src/mqtt/HomeAssistantMqttHandler.cpp +++ b/src/mqtt/HomeAssistantMqttHandler.cpp @@ -25,6 +25,8 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En ); mqtt->publish(topic + "/energy", json); } + String meterModel = data->getMeterModel(); + meterModel.replace("\\", "\\\\"); if(data->getListType() == 1) { // publish power counts snprintf_P(json, BufferSize, HA1_JSON, data->getActiveImportPower() @@ -34,7 +36,7 @@ bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, En snprintf_P(json, BufferSize, HA3_JSON, data->getListId().c_str(), data->getMeterId().c_str(), - data->getMeterModel().c_str(), + meterModel.c_str(), data->getActiveImportPower(), data->getReactiveImportPower(), data->getActiveExportPower(), diff --git a/src/mqtt/JsonMqttHandler.cpp b/src/mqtt/JsonMqttHandler.cpp index aa7c5dd9..b5d6103a 100644 --- a/src/mqtt/JsonMqttHandler.cpp +++ b/src/mqtt/JsonMqttHandler.cpp @@ -12,6 +12,8 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou if(topic.isEmpty() || !mqtt->connected()) return false; + String meterModel = data->getMeterModel(); + meterModel.replace("\\", "\\\\"); if(data->getListType() == 1) { snprintf_P(json, BufferSize, JSON1_JSON, WiFi.macAddress().c_str(), @@ -39,7 +41,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou hw->getTemperature(), data->getListId().c_str(), data->getMeterId().c_str(), - data->getMeterModel().c_str(), + meterModel.c_str(), data->getActiveImportPower(), data->getReactiveImportPower(), data->getActiveExportPower(), @@ -67,7 +69,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou hw->getTemperature(), data->getListId().c_str(), data->getMeterId().c_str(), - data->getMeterModel().c_str(), + meterModel.c_str(), data->getActiveImportPower(), data->getReactiveImportPower(), data->getActiveExportPower(), @@ -100,7 +102,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou hw->getTemperature(), data->getListId().c_str(), data->getMeterId().c_str(), - data->getMeterModel().c_str(), + meterModel.c_str(), data->getActiveImportPower(), data->getReactiveImportPower(), data->getActiveExportPower(), diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 397ab63c..b1afdaa1 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -497,29 +497,24 @@ void AmsWebServer::configMqttHtml() { } html.replace("{f255}", mqtt.payloadFormat == 255 ? "selected" : ""); - #if defined(ESP8266) - html.replace("{sd}", "d-none"); - #else - html.replace("{sd}", ""); - html.replace("{s}", mqtt.ssl ? "checked" : ""); + html.replace("{s}", mqtt.ssl ? "checked" : ""); - if(LittleFS.begin()) { - html.replace("{dcu}", LittleFS.exists(FILE_MQTT_CA) ? "none" : ""); - html.replace("{dcf}", LittleFS.exists(FILE_MQTT_CA) ? "" : "none"); - html.replace("{deu}", LittleFS.exists(FILE_MQTT_CERT) ? "none" : ""); - html.replace("{def}", LittleFS.exists(FILE_MQTT_CERT) ? "" : "none"); - html.replace("{dku}", LittleFS.exists(FILE_MQTT_KEY) ? "none" : ""); - html.replace("{dkf}", LittleFS.exists(FILE_MQTT_KEY) ? "" : "none"); - LittleFS.end(); - } else { - html.replace("{dcu}", ""); - html.replace("{dcf}", "none"); - html.replace("{deu}", ""); - html.replace("{def}", "none"); - html.replace("{dku}", ""); - html.replace("{dkf}", "none"); - } - #endif + if(LittleFS.begin()) { + html.replace("{dcu}", LittleFS.exists(FILE_MQTT_CA) ? "none" : ""); + html.replace("{dcf}", LittleFS.exists(FILE_MQTT_CA) ? "" : "none"); + html.replace("{deu}", LittleFS.exists(FILE_MQTT_CERT) ? "none" : ""); + html.replace("{def}", LittleFS.exists(FILE_MQTT_CERT) ? "" : "none"); + html.replace("{dku}", LittleFS.exists(FILE_MQTT_KEY) ? "none" : ""); + html.replace("{dkf}", LittleFS.exists(FILE_MQTT_KEY) ? "" : "none"); + LittleFS.end(); + } else { + html.replace("{dcu}", ""); + html.replace("{dcf}", "none"); + html.replace("{deu}", ""); + html.replace("{def}", "none"); + html.replace("{dku}", ""); + html.replace("{dkf}", "none"); + } server.setContentLength(html.length() + HEAD_HTML_LEN + FOOT_HTML_LEN); server.send_P(200, MIME_HTML, HEAD_HTML); @@ -1675,8 +1670,9 @@ void AmsWebServer::firmwareDownload() { printI("Downloading firmware..."); HTTPClient httpClient; httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); - httpClient.setTimeout(20000); - httpClient.addHeader("User-Agent", "ams2mqtt/" + String(VERSION)); + httpClient.setReuse(false); + httpClient.setTimeout(60000); + httpClient.setUserAgent("ams2mqtt/" + String(VERSION)); #if defined(ESP8266) WiFiClient client; @@ -1705,13 +1701,11 @@ void AmsWebServer::firmwareDownload() { return; */ #elif defined(CONFIG_IDF_TARGET_ESP32S2) - WiFiClientSecure client; - client.setInsecure(); + httpClient.setConnectTimeout(60000); String url = "https://github.com/gskjold/AmsToMqttBridge/releases/download/" + version + "/ams2mqtt-esp32s2-" + versionStripped + ".bin"; httpClient.addHeader("Referer", "https://github.com/gskjold/AmsToMqttBridge/releases"); #elif defined(ESP32) - WiFiClientSecure client; - client.setInsecure(); + httpClient.setConnectTimeout(60000); #if defined(CONFIG_FREERTOS_UNICORE) String url = "https://github.com/gskjold/AmsToMqttBridge/releases/download/" + version + "/ams2mqtt-esp32solo-" + versionStripped + ".bin"; #else @@ -1721,7 +1715,14 @@ void AmsWebServer::firmwareDownload() { httpClient.addHeader("Referer", "https://github.com/gskjold/AmsToMqttBridge/releases"); #endif + printD("Downloading from URL:"); + printD(url); + + #if defined(ESP8266) if(httpClient.begin(client, url)) { + #elif defined(ESP32) + if(httpClient.begin(url)) { + #endif printD("HTTP client setup successful"); int status = httpClient.GET(); if(status == HTTP_CODE_OK) { @@ -1749,8 +1750,8 @@ void AmsWebServer::firmwareDownload() { } } else { printE("Communication error: "); + debugger->printf("%d\n", status); printE(httpClient.errorToString(status)); - printI(url); printD(httpClient.getString()); server.sendHeader("Location","/"); server.send(303); @@ -1761,7 +1762,6 @@ void AmsWebServer::firmwareDownload() { server.send(303); } httpClient.end(); - client.stop(); } else { printI("No firmware version specified..."); server.sendHeader("Location","/"); diff --git a/web/mqtt.html b/web/mqtt.html index ba4c73f3..8871b3d9 100644 --- a/web/mqtt.html +++ b/web/mqtt.html @@ -74,7 +74,7 @@ -
+
diff --git a/web/thresholds.html b/web/thresholds.html index 3c0a2321..8c66b13c 100644 --- a/web/thresholds.html +++ b/web/thresholds.html @@ -8,7 +8,7 @@
1
- +
kWh