21
21
SOFTWARE.
22
22
*/
23
23
24
- #include < ESP8266HTTPClient.h >
24
+ #pragma once
25
25
#include < ESP8266WiFi.h>
26
+ #include < ESP8266HTTPClient.h>
26
27
#include < ESP8266mDNS.h>
27
28
28
29
#include " SpotifyClient.h"
29
30
30
31
#define min (X, Y ) (((X) < (Y)) ? (X) : (Y))
31
32
32
- SpotifyClient::SpotifyClient (String clientId, String clientSecret, String redirectUri) {
33
+ SpotifyClient::SpotifyClient (String clientId, String clientSecret, String redirectUri, WiFiClientSecure *wifiClient ) {
33
34
this ->clientId = clientId;
34
35
this ->clientSecret = clientSecret;
35
36
this ->redirectUri = redirectUri;
37
+ this ->wifiClient = wifiClient;
36
38
}
37
39
38
40
uint16_t SpotifyClient::update (SpotifyData *data, SpotifyAuth *auth) {
@@ -41,14 +43,13 @@ uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
41
43
level = 0 ;
42
44
isDataCall = true ;
43
45
currentParent = " " ;
44
- WiFiClientSecure client = WiFiClientSecure ();
45
46
JsonStreamingParser parser;
46
47
parser.setListener (this );
47
48
48
49
String host = " api.spotify.com" ;
49
50
const int port = 443 ;
50
51
String url = " /v1/me/player/currently-playing" ;
51
- if (!client. connect (host.c_str (), port)) {
52
+ if (!wifiClient-> connect (host.c_str (), port)) {
52
53
Serial.println (" connection failed" );
53
54
return 0 ;
54
55
}
@@ -60,10 +61,10 @@ uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
60
61
" Connection: close\r\n\r\n " ;
61
62
// This will send the request to the server
62
63
Serial.println (request);
63
- client. print (request);
64
+ wifiClient-> print (request);
64
65
65
66
int retryCounter = 0 ;
66
- while (!client. available ()) {
67
+ while (!wifiClient-> available ()) {
67
68
executeCallback ();
68
69
retryCounter++;
69
70
if (retryCounter > 10 ) {
@@ -77,21 +78,20 @@ uint16_t SpotifyClient::update(SpotifyData *data, SpotifyAuth *auth) {
77
78
char c = ' ' ;
78
79
79
80
int size = 0 ;
80
- client.setNoDelay (false );
81
- // while(client.connected()) {
81
+ wifiClient->setNoDelay (false );
82
82
uint16_t httpCode = 0 ;
83
- while (client. connected () || client. available ()) {
84
- while ((size = client. available ()) > 0 ) {
83
+ while (wifiClient-> connected () || wifiClient-> available ()) {
84
+ while ((size = wifiClient-> available ()) > 0 ) {
85
85
86
86
if (isBody) {
87
87
uint16_t len = min (bufLen, size);
88
- c = client. readBytes (buf, len);
88
+ c = wifiClient-> readBytes (buf, len);
89
89
for (uint16_t i = 0 ; i < len; i++) {
90
90
parser.parse (buf[i]);
91
91
// Serial.print((char)buf[i]);
92
92
}
93
93
} else {
94
- String line = client. readStringUntil (' \r ' );
94
+ String line = wifiClient-> readStringUntil (' \r ' );
95
95
Serial.println (line);
96
96
if (line.startsWith (" HTTP/1." )) {
97
97
httpCode = line.substring (9 , line.indexOf (' ' , 9 )).toInt ();
@@ -121,14 +121,13 @@ uint16_t SpotifyClient::playerCommand(SpotifyAuth *auth, String method, String c
121
121
level = 0 ;
122
122
isDataCall = true ;
123
123
currentParent = " " ;
124
- WiFiClientSecure client = WiFiClientSecure ();
125
124
JsonStreamingParser parser;
126
125
parser.setListener (this );
127
126
128
127
String host = " api.spotify.com" ;
129
128
const int port = 443 ;
130
129
String url = " /v1/me/player/" + command;
131
- if (!client. connect (host.c_str (), port)) {
130
+ if (!wifiClient-> connect (host.c_str (), port)) {
132
131
Serial.println (" connection failed" );
133
132
return 0 ;
134
133
}
@@ -141,10 +140,10 @@ uint16_t SpotifyClient::playerCommand(SpotifyAuth *auth, String method, String c
141
140
" Connection: close\r\n\r\n " ;
142
141
// This will send the request to the server
143
142
Serial.println (request);
144
- client. print (request);
143
+ wifiClient-> print (request);
145
144
146
145
int retryCounter = 0 ;
147
- while (!client. available ()) {
146
+ while (!wifiClient-> available ()) {
148
147
executeCallback ();
149
148
150
149
retryCounter++;
@@ -159,20 +158,19 @@ uint16_t SpotifyClient::playerCommand(SpotifyAuth *auth, String method, String c
159
158
char c = ' ' ;
160
159
161
160
int size = 0 ;
162
- client.setNoDelay (false );
163
- // while(client.connected()) {
161
+ wifiClient->setNoDelay (false );
164
162
uint16_t httpCode = 0 ;
165
- while (client. connected () || client. available ()) {
166
- while ((size = client. available ()) > 0 ) {
163
+ while (wifiClient-> connected () || wifiClient-> available ()) {
164
+ while ((size = wifiClient-> available ()) > 0 ) {
167
165
if (isBody) {
168
166
uint16_t len = min (bufLen, size);
169
- c = client. readBytes (buf, len);
167
+ c = wifiClient-> readBytes (buf, len);
170
168
for (uint16_t i = 0 ; i < len; i++) {
171
169
parser.parse (buf[i]);
172
170
// Serial.print((char)buf[i]);
173
171
}
174
172
} else {
175
- String line = client. readStringUntil (' \r ' );
173
+ String line = wifiClient-> readStringUntil (' \r ' );
176
174
Serial.println (line);
177
175
if (line.startsWith (" HTTP/1." )) {
178
176
httpCode = line.substring (9 , line.indexOf (' ' , 9 )).toInt ();
@@ -194,13 +192,16 @@ void SpotifyClient::getToken(SpotifyAuth *auth, String grantType, String code) {
194
192
isDataCall = false ;
195
193
JsonStreamingParser parser;
196
194
parser.setListener (this );
197
- WiFiClientSecure client;
195
+
198
196
// https://accounts.spotify.com/api/token
199
197
const char *host = " accounts.spotify.com" ;
200
198
const int port = 443 ;
201
199
String url = " /api/token" ;
202
- if (!client.connect (host, port)) {
203
- Serial.println (" connection failed" );
200
+ wifiClient->setInsecure ();
201
+ wifiClient->connect (host, port);
202
+ if (!wifiClient->connected ()) {
203
+ Serial.printf (" Connection to %s:%d failed; returning.\n " , host, port);
204
+ wifiClient->stop ();
204
205
return ;
205
206
}
206
207
@@ -221,39 +222,33 @@ void SpotifyClient::getToken(SpotifyAuth *auth, String grantType, String code) {
221
222
" Connection: close\r\n\r\n " +
222
223
content;
223
224
Serial.println (request);
224
- client. print (request);
225
+ wifiClient-> print (request);
225
226
226
- int retryCounter = 0 ;
227
- while (!client.available ()) {
228
- executeCallback ();
229
- retryCounter++;
230
- if (retryCounter > 10 ) {
231
- return ;
232
- }
233
- delay (10 );
234
- }
235
-
236
- int pos = 0 ;
237
- boolean isBody = false ;
238
227
char c;
239
-
240
- int size = 0 ;
241
- client.setNoDelay (false );
242
- while (client.connected () || client.available ()) {
243
- while ((size = client.available ()) > 0 ) {
244
- c = client.read ();
228
+ boolean isBody = false ;
229
+ unsigned long timeoutMillis = 10000UL ;
230
+ unsigned long startMillis = millis ();
231
+ while (wifiClient->connected () || wifiClient->available ()) {
232
+ if (wifiClient->available ()) {
233
+ if ((millis () - startMillis) > timeoutMillis) {
234
+ Serial.printf (" HTTP timeout after %ds.\n " , (timeoutMillis/1000 ));
235
+ wifiClient->stop ();
236
+ ESP.restart ();
237
+ }
238
+ c = wifiClient->read ();
239
+ Serial.print (c);
245
240
if (c == ' {' || c == ' [' ) {
246
241
isBody = true ;
247
242
}
248
243
if (isBody) {
249
244
parser.parse (c);
250
- Serial.print (c);
251
- } else {
252
- Serial.print (c);
253
245
}
254
246
}
255
247
executeCallback ();
248
+ // give WiFi and TCP/IP libraries a chance to handle pending events
249
+ yield ();
256
250
}
251
+ wifiClient->stop ();
257
252
258
253
this ->data = nullptr ;
259
254
}
@@ -284,7 +279,7 @@ String SpotifyClient::startConfigPortal(const String mDnsName) {
284
279
285
280
oneWayCode = server.arg (" code" );
286
281
Serial.printf (" Code: %s\n " , oneWayCode.c_str ());
287
- String message = " <html><head></head><body>Succesfully authentiated This device with Spotify. Restart your device now</body></html>" ;
282
+ String message = " <html><head></head><body>Succesfully authentiated this device with Spotify. Restart your device now</body></html>" ;
288
283
server.send (200 , " text/html" , message);
289
284
});
290
285
@@ -295,7 +290,7 @@ String SpotifyClient::startConfigPortal(const String mDnsName) {
295
290
if (!MDNS.begin (mDnsName )) {
296
291
Serial.println (" Error setting up MDNS responder!" );
297
292
}
298
- Serial.println (" mDNS responder started" );
293
+ Serial.println (" MDNS responder started" );
299
294
Serial.println (" Open browser at http://" + mDnsName + " .local" );
300
295
301
296
while (oneWayCode == " " ) {
@@ -306,6 +301,8 @@ String SpotifyClient::startConfigPortal(const String mDnsName) {
306
301
307
302
Serial.println (" Stopping HTTP server" );
308
303
server.stop ();
304
+ Serial.println (" Stopping MDNS responder" );
305
+ MDNS.close ();
309
306
return oneWayCode;
310
307
}
311
308
0 commit comments