Skip to content

Commit 447b90a

Browse files
committed
Communication examples - DRAFT
1 parent 4f14430 commit 447b90a

File tree

9 files changed

+176
-1
lines changed

9 files changed

+176
-1
lines changed

examples/examples-communication/example-bt-receive/example-bt-receive.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
*
88
* @copyright Copyright (c) 2022
99
*
10+
* ATTENTION: DRAFT - not tested yet
1011
*/
1112

13+
#include "AudioTools.h"
14+
#include "BluetoothSerial.h"
15+
1216
uint16_t sample_rate = 44100;
1317
uint8_t channels = 2; // The stream will have 2 channels
1418
BluetoothSerial SerialBT;

examples/examples-communication/example-bt-send/example-bt-send.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
*
88
* @copyright Copyright (c) 2022
99
*
10+
* ATTENTION: DRAFT - not tested yet
1011
*/
1112

13+
#include "AudioTools.h"
1214
#include "BluetoothSerial.h"
1315

1416
uint16_t sample_rate = 44100;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @file example-serial-receive.ino
3+
* @author Phil Schatzmann
4+
* @brief Receiving audio via ESPNow
5+
* @version 0.1
6+
* @date 2022-03-09
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
* ATTENTION: DRAFT - not tested yet
11+
*/
12+
13+
#include "AudioTools.h"
14+
#include "AudioLibs/Communication.h"
15+
16+
uint16_t sample_rate = 44100;
17+
uint8_t channels = 2; // The stream will have 2 channels
18+
ESPNowStream now;
19+
I2SStream out;
20+
StreamCopy copier(out, now);
21+
const char *ssd = "ssid";
22+
const char *password = "password";
23+
const char *peers[] = {"A8:48:FA:0B:93:40"}
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
28+
29+
now.addPeers(peers);
30+
now.begin(ssid, pasword);
31+
32+
// start I2S
33+
Serial.println("starting I2S...");
34+
auto config = out.defaultConfig(TX_MODE);
35+
config.sample_rate = sample_rate;
36+
config.channels = channels;
37+
config.bits_per_sample = 16;
38+
out.begin(config);
39+
40+
Serial.println("started...");
41+
}
42+
43+
void loop() {
44+
copier.copy();
45+
}

examples/examples-communication/example-espnow-send/example-serial-send.ino renamed to examples/examples-communication/example-espnow-send/example-espnow-send.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
*
88
* @copyright Copyright (c) 2022
99
*
10+
* ATTENTION: DRAFT - not tested yet
1011
*/
1112

13+
#include "AudioTools.h"
14+
#include "AudioLibs/Communication.h"
15+
1216
uint16_t sample_rate = 44100;
1317
uint8_t channels = 2; // The stream will have 2 channels
1418
SineWaveGenerator<sound_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
@@ -17,12 +21,13 @@ ESPNowStream now;
1721
StreamCopy copier(now, sound); // copies sound into i2s
1822
const char *ssd = "ssid";
1923
const char *password = "password";
24+
const char *peers[] = {"A8:48:FA:0B:93:40"}
2025

2126
void setup() {
2227
Serial.begin(115200);
2328
AudioLogger::instance().begin(Serial, AudioLogger::Info);
2429

25-
now.add
30+
now.addPeers(peers);
2631
now.begin(ssid, pasword);
2732

2833
// Setup sine wave
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @file example-serial-receive.ino
3+
* @author Phil Schatzmann
4+
* @brief Receiving audio via serial
5+
* @version 0.1
6+
* @date 2022-03-09
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
* ATTENTION: DRAFT - not tested yet
11+
*/
12+
13+
#include "AudioTools.h"
14+
#include <WiFi.h>
15+
16+
uint16_t sample_rate = 44100;
17+
uint8_t channels = 2; // The stream will have 2 channels
18+
WiFiServer server(80);
19+
WiFiClient client;
20+
I2SStream out;
21+
StreamCopy copier(out, client);
22+
const char* ssid = "yourssid";
23+
const char* password = "yourpasswd";
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
28+
29+
// start server
30+
server.begin();
31+
32+
// start I2S
33+
Serial.println("starting I2S...");
34+
auto config = out.defaultConfig(TX_MODE);
35+
config.sample_rate = sample_rate;
36+
config.channels = channels;
37+
config.bits_per_sample = 16;
38+
out.begin(config);
39+
40+
Serial.println("started...");
41+
}
42+
43+
void loop() {
44+
// get a new connection
45+
if (!client){
46+
client = server.available();
47+
}
48+
// copy data if we are connected
49+
if (client.connected()){
50+
copier.copy();
51+
}
52+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @file example-serial-send.ino
3+
* @author Phil Schatzmann
4+
* @brief Sending audio over serial
5+
* @version 0.1
6+
* @date 2022-03-09
7+
*
8+
* @copyright Copyright (c) 2022
9+
*
10+
* ATTENTION: DRAFT - not tested yet
11+
*/
12+
13+
#include "AudioTools.h"
14+
#include <WiFi.h>
15+
16+
uint16_t sample_rate = 44100;
17+
uint8_t channels = 2; // The stream will have 2 channels
18+
SineWaveGenerator<sound_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
19+
GeneratedSoundStream<sound_t> sound( sineWave); // Stream generated from sine wave
20+
WiFiClient client;
21+
StreamCopy copier(client, sound); // copies sound into i2s
22+
const char *ssid = "ssid";
23+
24+
void setup() {
25+
Serial.begin(115200);
26+
AudioLogger::instance().begin(Serial, AudioLogger::Info);
27+
28+
// connect to WIFI
29+
WiFi.begin(ssid, password);
30+
while (WiFi.status() != WL_CONNECTED) {
31+
delay(500);
32+
Serial.print(".");
33+
}
34+
Serial.println();
35+
36+
// Try to connect to ip / port
37+
while (!client.connect("134.209.234.6", 80)) {
38+
Serial.println("trying to connect...");
39+
delay(5000);
40+
}
41+
42+
// Setup sine wave
43+
sineWave.begin(channels, sample_rate, N_B4);
44+
Serial.println("started...");
45+
}
46+
47+
void loop() {
48+
copier.copy();
49+
}

examples/examples-communication/example-serial-receive/example-serial-receive.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
*
88
* @copyright Copyright (c) 2022
99
*
10+
* ATTENTION: DRAFT - not tested yet
1011
*/
1112

13+
#include "AudioTools.h"
14+
1215
#define RXD2 16
1316
#define TXD2 17
1417

examples/examples-communication/example-serial-send/example-serial-send.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
*
88
* @copyright Copyright (c) 2022
99
*
10+
* ATTENTION: DRAFT - not tested yet
1011
*/
1112

13+
#include "AudioTools.h"
14+
1215
#define RXD2 16
1316
#define TXD2 17
1417

src/AudioLibs/Communication.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class ESPNowStream : public AudioStreamX {
3131
return result == ESP_OK;
3232
}
3333

34+
/// Adds an array of
35+
template<size_t size>
36+
bool addPeers(const char*(&array)[size]) {
37+
bool result = true;
38+
for (int j=0;j<size;j++){
39+
if(!addPeer(array[j])){
40+
result = false;
41+
}
42+
}
43+
return result;
44+
}
45+
3446
/// Adds a peer to which we can send info or from which we can receive info
3547
bool addPeer(const char *address) {
3648
esp_now_peer_info_t peer;

0 commit comments

Comments
 (0)