
I want to tell about my small project of the day off, highlight the difficulties that I had to face, further plans and get advice from more experienced Habrayusers. The format of the article survey, so to speak, at a gallop across Europe. If there is interest, I will go more in detail on separate parts.
Briefly describe the operation of the device can be as follows:
Electret Microphone → Max9812 Amplifier → STM32F103 Microcontroller → Software Detection of Audio Presence → Audio Encoding in Speex → Sending to ESP8266 to Server → PHP Data Acquisition Script and Formatting in Ogg Audio Container. Who cares, I ask under the cat.
Why is all this necessary?
')
The very idea of ​​the device I came to the birth of a daughter. In the future, I want to make some kind of baby monitor: I plug it into the outlet next to the baby cot, and you watch the TV serial in another room, then the hop - a notification message came to the phone and you can listen to what is happening there. But on the implementation of the last points I have more questions than answers. More on that later.
Perhaps someone will see another use for such an article, but I assure you that the
device is not created for the purpose of secretly obtaining information (Art.138.1 of the Criminal Code of the Russian Federation) and any such attempt will be prosecuted by law .
I’ll ask you not to criticize the idea right away, I need to work with sound and Wi-Fi in another project that I’m just thinking about. And this is a separate piece, implemented into an independent device.
Housing

According to the “Design first” principle, I began to think about how to make a device. In search of ready-made cases, Ali stumbled upon cases for usb with a cable hole and thought to himself that this was an ideal option - a microphone would take up a hole, and let us stick the rest somehow.
Only here I did not want to order 10pcs for $ 5 and started looking for options. As a result, I ordered the USB-RS485 adapter in exactly this package for $ 0.84. And the adapter itself is useful at work, and then the Bolids, worth 1.5 thousand rubles, go as an expendable material.
We buy everything you need
First, when I got acquainted with the subject area of ​​the upcoming crafts, I came across the article
Speech Recognition on STM32F4-Discovery . There I read about the Speex codec and its application on microcontrollers. I admit, this is my first experience with STM microcontrollers.

So, we order the most common debugging with the STM32F103C8T6 microcontroller, the St-link v2 debugger to it, we finish it on the knee and we can already live. According to rough estimates, the resources of the STM32F103C8T6 should have been enough, but speex is very voracious, and the HAL drivers themselves are not that compact, in general the memory for all libraries was not enough. The final device is STM32F103CBT6 with double the number of flash.
Secondly, you need a microphone. We take the max9812 microphone module with the first line in the search, and in the appendage there is a handful of more miniature microphones of the standard size 6050 (after all, we already have a case with an opening where this microphone should go).
Thirdly, you need to pick up the module wi-fi, but such that got into the purchased case. The choice was made on a miniature ESP-03 with a ceramic antenna and a few pieces of ESP-12 for prototyping.
Layout and programming

In STM32CubeMX, we quickly assemble the necessary peripherals and move on; in the process, we still often have to go back to Cuba. The main thing here is to write code in specially designated places / * USER CODE * / and then the new generation of the project will not affect the already written one.
Sampling of the ADC goes through the DMA on the timer trigger at a frequency of 8 kHz into two circular buffers with a size of 160 samples, one buffer is obtained equal to 20 ms. Faced a moment that I did not know and lost time on it: DMA continues to work on the debugger breakpoint, with both the HT (half transfer) and TC (transfer complete) flags set, and the buffers are always full.
Speex did not immediately understand the library, the controller constantly flew to HardFault. It turned out just not enough stack sizes and heaps. 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. Send command format: POST header + data. I limited the size of the data packet to 1,800 bytes (90 frames by 20 ms).
Received data made in PHP. I was ashamed to upload the code, especially since this is my first experience of the PLO in life. Please do not heytit, this is not my area, better teach how to properly. The essence of the script is to take data from php: // input, form a header, calculate the necessary checksums and save everything to the Ogg file (either finish the old one or create a new one). But I have a terrible premonition that it is very stupid to receive audio through POST requests ...
Question to the community: what do you recommend for the server part? In the future, I want to receive stream audio in real time. I personally have a slight desire to get acquainted with Node.js.
And on github there is a speex
JS decoding library on the fly, but I don’t know how to use it yet. Maybe someone will tell you how to work with it. But here's another question: will iOS play it? An example of a recorded sound is in
the project repository on Github .
Schematic diagram and PCB layout
I drew the scheme and layout in the free version of Eagle CAD, since the board size is small. Here they are. No comments.
Order boards

Boards ordered in American
OSH Park . What is remarkable about this service is the fact that the price is based on the size of the board ($ 5 per square inch), and the delivery is free. On top of that, you can load the .brd file itself from the Eagle to the site and there is a preview, you do not need to overtake it in Gerber.
It turned out 3 fees for $ 3.35. For the mock-up sample, it is more profitable than to overpay the Chinese for $ 15 for unnecessary 10pcs. Until now, houses are lying mountains of unnecessary fees from other projects. And besides, it all came in a corporate package with candies inside. Trifle, but nice. The track number was not 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 pleased with the quality of the boards. Tracks 8 mil. Transient 13 mil. The boards came with gilding, the layer of the mask relative to the sites is flat everywhere.
Build and configure
I still do not understand what is the matter, but the Esp-03 module practically does not catch the signal, which is strange in the presence of a ceramic antenna and the distance to the phone (which at work acts as a wi-fi distributor) within half a meter, while the module heats up noticeably. When you bring the phone close starts to detect the network and connects to it. Order a new or what could be the case?
Future plans
1. Modify the VAD algorithm (Voice Activity Detection, recording when sound is detected) to a more adequate one.
2. Check the ability to play sound on the phone.
3. Make it possible to configure remotely. Now it is implemented by a server response (Settings = 40.2000.10,), where the sensitivity of the current VAD algorithm is set (the signal should exceed 2000 bubook 40 times) and the recording duration in seconds.
Final option
Costing
$ 2.03 - ESP-03 Module
$ 2.13 - STM32F103CBT6 microcontroller
$ 0.39 - Microphones size 6050
$ 1.12 - microphone module with MAX9812 and strapping
$ 0.84 - USB-RS485 converter (case + usb type A male)
$ 0.50 - NCP3335A 3.3V Stabilizer
Total: $ 7.01 (~ 420 rubles) for one device.
Prices may vary due to the latest developments on Ali. The cost does not include the programmer and spreading.
Materials
1.
The repository with the project on Github
There are projects CubeMX, IAR, Eagle, PHP, as well as an example of recorded sound. You may need drivers for playback, but I don’t remember what I put in what order, look on the Internet.
2.
Speex Codec Manual
3.
Application Note from Silicon Labs
Thanks for attention!
Questions and suggestions write to the comments. Also contact or telegram is allowed, there is only one nickname.
Continued: Audio digitization on STM32 (ADC + DMA) and Speex encoding for transmission