From 81f7f217a582360816c2c4c3a38b5731c3c489e4 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 27 Mar 2018 21:33:34 -0700 Subject: [PATCH] Add I2S input and transmit over WiFi example Very simple example sends I2S microphone data using UDP over WiFi. Runs very well and proves the I2S receive DMA buffer management is working pretty well. --- .../examples/I2STransmit/I2STransmit.ino | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 libraries/esp8266/examples/I2STransmit/I2STransmit.ino diff --git a/libraries/esp8266/examples/I2STransmit/I2STransmit.ino b/libraries/esp8266/examples/I2STransmit/I2STransmit.ino new file mode 100644 index 0000000000..17c5cbd985 --- /dev/null +++ b/libraries/esp8266/examples/I2STransmit/I2STransmit.ino @@ -0,0 +1,75 @@ +/* + I2S stereo microphone (input) UDP transmitter + Needs a UDP listener (netcat/etc.) on port 8266 on the PC + + Under Linux: + nc -u -p 8266 -l | play -t raw -r 11025 -b 16 -c 2 -e signed-integer - + + Released to the Public Domain by Earle F. Philhower, III +*/ + +#include +#include +#include + +// Set your network here +const char *SSID = "...."; +const char *PASS = "...."; + +WiFiUDP udp; +// Set your listener PC's IP here: +const IPAddress listener = { 192, 168, 1, 2 }; +const int port = 8266; + +int16_t buffer[100][2]; // Temp staging for samples + +void setup() { + Serial.begin(115200); + + // Connect to WiFi network + Serial.println(); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(SSID); + + WiFi.begin(SSID, PASS); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.print("My IP: "); + Serial.println(WiFi.localIP()); + + i2s_rxtx_begin(true, false); // Enable I2S RX + i2s_set_rate(11025); + + Serial.print("\nStart the listener on "); + Serial.print(listener); + Serial.print(":"); + Serial.println(port); + Serial.println("ex: nc -u -p 8266 -l | play -t raw -r 11025 -b 16 -c 2 -e signed-integer -"); + + udp.beginPacket(listener, port); + udp.write("I2S Receiver\r\n"); + udp.endPacket(); + +} + +void loop() { + static int cnt = 0; + // Each loop will send 100 raw samples (400 bytes) + // UDP needs to be < TCP_MSS which can be 500 bytes in LWIP2 + for (int i=0; i<100; i++) { + i2s_read_sample(&buffer[i][0], &buffer[i][1], true); + } + udp.beginPacket(listener, port); + udp.write((uint8_t*)buffer, sizeof(buffer)); + udp.endPacket(); + cnt++; + if ((cnt % 100) == 0) { + Serial.printf("%d\n", cnt); + } +}