Some changes during testing

This commit is contained in:
Gunnar Skjold 2020-08-06 13:11:31 +02:00
parent d730aac33b
commit e71f937856
6 changed files with 134 additions and 101 deletions

View File

@ -128,6 +128,7 @@ void setup() {
hw.setTempSensorPin(config.getTempSensorPin());
hw.setVccPin(config.getVccPin());
hw.setVccMultiplier(config.getVccMultiplier());
hw.setVccOffset(config.getVccOffset());
hw.ledBlink(LED_INTERNAL, 1);
hw.ledBlink(LED_RED, 1);
hw.ledBlink(LED_YELLOW, 1);
@ -276,7 +277,7 @@ bool longPressActive = false;
bool wifiConnected = false;
unsigned long lastTemperatureRead = -30000;
unsigned long lastTemperatureRead = 0;
float temperatures[32];
unsigned long lastRead = 0;
@ -327,39 +328,41 @@ void loop() {
}
}
if(now - lastTemperatureRead > 10000) {
if(now - lastTemperatureRead > 15000) {
hw.updateTemperatures();
lastTemperatureRead = now;
bool anyChanged = false;
uint8_t c = hw.getTempSensorCount();
for(int i = 0; i < c; i++) {
bool changed = false;
TempSensorData* data = hw.getTempSensorData(i);
if(data->lastValidRead > -85) {
changed = data->lastValidRead != temperatures[i];
temperatures[i] = data->lastValidRead;
}
if((changed && config.getMqttPayloadFormat() == 1) || config.getMqttPayloadFormat() == 2) {
mqtt.publish(String(config.getMqttPublishTopic()) + "/temperature/" + toHex(data->address), String(temperatures[i], 2));
}
anyChanged |= changed;
}
if(anyChanged && config.getMqttPayloadFormat() == 0) {
StaticJsonDocument<512> json;
JsonObject temps = json.createNestedObject("temperatures");
if(strlen(config.getMqttHost()) > 0) {
bool anyChanged = false;
uint8_t c = hw.getTempSensorCount();
for(int i = 0; i < c; i++) {
TempSensorData* data = hw.getTempSensorData(i);
JsonObject obj = temps.createNestedObject(toHex(data->address));
obj["name"] = data->name;
obj["value"] = serialized(String(temperatures[i], 2));
bool changed = false;
TempSensorData* data = hw.getTempSensorData(i);
if(data->lastValidRead > -85) {
changed = data->lastValidRead != temperatures[i];
temperatures[i] = data->lastValidRead;
}
if((changed && config.getMqttPayloadFormat() == 1) || config.getMqttPayloadFormat() == 2) {
mqtt.publish(String(config.getMqttPublishTopic()) + "/temperature/" + toHex(data->address), String(temperatures[i], 2));
}
anyChanged |= changed;
}
if(anyChanged && config.getMqttPayloadFormat() == 0) {
StaticJsonDocument<512> json;
JsonObject temps = json.createNestedObject("temperatures");
for(int i = 0; i < c; i++) {
TempSensorData* data = hw.getTempSensorData(i);
JsonObject obj = temps.createNestedObject(toHex(data->address));
obj["name"] = data->name;
obj["value"] = serialized(String(temperatures[i], 2));
}
String msg;
serializeJson(json, msg);
mqtt.publish(config.getMqttPublishTopic(), msg.c_str());
}
String msg;
serializeJson(json, msg);
mqtt.publish(config.getMqttPublishTopic(), msg.c_str());
}
}
@ -384,7 +387,7 @@ void loop() {
debugI("Successfully connected to WiFi!");
debugI("IP: %s", WiFi.localIP().toString().c_str());
}
if(strlen(config.getWifiHostname()) > 0) {
if(strlen(config.getWifiHostname()) > 0 && config.isMdnsEnable()) {
MDNS.begin(config.getWifiHostname());
MDNS.addService("http", "tcp", 80);
}

View File

@ -88,38 +88,53 @@ bool HwTools::updateTemperatures() {
sensorApi->begin();
delay(50);
tempSensorInit = true;
}
DeviceAddress addr;
sensorApi->requestTemperatures();
int c = sensorApi->getDeviceCount();
for(int i = 0; i < c; i++) {
bool found = false;
sensorApi->getAddress(addr, i);
float t = sensorApi->getTempC(addr);
for(int x = 0; x < sensorCount; x++) {
TempSensorData *data = tempSensors[x];
if(isSensorAddressEqual(data->address, addr)) {
found = true;
DeviceAddress addr;
sensorApi->requestTemperatures();
int c = sensorApi->getDeviceCount();
Serial.print("Sensors found: ");
Serial.println(c);
for(int i = 0; i < c; i++) {
bool found = false;
sensorApi->getAddress(addr, i);
float t = sensorApi->getTempC(addr);
for(int x = 0; x < sensorCount; x++) {
TempSensorData *data = tempSensors[x];
if(isSensorAddressEqual(data->address, addr)) {
found = true;
data->lastRead = t;
if(t > -85) {
data->lastValidRead = t;
}
}
}
if(!found) {
TempSensorData *data = new TempSensorData();
memcpy(data->address, addr, 8);
data->common = true;
data->lastRead = t;
if(t > -85) {
data->lastValidRead = t;
}
tempSensors[sensorCount] = data;
sensorCount++;
}
delay(10);
}
if(!found) {
TempSensorData *data = new TempSensorData();
memcpy(data->address, addr, 8);
data->common = true;
} else {
sensorApi->requestTemperatures();
for(int x = 0; x < sensorCount; x++) {
TempSensorData *data = tempSensors[x];
float t = sensorApi->getTempC(data->address);
data->lastRead = t;
if(t > -85) {
data->lastValidRead = t;
}
tempSensors[sensorCount] = data;
sensorCount++;
}
}
return true;
}
return false;

View File

@ -41,6 +41,7 @@ void AmsWebServer::setup(AmsConfiguration* config, MQTTClient* mqtt) {
server.on("/", HTTP_POST, std::bind(&AmsWebServer::handleSetup, this));
server.on("/application.js", HTTP_GET, std::bind(&AmsWebServer::applicationJs, this));
server.on("/temperature", HTTP_GET, std::bind(&AmsWebServer::temperature, this));
server.on("/temperature", HTTP_POST, std::bind(&AmsWebServer::temperaturePost, this));
server.on("/config-meter", HTTP_GET, std::bind(&AmsWebServer::configMeterHtml, this));
server.on("/config-wifi", HTTP_GET, std::bind(&AmsWebServer::configWifiHtml, this));
server.on("/config-mqtt", HTTP_GET, std::bind(&AmsWebServer::configMqttHtml, this));
@ -132,20 +133,40 @@ void AmsWebServer::temperature() {
server.sendHeader("Expires", "-1");
String html;
uint8_t c = hw->getTempSensorCount();
for(int i = 0; i < c; i++) {
int c = hw->getTempSensorCount();
int start = server.hasArg("start") && !server.arg("start").isEmpty() ? server.arg("start").toInt() : 0;
int end = min(start + 4, c);
for(int i = start; i < end; i++) {
TempSensorData* data = hw->getTempSensorData(i);
String row = String((const __FlashStringHelper*) TEMPERATURE_ROW_HTML);
row.replace("${index}", String(i, DEC));
row.replace("${address}", toHex(data->address));
row.replace("${index}", String(i-start, DEC));
row.replace("${address}", toHex(data->address, 8));
row.replace("${name}", data->name);
row.replace("${common}", data->common ? "checked" : "");
row.replace("${value}", String(data->lastRead, 1));
row.replace("${value}", data->lastRead > -85 ? String(data->lastRead, 1) : "N/A");
html += row;
}
if(start > 0 || end < c) {
html += "<div class=\"row\"><div class=\"col-6\">";
if(start > 0) {
html += "<a href=\"?start=";
html += String(start-4, DEC);
html += "\" class=\"btn btn-sm btn-outline-secondary\">previous</a>";
}
html += "</div><div class=\"col-6 text-right\">";
if(end < c) {
html += "<a href=\"?start=";
html += String(end, DEC);
html += "\" class=\"btn btn-sm btn-outline-secondary\">next</a>";
}
html += "</div></div>";
}
server.setContentLength(html.length() + HEAD_HTML_LEN + TEMPERATURE_HEAD_HTML_LEN + FOOT_HTML_LEN + TEMPERATURE_FOOT_HTML_LEN);
server.send_P(200, "text/html", HEAD_HTML);
server.sendContent_P(TEMPERATURE_HEAD_HTML);
@ -154,6 +175,28 @@ void AmsWebServer::temperature() {
server.sendContent_P(FOOT_HTML);
}
void AmsWebServer::temperaturePost() {
int start = server.hasArg("start") && !server.arg("start").isEmpty() ? server.arg("start").toInt() : 0;
for(int i = 0; i < 4; i++) {
if(!server.hasArg("sensor" + String(i, DEC))) break;
String address = server.arg("sensor" + String(i, DEC));
String name = server.arg("sensor" + String(i, DEC) + "name");
bool common = server.hasArg("sensor" + String(i, DEC) + "common") && server.arg("sensor" + String(i, DEC) + "common") == "true";
config->updateTempSensorConfig(fromHex(address, 8), name.c_str(), common);
}
if(config->save()) {
server.sendHeader("Location", String("/temperature?start=") + String(start, DEC), true);
server.send (302, "text/plain", "");
uint8_t c = config->getTempSensorCount();
for(int i = 0; i < c; i++) {
TempSensorConfig* tsc = config->getTempSensorConfig(i);
hw->confTempSensor(tsc->address, tsc->name, tsc->common);
}
}
}
void AmsWebServer::indexHtml() {
printD("Serving /index.html over http...");
@ -270,28 +313,12 @@ void AmsWebServer::configMeterHtml() {
}
html.replace("${config.productionCapacity}", String(config->getProductionCapacity()));
byte encryptionKey[16];
memcpy(encryptionKey, config->getMeterEncryptionKey(), 16);
String encryptionKeyHex = "0x";
for(int i = 0; i < sizeof(encryptionKey); i++) {
if(encryptionKey[i] < 0x10) {
encryptionKeyHex += '0';
}
encryptionKeyHex += String(encryptionKey[i], HEX);
}
encryptionKeyHex.toUpperCase();
encryptionKeyHex += toHex(config->getMeterEncryptionKey(), 16);
html.replace("${config.meterEncryptionKey}", encryptionKeyHex);
byte authenticationKey[16];
memcpy(authenticationKey, config->getMeterAuthenticationKey(), 16);
String authenticationKeyHex = "0x";
for(int i = 0; i < sizeof(authenticationKey); i++) {
if(authenticationKey[i] < 0x10) {
authenticationKeyHex += '0';
}
authenticationKeyHex += String(authenticationKey[i], HEX);
}
authenticationKeyHex.toUpperCase();
authenticationKeyHex += toHex(config->getMeterAuthenticationKey(), 16);
html.replace("${config.meterAuthenticationKey}", authenticationKeyHex);
html.replace("${config.substituteMissing}", config->isSubstituteMissing() ? "checked" : "");
@ -303,9 +330,9 @@ void AmsWebServer::configMeterHtml() {
server.sendContent_P(FOOT_HTML);
}
String AmsWebServer::toHex(uint8_t* in) {
String AmsWebServer::toHex(uint8_t* in, uint8_t size) {
String hex;
for(int i = 0; i < sizeof(in)*2; i++) {
for(int i = 0; i < size; i++) {
if(in[i] < 0x10) {
hex += '0';
}
@ -343,6 +370,7 @@ void AmsWebServer::configWifiHtml() {
html.replace("${config.wifiDns1}", config->getWifiDns1());
html.replace("${config.wifiDns2}", config->getWifiDns2());
html.replace("${config.wifiHostname}", config->getWifiHostname());
html.replace("${config.mDnsEnable}", config->isMdnsEnable() ? "checked" : "");
server.setContentLength(html.length() + HEAD_HTML_LEN + FOOT_HTML_LEN);
server.send_P(200, "text/html", HEAD_HTML);
@ -727,6 +755,9 @@ void AmsWebServer::handleSetup() {
}
if(server.hasArg("wifiHostname") && !server.arg("wifiHostname").isEmpty()) {
config->setWifiHostname(server.arg("wifiHostname").c_str());
config->setMdnsEnable(true);
} else {
config->setMdnsEnable(false);
}
if(config->save()) {
performRestart = true;
@ -754,21 +785,13 @@ void AmsWebServer::handleSave() {
String encryptionKeyHex = server.arg("meterEncryptionKey");
if(!encryptionKeyHex.isEmpty()) {
encryptionKeyHex.replace("0x", "");
uint8_t encryptionKey[16];
for(int i = 0; i < 32; i += 2) {
encryptionKey[i/2] = strtol(encryptionKeyHex.substring(i, i+2).c_str(), 0, 16);
}
config->setMeterEncryptionKey(encryptionKey);
config->setMeterEncryptionKey(fromHex(encryptionKeyHex, 16));
}
String authenticationKeyHex = server.arg("meterAuthenticationKey");
if(!authenticationKeyHex.isEmpty()) {
authenticationKeyHex.replace("0x", "");
uint8_t authenticationKey[16];
for(int i = 0; i < 32; i += 2) {
authenticationKey[i/2] = strtol(authenticationKeyHex.substring(i, i+2).c_str(), 0, 16);
}
config->setMeterAuthenticationKey(authenticationKey);
config->setMeterAuthenticationKey(fromHex(authenticationKeyHex, 16));
}
}
@ -785,6 +808,7 @@ void AmsWebServer::handleSave() {
config->clearWifiIp();
}
config->setWifiHostname(server.arg("wifiHostname").c_str());
config->setMdnsEnable(server.hasArg("mDnsEnable") && server.arg("mDnsEnable") == "true");
}
if(server.hasArg("mqttConfig") && server.arg("mqttConfig") == "true") {
@ -889,16 +913,6 @@ void AmsWebServer::handleSave() {
config->setNtpServer(server.arg("ntpServer").c_str());
}
if(server.hasArg("tempConfig") && server.arg("tempConfig") == "true") {
for(int i = 0; i < 32; i++) {
if(!server.hasArg("sensor" + String(i, DEC))) break;
String address = server.arg("sensor" + String(i, DEC));
String name = server.arg("sensor" + String(i, DEC) + "name");
bool common = server.hasArg("sensor" + String(i, DEC) + "common") && server.arg("sensor" + String(i, DEC) + "common") == "true";
config->updateTempSensorConfig(fromHex(address, 8), name.c_str(), common);
}
}
printI("Saving configuration now...");
if (debugger->isActive(RemoteDebug::DEBUG)) config->print(debugger);
@ -918,11 +932,6 @@ void AmsWebServer::handleSave() {
hw->setVccPin(config->getVccPin());
hw->setVccOffset(config->getVccOffset());
hw->setVccMultiplier(config->getVccMultiplier());
uint8_t c = config->getTempSensorCount();
for(int i = 0; i < c; i++) {
TempSensorConfig* tsc = config->getTempSensorConfig(i);
hw->confTempSensor(tsc->address, tsc->name, tsc->common);
}
}
} else {
printE("Error saving configuration");

View File

@ -61,6 +61,7 @@ private:
void indexHtml();
void applicationJs();
void temperature();
void temperaturePost();
void configMeterHtml();
void configWifiHtml();
void configMqttHtml();
@ -104,7 +105,7 @@ private:
void notFound();
String toHex(uint8_t* in);
String toHex(uint8_t* in, uint8_t size);
uint8_t* fromHex(String in, uint8_t size);
void printD(String fmt, ...);

View File

@ -24,6 +24,11 @@
<span class="input-group-text">Hostname</span>
</div>
<input type="text" name="wifiHostname" class="form-control" maxlength="32" pattern="[a-z0-9_-]+" placeholder="Optional, ex.: ams-reader" value="${config.wifiHostname}"/>
<div class="input-group-append" title="Enable mDNS">
<label class="input-group-text">
<input type="checkbox" name="mDnsEnable" value="true" ${config.mDnsEnable}/> mDNS
</label>
</div>
</div>
</div>
<div class="col-xl-2 col-md-6 form-group">

View File

@ -1,3 +1,3 @@
<form method="post" action="/save">
<form method="post">
<input type="hidden" name="tempConfig" value="true"/>
<div class="my-3 p-3 bg-white rounded shadow">