-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathd1-web-radio.ino
588 lines (492 loc) · 13.9 KB
/
d1-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
//
// d1-web-radio.ino
//
// An internet-connected radio! We serve a simple HTTP-page allowing the user
// to search up/down, enter a frequency directly, and mute/unmute.
//
// For clients connected via serial we can also interact via that.
//
// Omissions in this project are caused by lack of hardware support:
//
// * Volume control.
// * RDS.
//
// Steve
// --
//
//
// WiFi manager library, for working as an access-point, etc.
//
#include "WiFiManager.h"
//
// WiFi & over the air updates
//
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
//
// Debug messages over the serial console.
//
#include "debug.h"
//
// Decode URL parameters.
//
#include "url_parameters.h"
//
// Radio-library
//
#include "TEA5767.h"
#include <Wire.h>
//
// The name of this project.
//
// Used for the Access-Point name, and for OTA-identification.
//
#define PROJECT_NAME "WEB-RADIO"
//
// The HTTP-server we present runs on port 80.
//
WiFiServer server(80);
//
// The Radio object.
//
TEA5767 Radio;
//
// Are we searching?
//
int search_mode = 0;
//
// Are we muted?
//
int g_muted = false;
//
// If we're searching then this holds the direction.
//
int search_direction;
//
// Buffer for reading the radio-status
//
unsigned char buf[5];
//
// This function is called when the device is powered-on.
//
void setup()
{
//
// Enable our serial port.
//
#ifdef DEBUG
Serial.begin(115200);
Serial.setTimeout(25);
#endif
//
// So we get working mdns.
//
WiFi.hostname(PROJECT_NAME);
//
// Handle Connection.
//
WiFiManager wifiManager;
wifiManager.setAPCallback(access_point_callback);
wifiManager.autoConnect(PROJECT_NAME);
//
// Now we're connected show the local IP address.
//
DEBUG_LOG("\nWiFi Connected with IP %s\n",
WiFi.localIP().toString().c_str());
//
// Now we can start our HTTP server
//
server.begin();
DEBUG_LOG("HTTP-Server started on http://%s\n",
WiFi.localIP().toString().c_str());
//
// Allow over the air updates
//
// This is documented here:
// https://randomnerdtutorials.com/esp8266-ota-updates-with-arduino-ide-over-the-air/
//
// Hostname defaults to esp8266-[ChipID]
//
ArduinoOTA.setHostname(PROJECT_NAME);
ArduinoOTA.onStart([]()
{
DEBUG_LOG("OTA Started:");
});
ArduinoOTA.onEnd([]()
{
DEBUG_LOG("OTA Ended.");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
{
char buf[64];
memset(buf, '\0', sizeof(buf));
snprintf(buf, sizeof(buf) - 1, "Upgrade - %02u%%", (progress / (total / 100)));
DEBUG_LOG(buf);
});
ArduinoOTA.onError([](ota_error_t error)
{
if (error == OTA_AUTH_ERROR)
DEBUG_LOG("\tAuth Failed\n");
else if (error == OTA_BEGIN_ERROR)
DEBUG_LOG("\tBegin Failed\n");
else if (error == OTA_CONNECT_ERROR)
DEBUG_LOG("\tConnect Failed\n");
else if (error == OTA_RECEIVE_ERROR)
DEBUG_LOG("\tReceive Failed\n");
else if (error == OTA_END_ERROR)
DEBUG_LOG("\tEnd Failed\n");
});
//
// Ensure the OTA process is running & listening.
//
ArduinoOTA.begin();
//
// Setup the radio
//
Wire.begin();
Radio.init();
//
// Pick a default frequency.
//
Radio.set_frequency(94.90);
}
//
// If we're unconfigured we run an access-point.
//
// Show that, explicitly.
//
void access_point_callback(WiFiManager* myWiFiManager)
{
DEBUG_LOG("AccessPoint Mode Enabled.\n");
}
//
// This function is called continously.
//
void loop()
{
//
// Handle any pending over the air updates.
//
ArduinoOTA.handle();
//
// Are we searching?
//
if (search_mode == 1)
{
//
// If so try to do the search, and if it succeeds then
// cancel further searching.
//
if (Radio.read_status(buf) == 1)
{
if (Radio.process_search(buf, search_direction) == 1)
{
search_mode = 0;
}
}
}
//
// Check if a client has connected to our HTTP-server.
//
// If so handle it.
//
WiFiClient client = server.available();
if (client)
processHTTPRequest(client);
//
// Is there serial-input available?
//
if (Serial.available())
{
//
// Read a command from the serial-console.
//
char buffer[80] = { '\0' };
int read = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
//
// Remove non-printable characters
//
// (mostly this is to remove `\r`, `\n`, etc.
//
for (int i = 0; i < strlen(buffer); i++)
{
if (! isprint(buffer[i]))
buffer[i] = '\0';
}
//
// Now look for commands we know of.
//
if (strcmp(buffer, "search up") == 0)
{
searchUp();
}
else if (strcmp(buffer, "search down") == 0)
{
searchDown();
}
else if (strcmp(buffer, "mute") == 0)
{
setMute(true);
}
else if (strcmp(buffer, "info") == 0)
{
info();
}
else if (strcmp(buffer, "unmute") == 0)
{
setMute(false);
}
else if ((strncmp(buffer, "tune ", 5) == 0) && (strlen(buffer) > 5))
{
double f = atof(buffer + 4);
tuneTo(f);
}
else
{
DEBUG_LOG("Ignoring input: '%s'. Valid commands are:\n", buffer);
DEBUG_LOG("\tinfo\n");
DEBUG_LOG("\tmute\n");
DEBUG_LOG("\tsearch up\n");
DEBUG_LOG("\tsearch down\n");
DEBUG_LOG("\ttune 12.3\n");
DEBUG_LOG("\tunmute\n");
}
}
}
//
// Serve a redirect to the server-root
//
void redirectIndex(WiFiClient client)
{
client.println("HTTP/1.1 302 Found");
client.print("Location: http://");
client.print(WiFi.localIP().toString().c_str());
client.println("/");
}
//
// This is a bit horrid.
//
// Serve a HTML-page to any clients who connect, via a browser.
//
void serveHTML(WiFiClient client)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE html>");
client.println("<html lang=\"en\">");
client.println("<head>");
client.println("<title>Web Radio</title>");
client.println("<meta charset=\"utf-8\">");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
client.println("<link href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" rel=\"stylesheet\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\">");
client.println("<script src=\"https://code.jquery.com/jquery-1.12.4.min.js\" integrity=\"sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU=\" crossorigin=\"anonymous\"></script>");
client.println("<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\" integrity=\"sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa\" crossorigin=\"anonymous\"></script>");
client.println("</head>");
client.println("<body>");
client.println("<nav id=\"nav\" class = \"navbar navbar-default\" style=\"padding-left:50px; padding-right:50px;\">");
client.println("<div class = \"navbar-header\">");
client.println("<h1 class=\"banner\"><a href=\"/\">Web Radio</a> - <small>by Steve</small></h1>");
client.println("</div>");
client.println("<ul class=\"nav navbar-nav navbar-right\">");
client.println("<li><a href=\"https://steve.fi/Hardware/\">Steve's Projects</a></li>");
client.println("</ul>");
client.println("</nav>");
client.println("<div class=\"container-fluid\">");
// Start of body
// Row
client.println("<div class=\"row\">");
client.println("<div class=\"col-md-3\"></div>");
client.println("<div class=\"col-md-9\"><h1>Web Radio</h1><p> </p></div>");
client.println("</div>");
client.println("<div class=\"row\">");
client.println("<div class=\"col-md-4\"></div>");
client.println("<div class=\"col-md-4\">");
if (Radio.read_status(buf) == 1)
{
double current_freq = floor(Radio.frequency_available(buf) / 100000 + .5) / 10;
int stereo = Radio.stereo(buf);
int signal_level = Radio.signal_level(buf);
client.print("<p>Currently listening to ");
client.println(current_freq);
client.println("FM.</p>");
client.print("<p>The signal strength is ");
client.print(signal_level);
client.print("/15 ");
if (stereo)
client.println(" (stereo).</p>");
else
client.println(" (mono).</p>");
client.println("</div>");
client.println("<div class=\"col-md-4\"></div>");
client.println("</div>");
// Row
client.println("<div class=\"row\">");
client.println("<div class=\"col-md-3\"></div>");
client.println("<div class=\"col-md-9\"> <h2>Change Frequency</h2></div>");
client.println("</div>");
client.println("<div class=\"row\">");
client.println("<div class=\"col-md-4\"></div>");
client.println("<div class=\"col-md-4\">");
client.print("<p>Here you can change the frequency directly:</p>");
client.println("<form action=\"/\" method=\"GET\"><input type=\"text\" name=\"freq\" value=\"");
client.println(current_freq);
client.println("\"><input type=\"submit\" value=\"Update\"></form>");
client.println("</div>");
client.println("<div class=\"col-md-4\"></div>");
client.println("</div>");
// Row
client.println("<div class=\"row\">");
client.println("<div class=\"col-md-3\"></div>");
client.println("<div class=\"col-md-9\"> <h2>Operations</h2></div>");
client.println("</div>");
client.println("<div class=\"row\">");
client.println("<div class=\"col-md-4\"></div>");
client.println("<div class=\"col-md-4\">");
client.print("<p>Search <a href=\"/?search=up\">up</a>, or <a href=\"/?search=down\">down</a>.</p>");
//
// Only show mute/unmute if we're in the opposite state.
//
if (g_muted)
{
client.print("<p><a href=\"/?unmute=1\">Unmute</a>.</p>");
}
else
{
client.print("<p><a href=\"/?mute=1\">Mute</a>.</p>");
}
client.println("</div>");
client.println("<div class=\"col-md-4\"></div>");
client.println("</div>");
}
else
{
client.println("<p>Failed to find radio-details..</p>");
}
// End of body
client.println("</div>");
client.println("</body>");
client.println("</html>");
}
//
// Process an incoming HTTP-request.
//
void processHTTPRequest(WiFiClient client)
{
// Wait until the client sends some data
while (client.connected() && !client.available())
delay(1);
// Read the first line of the request
String request = client.readStringUntil('\r');
client.flush();
//
// Find the URL we were requested
//
// We'll have something like "GET XXXXX HTTP/XX"
// so we split at the space and send the "XXX HTTP/XX" value
//
request = request.substring(request.indexOf(" ") + 1);
//
// Now we'll want to peel off any HTTP-parameters that might
// be present, via our utility-helper.
//
URL url(request.c_str());
//
// Does the user want to tune directly?
//
char *number = url.param("freq");
if (number != NULL)
{
double var = atof(number);
tuneTo(var);
// Redirect to the server-root
redirectIndex(client);
return;
}
//
// Does the user want to scan?
//
char *search = url.param("search");
if (search != NULL)
{
if (strcmp(search, "up") == 0)
searchUp();
if (strcmp(search, "down") == 0)
searchDown();
// Redirect to the server-root
redirectIndex(client);
return;
}
char *mute = url.param("mute");
if (mute != NULL)
{
setMute(true);
redirectIndex(client);
return;
}
char *unmute = url.param("unmute");
if (unmute != NULL)
{
setMute(false);
redirectIndex(client);
return;
}
// Return a simple response
serveHTML(client);
}
//
// Radio operations - defined here so they can be called by the
// web-handler, or via the serial-console handler
//
void info()
{
if (Radio.read_status(buf) == 1)
{
double current_freq = floor(Radio.frequency_available(buf) / 100000 + .5) / 10;
int stereo = Radio.stereo(buf);
int signal_level = Radio.signal_level(buf);
DEBUG_LOG("Tuned to %fFM\n", current_freq);
DEBUG_LOG("Signal strength: %d/15\n", signal_level);
if (stereo)
DEBUG_LOG("(stereo)\n");
else
DEBUG_LOG("(mono)\n");
if (g_muted)
DEBUG_LOG("(muted)\n");
}
}
void setMute(bool state)
{
if (state)
{
Radio.mute();
}
else
{
if (Radio.read_status(buf) == 1)
{
double current_freq = floor(Radio.frequency_available(buf) / 100000 + .5) / 10;
Radio.set_frequency(current_freq);
}
}
g_muted = state;
}
void searchDown()
{
search_mode = 1;
search_direction = TEA5767_SEARCH_DIR_DOWN;
Radio.search_down(buf);
DEBUG_LOG("Searching down ..\n");
}
void searchUp()
{
search_mode = 1;
search_direction = TEA5767_SEARCH_DIR_UP;
Radio.search_up(buf);
DEBUG_LOG("Searching up ..\n");
}
void tuneTo(double freq)
{
DEBUG_LOG("Tuning to %f\n", freq);
Radio.set_frequency(freq);
}