-
Notifications
You must be signed in to change notification settings - Fork 4
/
ESP32_Web_Radio.ino
632 lines (570 loc) · 22.1 KB
/
ESP32_Web_Radio.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// ESP32_Web_Radio.ino
//
// Project based on
// ESP32 Internet Radio Project v1.00
// http://educ8s.tv/esp32-internet-radio
#include <VS1053.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <HTTPClient.h>
#include <esp_wifi.h>
#include <Nextion.h>
#include <Arduino.h>
#include <credentials.h>
#include "RadioStation.h"
#define DEBUG true
#define LOGSERVER "192.168.1.4"
#define UDP_LOGGER
#include <SimpleUDPLogger.h>
const char* ssid = mySSID;
const char* pass = myPASSWORD;
const char* host = "esp32radio";
#define FILESYSTEM SPIFFS
#include <SPIFFS.h>
File fsUploadFile;
WebServer server(80);
//------------------------------------------------------------------------------
// mp3 module
//------------------------------------------------------------------------------
#define VS1053_CS 32
#define VS1053_DCS 33
#define VS1053_DREQ 35
#define MIN_VOLUME 70
#define MAX_VOLUME 100
uint8_t volume = 82; // volume level 0-100
uint8_t mp3buff[32]; // vs1053 likes 32 bytes at a time
VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);
//------------------------------------------------------------------------------
void initMP3Decoder()
//------------------------------------------------------------------------------
{
player.begin();
player.switchToMp3Mode(); // optional, some boards require this
player.setVolume(volume);
}
//------------------------------------------------------------------------------
// freertos multitasking and queueing
//------------------------------------------------------------------------------
#define QUEUELENGTH 1024
QueueHandle_t xQueueMp3;
// Dimensions the buffer that the task being created will use as its stack.
// NOTE: This is the number of bytes the stack will hold, not the number of
// words as found in vanilla FreeRTOS.
#define STACK_SIZE 65536
TaskHandle_t xHandle = NULL;
//------------------------------------------------------------------------------
void task_mp3play(void * pvParameters)
//------------------------------------------------------------------------------
{
byte mp3chunk[32];
uint16_t queue_depth;
// loop forever
while(true)
{
// Receive a message on the created queue. Block for 10 ticks if a
// message is not immediately available.
if( xQueueReceive( xQueueMp3, &( mp3chunk ), ( TickType_t ) 10 ) )
{
// we now can consume mp3chunk
player.playChunk(mp3chunk, 32);
yield();
}
}
}
//------------------------------------------------------------------------------
void mp3Flush() // clear queue from previous station
//------------------------------------------------------------------------------
{
char rc;
while(rc != pdPASS)
rc = xQueueReset(xQueueMp3);
}
//------------------------------------------------------------------------------
// radio stations
//------------------------------------------------------------------------------
StationList stations;
//------------------------------------------------------------------------------
// Nextion
//------------------------------------------------------------------------------
// don't use UART0, conflict with sketch upload
// use UART2 on Pins 16 RXD & 17 TXD
// has to be changed in NexConfig.h (!)
// #define nexSerial Serial2
NexText t0 = NexText(0, 2, "t0");
//------------------------------------------------------------------------------
// Rotary Encoders
// copied from https://github.com/Edzelf/ESP32-Radio
//------------------------------------------------------------------------------
#define sv DRAM_ATTR static volatile // alias
//-----------------------------------------------------------------------------
// radio station
//-----------------------------------------------------------------------------
#define ENC_STATION_CLK_PIN 21
#define ENC_STATION_DT_PIN 22
sv int16_t rotationcount_station = 0;
int16_t oldcount_station = 0;
//-----------------------------------------------------------------------------
static void IRAM_ATTR isr_enc_station()
//-----------------------------------------------------------------------------
{
sv uint32_t old_state = 0x0001 ; // Previous state
sv int16_t locrotcount = 0 ; // Local rotation count
uint8_t act_state = 0 ; // The current state of the 2 PINs
uint8_t inx ; // Index in enc_state
sv const int8_t enc_states [] = // Table must be in DRAM (iram safe)
{ 0, // 00 -> 00
-1, // 00 -> 01 // dt goes HIGH
1, // 00 -> 10
0, // 00 -> 11
1, // 01 -> 00 // dt goes LOW
0, // 01 -> 01
0, // 01 -> 10
-1, // 01 -> 11 // clk goes HIGH
-1, // 10 -> 00 // clk goes LOW
0, // 10 -> 01
0, // 10 -> 10
1, // 10 -> 11 // dt goes HIGH
0, // 11 -> 00
1, // 11 -> 01 // clk goes LOW
-1, // 11 -> 10 // dt goes HIGH
0 // 11 -> 11
} ;
// Read current state of CLK, DT pin. Result is a 2 bit binary number: 00, 01, 10 or 11.
act_state = ( digitalRead ( ENC_STATION_CLK_PIN ) << 1 ) +
digitalRead ( ENC_STATION_DT_PIN ) ;
inx = ( old_state << 2 ) + act_state ; // Form index in enc_states
locrotcount += enc_states[inx] ; // Get delta: 0, +1 or -1
if ( locrotcount == 4 )
{
rotationcount_station++ ; // Divide by 4
locrotcount = 0 ;
}
else if ( locrotcount == -4 )
{
rotationcount_station-- ; // Divide by 4
locrotcount = 0 ;
}
old_state = act_state ; // Remember current status
}
//-----------------------------------------------------------------------------
// volume
//-----------------------------------------------------------------------------
#define ENC_VOLUME_CLK_PIN 25
#define ENC_VOLUME_DT_PIN 26
sv int16_t rotationcount_volume = volume;
int16_t oldcount_volume = 0;
//-----------------------------------------------------------------------------
static void IRAM_ATTR isr_enc_volume()
//-----------------------------------------------------------------------------
{
sv uint32_t old_state = 0x0001 ; // Previous state
sv int16_t locrotcount = 0 ; // Local rotation count
uint8_t act_state = 0 ; // The current state of the 2 PINs
uint8_t inx ; // Index in enc_state
sv const int8_t enc_states [] = { 0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0 } ;
// Read current state of CLK, DT pin. Result is a 2 bit binary number: 00, 01, 10 or 11.
act_state = ( digitalRead ( ENC_VOLUME_CLK_PIN ) << 1 ) +
digitalRead ( ENC_VOLUME_DT_PIN ) ;
inx = ( old_state << 2 ) + act_state ; // Form index in enc_states
locrotcount += enc_states[inx] ; // Get delta: 0, +1 or -1
if ( locrotcount == 4 )
{
rotationcount_volume++ ; // Divide by 4
locrotcount = 0 ;
}
else if ( locrotcount == -4 )
{
rotationcount_volume-- ; // Divide by 4
locrotcount = 0 ;
}
old_state = act_state ; // Remember current status
}
//------------------------------------------------------------------------------
void encoderStation_loop()
//------------------------------------------------------------------------------
{
//lets see if anything changed
if ( rotationcount_station != oldcount_station )
{
if ( rotationcount_station > stations.numStations )
{
rotationcount_station = 0;
}
if ( rotationcount_station < 0 )
{
rotationcount_station = stations.numStations;
}
oldcount_station = rotationcount_station;
stations.radioStation = rotationcount_station;
}
}
//------------------------------------------------------------------------------
void encoderVolume_loop()
//------------------------------------------------------------------------------
{
//lets see if anything changed
if ( rotationcount_volume != oldcount_volume )
{
if ( rotationcount_volume > MAX_VOLUME )
{
rotationcount_volume = MAX_VOLUME;
}
if ( rotationcount_volume < MIN_VOLUME )
{
rotationcount_volume = MIN_VOLUME;
}
oldcount_volume = rotationcount_volume;
volume = (int) rotationcount_volume;
player.setVolume(volume);
UDP_LOG_INFO("set volume to %lu", volume);
}
}
//------------------------------------------------------------------------------
// Network
//------------------------------------------------------------------------------
HTTPClient http;
WiFiClient * client;
//------------------------------------------------------------------------------
void connectToWIFI()
//------------------------------------------------------------------------------
{
if(DEBUG) {Serial.print("Connecting to Wifi");}
WiFi.mode(WIFI_STA);
WiFi.persistent(false);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
if(DEBUG) {Serial.print(".");}
delay(500);
}
if(DEBUG) {Serial.println("\nWifi connected");}
}
//------------------------------------------------------------------------------
void station_connect (int station_no )
//------------------------------------------------------------------------------
{
if (http.connected())
{
http.end(); // stop old connection
}
boolean isConnected = false;
char rc[4];
int httpCode;
char url[250];
const char* headerNames[] = { "Location" };
String Location;
int pos;
if(DEBUG) { Serial.println(stations.station[station_no].label); }
if(DEBUG) { Serial.println(stations.station[station_no].url); }
UDP_LOG_INFO(stations.station[station_no].label);
UDP_LOG_INFO(stations.station[station_no].url);
t0.setText(stations.station[station_no].label);
//-------------------------------
//--- check for http redirect ---
//-------------------------------
http.begin(stations.station[station_no].url);
http.collectHeaders(headerNames, sizeof(headerNames)/sizeof(headerNames[0]));
httpCode = http.GET();
//------- follow redirect -----------
if (httpCode == 302)
{
if(DEBUG) { Serial.println("following redirect"); }
UDP_LOG_INFO("following redirect");
Location = http.header("Location");
Location.toCharArray(url, Location.length()+1);
if(DEBUG) { Serial.println(url); }
UDP_LOG_INFO(url);
http.end();
http.begin(url);
httpCode = http.GET();
}
//-------------------------------
if (httpCode == HTTP_CODE_OK)
{
client = http.getStreamPtr();
isConnected = true;
}
else
{
if(DEBUG) { Serial.println("connection to station failed"); }
UDP_LOG_INFO("connection to station failed");
t0.setText("Station Error");
}
}
//------------------------------------------------------------------------------
void mp3stream()
//------------------------------------------------------------------------------
{
uint16_t queue_depth, queue_avail;
uint16_t num_chunks;
// how many chunks are in the tcp buffer?
num_chunks = client->available() / 32;
// avoid queue overflow
queue_depth = uxQueueMessagesWaiting(xQueueMp3);
queue_avail = QUEUELENGTH - queue_depth;
if (queue_avail < num_chunks)
{
num_chunks = queue_avail;
}
// fill queue with mp3 chunks
while (num_chunks > 0)
{
uint8_t bytesread = client->read(mp3buff, 32);
// Send a chunk. Wait for 10 ticks for space to become
// available if necessary.
if( xQueueSend( xQueueMp3, ( void * ) &mp3buff, ( TickType_t ) 10 ) != pdPASS )
{ // Failed to post the message, even after 10 ticks.
t0.setText("Error writing to queue!");
UDP_LOG_ERROR("Error writing to queue!");
if(DEBUG) { Serial.println("Error writing to queue!"); }
}
num_chunks--;
}
}
//------------------------------------------------------------------------------
void importStationlist()
//------------------------------------------------------------------------------
{
if(DEBUG) { Serial.println("Reading station list..."); }
UDP_LOG_INFO("Reading station list...");
t0.setText("Reading station list...");
File file = FILESYSTEM.open("/stationlist.txt", "r");
if(!file)
{
if(DEBUG) { Serial.println("Error reading station list!"); }
UDP_LOG_INFO("Error reading station list!");
}
else
{
String payload = file.readString();
stations.parseStations(payload);
}
file.close();
}
//------------------------------------------------------------------------------
// Webserver
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
String formatBytes(size_t bytes)
//------------------------------------------------------------------------------
{
if (bytes < 1024) {
return String(bytes) + "B";
} else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
} else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
} else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
//------------------------------------------------------------------------------
String getContentType(String filename)
//------------------------------------------------------------------------------
{
if (server.hasArg("download")) {
return "application/octet-stream";
} else if (filename.endsWith(".htm")) {
return "text/html";
} else if (filename.endsWith(".html")) {
return "text/html";
} else if (filename.endsWith(".css")) {
return "text/css";
} else if (filename.endsWith(".js")) {
return "application/javascript";
} else if (filename.endsWith(".png")) {
return "image/png";
} else if (filename.endsWith(".gif")) {
return "image/gif";
} else if (filename.endsWith(".jpg")) {
return "image/jpeg";
} else if (filename.endsWith(".ico")) {
return "image/x-icon";
} else if (filename.endsWith(".xml")) {
return "text/xml";
} else if (filename.endsWith(".pdf")) {
return "application/x-pdf";
} else if (filename.endsWith(".zip")) {
return "application/x-zip";
} else if (filename.endsWith(".gz")) {
return "application/x-gzip";
}
return "text/plain";
}
//------------------------------------------------------------------------------
bool exists(String path)
//------------------------------------------------------------------------------
{
bool yes = false;
File file = FILESYSTEM.open(path, "r");
if(!file.isDirectory()){
yes = true;
}
file.close();
return yes;
}
//------------------------------------------------------------------------------
bool handleFileRead(String path)
//------------------------------------------------------------------------------
{
if(DEBUG) { Serial.println("handleFileRead: " + path); }
if (path.endsWith("/")) {
path += "index.htm";
}
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (exists(pathWithGz) || exists(path)) {
if (exists(pathWithGz)) {
path += ".gz";
}
File file = FILESYSTEM.open(path, "r");
server.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
//------------------------------------------------------------------------------
void handleFileSave() {
//------------------------------------------------------------------------------
if (server.uri() != "/save") {
return;
}
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) {
filename = "/" + filename;
}
if(DEBUG) { Serial.print("handleFileUpload Name: "); Serial.println(filename); }
fsUploadFile = FILESYSTEM.open(filename, "w");
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (fsUploadFile) {
fsUploadFile.write(upload.buf, upload.currentSize);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile) {
fsUploadFile.close();
}
if(DEBUG) { Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize); }
UDP_LOG_INFO("stationlist.txt saved");
importStationlist();
}
}
//------------------------------------------------------------------------------
void initWebserver()
//------------------------------------------------------------------------------
{
FILESYSTEM.begin();
{
File root = FILESYSTEM.open("/");
File file = root.openNextFile();
while(file){
String fileName = file.name();
size_t fileSize = file.size();
if(DEBUG) { Serial.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); }
file = root.openNextFile();
}
if(DEBUG) { Serial.printf("\n"); }
}
MDNS.begin(host);
if(DEBUG) {
Serial.print("Open http://");
Serial.println(host);
}
server.on("/save", HTTP_POST, []() {
server.send(200, "text/plain", "");
}, handleFileSave);
//called when the url is not defined here
//use it to load content from FILESYSTEM
server.onNotFound([]() {
if (!handleFileRead(server.uri())) {
server.send(404, "text/plain", "FileNotFound");
}
});
server.begin();
if(DEBUG) { Serial.println("Webserver started."); }
}
//------------------------------------------------------------------------------
// setup
//------------------------------------------------------------------------------
void setup ()
{
if(DEBUG) { Serial.begin(115200); }
SPI.begin();
/* Set the baudrate which is for DEBUG and communicate with Nextion screen. */
nexInit();
// connect to access point
t0.setText("Connecting to WiFi...");
connectToWIFI();
// setup rsyslog
UDP_LOG_BEGIN(LOGSERVER, LOG_MODE_DEBUG);
UDP_LOG_INFO("Starting Rubis Internet Radio");
// init webserver
initWebserver();
// import station list from SPIFF
importStationlist();
// initialize the mp3 board
if(DEBUG) { Serial.println("Initializing MP3 board..."); }
UDP_LOG_INFO("Initializing MP3 board...");
t0.setText("Initializing MP3 board...");
initMP3Decoder();
if (!player.isChipConnected())
{
if(DEBUG) { Serial.println("MP3 board failed!"); }
UDP_LOG_ERROR("MP3 board failed!");
t0.setText("MP3 board failed!");
while(true)
delay(1000);
}
// initialize rotary encoder
pinMode(ENC_STATION_CLK_PIN, INPUT_PULLUP);
pinMode(ENC_STATION_DT_PIN, INPUT_PULLUP);
attachInterrupt ( digitalPinToInterrupt(ENC_STATION_CLK_PIN), isr_enc_station, CHANGE ) ;
attachInterrupt ( digitalPinToInterrupt(ENC_STATION_DT_PIN), isr_enc_station, CHANGE ) ;
pinMode(ENC_VOLUME_CLK_PIN, INPUT_PULLUP);
pinMode(ENC_VOLUME_DT_PIN, INPUT_PULLUP);
attachInterrupt ( digitalPinToInterrupt(ENC_VOLUME_CLK_PIN), isr_enc_volume, CHANGE ) ;
attachInterrupt ( digitalPinToInterrupt(ENC_VOLUME_DT_PIN), isr_enc_volume, CHANGE ) ;
// Create a queue capable of containing 32 messages a 32 bytes values.
t0.setText("Creating queue...");
xQueueMp3 = xQueueCreate( QUEUELENGTH, // The number of items the queue can hold.
32 ); // The size of each item in the queue
// Create the mp3 player task
if(DEBUG) { Serial.println("Creating mp3 player task..."); }
UDP_LOG_INFO("Creating mp3 player task...");
t0.setText("Creating mp3 player task...");
xTaskCreate(
task_mp3play, // Function that implements the task.
"mp3player", // Text name for the task.
STACK_SIZE, // Stack size in bytes, not words.
NULL, // Parameter passed into the task.
1, // Priority at which the task is created.
&xHandle); // Task handle to keep track of created task
}
//------------------------------------------------------------------------------
// loop
//------------------------------------------------------------------------------
void loop()
{
encoderStation_loop();
encoderVolume_loop();
// switch radio station
if(stations.radioStation!=stations.previousRadioStation)
{
vTaskSuspend( xHandle ); // suspend mp3 player task
mp3Flush(); // clear mp3 queue from previous station
station_connect(stations.radioStation);
stations.previousRadioStation = stations.radioStation;
mp3stream(); // prefill Queue
vTaskResume( xHandle );
}
// read tcp stream and fill queue
mp3stream();
// Webserver
server.handleClient();
// house keeping
yield();
}