-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.cpp
More file actions
53 lines (38 loc) · 1.11 KB
/
Copy pathapi.cpp
File metadata and controls
53 lines (38 loc) · 1.11 KB
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
#include "api.h"
#include "wifi.h"
#include "sdcard.h"
#include "secrets.h"
#include "buzzer.h"
SdFat sd;
void getSound(String phrase) {
String encodedPhrase = urlEncode(phrase);
HTTPClient http;
String language = "pt-br";
String url = String("http://api.voicerss.org/?key=") +
API_KEY +
"&hl=" + language +
"&src=" + encodedPhrase;
http.begin(url);
int httpStatus = http.GET();
if (httpStatus == 200) {
if (!sd.exists("/sounds")) {
sd.mkdir("/sounds");
}
String filePath = "/sounds/voice.mp3";
File file = sd.open(filePath, FILE_WRITE);
WiFiClient *stream = http.getStreamPtr();
uint8_t buffer[512];
int bytesRead;
Serial.println("Dowloading sound...");
while (http.connected() && (bytesRead = stream -> readBytes(buffer, sizeof(buffer))) > 0) {
file.write(buffer, bytesRead);
}
file.close();
http.end();
Serial.println("Sound dowloaded sucessfully!");
playBuzzer();
} else {
Serial.println("Error accessing the API! Status: ");
Serial.println(httpStatus);
}
}