sending audio to mqtt broker through ESP8266

hello everyone!

I'm trying to stream voice to the MQTT broker. I'm using esp8266(NodeMCU1.0) and it's ADC to sampling the audio signal at 4KHz and 8-bit PCM audio format. I used Pubsubclient.h library to publish audio packets to the broker but on the other side when I receive packets and play them I have the interrupted voice and I have a delay between playing packets as long as my buffer size. please help me to have continues voice.

I have 2 main problems:

  1. delay between playing packets as long as my buffer size

example of what i received: ;D ;D ;D .....|||||..........|||||||...........|||||||||............||||||||

  1. Quality of voice: I receive a noisy voice.

Please help me! Thanks.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "Knight";
const char* password = "****";
const char* mqtt_server = "104.21.218.224";

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (4096)
int i;
byte voice[MSG_BUFFER_SIZE];

void setup_wifi() {

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

  WiFi.begin(ssid, password);

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

  randomSeed(micros());

  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...");
    // Create a random client ID
    String clientId = "ashkan";
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      if (!client.publish("doorphone/connected", "true")) {
        Serial.println("publish failed, either connection lost, or message too large");
      }
      else {
        Serial.println("publish succeeded");

      }
      // ... and resubscribe
      //      client.subscribe("doorphone/open");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  for (i = 0; i < MSG_BUFFER_SIZE; i++) {
    voice[i] = analogRead(A0);
    //        client.write(voice[i]);
            delayMicroseconds(125);
  }
  client.publish("doorphone/connected", voice, MSG_BUFFER_SIZE);
}

mqtt.ino (2.12 KB)

client.push() is a blocking function.

Thanks, but I didn't use client.push(). I used client.publish() that is for mqtt pusubclient library.