|
|
@ -3,6 +3,14 @@
|
|
|
|
#include <driver/i2s.h>
|
|
|
|
#include <driver/i2s.h>
|
|
|
|
#include <M5Atom.h>
|
|
|
|
#include <M5Atom.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
|
|
|
#include <WiFiMulti.h>
|
|
|
|
|
|
|
|
#include <WiFiClientSecure.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <WebSocketsClient.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define CONFIG_I2S_BCK_PIN 19
|
|
|
|
#define CONFIG_I2S_BCK_PIN 19
|
|
|
|
#define CONFIG_I2S_LRCK_PIN 33
|
|
|
|
#define CONFIG_I2S_LRCK_PIN 33
|
|
|
|
#define CONFIG_I2S_DATA_PIN 22
|
|
|
|
#define CONFIG_I2S_DATA_PIN 22
|
|
|
@ -17,6 +25,24 @@
|
|
|
|
uint8_t microphonedata0[1024 * 70];
|
|
|
|
uint8_t microphonedata0[1024 * 70];
|
|
|
|
int data_offset = 0;
|
|
|
|
int data_offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WebSocketsClient webSocket;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
|
|
|
|
|
|
|
|
const uint8_t* src = (const uint8_t*) mem;
|
|
|
|
|
|
|
|
Serial.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
|
|
|
|
if (i % cols == 0) {
|
|
|
|
|
|
|
|
Serial.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.printf("%02X ", *src);
|
|
|
|
|
|
|
|
src++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.printf("\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InitI2SSpeakerOrMic(int mode) {
|
|
|
|
void InitI2SSpeakerOrMic(int mode) {
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
|
|
|
|
|
|
|
@ -64,12 +90,71 @@ void InitI2SSpeakerOrMic(int mode) {
|
|
|
|
I2S_CHANNEL_MONO);
|
|
|
|
I2S_CHANNEL_MONO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void speaker_play(uint8_t *payload, uint32_t len){
|
|
|
|
|
|
|
|
size_t bytes_written;
|
|
|
|
|
|
|
|
InitI2SSpeakerOrMic(MODE_SPK);
|
|
|
|
|
|
|
|
i2s_write(SPEAKER_I2S_NUMBER, payload, len,
|
|
|
|
|
|
|
|
&bytes_written, portMAX_DELAY);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case WStype_DISCONNECTED:
|
|
|
|
|
|
|
|
Serial.printf("[WSc] Disconnected!\n");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WStype_CONNECTED:
|
|
|
|
|
|
|
|
Serial.printf("[WSc] Connected to url: %s\n", payload);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// send message to server when Connected
|
|
|
|
|
|
|
|
webSocket.sendTXT("Connected");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WStype_TEXT:
|
|
|
|
|
|
|
|
Serial.printf("[WSc] get text: %s\n", payload);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// send message to server
|
|
|
|
|
|
|
|
// webSocket.sendTXT("message here");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WStype_BIN:
|
|
|
|
|
|
|
|
Serial.printf("[WSc] get binary length: %u\n", length);
|
|
|
|
|
|
|
|
// hexdump(payload, length);
|
|
|
|
|
|
|
|
speaker_play(payload, length);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// send data to server
|
|
|
|
|
|
|
|
// webSocket.sendBIN(payload, length);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WStype_ERROR:
|
|
|
|
|
|
|
|
case WStype_FRAGMENT_TEXT_START:
|
|
|
|
|
|
|
|
case WStype_FRAGMENT_BIN_START:
|
|
|
|
|
|
|
|
case WStype_FRAGMENT:
|
|
|
|
|
|
|
|
case WStype_FRAGMENT_FIN:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void websocket_setup() {
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
WiFi.begin("Soundview_Guest", "");
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED){
|
|
|
|
|
|
|
|
delay(500);
|
|
|
|
|
|
|
|
Serial.println("connecting to WiFi");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("connected to WiFi");
|
|
|
|
|
|
|
|
webSocket.begin("192.168.68.71", 9001, "/");
|
|
|
|
|
|
|
|
webSocket.onEvent(webSocketEvent);
|
|
|
|
|
|
|
|
// webSocket.setAuthorization("user", "Password");
|
|
|
|
|
|
|
|
webSocket.setReconnectInterval(5000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
void setup() {
|
|
|
|
void setup() {
|
|
|
|
M5.begin(true, false, true);
|
|
|
|
M5.begin(true, false, true);
|
|
|
|
M5.dis.drawpix(0, CRGB(128, 128, 0));
|
|
|
|
M5.dis.drawpix(0, CRGB(128, 128, 0));
|
|
|
|
|
|
|
|
websocket_setup();
|
|
|
|
delay(2000);
|
|
|
|
delay(2000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
void loop() {
|
|
|
|
if (M5.Btn.isPressed()) {
|
|
|
|
if (M5.Btn.isPressed()) {
|
|
|
|
data_offset = 0;
|
|
|
|
data_offset = 0;
|
|
|
@ -86,10 +171,21 @@ void loop() {
|
|
|
|
if (M5.Btn.isReleased() || data_offset >= 71679) break;
|
|
|
|
if (M5.Btn.isReleased() || data_offset >= 71679) break;
|
|
|
|
// delay(60);
|
|
|
|
// delay(60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t bytes_written;
|
|
|
|
|
|
|
|
InitI2SSpeakerOrMic(MODE_SPK);
|
|
|
|
Serial.println(data_offset);
|
|
|
|
i2s_write(SPEAKER_I2S_NUMBER, microphonedata0, data_offset,
|
|
|
|
|
|
|
|
&bytes_written, portMAX_DELAY);
|
|
|
|
int chunk_size = 1004;
|
|
|
|
|
|
|
|
chunk_size = 4096;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
webSocket.sendBIN(microphonedata0, data_offset);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// size_t bytes_written;
|
|
|
|
|
|
|
|
// InitI2SSpeakerOrMic(MODE_SPK);
|
|
|
|
|
|
|
|
// i2s_write(SPEAKER_I2S_NUMBER, microphonedata0, data_offset,
|
|
|
|
|
|
|
|
// &bytes_written, portMAX_DELAY);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
M5.update();
|
|
|
|
M5.update();
|
|
|
|
|
|
|
|
webSocket.loop();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|