This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Speaker.ino
296 lines (218 loc) · 7.53 KB
/
Speaker.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
/****************************************************************************************************************************
Blinds.ino
For ESP32/ESP8266 boards
Based on and modified from SinricPro libarary (https://github.com/sinricpro/)
to support other boards such as SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
Built by Khoi Hoang https://github.com/khoih-prog/SinricPro_Generic
Licensed under MIT license
**********************************************************************************************************************************/
/*
Example for how to use SinricPro Speaker device
If you encounter any issues:
- check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
- ensure all dependent libraries are installed
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
- open serial monitor and check whats happening
- check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
- visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
#if !(defined(ESP8266) || defined(ESP32))
#error This code is intended to run on the ESP32/ESP8266 boards ! Please check your Tools->Board setting.
#endif
// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#if (ESP8266)
#include <ESP8266WiFi.h>
#elif (ESP32)
#include <WiFi.h>
#endif
#include "SinricPro_Generic.h"
#include "SinricProSpeaker.h"
#define WIFI_SSID "YOUR-WIFI-SSID"
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define SPEAKER_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need
#define BANDS_INDEX_BASS 0
#define BANDS_INDEX_MIDRANGE 1
#define BANDS_INDEX_TREBBLE 2
enum speakerModes
{
mode_movie,
mode_music,
mode_night,
mode_sport,
mode_tv
};
// we use a struct to store all states and values for our speaker
struct
{
bool power;
unsigned int volume;
bool muted;
speakerModes mode;
int bands[3] = {0, 0, 0};
} speakerState;
// Speaker device callbacks
bool onPowerState(const String &deviceId, bool &state)
{
(void) deviceId;
Serial.printf("Speaker turned %s\r\n", state ? "on" : "off");
speakerState.power = state; // set powerState
return true;
}
bool onSetVolume(const String &deviceId, int &volume)
{
(void) deviceId;
Serial.printf("Volume set to: %i\r\n", volume);
speakerState.volume = volume; // update Volume
return true;
}
bool onAdjustVolume(const String &deviceId, int &volumeDelta, bool volumeDefault)
{
(void) deviceId;
(void) volumeDefault;
speakerState.volume += volumeDelta; // calcualte new absolute volume
Serial.printf("Volume changed about %i to %i\r\n", volumeDelta, speakerState.volume);
volumeDelta = speakerState.volume; // return new absolute volume
return true;
}
bool onMute(const String &deviceId, bool &mute)
{
(void) deviceId;
Serial.printf("Speaker is %s\r\n", mute ? "muted" : "unmuted");
speakerState.muted = mute; // update muted state
return true;
}
bool onMediaControl(const String &deviceId, String &control)
{
(void) deviceId;
Serial.printf("MediaControl: %s\r\n", control.c_str());
if (control == "Play") {} // do whatever you want to do here
if (control == "Pause") {} // do whatever you want to do here
if (control == "Stop") {} // do whatever you want to do here
if (control == "StartOver") {} // do whatever you want to do here
if (control == "Previous") {} // do whatever you want to do here
if (control == "Next") {} // do whatever you want to do here
if (control == "Rewind") {} // do whatever you want to do here
if (control == "FastForward") {} // do whatever you want to do here
return true;
}
bool onSetMode(const String &deviceId, String &mode)
{
(void) deviceId;
Serial.printf("Speaker mode set to %s\r\n", mode.c_str());
if (mode == "MOVIE")
speakerState.mode = mode_movie;
if (mode == "MUSIC")
speakerState.mode = mode_music;
if (mode == "NIGHT")
speakerState.mode = mode_night;
if (mode == "SPORT")
speakerState.mode = mode_sport;
if (mode == "TV")
speakerState.mode = mode_tv;
return true;
}
bool onSetBands(const String& deviceId, const String &bands, int &level)
{
Serial.printf("Device %s bands %s set to %d\r\n", deviceId.c_str(), bands.c_str(), level);
int index;
if (bands == "BASS")
index = BANDS_INDEX_BASS;
if (bands == "MIDRANGE")
index = BANDS_INDEX_MIDRANGE;
if (bands == "TREBBLE")
index = BANDS_INDEX_TREBBLE;
speakerState.bands[index] = level;
return true;
}
bool onAdjustBands(const String &deviceId, const String &bands, int &levelDelta)
{
int index;
if (bands == "BASS")
index = BANDS_INDEX_BASS;
if (bands == "MIDRANGE")
index = BANDS_INDEX_MIDRANGE;
if (bands == "TREBBLE")
index = BANDS_INDEX_TREBBLE;
speakerState.bands[index] += levelDelta;
levelDelta = speakerState.bands[index]; // return absolute trebble level
Serial.printf("Device %s bands \"%s\" adjusted about %i to %d\r\n", deviceId.c_str(), bands.c_str(), levelDelta,
speakerState.bands[index]);
return true; // request handled properly
}
bool onResetBands(const String &deviceId, const String &bands, int &level)
{
int index;
if (bands == "BASS")
index = BANDS_INDEX_BASS;
if (bands == "MIDRANGE")
index = BANDS_INDEX_MIDRANGE;
if (bands == "TREBBLE")
index = BANDS_INDEX_TREBBLE;
speakerState.bands[index] = 0;
level = speakerState.bands[index]; // return new level
Serial.printf("Device %s bands \"%s\" reset to%d\r\n", deviceId.c_str(), bands.c_str(), speakerState.bands[index]);
return true; // request handled properly
}
// setup function for WiFi connection
void setupWiFi()
{
Serial.print("\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(250);
}
Serial.print("\n[WiFi]: IP-Address is ");
Serial.println(WiFi.localIP());
}
// setup function for SinricPro
void setupSinricPro()
{
// add device to SinricPro
SinricProSpeaker& speaker = SinricPro[SPEAKER_ID];
// set callback functions to device
speaker.onPowerState(onPowerState);
speaker.onSetVolume(onSetVolume);
speaker.onAdjustVolume(onAdjustVolume);
speaker.onMute(onMute);
speaker.onSetBands(onSetBands);
speaker.onAdjustBands(onAdjustBands);
speaker.onResetBands(onResetBands);
speaker.onSetMode(onSetMode);
speaker.onMediaControl(onMediaControl);
// setup SinricPro
SinricPro.onConnected([]()
{
Serial.println("Connected to SinricPro");
});
SinricPro.onDisconnected([]()
{
Serial.println("Disconnected from SinricPro");
});
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup()
{
Serial.begin(BAUD_RATE);
while (!Serial);
Serial.println("\nStarting Speaker on " + String(ARDUINO_BOARD));
Serial.println("Version : " + String(SINRICPRO_VERSION_STR));
setupWiFi();
setupSinricPro();
}
void loop()
{
SinricPro.handle();
}