# Smart Internet of Things Power Meter for Industrial and Domestic Applications
## 3 Proposed Solution
### Software Architecture
#### 3.2.1 Sampled Data Storage and FFT Library Selection  
The ADS131M04 ADC used in this device has a 24 bit resolution and a programmable sample rate between 250 SPS and 64 kSPS (kilo Samples Per Second). Given the 50 Hz frequency of the grid signal and the imposed requirement to analyze up the to 40 th harmonic of the signal, for a correct FFT analysis, a minimum sample rate of 4 kSPS is necessary (40 th harmonic frequency is 40 \(\times\) 50 Hz = 2000 Hz but according to the Nyquist theorem, the sample rate should be at least twice this value, resulting in a minimum of 4000 Hz sampling frequency). In this case, the FFT produces 2000 frequency bins with a resolution of 1 Hz. According to the electrical standards [22], the grid frequency can vary between 49.8 and 50.2 Hz, and in this case, the sampled signal frequency components may not align perfectly with the FFT bins, causing a spectral leakage [23]. To overcome this issue, we can increase the sampling rate to 8 kSPS or 16 kSPS, achieving a resolution of 0.5 Hz or 0.25 Hz, respectively. Using this method, and applying a windowing function, helps in increasing the accuracy of harmonic amplitude estimation.  
First, we need to store the voltage and current samples in the microcontroller's memory before any computing occurs. For a given sampling period of 1 s, a sample rate of 8 kSPS, and the 24 bit resolution of the ADC, two arrays of 24,000 bytes are needed, one for each signal. According to the ESP32 documentation [24], \"due to a technical limitation, the maximum statically allocated DRAM usage is 160 KB\". Moreover, the memory of the ESP32 is fragmented, making it impossible in some cases to allocate large-sized arrays. We came across this problem because the FFT libraries use large float buffers, usually with the dimension of the input data array. For example, in our case, for the same 1 s period and 8 kSPS sampling rate, at least two arrays of 8000 float variables would be needed for the FFT library input and output buffers. A float variable is represented using 32 bits (4 bytes), making it necessary to use 64,000 bytes only for the two arrays. To overcome this issue, we used PSRAM memory allocation for all the buffers, this feature being one of the main reasons why we selected the ESP32-Wrover model. We allocated memory for all the large buffers used in our algorithm and continued the initialization of the library used for the FFT analysis.  
To select a suitable FFT library, we tested several commonly used libraries available in the Arduino Library Manager: ArduinoFFT, ESP32 FFT, Fastdier, etc. Some of these were developed specifically for ESP32, while others were designed for different architectures (such as AVR) and were adapted by us to run on our microcontroller. The detailed testing procedure and the results obtained constitute the subject of another paper currently under development. We opted for the ESP32 FFT by [PERSON] library [25] because it produced the FFT analysis result in the shortest time compared to the other libraries tested. Additionally, it provides accurate estimations of harmonic amplitudes and fundamental frequency. Running this library on the ESP32-Wrover microcontroller, the average time required to finish the FFT analysis on a signal sampled at 8 kSPS for 1 s is 52.4 ms. The fundamental harmonic amplitude estimation error is 6.53%, and the frequency estimation error is 0.39%.