From b3738675626a5e53895a640a6eb824237a2c9e49 Mon Sep 17 00:00:00 2001 From: tcmal Date: Wed, 4 Sep 2024 19:06:42 +0100 Subject: Production --- src/main.cpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'src/main.cpp') 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; + } -- cgit v1.2.3