forked from douglaszuqueto/esp8266-controle-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266_controle_spotify.ino
136 lines (97 loc) · 2.87 KB
/
esp8266_controle_spotify.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
/************************** Inclusão das Bibliotecas **************************/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
/****************************** User Config ***********************************/
#include "user_config.h"
#include "user_config_override.h"
/**************************** DEBUG *******************************/
#ifdef DEBUG
#define DEBUG_PRINTLN(m) Serial.println(m)
#define DEBUG_PRINT(m) Serial.print(m)
#else
#define DEBUG_PRINTLN(m)
#define DEBUG_PRINT(m)
#endif
/**************************** Variaveis globais *******************************/
int playPause = D5;
bool playerStatus = true;
/************************* Declaração dos Prototypes **************************/
void initSerial();
void initWiFi();
/************************* Instanciação dos objetos **************************/
WiFiClientSecure client;
HTTPClient http;
/*********************** Implementação dos Prototypes *************************/
void initSerial() {
#ifdef DEBUG
Serial.begin(115200);
#endif
}
void initWiFi() {
delay(10);
DEBUG_PRINTLN("");
DEBUG_PRINT("[WIFI] Conectando-se em " + String(WIFI_SSID));
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
DEBUG_PRINT(".");
}
DEBUG_PRINTLN("");
DEBUG_PRINT(F("[WIFI] SSID: "));
DEBUG_PRINTLN(WIFI_SSID);
DEBUG_PRINT(F("[WIFI] IP: "));
DEBUG_PRINTLN(WiFi.localIP());
DEBUG_PRINT(F("[WIFI] Mac: "));
DEBUG_PRINTLN(WiFi.macAddress());
DEBUG_PRINTLN("");
}
void httpPUT(String endpoint) {
String uri = "/v1/me/player/" + endpoint;
http.begin(API_SPOTIFY, 443, uri, true, "AB BC 7C 9B 7A D8 5D 98 8B B2 72 A4 4C 13 47 9A 00 2F 70 B5");
http.addHeader("content-type", "application/json");
http.addHeader("Authorization", SPOTIFY_TOKEN);
int httpCode = http.PUT("");
if (httpCode < 0) {
Serial.println("request error - " + httpCode);
}
if (httpCode != HTTP_CODE_OK) {
Serial.println("request error - " + httpCode);
}
String response = http.getString();
http.end();
DEBUG_PRINTLN("[HTTP] Endpoint " + uri);
DEBUG_PRINTLN("[HTTP] Response " + response);
DEBUG_PRINTLN("[HTTP] Length: " + String(response.length()));
DEBUG_PRINTLN("[HTTP] Code: " + String(httpCode));
}
void play() {
httpPUT("play");
}
void pause() {
httpPUT("pause");
}
/********************************** Sketch ************************************/
void setup() {
initSerial();
initWiFi();
pinMode(playPause, INPUT_PULLUP);
delay(1000);
if (!client.connect(API_SPOTIFY, 443)) {
Serial.println("connection failed");
return;
} else {
DEBUG_PRINTLN("Conexão efetuada!");
}
}
void loop() {
yield();
if (digitalRead(playPause) == 0 and playerStatus) {
pause();
playerStatus = false;
}
if (digitalRead(playPause) == 0 and !playerStatus) {
play();
playerStatus = true;
}
}