基于 WEMOS-lolin32-lite 的录音设备。
Posted: Mon May 10, 2021 2:15 am
Code https://github.com/uk0/esp_32_wemos_lolin_lite_inmp441
以后会添加 热成像 以及 温度 湿度 风速 高度 等 模块,作为采集器,收集大自然信息
- #include <WiFi.h>
- #include <SPIFFS.h>
- #include <ArduinoWebsockets.h>
- #include "driver/i2s.h"
- #define I2S_WS_RX 27
- #define I2S_SCK_RX 14
- #define I2S_SD_RX 12
- /**
- #define I2S_WS_RX 27
- #define I2S_SCK_RX 14
- #define I2S_SD_RX 12
- #define I2S_WS_RX 15
- #define I2S_SCK_RX 2
- #define I2S_SD_RX 13
- */
- #define I2S_PORT I2S_NUM_1
- #define I2S_SAMPLE_RATE (16000)
- #define I2S_SAMPLE_BITS (32)
- #define I2S_READ_LEN (1024 * 4)
- #define I2S_CHANNEL_NUM (1)
- const char* ssid = "@PHICOMM_3E";
- const char* password = "asdasd110";
- const char* websockets_server_host = "192.168.2.40";
- const uint16_t websockets_server_port = 10086;
- TaskHandle_t i2sADCHandler = NULL;
- using namespace websockets;
- WebsocketsClient client;
- const i2s_config_t i2s_config_rx = {
- .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
- .sample_rate = I2S_SAMPLE_RATE,
- .bits_per_sample = i2s_bits_per_sample_t(I2S_SAMPLE_BITS),
- .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
- .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
- .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
- .dma_buf_count = 64,
- .dma_buf_len = 64
- };
- const i2s_pin_config_t pin_config_rx = {
- .bck_io_num = I2S_SCK_RX,
- .ws_io_num = I2S_WS_RX,
- .data_out_num = I2S_PIN_NO_CHANGE,
- .data_in_num = I2S_SD_RX
- };
- void setup() {
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- Serial.begin(115200);
- Serial.setDebugOutput(true);
- start_to_connect();
- }
- void start_to_connect(){
- Serial.println("===================================================================");
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(2000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());;
- client.onEvent(onEventsCallback);
- while(!client.connect(websockets_server_host, websockets_server_port, "/")){
- delay(100);
- Serial.print(".");
- }
- Serial.println("Socket Connected!");
- }
- void i2s_adc_data_scale(uint8_t * d_buff, uint8_t* s_buff, uint32_t len)
- {
- uint32_t j = 0;
- uint32_t dac_value = 0;
- for (int i = 0; i < len; i += 2) {
- dac_value = ((((uint16_t) (s_buff[i + 1] & 0xf) << 8) | ((s_buff[i + 0]))));
- d_buff[j++] = 0;
- d_buff[j++] = dac_value * 256 / 2048 ;
- }
- }
- static void i2s_adc_task(void *arg)
- {
- i2s_driver_install(I2S_NUM_1, &i2s_config_rx, 0, NULL);
- i2s_set_pin(I2S_NUM_1, &pin_config_rx);
- int i2s_read_len = I2S_READ_LEN;
- size_t bytes_read;
- char* i2s_read_buff = (char*) calloc(i2s_read_len, sizeof(char));
- uint8_t* flash_write_buff = (uint8_t*) calloc(i2s_read_len, sizeof(char));
- Serial.println(" *** Recording Start *** ");
- while (1) {
- i2s_read(I2S_PORT, (void*) i2s_read_buff, i2s_read_len, &bytes_read, portMAX_DELAY);
- i2s_adc_data_scale(flash_write_buff, (uint8_t*)i2s_read_buff, i2s_read_len);
- client.sendBinary((const char*)flash_write_buff, i2s_read_len);
- Serial.println("");
- Serial.print("SendBinary len = ");
- Serial.print(i2s_read_len);
- ets_printf("Never Used Stack Size: %u\n", uxTaskGetStackHighWaterMark(NULL));
- }
- free(i2s_read_buff);
- i2s_read_buff = NULL;
- free(flash_write_buff);
- flash_write_buff = NULL;
- }
- void loop() {
- }
- void onEventsCallback(WebsocketsEvent event, String data) {
- if(event == WebsocketsEvent::ConnectionOpened) {
- Serial.println("Connnection Opened");
- xTaskCreate(i2s_adc_task, "i2s_adc_task", 4096, NULL, 1, &i2sADCHandler);
- } else if(event == WebsocketsEvent::ConnectionClosed) {
- Serial.println("Connnection Closed");
- ESP.restart();
- }
- }
以后会添加 热成像 以及 温度 湿度 风速 高度 等 模块,作为采集器,收集大自然信息