Weird topic in MQTT explorer - am I spamming broker?

Thanks @bakman2 and @TotallyInformation :+1:
Making changes as suggested by yourselves and it's working as expected so far.

I got rid of the "QUIET" message altogether and added a 100ms delay in reading the MAX4466 sound sensor, it's still responsive enough.

Now to test it outside! :laughing:

I am attaching the code in case anyone has a similar situation in the future:

/****************************************
  Based on:
  Example Sound Level Sketch for the Adafruit Microphone Amplifier
  https://learn.adafruit.com/adafruit-microphone-amplifier-breakout/measuring-sound-levels

  877dev
  Hardware: Wemos D1 mini and MAX4466 microphone amplifier
  Required behaviour:
  When no sound, publish "QUIET" to MQTT and turn OFF built in LED
  When sound occurs, publish "SOUND" to MQTT and turn ON built in LED
  //
  This is to capture a dog barking noise
  //
****************************************/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//
#define wifi_ssid ""
#define wifi_password ""
//
#define mqtt_server "192.168.xx.xx"
#define mqtt_user ""
#define mqtt_password ""
//
#define sound_level_topic "garden/sound/level"
//
WiFiClient espClient;
PubSubClient client(espClient);
//
char *client_id = "NAME_HERE"; // MQTT Client ID


//MAX4466 configuration
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
//
const float vMax = 1.5;  // 3.3V is the maximum

unsigned long checkSoundTime;  // for calling of checkSound function time gap period
int checkSoundInterval = 100;


void setup()
{


  Serial.begin(9600);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  //
  while (!Serial) {} // Wait

  pinMode(LED_BUILTIN, OUTPUT);  // setup builtin LED for indication of high sound detected
}


void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect(client_id, mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}



void loop() {

  if (!client.connected())
  {
    reconnect();
  }
  client.loop();

  //check if interval has elapsed since sound was checked
  if (millis() > checkSoundTime + checkSoundInterval )
  {
    checkSound();
  }

}





void checkSound()
{

  checkSoundTime = millis(); //reset readTime to current millis, ensures this function is called every x seconds - see void loop

  unsigned long startMillis = millis(); // Start of sample window
  unsigned int peakToPeak = 0;   // peak-to-peak level

  unsigned int signalMax = 0;
  unsigned int signalMin = 1024;

  // collect data for 50 mS
  while (millis() - startMillis < sampleWindow)
  {
    sample = analogRead(0);
    if (sample < 1024)  // toss out spurious readings
    {
      if (sample > signalMax)
      {
        signalMax = sample;  // save just the max levels
      }
      else if (sample < signalMin)
      {
        signalMin = sample;  // save just the min levels
      }
    }
  }
  peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
  double volts = (peakToPeak * 3.3) / 1024;  // convert to volts

  Serial.println(volts);

  if (volts > vMax) {
    // turn the LED on (HIGH is the voltage level)
    digitalWrite(LED_BUILTIN, LOW);  // Turns LED ON
    Serial.println("Sound detected");
    client.publish(sound_level_topic, String("SOUND").c_str(), false);
  } else {
    // turn the LED off by making the voltage LOW
    digitalWrite(LED_BUILTIN, HIGH); // Turns LED OFF
  }
}