-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonos_remote.ino
339 lines (274 loc) · 8.63 KB
/
Sonos_remote.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**/
#include <WiServer.h>
bool useSerial = true;
bool useWifi = true;
byte volumePin = 2; // analog input pin for the volume potentio
byte playPin = 3;
byte presetPin = 9;
byte preset2Pin = 10;
byte preset3Pin = 4;
byte preset4Pin = 5;
byte redPin = 17;
byte greenPin = 18;
byte bluePin = 19;
int volumeSensorValue = 0;
byte playSensorValue = 0;
byte presetSensorValue = 0;
byte preset2SensorValue = 0;
byte preset3SensorValue = 0;
byte preset4SensorValue = 0;
byte canHttp = 1;
byte buttonTreshold = 30; // old buttons sometimes give false postives -> average input over x readings
bool isSleeping = false;
boolean RED[] = {LOW, HIGH, HIGH};
boolean GREEN[] = {HIGH, LOW, HIGH};
boolean BLUE[] = {HIGH, HIGH, LOW};
boolean OFF[] = {HIGH, HIGH, HIGH};
boolean color[] = {HIGH, HIGH, HIGH};
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,0,120}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,0,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"Steffest Home"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"**********"}; // max 64 characters
// WEP 128-bit keys
unsigned char wep_keys[] = {};
// setup the wireless mode; infrastructure - connect to AP; adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
// Function that prints data from the server
void printData(char* data, int len) {
// Print the data returned by the server
// Note that the data is not null-terminated, may be broken up into smaller packets, and
// includes the HTTP header.
if (useSerial){
while (len-- > 0) {
Serial.print(*(data++));
}
}
canHttp = 1;
setColor(GREEN);
}
// IP Address for steffest Home server
uint8 ip[] = {192,168,0,140};
String urlCommand = "";
char ch_urlCommand[44] = "/groupVolume/10";
// The GET request
GETrequest httpGet(ip, 4044, "home.stef.be", ch_urlCommand);
void setColor(boolean* newColor) {
color[0] = newColor[0];
color[1] = newColor[1];
color[2] = newColor[2];
}
void setLED(){
digitalWrite(redPin, color[0]);
digitalWrite(greenPin, color[1]);
digitalWrite(bluePin, color[2]);
}
void setup() {
// use the internal resistor of the "button" pins
pinMode(playPin,INPUT_PULLUP);
pinMode(presetPin,INPUT_PULLUP);
pinMode(preset2Pin,INPUT_PULLUP);
pinMode(preset3Pin,INPUT_PULLUP);
pinMode(preset4Pin,INPUT_PULLUP);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
setColor(RED);
setLED();
if (useSerial){
Serial.begin(57600);
Serial.println("Connecting to Wifi ...");
}
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
if (useWifi){
WiServer.init(NULL);
WiServer.enableVerboseMode(useSerial);
}
if (useSerial) Serial.println("Connection done");
setColor(GREEN);
setLED();
// Have the processData function called when data is returned by the server
httpGet.setReturnFunc(printData);
delay(500);
}
byte currentVolume = 0;
byte currentPreset = 0;
int newVolume = 0;
byte changeVolume = 0;
byte isPlaying = 1;
byte initDone = 0;
int actionDelay = 0;
byte hasAction = 0;
byte counter = 0;
void loop(){
if (isSleeping){
setColor(OFF);
}
setLED();
if (initDone == 0){
if (useSerial) Serial.println("Init Done");
initDone = 1;
}
volumeSensorValue = analogRead(volumePin);
if (digitalRead(playPin) == 0){
if (playSensorValue>0) playSensorValue--;
}else{
if (playSensorValue<buttonTreshold) playSensorValue++;
}
if (digitalRead(presetPin) == 0){
if (presetSensorValue>0) presetSensorValue--;
}else{
if (presetSensorValue<buttonTreshold) presetSensorValue++;
}
if (digitalRead(preset2Pin) == 0){
if (preset2SensorValue>0) preset2SensorValue--;
}else{
if (preset2SensorValue<buttonTreshold) preset2SensorValue++;
}
if (digitalRead(preset3Pin) == 0){
if (preset3SensorValue>0) preset3SensorValue--;
}else{
if (preset3SensorValue<buttonTreshold) preset3SensorValue++;
}
if (digitalRead(preset4Pin) == 0){
if (preset4SensorValue>0) preset4SensorValue--;
}else{
if (preset4SensorValue<buttonTreshold) preset4SensorValue++;
}
// use average to dampen spikes
newVolume = (byte) (newVolume + (volumeSensorValue/10))/2;
changeVolume = (newVolume-currentVolume);
if (changeVolume<0)changeVolume = 0-changeVolume;
if (actionDelay == 0){
// no action queued
if (changeVolume>6){
// trigger action with a small delay to get more relevant current data (turning a knob takes some time)
actionDelay=1;
}else{
if (isPlaying == 1 && playSensorValue == buttonTreshold){
// pause button is pressed
hasAction = 1;
isPlaying = 0;
urlCommand = "/Pause";
}
if (hasAction == 0 && isPlaying == 0 && playSensorValue == 0){
// pause button is not pressed
hasAction = 1;
isPlaying = 1;
urlCommand = "/Play";
}
if (hasAction == 0 && currentPreset != 1 && presetSensorValue == buttonTreshold){
// preset button is pressed
hasAction = 1;
currentPreset = 1;
urlCommand = "/favorite/StuBru";
//stringOne.toCharArray(urlCommand, 44);
}
if (hasAction == 0 && currentPreset != 2 && preset2SensorValue == buttonTreshold){
// preset button is pressed
hasAction = 1;
currentPreset = 2;
urlCommand = "/favorite/Nostalgie";
//stringOne.toCharArray(urlCommand, 44);
}
if (hasAction == 0 && currentPreset != 3 && preset3SensorValue == buttonTreshold){
// preset button is pressed
hasAction = 1;
currentPreset = 3;
urlCommand = "/favorite/Sterrekes";
//stringOne.toCharArray(urlCommand, 44);
}
if (hasAction == 0 && currentPreset != 4 && preset4SensorValue == buttonTreshold){
// preset button is pressed
// no action, just activate the pause/volume keys
currentPreset = 4;
}
if (presetSensorValue == 0 && preset2SensorValue == 0 && preset3SensorValue == 0 && preset4SensorValue == 0){
// no button pressed
// set to off
currentPreset = 0;
}
}
if (currentPreset == 0){
isSleeping = true;
}else{
if (isSleeping){
// waking up
setColor(GREEN);
}
isSleeping = false;
}
}else{
actionDelay = actionDelay+1;
if (actionDelay>30){
// trigger action with the most current value
actionDelay = 0;
if (hasAction == 0 && changeVolume>6){
if (newVolume>99) newVolume=99;
currentVolume = newVolume;
if (useSerial){
Serial.print("Setting Volume to ");
Serial.print(currentVolume);
Serial.println();
}
urlCommand = "/groupVolume/";
urlCommand = urlCommand + currentVolume;
hasAction = 1;
}
}
}
if (hasAction == 1){
if (canHttp == 0){
if (useSerial){
Serial.println("HTTP busy");
Serial.println(urlCommand);
}
hasAction = 0; // maybe set to 0 to drop the last action?
setColor(RED);
}else{
if (useSerial){
Serial.println("Sending HTTP");
Serial.println(urlCommand);
}
hasAction = 0;
setColor(BLUE);
if (useWifi && !isSleeping){
canHttp = 0;
urlCommand.toCharArray(ch_urlCommand, 44);
httpGet.submit();
}
}
}
counter = counter+1;
if (counter>200){
if (useSerial){
Serial.print(": ");
Serial.print(newVolume);
Serial.print(",");
Serial.print(playSensorValue);
Serial.print(",");
Serial.print(presetSensorValue);
Serial.print(",");
Serial.print(preset2SensorValue);
Serial.print(",");
Serial.print(preset3SensorValue);
Serial.print(",");
Serial.print(preset4SensorValue);
Serial.println();
}
counter = 0;
}
// Run WiServer
if (useWifi){
WiServer.server_task();
}
delay(10);
}