Merge branch 'master' into new-parser

This commit is contained in:
Gunnar Skjold 2022-06-28 18:16:06 +02:00
commit caf00f99e3
9 changed files with 99 additions and 56 deletions

View File

@ -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.

View File

@ -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]

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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(),

View File

@ -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(),

View File

@ -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","/");

View File

@ -74,7 +74,7 @@
</div>
</div>
</div>
<div class="my-3 p-3 bg-white rounded shadow {sd}">
<div class="my-3 p-3 bg-white rounded shadow">
<div class="row">
<div class="col-md-2">
<label class="m-2"><input id="s" type="checkbox" name="s" value="true" {s}/> SSL</label>

View File

@ -8,7 +8,7 @@
<div class="input-group-prepend">
<span class="input-group-text">1</span>
</div>
<input class="form-control text-right" name="t0" type="number" min="5" max="255" step="1" value="{t0}"/>
<input class="form-control text-right" name="t0" type="number" min="1" max="255" step="1" value="{t0}"/>
<div class="input-group-append">
<span class="input-group-text">kWh</span>
</div>