Skip to content

Commit 7524925

Browse files
committed
Save more strings in flash
Also just send the HTTP post request to the root
1 parent b0adbbc commit 7524925

File tree

2 files changed

+81
-77
lines changed

2 files changed

+81
-77
lines changed

PlantWateringESP8266.ino

Lines changed: 80 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -298,94 +298,98 @@ static void startAsyncHotspot(bool *p_run_hotspot, eeprom_config_t *eeprom_confi
298298
// Add routes for the different files and pages
299299
httpServer.on("/pure-min.css", HTTP_GET, [](AsyncWebServerRequest *request) {
300300
// Use Pure to style the from: https://purecss.io/forms/
301-
request->send(SPIFFS, "/pure-min.css", "text/css");
301+
request->send(SPIFFS, F("/pure-min.css"), F("text/css"));
302302
});
303303
httpServer.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
304304
// Som additional css to make it look a little nicer
305-
request->send(SPIFFS, "/style.css", "text/css");
305+
request->send(SPIFFS, F("/style.css"), F("text/css"));
306306
});
307307

308308
// This is the main page with the form for configuring the device
309309
httpServer.on("/", HTTP_GET, [&eeprom_config, &p_run_hotspot](AsyncWebServerRequest *request) {
310310
auto processor = [&eeprom_config](const String &var) {
311-
if (var == "WIFI_SSID")
311+
if (var == F("WIFI_SSID"))
312312
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->wifi_ssid : WIFI_SSID);
313-
else if (var == "WIFI_PASSWORD")
313+
else if (var == F("WIFI_PASSWORD"))
314314
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->wifi_password : WIFI_PASSWORD);
315-
else if (var == "THINGSPEAK_API_KEY")
315+
else if (var == F("THINGSPEAK_API_KEY"))
316316
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->thingspeak_api_key : THINGSPEAK_API_KEY);
317-
else if (var == "MQTT_HOST")
317+
else if (var == F("MQTT_HOST"))
318318
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->mqtt_host : MQTT_HOST);
319-
else if (var == "MQTT_PORT")
319+
else if (var == F("MQTT_PORT"))
320320
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->mqtt_port : MQTT_PORT);
321-
else if (var == "MQTT_USERNAME")
321+
else if (var == F("MQTT_USERNAME"))
322322
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->mqtt_username : MQTT_USERNAME);
323-
else if (var == "MQTT_PASSWORD")
323+
else if (var == F("MQTT_PASSWORD"))
324324
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->mqtt_password : MQTT_PASSWORD);
325-
else if (var == "MQTT_BASE_TOPIC")
325+
else if (var == F("MQTT_BASE_TOPIC"))
326326
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->mqtt_base_topic : MQTT_BASE_TOPIC);
327-
else if (var == "sleep_time")
327+
else if (var == F("sleep_time"))
328328
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->sleep_time : DEFAULT_SLEEP_TIME);
329-
else if (var == "watering_delay")
329+
else if (var == F("watering_delay"))
330330
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->watering_delay : DEFAULT_WATERING_DELAY);
331-
else if (var == "watering_threshold")
331+
else if (var == F("watering_threshold"))
332332
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->watering_threshold : DEFAULT_WATERING_THRESHOLD);
333-
else if (var == "watering_time")
333+
else if (var == F("watering_time"))
334334
return String(eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->watering_time : DEFAULT_WATERING_TIME);
335335

336336
return String();
337337
};
338338

339339
// Send the index as a template
340-
request->send(SPIFFS, "/index.html", String(), false, processor);
340+
request->send(SPIFFS, F("/index.html"), F("text/html"), false, processor);
341341
});
342342

