Greetings,
I am trying to make a program that records mic input and sends it over UDP to another esp32(tiny pico) and outputs that data to a speaker.
Sender Specs:
tiny pico with SPH0645 i2s mic
Receiver Specs:
tiny pico with Max 98357A i2s amp
The problem, I think I am having, is RECEIVING the data and converting back to the original format(int), so the I2S.write() can output the sound.
- From I2S.read() the data returned is of int type
- I then attempt to store 256 samples in an array and send out the data
- Similarly, on the other pico, I attempt to retrieve that same array data using another for loop
- I see the data and get the values from I2S.read(), but in order to use I2S.write() on the receive side, the data needs to be of int type and I am afraid that I am confusing some simple data type concepts.
Any help would be greatly appreciated!
-Twiggs
Transmit Code
/*
* This sketch sends random data over UDP on a ESP32 device
*
*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include <I2S.h>
// WiFi network name and password:
const char * networkName = "";
const char * networkPswd = "";
//IP address to send UDP data to:
// either use the ip address of the server or
// a network broadcast address
const char * udpAddress = "";
const int udpPort = 5432;
//Are we currently connected?
boolean connected = false;
////////////////////////////////////////////////
////////////////////////////////////////////
const char* sound_wav[256];
int wav[256];
int bytesPacked = 0;
int TempSample = 0;
////////////////////////////////////////////////////////
////////////////////////////////////////////
//The udp library class
WiFiUDP udp;
void setup(){
// Initilize hardware serial:
Serial.begin(115200);
//Connect to the WiFi network
connectToWiFi(networkName, networkPswd);
// start I2S at 8 kHz with 32-bits per sample
if (!I2S.begin(I2S_PHILIPS_MODE, 8000, 32))
{
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
Serial.println("Set up done");
}
void loop(){
//////////////////////////////////////////////
// read a sample(s) into 256 byte buffer the send over UDP
////////////////////////////////////////
for( int n = 0; n <= 255; n++)
{
TempSample = I2S.read();
//Serial.println(TempSample);
delay(1);
wav[n] = TempSample;
Serial.print("TempSample:::");
Serial.println(TempSample);
bytesPacked++;
}
Serial.println(bytesPacked); //verify array is in bounds
if(connected && bytesPacked >=255)// check if i2s buffer is full to then send out via UDP
{
Serial.println("bytes packed reset");
bytesPacked = 0;
for(int n2 = 0; n2 <= 256; n2++)
{
//Send a packet
udp.beginPacket(udpAddress,udpPort);
//udp.printf("Printing values:");
//int len = sizeof(wav[n2]);
Serial.print("your N2 values:");
Serial.println(n2);
Serial.print("your values for data:");
Serial.println(wav[n2]);
udp.print(wav[n2]); //data is sent correctly, but when received, I am confused on converting it back
udp.endPacket();
}
}
Serial.println("Done with all loops");
delay(5000);
}
void connectToWiFi(const char * ssid, const char * pwd){
Serial.println("Connecting to WiFi network: " + String(ssid));
// delete old config
WiFi.disconnect(true);
//register event handler
WiFi.onEvent(WiFiEvent);
//Initiate connection
WiFi.begin(ssid, pwd);
Serial.println("Waiting for WIFI connection...");
}
//wifi event handler
void WiFiEvent(WiFiEvent_t event){
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
//When connected set
Serial.print("WiFi connected! IP address: ");
Serial.println(WiFi.localIP());
//initializes the UDP state
//This initializes the transfer buffer
udp.begin(WiFi.localIP(),udpPort);
connected = true;
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
connected = false;
break;
default: break;
}
}
Receive side pico code
/*
* This sketch sends random data over UDP on a ESP32 device
*
*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include <I2S.h>
// WiFi network name and password:
const char * ssid = "";
const char * password = "";
//IP address to send UDP data to:
// either use the ip address of the server or
// a network broadcast address
const char * udpAddress = "";
const int udpPort = 5432;
//Are we currently connected?
boolean connected = false;
//////////////////////////////////////////////////////////////////////////////
//ADC buffer
char* sound_wav[255]; //one of two types of array
int bytesPacked = 0; //counter
char packetBuffer[255]; //buffer holds incoming packet
int wav[255]; //second int array
////////////////////////////////////////////////////////////////////////////////
//The udp library class
WiFiUDP udp;
void setup(){
// Initilize hardware serial:
Serial.begin(115200);
//Connect to the WiFi network
connectToWiFi(ssid, password);
delay(1000);
udp.begin(udpPort);
delay(1000);
//I2S.setAllPins(14, 25, 26, 26, 33); // you can change default pins; order of pins = (CLK, WS, Fs, IN, OUT)
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
}
// start I2S at 8 kHz with 32-bits per sample
if (!I2S.begin(I2S_PHILIPS_MODE, 8000, 32)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
Serial.println("Set up done");
}
void loop(){
if(udp.parsePacket() && connected)
{
for(int n= 0; n<=255; n++)
{
udp.read(packetBuffer,255);
//udp.read(packetBuffer,255);
Serial.print("Contents from packet buffer RAW: ");
Serial.println(packetBuffer); //======>value is correct, but this is a Char*
//wav[n] = (int)packetBuffer;// convert contents into basic int , but removes negative sign or truncates values
//Serial.println(wav[n] );
}
Serial.println("DONE COUNTING");
/*//waiting to get correct data type before proceeding to output via i2s
Serial.println("starting I2S output");
int tempSample;
for(int n =0; n<=256; n++)
{
tempSample = wav[n];
I2S.write(tempSample );
}
*/
}
Serial.println("delay fives seconds and chill now");
delay(5000);
}
void connectToWiFi(const char * ssid, const char * pwd){
Serial.println("Connecting to WiFi network: " + String(ssid));
// delete old config
WiFi.disconnect(true);
//register event handler
WiFi.onEvent(WiFiEvent);
//Initiate connection
WiFi.begin(ssid, pwd);
Serial.println("Waiting for WIFI connection...");
}
//wifi event handler
void WiFiEvent(WiFiEvent_t event){
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
//When connected set
Serial.print("WiFi connected! IP address: ");
Serial.println(WiFi.localIP());
//initializes the UDP state
//This initializes the transfer buffer
udp.begin(WiFi.localIP(),udpPort);
connected = true;
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
connected = false;
break;
default: break;
}
}