-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkuehl.ino
290 lines (241 loc) · 7.28 KB
/
kuehl.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
/*
* Required libraries
* - Arduino Keypad
* - NeoPixelBus
* - WifiManager, https://github.com/tzapu/WiFiManager/tree/development
* - ArduinoJson, version 5.x
*/
#include <FS.h>
#include <SPIFFS.h>
#include <Keypad.h>
#include <NeoPixelBus.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <WiFiManager.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
/*****************************************************
* RGB LED configuration
****************************************************/
#define RGB_PIN 4
#define NUMPIXELS 1
/*****************************************************
* Keypad configuration
****************************************************/
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
// Keypad pin configuration (pin 1 and 10 are unused)
// Pins 2-5 on keypad
byte colPins[KEYPAD_COLS] = {32, 33, 25, 26};
// Pins 6-9 on keypad
byte rowPins[KEYPAD_ROWS] = {27, 14, 12, 13};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
/*****************************************************
* Declarations in global scope
****************************************************/
enum ledColor {
yellow,
red,
blue
};
char token[64];
char projectId[64];
bool shouldSaveConfig = false;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS );
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> pixels(NUMPIXELS, RGB_PIN);
WiFiManager wifiManager;
HTTPClient http;
WiFiManagerParameter custom_todoist_token("token", "Todoist Token", token, 64);
WiFiManagerParameter custom_todoist_projectId("projectId", "Todoist Project ID", projectId, 64);
WiFiManagerParameter custom_text("<p>Configure access to Todoist:</p>");
/*****************************************************
* Utility functions
****************************************************/
String chipId() {
uint64_t chipid = ESP.getEfuseMac();
char chipIdString[13];
sprintf(chipIdString, "%04X%08X",(uint16_t)(chipid>>32), (uint32_t)chipid);
return String(chipIdString);
}
String uuid() {
char buffer[33]; // buffer to hold 32 Hex Digit + /0
int i;
for(i = 0; i < 4; i++) {
sprintf (buffer + (i*8), "%08x", esp_random());
}
return String(buffer);
}
void showLed(ledColor colorName) {
RgbColor colorValue;
switch(colorName) {
case yellow:
colorValue = RgbColor(150, 150, 0);
break;
case red:
colorValue = RgbColor(0, 150, 0);
break;
case blue:
colorValue = RgbColor(0, 0, 150);
break;
default:
colorValue = RgbColor(150, 150, 150);
break;
}
pixels.SetPixelColor(0, colorValue);
pixels.Show();
}
void turnOffLed() {
pixels.SetPixelColor(0, RgbColor(0));
pixels.Show();
}
String urlencode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
/*****************************************************
* Setup
****************************************************/
void setup() {
Serial.begin(115200);
pixels.Begin();
pixels.Show();
showLed(yellow);
if (SPIFFS.begin(true)) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(token, json["todoist_token"]);
strcpy(projectId, json["todoist_projectId"]);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&custom_text);
wifiManager.addParameter(&custom_todoist_token);
wifiManager.addParameter(&custom_todoist_projectId);
wifiManager.setAPCallback(configModeCallback);
char chipIdString[13];
chipId().toCharArray(chipIdString, 13);
wifiManager.autoConnect("Kuehl01", chipIdString);
if (shouldSaveConfig) {
strcpy(token, custom_todoist_token.getValue());
strcpy(projectId, custom_todoist_projectId.getValue());
Serial.println("Saving configuration..");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["todoist_token"] = token;
json["todoist_projectId"] = projectId;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
turnOffLed();
}
void configModeCallback(WiFiManager *myWiFiManager) {
Serial.println("***************************************************");
Serial.println("* No Wifi configuration or not possible to connect.");
Serial.println("* Entered config mode. Connect to me using:");
Serial.println("*");
Serial.print("* Wifi SSID: ");
Serial.println(myWiFiManager->getConfigPortalSSID());
Serial.print("* Wifi Password: ");
Serial.println(chipId());
Serial.print("* Web IP address: ");
Serial.println(WiFi.softAPIP());
Serial.println("***************************************************");
showLed(red);
}
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
turnOffLed();
}
/*****************************************************
* Loop
****************************************************/
void loop(){
char key = keypad.getKey();
if (key){
if(String(key) == "*") {
wifiManager.resetSettings();
ESP.restart();
} else {
showLed(blue);
Serial.println(key);
String requestUrl = "https://todoist.com/api/v7/sync?token=" + String(token) + "&commands=" + urlencode(
"[{\"type\": \"item_add\", \"temp_id\": \"" +
uuid() +
"\", \"uuid\": \"" +
uuid() + "\", \"args\": {\"content\": \"Task " +
key +
"\", \"project_id\": " + String(projectId) + "}}]");
Serial.print("Request URL: ");
Serial.println(requestUrl);
http.begin(requestUrl);
int httpCode = http.GET();
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
turnOffLed();
}
}
}