-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackMain.ino
More file actions
177 lines (135 loc) · 3.87 KB
/
stackMain.ino
File metadata and controls
177 lines (135 loc) · 3.87 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
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
#include <M5Stack.h>
#include <WiFi.h>
#include "AudioFileSourceHTTPStream.h"
#include "AudioFileSourceBuffer.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
#include "ArduinoJson.h"
#include <HTTPClient.h>
#include <base64.h>
// Enter your WiFi setup here:
const char *SSID = "xxxx";
const char *PASSWORD = "xxxx";
AudioGeneratorMP3 *mp3;
AudioFileSourceHTTPStream *file;
AudioFileSourceBuffer *buff;
AudioOutputI2S *out;
// Called when there's a warning or error (like a buffer underflow or decode hiccup)
void StatusCallback(void *cbData, int code, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
// Note that the string may be in PROGMEM, so copy it to RAM for printf
char s1[64];
strncpy_P(s1, string, sizeof(s1));
s1[sizeof(s1)-1]=0;
Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
Serial.flush();
}
void setup()
{
M5.begin();
Serial.begin(115200);
delay(1000);
Serial.println("Connecting to WiFi");
M5.Lcd.setTextSize(2);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.drawString("Connecting to WiFi", 160, 120);
WiFi.disconnect();
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
// Try forever
while (WiFi.status() != WL_CONNECTED) {
Serial.println("...Connecting to WiFi");
delay(1000);
}
M5.Speaker.setVolume(1);
M5.Speaker.update();
M5.Lcd.fillScreen(BLACK);
M5.Lcd.drawString("Connected", 160, 120);
// ConnectToClient();
M5.Lcd.fillScreen(BLACK);
}
void say(String text) {
// mp3 url
String URL="http://<cloudFunctions url>/textToSpeech/output?text=";
String encoded = base64::encode(text);
encoded.replace("+", "-");
encoded.replace("/", "_");
encoded.replace("=", "");
String url = URL + encoded;
// M5.Lcd.drawString(URL, 0, 0);
// String url = URL + "たにくん だいすき";
// const char *url_complete = url.c_str();
const char *url_complete = url.c_str();
file = new AudioFileSourceHTTPStream(url_complete);
buff = new AudioFileSourceBuffer(file, 40960);
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
out = new AudioOutputI2S(0, 1); // Output to builtInDAC
out->SetGain(0.1);
out->SetOutputModeMono(true);
mp3 = new AudioGeneratorMP3();
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
mp3->begin(buff, out);
}
String getFromAPI() {
HTTPClient client;
client.begin("http://newsapi.org/v2/top-headlines?country=jp&pageSize=6&apiKey=wwwwwwww");
int httpCode = client.GET();
String result = client.getString();
client.end();
return result;
}
static int lastms = 0;
static bool saying = false;
int newsIndex = 0;
void loop()
{
M5.Lcd.drawString("loading json", 160, 120);
if (saying) {
if (mp3->isRunning()) {
M5.Lcd.drawString("running", 160, 120);
if (!mp3->loop()){
out->stop();
mp3->stop();
buff->close();
delete buff;
buff = NULL;
file->close();
delete file;
file = NULL;
}
} else {
M5.Lcd.drawString("fin", 160, 140);
delay(10000);
saying = false;
lastms = 0;
if (newsIndex < 6){
newsIndex++;
} else {
delay(1800000);
newsIndex = 0;
}
}
} else {
String json = getFromAPI();
delay(2000);
M5.Lcd.drawString("loaded", 160, 120);
M5.Lcd.drawString("parse json", 160, 120);
M5.update();
M5.Lcd.drawString((String)json.length(), 160, 100);
DynamicJsonDocument doc(json.length()+1000);
M5.Lcd.drawString(json, 0, 0);
DeserializationError er = deserializeJson(doc, json);
if (er) {
M5.Lcd.drawString(String(er.c_str()), 160, 140);
return;
}
const char* st = doc["status"];
const char* title = doc["articles"][newsIndex]["title"];
M5.Lcd.drawString(String(title), 0, 20);
saying = true;
say(String(title));
}
}