Back to Home

Home-made USB whistle with microphone, STM32 and ESP8266 on board

Stm32 · Esp8266 · VoIP · IoT · microcontrollers

Home-made USB whistle with microphone, STM32 and ESP8266 on board

    imageI want to talk about my small weekend project, to highlight the difficulties that I had to face, further plans and get advice from more experienced Habrausers. The format of the article is a survey, so to speak, at a gallop across Europe. If there is interest, I will go into more detail about the individual parts.

    Briefly describe the operation of the device as follows:

    Electret microphone → Amplifier Max9812 → Microcontroller STM32F103 → Software for detecting the presence of sound → Encoding sound in Speex → Sending via ESP8266 to the server → PHP script for receiving data and processing into an Ogg sound container. Who cares, please, under the cat.

    Why is all this necessary?

    The very idea of ​​a device came to my birth daughter. In the future I want to make something like a baby monitor: I plugged it into a socket next to a crib, and you watch a TV show in another room, then there’s a hop - a notification has arrived on your phone and you can listen to what is happening there. But on the implementation of the last paragraphs, I have more questions than answers. More about this later.

    Perhaps someone will see another use for such a craft, but I assure you that the device is not created for the purpose of secretly obtaining information (Article 13.1.1 of the Criminal Code) and any such attempt will be prosecuted .

    I’ll ask you not to criticize the idea right away, I need to work with sound and Wi-Fi in another project, which I’m only thinking about. And this is a separate piece, implemented in an independent device.

    Housing

    imageOn the basis of the “Design first” principle, I began to think in which construct to make the device. In search of ready-made cases on Ali, I came across a usb case with a hole for the cable and thought to myself that this is ideal - a microphone will take a hole and shove the rest somehow.

    But I didn’t want to order 10 pieces for $ 5 and started looking for options. As a result, I ordered a USB-RS485 adapter in just such a case for $ 0.84. And the adapter itself will come in handy at work, otherwise Bolids, worth 1.5 thousand rubles, go away as consumables.

    We buy everything you need

    Firstly, when familiarizing myself with the subject area of ​​the upcoming craft, I came across an article Speech Recognition on STM32F4-Discovery. There I read about the Speex codec and its use on microcontrollers. I admit, this is my first experience with STM microcontrollers.

    imageSo, we order the most common debugging with the STM32F103C8T6 microcontroller, to it the St-link v2 debugger, we finish it on the knee and you can already live. According to rough estimates of resources, the STM32F103C8T6 should have been enough, but speex is very voracious, and the HAL drivers themselves are not so compact, in general, the memory for all the libraries was not enough. The final device costs STM32F103CBT6 with twice the amount of flash.

    Secondly, you need a microphone. The first line in the search is a microphone module with a max9812 amplifier, and in the appendage a handful of more miniature microphones of size 6050 (after all, we already have a case with a hole where this microphone should fit).

    Thirdly, you need to choose a wi-fi module, so that it fits into the purchased case. The choice was made on a miniature ESP-03 with a ceramic antenna and several pieces of ESP-12 for prototyping.

    Prototyping and programming

    image In STM32CubeMX we quickly collect the necessary peripherals and move on, in the process of work you will still have to often return to Cuba. The main thing here is to write the code in specially designated places / * USER CODE * / and then the new project generation will not affect the already written one.

    ADC sampling is performed via DMA via a timer trigger at a frequency of 8 kHz into two ring buffers of 160 samples in size, one buffer being equal to 20 ms. Faced a moment that I did not know and lost at that time: DMA continues to work on the debugger breakpoint, with both the HT (half transmission) and TC (transmission completed) flags set, and the buffers are always full.

    I didn’t understand Speex library right away, the controller constantly crashed into HardFault. It turned out that there was simply not enough stack and heap sizes. Found Application Note from Silicon Labs with a description of the necessary resources for encoding / decoding, at the end of the article there is a link. Set values ​​with a small margin of CSTACK 0x800 and HEAP 0x1600. At the coding output, we get a frame size of 20 bytes. We collect them in packages for sending.

    The Esp8266 module can accept no more than 2048 bytes of data at a time. The format of the send command: POST-header + data. I limited the data packet size to 1800 bytes (90 frames of 20ms each).

    Receive data made in PHP. I was ashamed to upload the code, especially since this is my first OOP experience in my life. Please do not hate, this is not my area, it’s better to teach how to do it right. The essence of the script is to take data from php: // input, create a header, calculate the necessary checksums and save everything to an Ogg file (either add the old one or create a new one). But I have a terrible feeling that accepting audio via POST requests is very stupid ...

    Question to the community: what do you recommend for the server side? In the future, I want to receive a stream of audio in real time. I personally have a slight desire to meet Node.js.

    And on github have a JS library speex decoding on the fly, but do not know how to use it. Perhaps someone will tell you how to work with this. But there’s another question: will iOS play this? An example of the recorded sound is in the project repository on Github .

    Schematic diagram and wiring of the printed circuit board I

    painted the circuit and wiring in the free version of Eagle CAD, since the board is small. Here they are. No comments.

    image

    image

    Custom boards

    image boards ordered in an American OSH Park. What is remarkable about this service is the fact that the price is formed on the size of the board ($ 5 per square inch), and delivery is free. Everything else, you can upload the .brd file from Orel to the site and there is a preview, you do not have to transfer it to Gerber.

    It turned out 3 boards for $ 3.35. For a prototype, this is more profitable than overpaying the Chinese $ 15 for unnecessary 10pcs. Still at home, there are mountains of unnecessary boards from other projects. And besides, it all came in a corporate package with sweets inside. A trifle, but nice. The track number was not, it reached 40 days from the date of order. There were 10 days in production (from December 29 to January 9, perhaps the holidays somehow affected the timing).

    I was satisfied with the quality of the boards. Lanes 8 mil. Transitional 13 mil. The boards came with gilding, the mask layer relative to the sites is always flat.

    Assembly and setup I have

    not yet understood what’s the matter, but the Esp-03 module practically doesn’t pick up a signal, which is strange in the presence of a ceramic antenna and the distance to the phone (which acts as a wi-fi distributor at work) within a half meter, while the module Heats up significantly. When the phone is brought up, it starts to detect the network and connects to it. Order a new one or what could be the matter?

    image

    Future plans

    1. To refine the VAD (Voice Activity Detection, recording when sound is detected) algorithm to a more adequate one.
    2. Check the ability to play sound on the phone.
    3. Make it possible to configure remotely. Now this is implemented by the server’s response (Settings = 40,2000,10,), where the sensitivity of the current VAD algorithm (the signal should exceed 40 buboks 40 times) and the recording duration in seconds are set.

    Final version

    image

    Costing
    $ 2.03 - ESP-03 module
    $ 2.13 - microcontroller STM32F103CBT6
    $ 0.39 - microphones of size 6050
    $ 1.12 - microphone module with MAX9812 and strapping
    $ 0.84 - USB-RS485 converter (case + usb type A male)
    $ 0.50 - stabilizer NCP3335A to

    7.01 Total: (~ 420 rubles) for one device.

    Prices may vary due to recent developments on Ali. The cost does not include the programmer and loose.

    Materials

    1.Repository with a project on Github
    There are projects CubeMX, IAR, Eagle, PHP, as well as an example of recorded sound. Perhaps for playback you will need drivers, but I don’t remember what and in what order I installed, look on the Internet.
    2. Speex Codec Manual
    3. Application Note from Silicon Labs

    Thank you for your attention!

    Questions and suggestions write in the comments. Contact or telegrams are also allowed, nickname is everywhere alone.

    Continuation: Sound digitization on STM32 (ADC + DMA) and Speex encoding for transmission

    Only registered users can participate in the survey. Please come in.

    Poll: About what to write the following article?

    • 34.7% Read more about digitizing sound (ADC + DMA) and encoding in Speex 99
    • 12.2% Finalization of Chinese ST-link v2 by adding swo and reset 35
    • 13.3% Working with STM32CubeMX, flashing, debugging in IAR 38
    • 27% PCB layout in Eagle Cad, production order 77
    • 6.6% Making Audio Data into an Ogg 19 Container
    • 5.9% No thanks, no more 17

    Read Next