1 | /*
|
2 | #
|
3 | * Dieser Sketch sampelt Daten von einen ADC-Input, führt damit eine FFT-Analyse durch und überträgt das Ergebnis per UDP zu einem Server.
|
4 | // Core 1: Sampling: Core 0: FFT und UDP-Übertragung
|
5 | *
|
6 | */
|
7 |
|
8 | #include <WiFi.h>
|
9 | #include <WiFiUdp.h>
|
10 |
|
11 | #include <Wire.h>
|
12 | #include "arduinoFFT.h" // Standard Arduino FFT library https://github.com/kosme/arduinoFFT
|
13 | arduinoFFT FFT = arduinoFFT();
|
14 |
|
15 | const char* ssid = "FlyingDutchman";
|
16 | const char* password = "*********************************";
|
17 |
|
18 | const char* host = "192,168,178,24";
|
19 |
|
20 | int Verbunden;
|
21 | int pingResult;
|
22 |
|
23 | ////////////////////////////////////////////////////////////////
|
24 | #define SAMPLES 512 // Must be a power of 2
|
25 | #define SAMPLING_FREQUENCY 10000 // Hz, must be 40000 or less due to ADC conversion time. Determines maximum frequency that can be analysed by the FFT Fmax=sampleF/2.
|
26 | #define amplitude 150 // Depending on your audio source level, you may need to increase this value
|
27 | #define LED_GREEN 27
|
28 | #define mitte 2800
|
29 | #define oBufSize 1024
|
30 |
|
31 | const int httpPort = 50000;
|
32 | const int ownPort = 50000;
|
33 |
|
34 | unsigned int sampling_period_us;
|
35 | unsigned long microseconds;
|
36 | byte peak[] = {0,0,0,0,0,0,0,0};
|
37 | double t0_vReal[SAMPLES];
|
38 | double t0_vImag[SAMPLES];
|
39 | double t1_vReal[SAMPLES];
|
40 | double t1_vImag[SAMPLES];
|
41 | unsigned long newTime, oldTime;
|
42 | int dominant_value;
|
43 | byte oBuf[oBufSize];
|
44 | int erg;
|
45 | double maxi;
|
46 | int mini;
|
47 | byte tp;
|
48 | byte indi;
|
49 | long sum;
|
50 | int ave;
|
51 | long th;
|
52 | byte swi;
|
53 | byte swi2;
|
54 | long setPnt;
|
55 | byte setActive;
|
56 | long zh;
|
57 | byte prea[6] = {123,205,007,197,0,0};
|
58 | byte ack[2];
|
59 | long cnt;
|
60 | long mwe;
|
61 | long z1;
|
62 | long z2;
|
63 |
|
64 | TaskHandle_t Core0TaskHnd ;
|
65 |
|
66 | /////////////////////////////////////////////////////////////////////////
|
67 |
|
68 | WiFiClient client;
|
69 | WiFiUDP udp;
|
70 |
|
71 | void setup()
|
72 | {
|
73 |
|
74 | xTaskCreatePinnedToCore(CoreTask0,"CPU_0",1200,NULL,1,&Core0TaskHnd,0);
|
75 |
|
76 | sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
|
77 |
|
78 | Serial.begin(115200);
|
79 | delay(10);
|
80 |
|
81 | // We start by connecting to a WiFi network
|
82 |
|
83 | Serial.println();
|
84 | Serial.println();
|
85 | Serial.print("Connecting to ");
|
86 | Serial.println(ssid);
|
87 |
|
88 | WiFi.begin(ssid, password);
|
89 | int cnt=0;
|
90 | while (WiFi.status() != WL_CONNECTED) {
|
91 | delay(500);
|
92 | Serial.print(".");
|
93 | cnt++;
|
94 | if(cnt=10){
|
95 | break;
|
96 | }
|
97 | }
|
98 | delay(1000);
|
99 | Serial.print("Erneuter Versuch");
|
100 | WiFi.begin(ssid, password);
|
101 | while (WiFi.status() != WL_CONNECTED) {
|
102 | delay(500);
|
103 | Serial.print(".");
|
104 | }
|
105 |
|
106 | Serial.println("");
|
107 | Serial.println("WiFi connected");
|
108 | Serial.println("IP address: ");
|
109 | Serial.println(WiFi.localIP());
|
110 | delay(500);
|
111 | Serial.print("connecting to ");
|
112 | Serial.println(host);
|
113 | Serial.println("Version; FFT_UDP_02");
|
114 | pinMode(LED_GREEN, OUTPUT);
|
115 | digitalWrite(LED_GREEN, HIGH);
|
116 | int ret = udp.begin(ownPort);
|
117 | cnt = 0;
|
118 | Serial.println(" ");
|
119 | Serial.println("Vers.: Sprache Aufnehmen, FFT und Ergebnis nach PC (MSP32)");
|
120 | Serial.println("Auf PC starten auf Base, Button Start UDP");
|
121 | Serial.println("FFT_UDP_02");
|
122 | Serial.println(" ");
|
123 | }
|
124 |
|
125 | void loop()
|
126 | {
|
127 | sum = 0;
|
128 | for (int i = 0; i < SAMPLES; i++)
|
129 | {
|
130 | newTime = micros();
|
131 | int we = analogRead(A0); // Using Arduino ADC nomenclature. A conversion takes about 1uS on an ESP32
|
132 |
|
133 | // Fill the corresponding Buffer
|
134 | if(tp == 0)
|
135 | {
|
136 | t0_vReal[i] = we;
|
137 | t0_vImag[i] = 0;
|
138 | }
|
139 | else
|
140 | {
|
141 | t1_vReal[i] = we;
|
142 | t1_vImag[i] = 0;
|
143 | }
|
144 |
|
145 | //Update sum for average calculation
|
146 | if(we >= mitte)
|
147 | {
|
148 | sum = sum + (we - mitte);
|
149 | }
|
150 | else
|
151 | {
|
152 | sum = sum + (mitte - we);
|
153 | }
|
154 |
|
155 | while ((micros() - newTime) < sampling_period_us)
|
156 | { /* do nothing to wait */
|
157 | }
|
158 | }
|
159 |
|
160 | zh = micros();
|
161 | //calculate average
|
162 | ave = sum / SAMPLES;
|
163 | //Serial.println(ave,DEC);
|
164 |
|
165 | if(ave > 210)
|
166 | {
|
167 | digitalWrite(LED_GREEN, LOW);
|
168 | swi = 0;
|
169 | if(swi2 == 0)
|
170 | {
|
171 | // Trigger speach
|
172 | //Activate FFT-Processing
|
173 | swi2 = 1;
|
174 | setPnt = 0;
|
175 | setActive = 1;
|
176 | }
|
177 | }
|
178 | else
|
179 | {
|
180 | if(swi == 0)
|
181 | {
|
182 | swi = 1;
|
183 | th = micros();
|
184 | }
|
185 | else
|
186 | {
|
187 | if(micros() - th > 350000)
|
188 | {
|
189 | digitalWrite(LED_GREEN, HIGH);
|
190 | swi = 0;
|
191 | if(swi2 == 1)
|
192 | {
|
193 | // speech end detected
|
194 | swi2 = 0;
|
195 | // fill unused OBuf with Zerro
|
196 | setPnt = 0;
|
197 | //deactivate FFT-processing
|
198 | setActive = 0;
|
199 | }
|
200 | }
|
201 | }
|
202 | }
|
203 | //Serial.println(micros() - zh,DEC);
|
204 |
|
205 | //swap buffer-pointer
|
206 | tp = (tp-1) * -1;
|
207 | // set indi for data available
|
208 | indi = 1;
|
209 |
|
210 | //Serial.println(micros() - zh,DEC);
|
211 | }
|
212 |
|
213 | void CoreTask0( void * parameter )
|
214 | {
|
215 | for (;;)
|
216 | {
|
217 | //-----------------------------------------------------
|
218 | while(indi == 0)
|
219 | {
|
220 | vTaskDelay(10); // wait for Sample-Buffer
|
221 | }
|
222 | indi = 0;
|
223 | if(tp == 0)
|
224 | {
|
225 | FFT.Windowing(t1_vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
|
226 | FFT.Compute(t1_vReal, t1_vImag, SAMPLES, FFT_FORWARD);
|
227 | FFT.ComplexToMagnitude(t1_vReal, t1_vImag, SAMPLES);
|
228 | //peak = FFT.MajorPeak(t1_vReal, SAMPLES, SAMPLING_FREQUENCY);
|
229 |
|
230 | for(int i = 0; i < SAMPLES / 2; i++)
|
231 | {
|
232 | mwe = t1_vReal[i];
|
233 |
|
234 | oBuf[i * 4] = int(mwe / 16777216);
|
235 | z1 = mwe % 16777216;
|
236 | oBuf[i * 4 + 1] = int(z1 / 65536);
|
237 | z2 = z1 % 65536;
|
238 | oBuf[i * 4 + 2] = int(z2 / 256);
|
239 | oBuf[i * 4 + 3] = int(z2 % 256);
|
240 | }
|
241 | udp.beginPacket("192.168.178.24", httpPort);
|
242 | udp.write(oBuf, oBufSize);
|
243 | int ret = udp.endPacket();
|
244 | }
|
245 | else
|
246 | {
|
247 | FFT.Windowing(t0_vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
|
248 | FFT.Compute(t0_vReal, t0_vImag, SAMPLES, FFT_FORWARD);
|
249 | FFT.ComplexToMagnitude(t0_vReal, t0_vImag, SAMPLES);
|
250 | //peak = FFT.MajorPeak(t0_vReal, SAMPLES, SAMPLING_FREQUENCY);
|
251 |
|
252 | for(int i = 0; i < SAMPLES / 2; i++)
|
253 | {
|
254 | mwe = t0_vReal[i];
|
255 | oBuf[i * 4] = int(mwe / 16777216);
|
256 | z1 = mwe % 16777216;
|
257 | oBuf[i * 4 + 1] = int(z1 / 65536);
|
258 | z2 = z1 % 65536;
|
259 | oBuf[i * 4 + 2] = int(z2 / 256);
|
260 | oBuf[i * 4 + 3] = int(z2 % 256);
|
261 | }
|
262 | udp.beginPacket("192.168.178.24", httpPort);
|
263 | udp.write(oBuf, oBufSize);
|
264 | int ret = udp.endPacket();
|
265 | }
|
266 |
|
267 | //-----------------------------------------------------
|
268 |
|
269 | cnt++;
|
270 | if(cnt > 50)
|
271 | {
|
272 | Serial.println(maxi,DEC);
|
273 | cnt = 0;
|
274 | maxi = 0;
|
275 | }
|
276 | }
|
277 | }
|