基于 WEMOS-lolin32-lite 的录音设备。

firshme
Posts: 1
Joined: Mon May 10, 2021 2:12 am

基于 WEMOS-lolin32-lite 的录音设备。

Postby firshme » Mon May 10, 2021 2:15 am

Code https://github.com/uk0/esp_32_wemos_lolin_lite_inmp441




  1.  
  2. #include <WiFi.h>
  3. #include <SPIFFS.h>
  4. #include <ArduinoWebsockets.h>
  5. #include "driver/i2s.h"
  6.  
  7.  
  8. #define I2S_WS_RX  27
  9. #define I2S_SCK_RX 14
  10. #define I2S_SD_RX  12
  11. /**
  12. #define I2S_WS_RX  27
  13. #define I2S_SCK_RX 14
  14. #define I2S_SD_RX  12
  15. #define I2S_WS_RX  15
  16. #define I2S_SCK_RX 2
  17. #define I2S_SD_RX  13
  18. */
  19.  
  20. #define I2S_PORT I2S_NUM_1
  21. #define I2S_SAMPLE_RATE   (16000)
  22. #define I2S_SAMPLE_BITS   (32)
  23. #define I2S_READ_LEN      (1024 * 4)
  24. #define I2S_CHANNEL_NUM   (1)
  25.  
  26.  
  27. const char* ssid = "@PHICOMM_3E";
  28. const char* password = "asdasd110";
  29.  
  30. const char* websockets_server_host = "192.168.2.40";
  31. const uint16_t websockets_server_port = 10086;
  32.  
  33. TaskHandle_t i2sADCHandler = NULL;
  34.  
  35. using namespace websockets;
  36. WebsocketsClient client;
  37.  
  38. const i2s_config_t i2s_config_rx = {
  39.   .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
  40.   .sample_rate = I2S_SAMPLE_RATE,
  41.   .bits_per_sample = i2s_bits_per_sample_t(I2S_SAMPLE_BITS),
  42.   .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  43.   .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
  44.     .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  45.     .dma_buf_count = 64,
  46.     .dma_buf_len = 64
  47. };
  48.  
  49. const i2s_pin_config_t pin_config_rx = {
  50.   .bck_io_num = I2S_SCK_RX,
  51.   .ws_io_num = I2S_WS_RX,
  52.   .data_out_num = I2S_PIN_NO_CHANGE,
  53.   .data_in_num = I2S_SD_RX
  54. };
  55.  
  56. void setup() {
  57.  
  58.    WiFi.mode(WIFI_STA);
  59.    WiFi.disconnect();
  60.    Serial.begin(115200);
  61.    Serial.setDebugOutput(true);
  62.    start_to_connect();
  63. }
  64.  
  65. void start_to_connect(){
  66.   Serial.println("===================================================================");
  67.   WiFi.begin(ssid, password);
  68.   while (WiFi.status() != WL_CONNECTED) {
  69.     delay(2000);
  70.     Serial.print(".");
  71.   }
  72.   Serial.println("");
  73.  
  74.   Serial.println("WiFi connected");
  75.   Serial.println("IP address: ");
  76.   Serial.println(WiFi.localIP());;
  77.  
  78.   client.onEvent(onEventsCallback);
  79.   while(!client.connect(websockets_server_host, websockets_server_port, "/")){
  80.     delay(100);
  81.     Serial.print(".");
  82.   }
  83.  
  84.   Serial.println("Socket Connected!");
  85. }
  86.  
  87. void i2s_adc_data_scale(uint8_t * d_buff, uint8_t* s_buff, uint32_t len)
  88. {
  89.     uint32_t j = 0;
  90.     uint32_t dac_value = 0;
  91.     for (int i = 0; i < len; i += 2) {
  92.         dac_value = ((((uint16_t) (s_buff[i + 1] & 0xf) << 8) | ((s_buff[i + 0]))));
  93.         d_buff[j++] = 0;
  94.         d_buff[j++] = dac_value * 256 / 2048 ;
  95.     }
  96. }
  97.  
  98. static void i2s_adc_task(void *arg)
  99. {
  100.     i2s_driver_install(I2S_NUM_1, &i2s_config_rx, 0, NULL);
  101.     i2s_set_pin(I2S_NUM_1, &pin_config_rx);
  102.  
  103.     int i2s_read_len = I2S_READ_LEN;
  104.     size_t bytes_read;
  105.  
  106.     char* i2s_read_buff = (char*) calloc(i2s_read_len, sizeof(char));
  107.     uint8_t* flash_write_buff = (uint8_t*) calloc(i2s_read_len, sizeof(char));
  108.     Serial.println(" *** Recording Start *** ");
  109.     while (1) {
  110.         i2s_read(I2S_PORT, (void*) i2s_read_buff, i2s_read_len, &bytes_read, portMAX_DELAY);
  111.         i2s_adc_data_scale(flash_write_buff, (uint8_t*)i2s_read_buff, i2s_read_len);
  112.         client.sendBinary((const char*)flash_write_buff, i2s_read_len);
  113.         Serial.println("");
  114.         Serial.print("SendBinary  len = ");
  115.         Serial.print(i2s_read_len);
  116.         ets_printf("Never Used Stack Size: %u\n", uxTaskGetStackHighWaterMark(NULL));
  117.     }
  118.  
  119.     free(i2s_read_buff);
  120.     i2s_read_buff = NULL;
  121.     free(flash_write_buff);
  122.     flash_write_buff = NULL;
  123. }
  124.  
  125. void loop() {
  126.  
  127. }
  128.  
  129. void onEventsCallback(WebsocketsEvent event, String data) {
  130.     if(event == WebsocketsEvent::ConnectionOpened) {
  131.         Serial.println("Connnection Opened");
  132.         xTaskCreate(i2s_adc_task, "i2s_adc_task", 4096, NULL, 1, &i2sADCHandler);
  133.     } else if(event == WebsocketsEvent::ConnectionClosed) {
  134.         Serial.println("Connnection Closed");
  135.         ESP.restart();
  136.     }
  137. }
  138.  
  139.  


以后会添加 热成像 以及 温度 湿度 风速 高度 等 模块,作为采集器,收集大自然信息

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: 基于 WEMOS-lolin32-lite 的录音设备。

Postby ESP_Gargamel » Mon May 10, 2021 2:22 am

感谢分享,看上去这是个分享,所以我打算把这个贴移到:https://www.esp32.com/viewforum.php?f=36

Who is online

Users browsing this forum: No registered users and 3 guests