# Weird topic in MQTT explorer - am I spamming broker?

**URL:** https://discourse.nodered.org/t/weird-topic-in-mqtt-explorer-am-i-spamming-broker/15235
**Category:** General
**Created:** [7 September 2019 13:59 UTC](https://discourse.nodered.org/t/weird-topic-in-mqtt-explorer-am-i-spamming-broker/15235 "2019-09-07T13:59:23Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)
#### Post date: [7 September 2019 13:59 UTC](https://discourse.nodered.org/t/weird-topic-in-mqtt-explorer-am-i-spamming-broker/15235/1 "2019-09-07T13:59:23Z")

</div>

I was torn between posting over on the Arduino forum but decided I would start here, I am having issues with a new project but it's a general question really.

If you see the below screenshot, there's a strange topic which is corrupted. As I have the client.publish in my void loop I think this may be the cause, correct?

 ![09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7c2bd1d60a3aa104ab427b9e50e2b2185b872d83.png)

My project is to capture a regular loud sound event (barking) and send this to node red, so the MAX4466 sensor is always sampling every 50ms.

I'm sure with more coding knowledge this can be allowed for in the code, but I am stuck at present. So I have used a switch and trigger node to filter the stream of results.

- Firstly I was wondering if I am effectively "spamming" the MQTT server which is causing the circled problem?

- Secondly, any general tips would be appreciated in processing the detected sound (I know there will be false alarms).

Full code:

```auto
/ ****************************************
  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
  //
**************************************** /

#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 = 3.2; // 3.3V is the maximum

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()
{

  //Check MQTT connected
  if (!client.connected())
  {
    reconnect();
  }
  client.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
    client.publish(sound_level_topic, String("QUIET").c_str(), false);
  }
}

```

---

_[View the full topic](https://discourse.nodered.org/t/weird-topic-in-mqtt-explorer-am-i-spamming-broker/15235)._
