@@ -298,94 +298,98 @@ static void startAsyncHotspot(bool *p_run_hotspot, eeprom_config_t *eeprom_confi
298
298
// Add routes for the different files and pages
299
299
httpServer.on (" /pure-min.css" , HTTP_GET, [](AsyncWebServerRequest *request) {
300
300
// 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" ) );
302
302
});
303
303
httpServer.on (" /style.css" , HTTP_GET, [](AsyncWebServerRequest *request) {
304
304
// 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" ) );
306
306
});
307
307
308
308
// This is the main page with the form for configuring the device
309
309
httpServer.on (" /" , HTTP_GET, [&eeprom_config, &p_run_hotspot](AsyncWebServerRequest *request) {
310
310
auto processor = [&eeprom_config](const String &var) {
311
- if (var == " WIFI_SSID" )
311
+ if (var == F ( " WIFI_SSID" ) )
312
312
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" ) )
314
314
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" ) )
316
316
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" ) )
318
318
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" ) )
320
320
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" ) )
322
322
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" ) )
324
324
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" ) )
326
326
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" ) )
328
328
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" ) )
330
330
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" ) )
332
332
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" ) )
334
334
return String (eeprom_config->magic_number == MAGIC_NUMBER ? eeprom_config->watering_time : DEFAULT_WATERING_TIME);
335
335
336
336
return String ();
337
337
};
338
338
339
339
// 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);
341
341
});
342
342
343
343
// 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" ) )) {
351
351
request->send (400 , F (" text/plain" ), F (" 400: Invalid request" ));
352
352
return ;
353
353
}
354
354
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 );
356
356
eeprom_config->wifi_ssid [sizeof (eeprom_config->wifi_ssid ) - 1 ] = ' \0 ' ; // Make sure the buffer is null-terminated
357
357
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 );
359
359
eeprom_config->wifi_password [sizeof (eeprom_config->wifi_password ) - 1 ] = ' \0 ' ; // Make sure the buffer is null-terminated
360
360
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 );
362
362
eeprom_config->thingspeak_api_key [sizeof (eeprom_config->thingspeak_api_key ) - 1 ] = ' \0 ' ; // Make sure the buffer is null-terminated
363
363
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 );
365
365
eeprom_config->mqtt_host [sizeof (eeprom_config->mqtt_host ) - 1 ] = ' \0 ' ; // Make sure the buffer is null-terminated
366
366
367
- eeprom_config->mqtt_port = request->arg (" mqtt_port" ).toInt ();
367
+ eeprom_config->mqtt_port = request->arg (F ( " mqtt_port" ) ).toInt ();
368
368
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 );
370
370
eeprom_config->mqtt_username [sizeof (eeprom_config->mqtt_username ) - 1 ] = ' \0 ' ; // Make sure the buffer is null-terminated
371
371
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 );
373
373
eeprom_config->mqtt_password [sizeof (eeprom_config->mqtt_password ) - 1 ] = ' \0 ' ; // Make sure the buffer is null-terminated
374
374
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 );
376
376
eeprom_config->mqtt_base_topic [sizeof (eeprom_config->mqtt_base_topic ) - 1 ] = ' \0 ' ; // Make sure the buffer is null-terminated
377
377
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 ();
382
382
383
383
eeprom_config->override_retained_config_topic = true ; // Make sure the config topic gets overriden on the next boot
384
384
385
385
// The values where succesfully configured
386
386
eeprom_config->magic_number = MAGIC_NUMBER;
387
387
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);
389
393
390
394
*p_run_hotspot = false ; // Stop the hotspot when the user submits new values
391
395
});
@@ -560,10 +564,10 @@ void setup() {
560
564
return ;
561
565
}
562
566
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" ) ];
567
571
568
572
if (sleep_time_variant.isNull () || watering_delay_variant.isNull () ||
569
573
watering_threshold_variant.isNull () || watering_time_variant.isNull ()) {
@@ -637,10 +641,10 @@ void setup() {
637
641
eeprom_config.override_retained_config_topic = false ; // Will be writen to the EEPROM further down
638
642
639
643
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 ;
644
648
645
649
size_t n = serializeJson (jsonDoc, jsonBuffer, sizeof (jsonBuffer));
646
650
if (mqttPublishBlocking (config_topic, jsonBuffer, n, true , 5 * 10 ))
@@ -655,20 +659,20 @@ void setup() {
655
659
656
660
// Send messsages, so the sensor is auto discovered by Home Assistant - see: https://www.home-assistant.io/docs/mqtt/discovery/
657
661
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" );
667
671
668
672
// 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));
672
676
673
677
size_t n = serializeJson (jsonDoc, jsonBuffer, sizeof (jsonBuffer));
674
678
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() {
678
682
679
683
// Send the voltage "sensor" as well
680
684
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" );
690
694
691
695
// 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));
695
699
696
700
n = serializeJson (jsonDoc, jsonBuffer, sizeof (jsonBuffer));
697
701
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() {
700
704
Serial.println (F (" Failed to send voltage discovery message due to timeout" ));
701
705
702
706
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;
712
716
713
717
n = serializeJson (jsonDoc, jsonBuffer, sizeof (jsonBuffer));
714
718
if (mqttPublishBlocking (String (F (" plant/" )) + String (eeprom_config.mqtt_base_topic ) + F (" /state" ), jsonBuffer, n, false , 5 * 10 ))
0 commit comments