Skip to content

Commit 9970be6

Browse files
committed
connect to server when calling begin()
1 parent 622c4e6 commit 9970be6

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// We just test the output by providing a hex dump of the received data
2+
3+
#include <Ethernet.h>
4+
#include <SPI.h>
5+
#include "AudioTools.h"
6+
#include "SnapClient.h"
7+
8+
// Example settings for RP2040
9+
#define ETH_MISO 0
10+
#define ETH_MOSI 3
11+
#define ETH_SCLK 2
12+
13+
EthernetClient eth;
14+
HexDumpOutput out; // final output
15+
CopyDecoder codec;
16+
SnapClient client(eth, out, codec);
17+
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
18+
IPAddress ip(192, 168, 1, 177);
19+
20+
void setup() {
21+
Serial.begin(115200);
22+
23+
SPI.setRX(ETH_MISO);
24+
SPI.setTX(ETH_MOSI);
25+
SPI.setSCK(ETH_SCLK);
26+
SPI.begin();
27+
28+
// start the Ethernet connection:
29+
if (!eth.begin(mac, ip)) {
30+
Serial.print("Ethernet error");
31+
while (true);
32+
}
33+
34+
// Define CONFIG_SNAPCAST_SERVER_HOST in SnapConfig.h or here
35+
client.setServerIP(IPAddress(192, 168, 1, 38));
36+
37+
// start snap client
38+
if (!client.begin()) {
39+
Serial.print("Connection error");
40+
while (true);
41+
}
42+
}
43+
44+
void loop() { client.doLoop(); }

src/SnapClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class SnapClient {
156156
}
157157

158158
/// Call from Arduino Loop - to receive and process the audio data
159-
void doLoop() { p_snapprocessor->doLoop(); }
159+
bool doLoop() { return p_snapprocessor->doLoop(); }
160160

161161
protected:
162162
const char *TAG = "SnapClient";

src/api/SnapProcessor.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ class SnapProcessor {
3232
p_snap_output = &snap_output;
3333
}
3434

35+
/// Sets up the output and the client
3536
virtual bool begin() {
36-
bool result = false;
37+
bool result = true;
38+
// start output chain
3739
if (output_start) {
3840
result = audioBegin();
3941
}
4042

43+
// connect to snapcast server
44+
if(!connectClient()){
45+
result = false;
46+
}
47+
4148
if (http_task_start) {
4249
last_time_sync = 0;
4350
id_counter = 0;
@@ -71,11 +78,11 @@ class SnapProcessor {
7178
void setStartTask(bool flag) { http_task_start = flag; }
7279

7380
/// Call via SnapClient in Arduino Loop!
74-
virtual void doLoop() {
81+
bool doLoop() {
7582
if (is_fast_loop)
76-
processLoopStepFast();
83+
return processLoopStepFast();
7784
else
78-
processLoopStep();
85+
return processLoopStep();
7986
}
8087

8188
/// Defines the output class
@@ -146,7 +153,7 @@ class SnapProcessor {
146153
ESP_LOGI(TAG, "... connected");
147154
} else {
148155
delay(10);
149-
return true;
156+
return false;
150157
}
151158

152159
now = snap_time.time();
@@ -188,7 +195,7 @@ class SnapProcessor {
188195
ESP_LOGI(TAG, "... connected");
189196
} else {
190197
delay(10);
191-
return true;
198+
return false;
192199
}
193200

194201
now = snap_time.time();
@@ -275,9 +282,11 @@ class SnapProcessor {
275282
return true;
276283
}
277284

285+
/// connects to the server: returns true if we are connected
278286
bool connectClient() {
279287
ESP_LOGD(TAG, "start");
280288
if (p_client->connected()) return true;
289+
p_client->stop(); // for Ethernet.h
281290
p_client->setTimeout(CONFIG_CLIENT_TIMEOUT_SEC);
282291
if (!p_client->connect(server_ip, server_port)) {
283292
char str_address[50];

0 commit comments

Comments
 (0)