Skip to content

Commit cce11ab

Browse files
committed
Update LED signals, fix small bugs inthe client
1 parent f59ba2c commit cce11ab

File tree

2 files changed

+145
-125
lines changed

2 files changed

+145
-125
lines changed

sb_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
def on_message(ws, message):
1414
bandJson = json.loads(message)
15-
print('[ESP] Band amplitude measurments: ' + bandJson)
15+
print(f'[ESP] Band amplitude measurments: {bandJson}')
1616

1717
def on_close(ws, close_status_code, close_msg):
1818
print("[ESP] CLOSED")

sketch_fft/sketch_fft.ino

Lines changed: 144 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
#define SAMPLING_FREQ 38000 // Hz, must be 40000 or less due to ADC conversion time. Determines maximum frequency that can be analysed by the FFT Fmax=sampleF/2.
1313
#define AMPLITUDE 1000 // Depending on your audio source level, you may need to alter this value. Can be used as a 'sensitivity' control.
1414
#define AUDIO_IN_PIN 35 // Signal in on this pin
15-
1615
#define NUM_BANDS 8 // Number of frequency bands to use
17-
1816
#define NOISE 600 // Used as a crude noise filter, values below this are ignored
1917

2018
unsigned int sampling_period_us;
21-
byte peak[] = {0,0,0,0,0,0,0,0}; // The length of these arrays must be >= NUM_BANDS
2219
int bandValues[] = {0,0,0,0,0,0,0,0};
2320
double vReal[SAMPLES];
2421
double vImag[SAMPLES];
@@ -28,11 +25,8 @@ arduinoFFT FFT = arduinoFFT(vReal, vImag, SAMPLES, SAMPLING_FREQ);
2825
// WIFI server stuff
2926
const char* ssid = WIFI_SSID;
3027
const char* password = WIFI_PASSWORD;
31-
3228
WebSocketsServer server = WebSocketsServer(80);
33-
3429
StaticJsonDocument<500> sendJson;
35-
StaticJsonDocument<500> receiveJson;
3630