343343
// Handle the post request
344-
httpServer.on("/config", HTTP_POST, [&eeprom_config, &p_run_hotspot](AsyncWebServerRequest *request) {
345-
if (!request->hasArg("wifi_ssid") || !request->hasArg("wifi_password")
346-
|| !request->hasArg("thingspeak_api_key")
347-
|| !request->hasArg("mqtt_host") || !request->hasArg("mqtt_port")
348-
|| !request->hasArg("mqtt_username") || !request->hasArg("mqtt_password") || !request->hasArg("mqtt_base_topic")
349-
|| !request->hasArg("sleep_time") || !request->hasArg("watering_delay")
350-
|| !request->hasArg("watering_threshold") || !request->hasArg("watering_time")) {
344+
httpServer.on("/", HTTP_POST, [&eeprom_config, &p_run_hotspot](AsyncWebServerRequest *request) {
345+
if (!request->hasArg(F("wifi_ssid")) || !request->hasArg(F("wifi_password"))
346+
|| !request->hasArg(F("thingspeak_api_key"))
347+
|| !request->hasArg(F("mqtt_host")) || !request->hasArg(F("mqtt_port"))
348+
|| !request->hasArg(F("mqtt_username")) || !request->hasArg(F("mqtt_password")) || !request->hasArg(F("mqtt_base_topic"))
349+
|| !request->hasArg(F("sleep_time")) || !request->hasArg(F("watering_delay"))
350+
|| !request->hasArg(F("watering_threshold")) || !request->hasArg(F("watering_time"))) {
351351
request->send(400, F("text/plain"), F("400: Invalid request"));
352352
return;
353353
}
354354

355-
strncpy(eeprom_config->wifi_ssid, request->arg("wifi_ssid").c_str(), sizeof(eeprom_config->wifi_ssid) - 1);
355+
strncpy(eeprom_config->wifi_ssid, request->arg(F("wifi_ssid")).c_str(), sizeof(eeprom_config->wifi_ssid) - 1);
356356
eeprom_config->wifi_ssid[sizeof(eeprom_config->wifi_ssid) - 1] = '\0'; // Make sure the buffer is null-terminated
357357

358-
strncpy(eeprom_config->wifi_password, request->arg("wifi_password").c_str(), sizeof(eeprom_config->wifi_password) - 1);
358+
strncpy(eeprom_config->wifi_password, request->arg(F("wifi_password")).c_str(), sizeof(eeprom_config->wifi_password) - 1);
359359
eeprom_config->wifi_password[sizeof(eeprom_config->wifi_password) - 1] = '\0'; // Make sure the buffer is null-terminated
360360

361-
strncpy(eeprom_config->thingspeak_api_key, request->arg("thingspeak_api_key").c_str(), sizeof(eeprom_config->thingspeak_api_key) - 1);
361+
strncpy(eeprom_config->thingspeak_api_key, request->arg(F("thingspeak_api_key")).c_str(), sizeof(eeprom_config->thingspeak_api_key) - 1);
362362
eeprom_config->thingspeak_api_key[sizeof(eeprom_config->thingspeak_api_key) - 1] = '\0'; // Make sure the buffer is null-terminated
363363

364-
strncpy(eeprom_config->mqtt_host, request->arg("mqtt_host").c_str(), sizeof(eeprom_config->mqtt_host) - 1);
364+
strncpy(eeprom_config->mqtt_host, request->arg(F("mqtt_host")).c_str(), sizeof(eeprom_config->mqtt_host) - 1);
365365
eeprom_config->mqtt_host[sizeof(eeprom_config->mqtt_host) - 1] = '\0'; // Make sure the buffer is null-terminated
366366

367-
eeprom_config->mqtt_port = request->arg("mqtt_port").toInt();
367+
eeprom_config->mqtt_port = request->arg(F("mqtt_port")).toInt();
368368

369-
strncpy(eeprom_config->mqtt_username, request->arg("mqtt_username").c_str(), sizeof(eeprom_config->mqtt_username) - 1);
369+
strncpy(eeprom_config->mqtt_username, request->arg(F("mqtt_username")).c_str(), sizeof(eeprom_config->mqtt_username) - 1);
370370
eeprom_config->mqtt_username[sizeof(eeprom_config->mqtt_username) - 1] = '\0'; // Make sure the buffer is null-terminated
371371

372-
strncpy(eeprom_config->mqtt_password, request->arg("mqtt_password").c_str(), sizeof(eeprom_config->mqtt_password) - 1);
372+
strncpy(eeprom_config->mqtt_password, request->arg(F("mqtt_password")).c_str(), sizeof(eeprom_config->mqtt_password) - 1);
373373
eeprom_config->mqtt_password[sizeof(eeprom_config->mqtt_password) - 1] = '\0'; // Make sure the buffer is null-terminated
374374

375-
strncpy(eeprom_config->mqtt_base_topic, request->arg("mqtt_base_topic").c_str(), sizeof(eeprom_config->mqtt_base_topic) - 1);
375+
strncpy(eeprom_config->mqtt_base_topic, request->arg(F("mqtt_base_topic")).c_str(), sizeof(eeprom_config->mqtt_base_topic) - 1);
376376
eeprom_config->mqtt_base_topic[sizeof(eeprom_config->mqtt_base_topic) - 1] = '\0'; // Make sure the buffer is null-terminated
377377

378-
eeprom_config->sleep_time = request->arg("sleep_time").toInt();
379-
eeprom_config->watering_delay = request->arg("watering_delay").toInt();
380-
eeprom_config->watering_threshold = request->arg("watering_threshold").toInt();
381-
eeprom_config->watering_time = request->arg("watering_time").toInt();
378+
eeprom_config->sleep_time = request->arg(F("sleep_time")).toInt();
379+
eeprom_config->watering_delay = request->arg(F("watering_delay")).toInt();
380+
eeprom_config->watering_threshold = request->arg(F("watering_threshold")).toInt();
381+
eeprom_config->watering_time = request->arg(F("watering_time")).toInt();
382382

383383
eeprom_config->override_retained_config_topic = true; // Make sure the config topic gets overriden on the next boot
384384

385385
// The values where succesfully configured
386386
eeprom_config->magic_number = MAGIC_NUMBER;
387387

388-
request->redirect(F("/")); // Redirect to the root
388+
// Close the connection
389+
AsyncWebServerResponse *response = request->beginResponse(302);
390+
response->addHeader(F("Connection"), F("close"));
391+
response->addHeader(F("Access-Control-Allow-Origin"), F("*"));
392+
request->send(response);
389393

390394
*p_run_hotspot = false; // Stop the hotspot when the user submits new values
391395
});
@@ -560,10 +564,10 @@ void setup() {
560564
return;
561565
}
562566

563-
JsonVariant sleep_time_variant = jsonDoc["sleep_time"];
564-
JsonVariant watering_delay_variant = jsonDoc["watering_delay"];
565-
JsonVariant watering_threshold_variant = jsonDoc["watering_threshold"];
566-
JsonVariant watering_time_variant = jsonDoc["watering_time"];
567+
JsonVariant sleep_time_variant = jsonDoc[F("sleep_time")];
568+
JsonVariant watering_delay_variant = jsonDoc[F("watering_delay")];
569+
JsonVariant watering_threshold_variant = jsonDoc[F("watering_threshold")];
570+
JsonVariant watering_time_variant = jsonDoc[F("watering_time")];
567571

568572
if (sleep_time_variant.isNull() || watering_delay_variant.isNull() ||
569573
watering_threshold_variant.isNull() || watering_time_variant.isNull()) {
@@ -637,10 +641,10 @@ void setup() {
637641
eeprom_config.override_retained_config_topic = false; // Will be writen to the EEPROM further down
638642

639643
jsonDoc.clear(); // Make sure we start with a blank document
640-
jsonDoc["sleep_time"] = eeprom_config.sleep_time;
641-
jsonDoc["watering_delay"] = eeprom_config.watering_delay;
642-
jsonDoc["watering_threshold"] = eeprom_config.watering_threshold;
643-
jsonDoc["watering_time"] = eeprom_config.watering_time;
644+
jsonDoc[F("sleep_time")] = eeprom_config.sleep_time;
645+
jsonDoc[F("watering_delay")] = eeprom_config.watering_delay;
646+
jsonDoc[F("watering_threshold")] = eeprom_config.watering_threshold;
647+
jsonDoc[F("watering_time")] = eeprom_config.watering_time;
644648

645649
size_t n = serializeJson(jsonDoc, jsonBuffer, sizeof(jsonBuffer));
646650
if (mqttPublishBlocking(config_topic, jsonBuffer, n, true, 5 * 10))
@@ -655,20 +659,20 @@ void setup() {
655659

656660
// Send messsages, so the sensor is auto discovered by Home Assistant - see: https://www.home-assistant.io/docs/mqtt/discovery/
657661
jsonDoc.clear(); // Make sure we start with a blank document
658-
jsonDoc["name"] = name + F(" Soil Moisture");
659-
jsonDoc["~"] = String(F("plant/")) + eeprom_config.mqtt_base_topic;
660-
jsonDoc["stat_t"] = F("~/state");
661-
jsonDoc["json_attr_t"] = F("~/state");
662-
jsonDoc["val_tpl"] = F("{{value_json.soil_moisture}}");
663-
jsonDoc["unit_of_meas"] = F("clk");
664-
jsonDoc["ic"] = F("mdi:sprout");
665-
jsonDoc["frc_upd"] = true; // Make sure that the sensor value is always stored and not just when it changes
666-
jsonDoc["uniq_id"] = String(chip_id) + F("_soil_moisture");
662+
jsonDoc[F("name")] = name + F(" Soil Moisture");
663+
jsonDoc[F("~")] = String(F("plant/")) + eeprom_config.mqtt_base_topic;
664+
jsonDoc[F("stat_t")] = F("~/state");
665+
jsonDoc[F("json_attr_t")] = F("~/state");
666+
jsonDoc[F("val_tpl")] = F("{{value_json.soil_moisture}}");
667+
jsonDoc[F("unit_of_meas")] = F("clk");
668+
jsonDoc[F("ic")] = F("mdi:sprout");
669+
jsonDoc[F("frc_upd")] = true; // Make sure that the sensor value is always stored and not just when it changes
670+
jsonDoc[F("uniq_id")] = String(chip_id) + F("_soil_moisture");
667671

668672
// Set device information used for the device registry
669-
jsonDoc["device"]["name"] = name + F(" Plant");
670-
jsonDoc["device"]["sw"] = SW_VERSION;
671-
jsonDoc["device"].createNestedArray("ids").add(String(chip_id));
673+
jsonDoc[F("device")][F("name")] = name + F(" Plant");
674+
jsonDoc[F("device")][F("sw")] = SW_VERSION;
675+
jsonDoc[F("device")].createNestedArray(F("ids")).add(String(chip_id));
672676

673677
size_t n = serializeJson(jsonDoc, jsonBuffer, sizeof(jsonBuffer));
674678
if (mqttPublishBlocking(String(F("homeassistant/sensor/")) + String(eeprom_config.mqtt_base_topic) + F("S/config"), jsonBuffer, n, true, 5 * 10))
@@ -678,20 +682,20 @@ void setup() {
678682

679683
// Send the voltage "sensor" as well
680684
jsonDoc.clear(); // Make sure we start with a blank document
681-
jsonDoc["name"] = name + F(" Voltage");
682-
jsonDoc["~"] = String(F("plant/")) + eeprom_config.mqtt_base_topic;
683-
jsonDoc["stat_t"] = F("~/state");
684-
jsonDoc["json_attr_t"] = F("~/state");
685-
jsonDoc["val_tpl"] = F("{{value_json.voltage}}");
686-
jsonDoc["unit_of_meas"] = F("V");
687-
jsonDoc["ic"] = F("mdi:solar-panel-large");
688-
jsonDoc["frc_upd"] = true; // Make sure that the sensor value is always stored and not just when it changes
689-
jsonDoc["uniq_id"] = String(chip_id) + F("_voltage");
685+
jsonDoc[F("name")] = name + F(" Voltage");
686+
jsonDoc[F("~")] = String(F("plant/")) + eeprom_config.mqtt_base_topic;
687+
jsonDoc[F("stat_t")] = F("~/state");
688+
jsonDoc[F("json_attr_t")] = F("~/state");
689+
jsonDoc[F("val_tpl")] = F("{{value_json.voltage}}");
690+
jsonDoc[F("unit_of_meas")] = F("V");
691+
jsonDoc[F("ic")] = F("mdi:solar-panel-large");
692+
jsonDoc[F("frc_upd")] = true; // Make sure that the sensor value is always stored and not just when it changes
693+
jsonDoc[F("uniq_id")] = String(chip_id) + F("_voltage");
690694

691695
// Set device information used for the device registry
692-
jsonDoc["device"]["name"] = name + F(" Plant");
693-
jsonDoc["device"]["sw"] = SW_VERSION;
694-
jsonDoc["device"].createNestedArray("ids").add(String(chip_id));
696+
jsonDoc[F("device")][F("name")] = name + F(" Plant");
697+
jsonDoc[F("device")][F("sw")] = SW_VERSION;
698+
jsonDoc[F("device")].createNestedArray(F("ids")).add(String(chip_id));
695699

696700
n = serializeJson(jsonDoc, jsonBuffer, sizeof(jsonBuffer));
697701
if (mqttPublishBlocking(String(F("homeassistant/sensor/")) + String(eeprom_config.mqtt_base_topic) + F("V/config"), jsonBuffer, n, true, 5 * 10))
@@ -700,15 +704,15 @@ void setup() {
700704
Serial.println(F("Failed to send voltage discovery message due to timeout"));
701705

702706
jsonDoc.clear(); // Make sure we start with a blank document
703-
jsonDoc["soil_moisture"] = soil_moisture;
704-
jsonDoc["voltage"] = voltage / 1000.0f;
705-
jsonDoc["sleep_time"] = eeprom_config.sleep_time;
706-
jsonDoc["watering_delay"] = eeprom_config.watering_delay;
707-
jsonDoc["watering_threshold"] = eeprom_config.watering_threshold;
708-
jsonDoc["watering_time"] = eeprom_config.watering_time;
709-
jsonDoc["sleep_num"] = sleep_data.sleep_num;
710-
jsonDoc["watering_delay_cycles"] = sleep_data.watering_delay_cycles;
711-
jsonDoc["version"] = SW_VERSION;
707+
jsonDoc[F("soil_moisture")] = soil_moisture;
708+
jsonDoc[F("voltage")] = voltage / 1000.0f;
709+
jsonDoc[F("sleep_time")] = eeprom_config.sleep_time;
710+
jsonDoc[F("watering_delay")] = eeprom_config.watering_delay;
711+
jsonDoc[F("watering_threshold")] = eeprom_config.watering_threshold;
712+
jsonDoc[F("watering_time")] = eeprom_config.watering_time;
713+
jsonDoc[F("sleep_num")] = sleep_data.sleep_num;
714+
jsonDoc[F("watering_delay_cycles")] = sleep_data.watering_delay_cycles;
715+
jsonDoc[F("version")] = SW_VERSION;
712716

713717
n = serializeJson(jsonDoc, jsonBuffer, sizeof(jsonBuffer));
714718
if (mqttPublishBlocking(String(F("plant/")) + String(eeprom_config.mqtt_base_topic) + F("/state"), jsonBuffer, n, false, 5 * 10))

data/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</head>
99

1010
<body>
11-
<form action="/config" method="POST" class="pure-form pure-form-aligned">
11+
<form action="/" method="POST" class="pure-form pure-form-aligned">
1212
<div class="pure-control-group">
1313
<label></label>
1414
<h2>Settings</h2>

0 commit comments

Comments
 (0)