Skip to content

Commit

Permalink
Reduce arduino device publishing rate to 1x every 10s
Browse files Browse the repository at this point in the history
Signed-off-by: Kate Goldenring <[email protected]>
  • Loading branch information
kate-goldenring committed Nov 7, 2024
1 parent 24c859b commit bd65535
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
3 changes: 2 additions & 1 deletion sound-sensor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions sound-sensor/mqttsound/mqtt_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define MQTT_CLIENT_USERNAME "user"
#define MQTT_CLIENT_PASSWORD "password"
41 changes: 28 additions & 13 deletions sound-sensor/mqttsound/mqttsound.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "WiFiS3.h"
#include "arduino_secrets.h"
#include "mqtt_secrets.h"
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit bd65535

Please sign in to comment.