From 7ca6199fd60be35f83e35a8b52ab9d1abbf7ccd4 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Tue, 27 Apr 2021 22:31:39 -0400 Subject: [PATCH] Make the secrets.h file more of a settings.h --- window_control/window_control.ino | 36 +++++++++---------- window_control/window_control_settings.h | 7 ++++ ...mple => window_control_settings.h.example} | 0 3 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 window_control/window_control_settings.h rename window_control/{window_control_secrets.h.example => window_control_settings.h.example} (100%) diff --git a/window_control/window_control.ino b/window_control/window_control.ino index a2840d0..a2d03fc 100644 --- a/window_control/window_control.ino +++ b/window_control/window_control.ino @@ -3,20 +3,11 @@ // Include the mqtt client lib #include -// Include any secrets. -#include "window_control_secrets.h" - -#define wifi_ssid SECRET_SSID -#define wifi_password SECRET_PASS - -#define mqtt_server "mqtt.kitsunehosting.net" -#define mqtt_port 1883 -#define mqtt_user SECRET_MQTT_USER -#define mqtt_password SECRET_MQTT_PASS - +// Include the settings.h. +#include "window_control_settings.h" #define window_name "window_0" -#define mqtt_root "/home/upstairs_lab/actuators/" window_name +#define mqtt_root "home/upstairs_lab/actuators/" window_name #define in_topic "/testing/in" #define out_topic "/testing/out" @@ -30,7 +21,7 @@ void setup() { Serial.begin(115200); setup_wifi(); client.setClient(espClient); - client.setServer(mqtt_server, mqtt_port); + client.setServer(MQTT_SERVER, MQTT_PORT); client.setCallback(callback); // initialize digital pin LED_BUILTIN as an output. @@ -44,13 +35,14 @@ void setup_wifi() { Serial.println(); WiFi.hostname("ESP-" window_name); Serial.print("Connecting to "); - Serial.println(wifi_ssid); + Serial.println(WIFI_SSID); - WiFi.begin(wifi_ssid, wifi_password); + WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.println("Not yet connected.. Waiting 500ms to check again.."); + delay(950); + tone(2, 1200, 50); + Serial.println("Not yet connected.. Waiting 1s to check again.."); } Serial.println(""); @@ -64,7 +56,7 @@ void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect - if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) { + if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) { Serial.println("connected"); } else { Serial.print("failed, rc="); @@ -92,14 +84,18 @@ void callback(char* topic, byte* payload, unsigned int length) { } void loop() { + // If we ever disconnect, reconnect. if (!client.connected()) { reconnect(); } + + // Run the client loop client.loop(); + // Publishes a random 0 and 1 like someone switching off and on randomly (random(2)) - client.publish(out_topic, String(random(2)).c_str(), true); + client.publish(mqtt_root "/random", String(random(2)).c_str(), true); delay(1000); - client.subscribe(in_topic); + client.subscribe(mqtt_root "/in"); tone(2, 2000, 500); delay(1000); } diff --git a/window_control/window_control_settings.h b/window_control/window_control_settings.h new file mode 100644 index 0000000..51fc8e5 --- /dev/null +++ b/window_control/window_control_settings.h @@ -0,0 +1,7 @@ +#define WIFI_SSID "muner" +#define WIFI_PASS "apollo11" + +#define MQTT_SERVER "mqtt.kitsunehosting.net" +#define MQTT_PORT 1883 +#define MQTT_USER "device" +#define MQTT_PASS "iamnotacrook" diff --git a/window_control/window_control_secrets.h.example b/window_control/window_control_settings.h.example similarity index 100% rename from window_control/window_control_secrets.h.example rename to window_control/window_control_settings.h.example