aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp30
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;
+
}