Skip to content

Commit 83f18a5

Browse files
committed
DRAFT WebSockets examples
1 parent 2284121 commit 83f18a5

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @brief A simple WebSocket client sketch that sends out some PCM audio
3+
* data using the https://github.com/Links2004/arduinoWebSockets library
4+
* @author Phil Schatzmann
5+
*/
6+
7+
#include <WebSocketsClient.h> // https://github.com/Links2004/arduinoWebSockets
8+
#include <WiFiMulti.h>
9+
10+
#include "AudioTools.h"
11+
#include "AudioTools/Communication/WebSocketOutput.h"
12+
13+
// websocket
14+
WiFiMulti WiFiMulti;
15+
WebSocketsClient webSocket;
16+
WebSocketOutput out(webSocket);
17+
// audio
18+
AudioInfo info(44100, 2, 16);
19+
SineWaveGenerator<int16_t> sineWave(32000);
20+
GeneratedSoundStream<int16_t> sound(sineWave);
21+
StreamCopy copier(out, sound);
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
26+
// connect to wifi
27+
WiFiMulti.addAP("SSID", "passpasspass");
28+
while (WiFiMulti.run() != WL_CONNECTED) {
29+
delay(100);
30+
}
31+
32+
// connect to server address, port and URL
33+
webSocket.begin("192.168.0.123", 81, "/");
34+
35+
// try ever 5000 again if connection has failed
36+
webSocket.setReconnectInterval(5000);
37+
38+
sineWave.begin(info, N_B4);
39+
}
40+
41+
void loop() {
42+
webSocket.loop();
43+
// generate audio only when we are connected
44+
if (webSocket.isConnected()) copier.copy();
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @brief A simple WebSocket server sketch that receives PCM audio data using the
3+
* https://github.com/Links2004/arduinoWebSockets library and outputs it via i2s
4+
* to an AudioKit for easy testing. Replace the output with whatever other class
5+
* you like.
6+
* @author Phil Schatzmann
7+
*/
8+
#include <WebSocketsServer.h> //https://github.com/Links2004/arduinoWebSockets
9+
#include <WiFi.h>
10+
#include <WiFiClientSecure.h>
11+
#include <WiFiMulti.h>
12+
13+
#include "AudioTools.h"
14+
15+
WiFiMulti WiFiMulti;
16+
WebSocketsServer webSocket(81);
17+
AudioInfo info(44100, 2, 16);
18+
AudioBoardStream i2s(AudioKitEs8388V1); // Access I2S as stream
19+
20+
/// Just output the audio data
21+
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload,
22+
size_t length) {
23+
if (type == WStype_BIN) {
24+
i2s.write(payload, length);
25+
}
26+
}
27+
28+
void setup() {
29+
Serial.begin(115200);
30+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
31+
32+
WiFiMulti.addAP("SSID", "passpasspass");
33+
while (WiFiMulti.run() != WL_CONNECTED) {
34+
delay(100);
35+
}
36+
37+
webSocket.begin();
38+
webSocket.onEvent(webSocketEvent);
39+
40+
// start i2s
41+
auto cfg = i2s.defaultConfig(TX_MODE);
42+
cfg.copyFrom(info);
43+
i2s.begin(cfg);
44+
}
45+
46+
void loop() { webSocket.loop(); }
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @brief A simple WebSocket client sketch that receives PCM audio data using the
3+
* https://github.com/Links2004/arduinoWebSockets library and outputs it via i2s
4+
* to an AudioKit for easy testing. Replace the output with whatever other class
5+
* you like.
6+
* @author Phil Schatzmann
7+
*/
8+
#include <WebSocketsClient.h> // https://github.com/Links2004/arduinoWebSockets
9+
#include <WiFiMulti.h>
10+
11+
#include "AudioTools.h"
12+
#include "AudioTools/Communication/WebSocketOutput.h"
13+
14+
// websocket
15+
WiFiMulti WiFiMulti;
16+
WebSocketsClient webSocket;
17+
WebSocketOutput out(webSocket); // or replace with I2SStream
18+
// audio
19+
AudioInfo info(44100, 2, 16);
20+
AudioBoardStream i2s(AudioKitEs8388V1); // Access I2S as stream
21+
22+
// write audio to i2s
23+
void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
24+
if (type == WStype_BIN) {
25+
i2s.write(payload, length);
26+
}
27+
}
28+
29+
void setup() {
30+
Serial.begin(115200);
31+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
32+
33+
// connect to wifk
34+
WiFiMulti.addAP("SSID", "passpasspass");
35+
while (WiFiMulti.run() != WL_CONNECTED) {
36+
delay(100);
37+
}
38+
39+
// server address, port and URL
40+
webSocket.begin("192.168.0.123", 81, "/");
41+
42+
// event handler
43+
webSocket.onEvent(webSocketEvent);
44+
45+
// try ever 5000 again if connection has failed
46+
webSocket.setReconnectInterval(5000);
47+
48+
// start i2s
49+
auto cfg = i2s.defaultConfig(TX_MODE);
50+
cfg.copyFrom(info);
51+
i2s.begin(cfg);
52+
}
53+
54+
void loop() {
55+
webSocket.loop();
56+
copier.copy();
57+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @brief A simple WebSocket server sketch using the
3+
* https://github.com/Links2004/arduinoWebSockets library to send out some
4+
* PCM audio data when some clients are connnected.
5+
* @author Phil Schatzmann
6+
*/
7+
8+
#include <WebSocketsServer.h> // https://github.com/Links2004/arduinoWebSockets
9+
#include <WiFi.h>
10+
#include <WiFiMulti.h>
11+
12+
#include "AudioTools.h"
13+
#include "AudioTools/Communication/WebSocketOutput.h"
14+
15+
WiFiMulti WiFiMulti;
16+
WebSocketsServer webSocket(81);
17+
WebSocketOutput out(webSocket);
18+
19+
// audio
20+
AudioInfo info(44100, 2, 16);
21+
SineWaveGenerator<int16_t> sineWave(32000);
22+
GeneratedSoundStream<int16_t> sound(sineWave);
23+
StreamCopy copier(out, sound); // copies sound into i2s
24+
25+
void setup() {
26+
// Serial.begin(921600);
27+
Serial.begin(115200);
28+
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
29+
30+
// connect to wifi
31+
WiFiMulti.addAP("SSID", "passpasspass");
32+
while (WiFiMulti.run() != WL_CONNECTED) {
33+
delay(100);
34+
}
35+
36+
webSocket.begin();
37+
38+
// start i2s
39+
sineWave.begin(info, N_B4);
40+
}
41+
42+
void loop() {
43+
webSocket.loop();
44+
// generate audio only when we have any clients
45+
if (webSocket.connectedClients() > 0) copier.copy();
46+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
#include "AudioTools/CoreAudio/AudioOutput.h"
3+
#include "WebSocketsClient.h" // https://github.com/Links2004/arduinoWebSockets
4+
#include "WebSocketsServer.h" // https://github.com/Links2004/arduinoWebSockets
5+
6+
namespace audio_tools {
7+
8+
/**
9+
* @brief A simple wrapper class that lets you use the standard
10+
* Arduino Print class output commands to send audio data over a WebSocket
11+
* connection.
12+
* Uses https://github.com/Links2004/arduinoWebSockets
13+
* @ingroup communications
14+
* @author Phil Schatzmann
15+
* @copyright GPLv3
16+
*/
17+
18+
class WebSocketOutput : public AudioOutput {
19+
public:
20+
WebSocketOutput() = default;
21+
/// @brief Constructor which defines an alternative WebSocket object. By
22+
/// default we use WebSocket
23+
WebSocketOutput(WebSocketsClient &ws) { setWebSocket(ws); }
24+
/// @brief Constructor which defines an alternative WebSocket object. By
25+
/// default we use WebSocket
26+
WebSocketOutput(WebSocketsServer &ws) { setWebSocket(ws); }
27+
28+
/// @brief Defines an alternative WebSocket object. By default we use
29+
/// WebSocket
30+
void setWebSocket(WebSocketsClient &ws) { p_ws = &ws; };
31+
/// @brief Defines an alternative WebSocket object. By default we use
32+
/// WebSocket
33+
void setWebSocket(WebSocketsServer &ws) { p_ws_server = &ws; };
34+
35+
/// Replys will be sent to the initial remote caller
36+
size_t write(const uint8_t *data, size_t len) override {
37+
bool rc = false;
38+
if (p_ws != nullptr) rc = p_ws->sendBIN(data, len);
39+
if (p_ws_server != nullptr) {
40+
if (clientNo >= 0) {
41+
rc = p_ws_server->sendBIN(clientNo, data, len);
42+
} else {
43+
// broadcast to all clients
44+
rc = p_ws_server->broadcastBIN(data, len);
45+
}
46+
rc = p_ws_server->broadcastBIN(data, len);
47+
}
48+
return rc ? len : 0;
49+
}
50+
51+
/// For WebSocketServer we can define an individual recipient!
52+
void setTargetNo(int clientNo) { this->clientNo = clientNo; }
53+
54+
protected:
55+
WebSocketsClient *p_ws = nullptr;
56+
WebSocketsServer *p_ws_server = nullptr;
57+
int clientNo = -1;
58+
};
59+
60+
} // namespace audio_tools

0 commit comments

Comments
 (0)