diff options
author | tcmal <me@aria.rip> | 2024-09-04 19:06:42 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-09-11 12:42:22 +0100 |
commit | b3738675626a5e53895a640a6eb824237a2c9e49 (patch) | |
tree | 4151c308000109b34ac4c3754401ca7252a5558d /src/main.cpp | |
parent | 8f42729c921784f006e42bab681130d339272465 (diff) |
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/main.cpp b/src/main.cpp index 3b4d531..ad91fa5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,11 @@ WiFiClient wifiClient; PubSubClient mqttClient(wifiClient); +#define BUTTON_PIN 14 +#define LED_PIN 2 + long lastMsg = 0; +long lastButtonState = LOW; // High/1 by default, Low/0 when pressed // Reconnect to the wifi / MQTT if required void reconnect() { @@ -20,6 +24,7 @@ void reconnect() { } while (!mqttClient.connected()) { + Serial.println("attempting mqtt connection..."); mqttClient.setServer(mqtt_broker, mqtt_port); if (!mqttClient.connect("Bark-Button")) { Serial.print("failed to connect to mqtt, rc="); @@ -34,22 +39,29 @@ void buttonPress() { char buf[40]; reconnect(); - sprintf(buf, "barks/%d", random(n_bark_sounds)); - mqttClient.publish("sound/g1/play", buf); - - // Don't waste cycles staying awake to send heartbeats - mqttClient.disconnect(); + sprintf(buf, "barks/%d.mp3", random(n_bark_sounds)); + mqttClient.publish(mqtt_topic, buf); } void setup() { Serial.begin(9600); reconnect(); - // TODO: Setup button press input + + pinMode(BUTTON_PIN, INPUT_PULLUP); + pinMode(LED_PIN, OUTPUT); } // Wait for button press void loop() { - // TODO: Wait for button press lol - delay(1000); - buttonPress(); + mqttClient.loop(); + + int buttonState = digitalRead(BUTTON_PIN); + if (buttonState != lastButtonState && buttonState == LOW) { + digitalWrite(LED_PIN, HIGH); + buttonPress(); + delay(100); + digitalWrite(LED_PIN, LOW); + } + lastButtonState = buttonState; + } |