diff options
author | tcmal <me@aria.rip> | 2024-09-04 18:17:13 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-09-04 19:05:53 +0100 |
commit | 8f42729c921784f006e42bab681130d339272465 (patch) | |
tree | 212b5552289be3cd2429da8432b3f6a3d750dddd /src/main.cpp |
POC
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..3b4d531 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,55 @@ +#include <WiFi.h> +#include <PubSubClient.h> +#include <Arduino.h> +#include "./local.h" // defines ssid and password + +WiFiClient wifiClient; +PubSubClient mqttClient(wifiClient); + +long lastMsg = 0; + +// Reconnect to the wifi / MQTT if required +void reconnect() { + if (WiFi.status() != WL_CONNECTED) { + WiFi.begin(ssid, password); + } + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("waiting for wifi, status="); + Serial.println(WiFi.status()); + } + + while (!mqttClient.connected()) { + mqttClient.setServer(mqtt_broker, mqtt_port); + if (!mqttClient.connect("Bark-Button")) { + Serial.print("failed to connect to mqtt, rc="); + Serial.println(mqttClient.state()); + delay(2000); + } + } +} + +// On button press, send a message to the MQTT broker +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(); +} + +void setup() { + Serial.begin(9600); + reconnect(); + // TODO: Setup button press input +} + +// Wait for button press +void loop() { + // TODO: Wait for button press lol + delay(1000); + buttonPress(); +} |