3731
// Supabase stuff
3832
Supabase db;
@@ -53,107 +47,7 @@ Adafruit_NeoPixel rgbWS = Adafruit_NeoPixel(numLED, pinDIN, NEO_GRB + NEO_KHZ800
5347
int start_time;
5448
bool perfrom_fft = false;
5549

56-
// Called when receiving any WebSocket message
57-
void onWebSocketEvent(uint8_t num,
58-
WStype_t type,
59-
uint8_t * payload,
60-
size_t length) {
61-
62-
// Figure out the type of WebSocket event
63-
switch(type) {
64-
65-
// Client has disconnected
66-
case WStype_DISCONNECTED:
67-
Serial.printf("[%u] Disconnected!\n", num);
68-
break;
69-
70-
// New client has connected
71-
case WStype_CONNECTED:
72-
{
73-
IPAddress ip = server.remoteIP(num);
74-
Serial.printf("[%u] Connection from ", num);
75-
Serial.println(ip.toString());
76-
}
77-
break;
78-
79-
// Echo text message back to client
80-
case WStype_TEXT:
81-
{
82-
String recieved_text = (char*) payload;
83-
84-
if(recieved_text == "STOP") {
85-
if(perfrom_fft == false) {
86-
Serial.println("Recieved stop message, but FFT is already stopped");
87-
break;
88-
}
89-
90-
perfrom_fft = false;
91-
Serial.println("Recieved stop message, stopping FFT");
92-
// Create new row with stop time
93-
String jsonOutString = "";
94-
JsonObject sendObject = sendJson.to<JsonObject>();
95-
float duration = (millis() - start_time) / 1000;
96-
sendObject["duration"] = duration;
97-
98-
if(duration > 10) {
99-
sendObject["is_music"] = true;
100-
}
101-
else {
102-
sendObject["is_music"] = false;
103-
}
104-
105-
serializeJson(sendObject, jsonOutString);
106-
107-
int HTTPresponseCode = db.insert(sb_table, jsonOutString, upsert);
108-
Serial.print("HTTP response code (SupaBase-insert): ");
109-
Serial.println(HTTPresponseCode);
110-
db.urlQuery_reset();
111-
112-
// neoPixel turn of the lights
113-
for(int i = 0; i < numLED; i++) {
114-
setRGB(0, 0, 0, i);
115-
}
116-
delay(500);
117-
} else if(recieved_text == "START") {
118-
if(perfrom_fft == true) {
119-
Serial.println("Recieved start message, but FFT is already started");
120-
break;
121-
}
122-
perfrom_fft = true;
123-
start_time = millis();
124-
125-
Serial.println("Recieved start message, starting FFT");
126-
127-
// neoPixel new connection indicator
128-
for(int i = 0; i < numLED; i++) {
129-
setRGB(255, 255, 255, i);
130-
delay(100);
131-
}
132-
delay(500);
133-
for(int i = 0; i < numLED; i++) {
134-
setRGB(0, 0, 0, i);
135-
}
136-
delay(500);
137-
}
138-
else {
139-
Serial.println("Recieved unknown message");
140-
}
141-
}
142-
break;
143-
144-
// For everything else: do nothing
145-
case WStype_BIN:
146-
case WStype_ERROR:
147-
case WStype_FRAGMENT_TEXT_START:
148-
case WStype_FRAGMENT_BIN_START:
149-
case WStype_FRAGMENT:
150-
case WStype_FRAGMENT_FIN:
151-
default:
152-
break;
153-
}
154-
}
155-
156-
50+
// -----------------------------------------------------------------MAIN FUNCTIONS-----------------------------------------------------------------
15751
void setup() {
15852
Serial.begin(115200);
15953
// FFT sampling period
@@ -184,29 +78,17 @@ void setup() {
18478
delay(1000);
18579

18680
// Supabase
187-
Serial.print("Connecting to Supabase .");
81+
Serial.print("Connecting to Supabase...");
18882
db.begin(sb_url, sb_api_key);
83+
Serial.println(" Connected");
18984

19085
start_time = millis();
19186

19287
delay(1000);
19388

19489
// NeoPixel
19590
rgbWS.begin();
196-
// 1 -> red color
197-
setRGB(255, 0 , 0 , 0);
198-
delay(500);
199-
// 2 -> green color
200-
setRGB(0 , 255, 0 , 1);
201-
delay(500);
202-
// 3 -> blue color
203-
setRGB(0 , 0 , 255, 2);
204-
delay(500);
205-
// turn them of - black color
206-
setRGB(0 , 0 , 0 , 0);
207-
setRGB(0 , 0 , 0 , 1);
208-
setRGB(0 , 0 , 0 , 2);
209-
delay(500);
91+
signalRGB('w');
21092
}
21193

21294
void loop() {
@@ -278,7 +160,7 @@ void loop() {
278160
}
279161
}
280162
}
281-
163+
// -----------------------------------------------------------------FFT FUNCTIONS-----------------------------------------------------------------
282164
bool performFFT() {
283165
// Reset bandValues[]
284166
for (int i = 0; i < NUM_BANDS; i++){
@@ -319,7 +201,7 @@ bool performFFT() {
319201

320202
return notNoise;
321203
}
322-
204+
// -----------------------------------------------------------------NeoPixel FUNCTIONS-----------------------------------------------------------------
323205
void setRGB(uint8_t r, uint8_t g, uint8_t b, int led_number) {
324206
uint32_t color = rgbWS.Color(r, g, b);
325207
// nastavení barvy pro danou LED diodu,
@@ -328,3 +210,141 @@ void setRGB(uint8_t r, uint8_t g, uint8_t b, int led_number) {
328210
// aktualizace barev na všech modulech
329211
rgbWS.show();
330212
}
213+
214+
void signalRGB(char color){
215+
uint8_t r, g, b;
216+
switch (color)
217+
{
218+
case 'r':
219+
r = 255;
220+
g = 0;
221+
b = 0;
222+
break;
223+
case 'g':
224+
r = 0;
225+
g = 255;
226+
b = 0;
227+
break;
228+
case 'b':
229+
r = 0;
230+
g = 0;
231+
b = 255;
232+
break;
233+
case 'w':
234+
r = 255;
235+
g = 255;
236+
b = 255;
237+
break;
238+
default:
239+
r = 0;
240+
g = 0;
241+
b = 0;
242+
break;
243+
}
244+
for(int i = 0; i < numLED; i++) {
245+
setRGB(r, g, b, i);
246+
delay(100);
247+
}
248+
delay(1000);
249+
for(int i = 0; i < numLED; i++) {
250+
setRGB(0, 0, 0, i);
251+
}
252+
}
253+
// -----------------------------------------------------------------WEBSERVER FUNCTIONS-----------------------------------------------------------------
254+
// Called when receiving any WebSocket message
255+
void onWebSocketEvent(uint8_t num,
256+
WStype_t type,
257+
uint8_t * payload,
258+
size_t length) {
259+
260+
// Figure out the type of WebSocket event
261+
switch(type) {
262+
263+
// Client has disconnected
264+
case WStype_DISCONNECTED:
265+
{
266+
Serial.printf("[%u] Disconnected!\n", num);
267+
signalRGB('r');
268+
break;
269+
}
270+
271+
// New client has connected
272+
case WStype_CONNECTED:
273+
{
274+
IPAddress ip = server.remoteIP(num);
275+
Serial.printf("[%u] Connection from ", num);
276+
Serial.println(ip.toString());
277+
278+
signalRGB('g');
279+
}
280+
break;
281+
282+
// Echo text message back to client
283+
case WStype_TEXT:
284+
{
285+
String recieved_text = (char*) payload;
286+
287+
if(recieved_text == "STOP") {
288+
if(perfrom_fft == false) {
289+
Serial.println("Recieved stop message, but FFT is already stopped");
290+
break;
291+
}
292+
293+
perfrom_fft = false;
294+
Serial.println("Recieved stop message, stopping FFT");
295+
// Create new row in Supabase database
296+
String jsonOutString = "";
297+
JsonObject sendObject = sendJson.to<JsonObject>();
298+
float duration = (millis() - start_time) / 1000;
299+
sendObject["duration"] = duration;
300+
301+
if(duration > 10) {
302+
sendObject["is_music"] = true;
303+
}
304+
else {
305+
sendObject["is_music"] = false;
306+
}
307+
308+
serializeJson(sendObject, jsonOutString);
309+
310+
int HTTPresponseCode = db.insert(sb_table, jsonOutString, upsert);
311+
Serial.print("HTTP response code (SupaBase-insert): ");
312+
Serial.println(HTTPresponseCode);
313+
db.urlQuery_reset();
314+
315+
// neoPixel turn of the lights
316+
for(int i = 0; i < numLED; i++) {
317+
setRGB(0, 0, 0, i);
318+
}
319+
delay(500);
320+
} else if(recieved_text == "START") {
321+
if(perfrom_fft == true) {
322+
Serial.println("Recieved start message, but FFT is already started");
323+
break;
324+
}
325+
perfrom_fft = true;
326+
start_time = millis();
327+
328+
Serial.println("Recieved start message, starting FFT");
329+
330+
// neoPixel new connection indicator
331+
signalRGB('w');
332+
}
333+
else {
334+
Serial.println("Recieved unknown message");
335+
}
336+
}
337+
break;
338+
339+
// For everything else: do nothing
340+
case WStype_BIN:
341+
case WStype_ERROR:
342+
case WStype_FRAGMENT_TEXT_START:
343+
case WStype_FRAGMENT_BIN_START:
344+
case WStype_FRAGMENT:
345+
case WStype_FRAGMENT_FIN:
346+
default:
347+
break;
348+
}
349+
}
350+

0 commit comments

Comments
 (0)