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 |
POC
Diffstat (limited to 'src')
-rw-r--r-- | src/local.h.tmpl | 8 | ||||
-rw-r--r-- | src/main.cpp | 55 | ||||
-rw-r--r-- | src/mosquitto.conf | 2 |
3 files changed, 65 insertions, 0 deletions
diff --git a/src/local.h.tmpl b/src/local.h.tmpl new file mode 100644 index 0000000..488b2bf --- /dev/null +++ b/src/local.h.tmpl @@ -0,0 +1,8 @@ +#pragma once + +const char *ssid = ""; +const char *password = ""; +const char *mqtt_broker = ""; +const int mqtt_port = 1883; +const char *mqtt_topic = ""; +const int n_bark_sounds = 1; 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(); +} diff --git a/src/mosquitto.conf b/src/mosquitto.conf new file mode 100644 index 0000000..c8348ac --- /dev/null +++ b/src/mosquitto.conf @@ -0,0 +1,2 @@ +listener 1883 +allow_anonymous true |