How to get audio using port over the wifi in python?

I can control smart home devices (such as lamps, etc.) using a simple voice recognition system in Python. Now I want to make a device that sends sound to Python via Wi-Fi. I found a program that sends audio in realtime using esp32 and wifi with port 4444.

Here is the Arduino code:

/* Audio streamer with ESP32 and Adafruit elected microphone board. 
 * Created by Julian Schroeter.
*/
#include <Arduino.h>
#include <WiFi.h>
#include <driver/adc.h>

#define AUDIO_BUFFER_MAX 800

uint8_t audioBuffer[AUDIO_BUFFER_MAX];
uint8_t transmitBuffer[AUDIO_BUFFER_MAX];
uint32_t bufferPointer = 0;

const char* ssid     = "****";
const char* password = "****";
const char* host     = "192.168.1.104";

bool transmitNow = false;

WiFiClient client;

hw_timer_t * timer = NULL; // our timer
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; 

void IRAM_ATTR onTimer() {
  portENTER_CRITICAL_ISR(&timerMux); // says that we want to run critical code and don't want to be interrupted
  int adcVal = adc1_get_voltage(ADC1_CHANNEL_0); // reads the ADC
  uint8_t value = map(adcVal, 0 , 4096, 0, 255);  // converts the value to 0..255 (8bit)
  audioBuffer[bufferPointer] = value; // stores the value
  bufferPointer++;
 
  if (bufferPointer == AUDIO_BUFFER_MAX) { // when the buffer is full
    bufferPointer = 0;
    memcpy(transmitBuffer, audioBuffer, AUDIO_BUFFER_MAX); // copy buffer into a second buffer
    transmitNow = true; // sets the value true so we know that we can transmit now
  }
  portEXIT_CRITICAL_ISR(&timerMux); // says that we have run our critical code
}


void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

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

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("MY IP address: ");
  Serial.println(WiFi.localIP());
  
  adc1_config_width(ADC_WIDTH_12Bit); // configure the analogue to digital converter
  adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_0db); // connects the ADC 1 with channel 0 (GPIO 36)

  const int port = 4444;
  while (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(1000);
  }

  Serial.println("connected to server");

  timer = timerBegin(0, 80, true); // 80 Prescaler
  timerAttachInterrupt(timer, &onTimer, true); // binds the handling function to our timer 
  timerAlarmWrite(timer, 125, true);
  timerAlarmEnable(timer);

}

void loop() {
  if (transmitNow) { // checks if the buffer is full
    transmitNow = false;
    client.write((const uint8_t *)audioBuffer, sizeof(audioBuffer)); // sending the buffer to our server
  }
}

Now, how can I receive this sound via Wi-Fi in Python?

Have you asked this question on a Python Forum?

your code opens a socket to 192.168.1.104 using port 4444

so you need to have a python program ready to listen on that port at that address

here is a quick example:

Arduino code:

#include <WiFi.h>

const char* ssid     = "xxx";
const char* password = "xxx";

const char* host = "10.86.1.147";  // modify this to be your computer's IP address where you run the python script
const int port = 4444;
char payload[] = "this is my paylaod";

void setup() {
  Serial.begin(115200); Serial.println();

  WiFi.begin(ssid, password);

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

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

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed; STOP");
    while (true) yield();
  }

  Serial.print("Sending :");
  Serial.write(payload, sizeof payload);
  Serial.println();

  client.write(payload, sizeof payload);

  Serial.println("closing connection");
  client.stop();
}

void loop() {}

Python3:

import socket
address = ''
port = 4444

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((address, port))
server.listen(1)
client, clientAddress = server.accept()
print(f"incoming connection requests from {clientAddress}")
data = client.recv(1024)
if not data:
    print("Reception error.")
else:
    print(f"Got: {data}")
print("closing incoming connection.")
client.close()
print("Terminating server.")
server.close()
  • launch the python script in a terminal on your computer (I called mine tcp_listener.py)
  • upload the arduino sketch in your ESP32

terminal for Python

$$ python3 ./tcp_listener.py
incoming connection requests from ('10.86.1.181', 63338)
Got: b'this is my paylaod\x00'
closing incoming connection.
Terminating server.

Serial Monitor (@ 115200 bauds) will show

.
WiFi connected
IP address: 10.86.1.181
Sending :this is my paylaod
closing connection

Thanks for the help
How can I get this sound through another esp and play it with a speaker?
that this esp has the role of a server and one or more esps can send data to this server?

Well you should send it directly to the target esp or the audio will lag

I’m not sure about what you are acquiring (looks like a buffer of ADC readings), so on the end device you would use a DAC and send the samples at the same rate

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.