From bd65535932ed32707f56cbf1d015c7b9bb52784d Mon Sep 17 00:00:00 2001 From: Kate Goldenring Date: Wed, 6 Nov 2024 17:29:18 -0800 Subject: [PATCH] Reduce arduino device publishing rate to 1x every 10s Signed-off-by: Kate Goldenring --- sound-sensor/README.md | 3 +- sound-sensor/mqttsound/mqtt_secrets.h | 2 ++ sound-sensor/mqttsound/mqttsound.ino | 41 ++++++++++++++++++--------- 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 sound-sensor/mqttsound/mqtt_secrets.h diff --git a/sound-sensor/README.md b/sound-sensor/README.md index 2094656..2e066d0 100644 --- a/sound-sensor/README.md +++ b/sound-sensor/README.md @@ -16,11 +16,12 @@ The [`mqttsound`](./sound-sensor/mqttsound) folder contains an Anduino program t Before uploading the program to your device, be sure to do the following: 1. Add the netword SSID (name) and password to [`arduino_secrets.h`](./mqttsound/arduino_secrets.h) +1. Set username and password credentials for your MQTT broker in [`mqtt_secrets.h`](./mqttsound/mqtt_secrets.h) 1. Configure constants in the [arduino sketch](./mqttsound/mqttsound.ino): 1. Set the address and port for your MQTT broker. Defaults to the [public Mosquitto broker](https://test.mosquitto.org/) at host `test.mosquitto.org` and port `1883`. 1. Configure the MQTT topic for the device. Defaults to `booth/demo`. 1. Configure the sound threshold for the device. Any volume above this threshold is published. Defaults to 200. -1. If using a broker that requires authentication, be sure to set a username and password in the sketch and uncomment the line that does authentication. +1. If using a broker that does not require authentication, be sure to comment out `mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);` in the sketch. ### Troubleshooting Arduino Uno diff --git a/sound-sensor/mqttsound/mqtt_secrets.h b/sound-sensor/mqttsound/mqtt_secrets.h new file mode 100644 index 0000000..1949894 --- /dev/null +++ b/sound-sensor/mqttsound/mqtt_secrets.h @@ -0,0 +1,2 @@ +#define MQTT_CLIENT_USERNAME "user" +#define MQTT_CLIENT_PASSWORD "password" \ No newline at end of file diff --git a/sound-sensor/mqttsound/mqttsound.ino b/sound-sensor/mqttsound/mqttsound.ino index d05e0ed..83743e4 100644 --- a/sound-sensor/mqttsound/mqttsound.ino +++ b/sound-sensor/mqttsound/mqttsound.ino @@ -1,5 +1,6 @@ #include "WiFiS3.h" #include "arduino_secrets.h" +#include "mqtt_secrets.h" #include #include @@ -8,14 +9,15 @@ int sound_sensor = A2; // Sound sensor should be plugged into pin A2 char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) -// char mqtt_user[] = ""; -// char mqtt_pass[] = ""; +char mqtt_user[] = MQTT_CLIENT_USERNAME; +char mqtt_pass[] = MQTT_CLIENT_PASSWORD; -const char broker[] = "test.mosquitto.org"; // IP address of the MQTT broker. +const char broker[] = "emqx.demo.fermyon.com"; // IP address of the MQTT broker. int port = 1883; const char publish_topic[] = "booth/demo"; // Unique topic for the "booth" this sensor is in -int threshold = 200; +int threshold = 50; int delay_ms = 100; +int pub_interval_ms = 10000; // Publish the maximum volume heard over 10 seconds WiFiClient wifiClient; MqttClient mqttClient(wifiClient); @@ -31,28 +33,41 @@ void setup() { } void loop() { + static unsigned long startTime = millis(); // Track the start time of the 10-second interval + static int maxSoundValue = 0; // Track the maximum sound value over 10 seconds + if (!mqttClient.connected()) { reconnectMQTT(); // Reconnect if the connection is lost } - int soundValue = 0; - for (int i = 0; i < 32; i++) { + + int soundValue = 0; + for (int i = 0; i < 32; i++) { // Read the sound sensor soundValue += analogRead(sound_sensor); - } + } soundValue >>= 5; // Bitshift operation Serial.println(soundValue); - // If a value higher than 300 is registered, we will publish the sound level to the MQTT broker - if (soundValue > threshold) { - // Send data - sendVolumeData(soundValue); + // Track the maximum sound value within the 10-second window + if (soundValue > maxSoundValue) { + maxSoundValue = soundValue; + } + + // Check if 10 seconds have elapsed + if (millis() - startTime >= pub_interval_ms) { + // Send the maximum sound value if it exceeds the threshold + if (maxSoundValue > threshold) { + sendVolumeData(maxSoundValue); + } + // Reset for the next interval + maxSoundValue = 0; + startTime = millis(); // Restart the timer } delay(delay_ms); // Delay between sound readings } - void connectWiFi() { Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); @@ -67,7 +82,7 @@ void connectWiFi() { void connectMQTT() { Serial.println("Attempting to connect to the MQTT broker."); - // mqttClient.setUsernamePassword(mqtt_user, mqtt_pass); + mqttClient.setUsernamePassword(mqtt_user, mqtt_pass); while (!mqttClient.connect(broker, port)) { Serial.print("."); delay(1000);