From 348a66a6de115c311897cef50451d394bf727608 Mon Sep 17 00:00:00 2001 From: Calvin McLean Date: Sun, 8 Sep 2024 14:42:06 -0700 Subject: [PATCH] Add mDNS to controller --- garden-app/controller/generate_config.go | 4 ---- garden-app/controller/generate_config_test.go | 16 ---------------- garden-app/controller/interactive.go | 9 --------- garden-controller/include/wifi_manager.h | 1 + garden-controller/src/wifi_manager.cpp | 13 ++++++++++--- 5 files changed, 11 insertions(+), 32 deletions(-) diff --git a/garden-app/controller/generate_config.go b/garden-app/controller/generate_config.go index 785c17ae..edd0fe49 100644 --- a/garden-app/controller/generate_config.go +++ b/garden-app/controller/generate_config.go @@ -20,13 +20,9 @@ const ( #define QUEUE_SIZE 10 -#define ENABLE_WIFI -#ifdef ENABLE_WIFI #define MQTT_ADDRESS "{{ .MQTTConfig.Broker }}" #define MQTT_PORT {{ .MQTTConfig.Port }} -#endif - #define NUM_ZONES {{ len .Zones }} #define VALVES { {{ range $index, $z := .Zones }}{{if $index}}, {{end}}{{ $z.ValvePin }}{{ end }} } #define PUMPS { {{ range $index, $z := .Zones }}{{if $index}}, {{end}}{{ $z.PumpPin }}{{ end }} } diff --git a/garden-app/controller/generate_config_test.go b/garden-app/controller/generate_config_test.go index 958c02d3..409e60b8 100644 --- a/garden-app/controller/generate_config_test.go +++ b/garden-app/controller/generate_config_test.go @@ -45,13 +45,9 @@ func TestGenerateMainConfig(t *testing.T) { #define QUEUE_SIZE 10 -#define ENABLE_WIFI -#ifdef ENABLE_WIFI #define MQTT_ADDRESS "localhost" #define MQTT_PORT 1883 -#endif - #define NUM_ZONES 1 #define VALVES { GPIO_NUM_16 } #define PUMPS { GPIO_NUM_18 } @@ -95,13 +91,9 @@ func TestGenerateMainConfig(t *testing.T) { #define QUEUE_SIZE 10 -#define ENABLE_WIFI -#ifdef ENABLE_WIFI #define MQTT_ADDRESS "localhost" #define MQTT_PORT 1883 -#endif - #define NUM_ZONES 1 #define VALVES { GPIO_NUM_16 } #define PUMPS { GPIO_NUM_18 } @@ -139,13 +131,9 @@ func TestGenerateMainConfig(t *testing.T) { #define QUEUE_SIZE 10 -#define ENABLE_WIFI -#ifdef ENABLE_WIFI #define MQTT_ADDRESS "localhost" #define MQTT_PORT 1883 -#endif - #define NUM_ZONES 1 #define VALVES { GPIO_NUM_16 } #define PUMPS { GPIO_NUM_18 } @@ -195,13 +183,9 @@ func TestGenerateMainConfig(t *testing.T) { #define QUEUE_SIZE 10 -#define ENABLE_WIFI -#ifdef ENABLE_WIFI #define MQTT_ADDRESS "localhost" #define MQTT_PORT 1883 -#endif - #define NUM_ZONES 4 #define VALVES { GPIO_NUM_16, GPIO_NUM_16, GPIO_NUM_16, GPIO_NUM_16 } #define PUMPS { GPIO_NUM_18, GPIO_NUM_18, GPIO_NUM_18, GPIO_NUM_18 } diff --git a/garden-app/controller/interactive.go b/garden-app/controller/interactive.go index 1dd0a8fc..92a5607a 100644 --- a/garden-app/controller/interactive.go +++ b/garden-app/controller/interactive.go @@ -64,15 +64,6 @@ func mqttPrompts(config *Config) error { }, Validate: survey.Required, }, - { - Name: "publish_health", - Prompt: &survey.Input{ - Message: "Enable health publishing?", - Default: fmt.Sprintf("%t", config.PublishHealth), - Help: "control whether or not healh publishing is enabled. Enable it unless you have a good reason not to", - }, - Validate: survey.Required, - }, } err := survey.Ask(qs, config) if err != nil { diff --git a/garden-controller/include/wifi_manager.h b/garden-controller/include/wifi_manager.h index 852625b9..b86fa315 100644 --- a/garden-controller/include/wifi_manager.h +++ b/garden-controller/include/wifi_manager.h @@ -5,6 +5,7 @@ #include #include #include +#include #define FORMAT_LITTLEFS_IF_FAILED true diff --git a/garden-controller/src/wifi_manager.cpp b/garden-controller/src/wifi_manager.cpp index e7949aa9..de228270 100644 --- a/garden-controller/src/wifi_manager.cpp +++ b/garden-controller/src/wifi_manager.cpp @@ -105,6 +105,10 @@ void connectWifiDirect() { printf("Wifi connected...\n"); + strcpy(mqtt_server, MQTT_ADDRESS); + strcpy(mqtt_topic_prefix, TOPIC_PREFIX); + mqtt_port = MQTT_PORT; + wifiManager.setEnableConfigPortal(false); wifiManager.setConfigPortalBlocking(false); wifiManager.autoConnect(); @@ -129,9 +133,7 @@ void setupWifiManager() { wifiManager.addParameter(&custom_mqtt_topic_prefix); wifiManager.addParameter(&custom_mqtt_port); - char hostname[50]; - snprintf(hostname, sizeof(hostname), "%s-controller", mqtt_topic_prefix); - wifiManager.setHostname(hostname); + wifiManager.setHostname(mqtt_topic_prefix); // wifiManager.resetSettings(); @@ -151,6 +153,11 @@ void setupWifiManager() { xTaskCreate(wifiManagerLoopTask, "WifiManagerLoopTask", 4096, NULL, 1, &wifiManagerLoopTaskHandle); + if (!MDNS.begin(mqtt_topic_prefix)) { + printf("error starting mDNS\n"); + return; + } + // Create event handler tp reconnect to WiFi WiFi.onEvent(wifiDisconnectHandler, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED); }