-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_photo_calendar.ino
248 lines (219 loc) · 6.71 KB
/
weather_photo_calendar.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
/*
* Weather + photo frame for InkPlate 10 and 6.
*
* It is based on https://github.com/e-radionicacom/Inkplate-Arduino-library/tree/master/examples/Inkplate10/Projects
*
* Author: zhangtemplar@gmail.com
*/
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#ifndef ARDUINO_INKPLATE10
#error "Wrong board selection for this example, please select Inkplate 10 in the boards menu."
#endif
// Please create your own settings.h file
#include "settings.h"
// Include Inkplate library to the sketch
#include "Inkplate.h"
// for reading the configuration from file
#include <ArduinoJson.h>
#include "SdFat.h"
// For Weather
#include "Weather.h"
// for local photo
#include "LocalPhoto.h"
// Flickr web photo
#include "Flickr.h"
// Delay between API calls
// wait for 4 hours before next photo update
#define PHOTO_DELAY_US 4ll * 60 * 60 * 1000 * 1000
// wait for 1 hour before next photo update
//#define CALENDAR_DELAY_US 60 * 60 * 1000 * 1000
// for weather, delay 1 minute before each call
//#define WEATHER_DELAY_US 60 * 1000 * 1000
// Inkplate object
// 3 Bit will fail weather display
Inkplate display(INKPLATE_1BIT);
#define MIN_VOLTAGE 3.4
/*
* which page to show
* 0: weather
* 1: photo
* 2: calendar
* others are not supported
*
* TODO: move to a dedicated class
*/
#define PAGE_WEATHER 0
#define PAGE_PHOTO 1
#define PAGE_CALENDAR 2
RTC_DATA_ATTR char page = PAGE_WEATHER;
RTC_DATA_ATTR char previousPage = -1;
Weather weather;
LocalPhoto localPhoto;
// Flickr flickr;
/*
* Refresh display when needed.
*
* The display will be cleared if any of the conditions met:
* - page is switched
* - forceClear is requested
*/
void refreshDisplay(bool forceClear);
// Read the latest touch pad event (via interrupt register)
void readTouchPad();
void imageUrl(char *a) {
String url;
HTTPClient http;
if (http.begin("https://source.unsplash.com/random/1200x800") && http.GET() > 0)
{
url = http.getString();
int urlStart = url.indexOf("href=\"") + 6;
int urlEnd = url.indexOf("\">", urlStart);
url = url.substring(urlStart, urlEnd);
url = url.substring(0, url.indexOf("?")) + "?crop=entropy&fit=crop&fm=png&h=800&w=1200";
Serial.println(url);
strcpy(a, url.c_str());
}
else
{
display.println("HTTP error");
display.display();
}
http.end();
}
void webPhoto() {
display.setDisplayMode(INKPLATE_3BIT);
// Join wifi
display.joinAP(SECRET_SSID, SECRET_PASS);
char url[256];
imageUrl(url);
Serial.print(F("to display image from "));
Serial.println(url);
Serial.println(display.drawImage(url, display.PNG, 0, 0, true));
display.display();
}
void readTouchPad() {
// According to the schema, touch pad are connected to port B 2, 3 and 4 accordingly
// and from https://github.com/e-radionicacom/Inkplate-Arduino-library/blob/451f49eb752d37d49c9beebefa1eb2817d541c86/src/include/Mcp.cpp
// we know this is matched to bit 10, 11 and 12 accordingly
uint16_t key = display.getINTstateInternal(MCP23017_INT_ADDR, display.mcpRegsInt);
if (key & (1 << 10))
{ // Check if first pad has been touched. If it is, decrement the number and refresh the screen.
page = PAGE_WEATHER;
Serial.println(F("key pressed for weather"));
}
if (key & (1 << 11))
{ // If you touched second touchpad, set number to zero and refresh screen by calling our displayNumber() function
page = PAGE_PHOTO;
Serial.println(F("key pressed for photo"));
}
if (key & (1 << 12))
{ // If you touched third touchpad, incerement the number and refresh the screen.
page = PAGE_CALENDAR;
Serial.println(F("key pressed for calendar"));
}
}
void refreshDisplay(bool forceClear)
{
bool shallClear = forceClear;
if (previousPage != page) {
shallClear = true;
previousPage = page;
}
// Initial cleaning of buffer and physical screen
if (shallClear) {
display.clearDisplay();
display.display();
}
}
/**
* @brief Check battery, if low show a warning
*
* @return true if battery is ok
* @return false if battery is low
*/
bool checkBattery() {
float voltage = display.readBattery();
if (voltage < MIN_VOLTAGE) {
display.setTextSize(8);
display.println("Battery low ");
display.print(voltage, 2);
display.println(" V");
display.display();
return false;
}
Serial.print(voltage, 2);
Serial.println("V");
return true;
}
/**
* @brief Read settings from a json file in local microsd card named as settings.json
*
* @return true if setting is read succesfully
* @return false if setting failed to read, then hard coded setting will be used instead.
*/
bool readSettings() {
if (!display.sdCardInit()) {
display.println(F("SD Card error!"));
Serial.println(F("SD Card error!"));
display.partialUpdate();
return false;
}
SdFile file2;
Serial.println(F("open settings.json"));
if (!file2.open("settings.json", O_RDONLY)) {
Serial.println(F("failed to open settings.json"));
return false;
}
Serial.println(F("parse settings.json"));
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, file2);
if (error) {
Serial.println(F("failed to read setttings from settings.json will use default value"));
return false;
}
SECRET_TIMEZONE = doc["timezone"] | SECRET_TIMEZONE;
strlcpy(SECRET_CITY, doc["city"] | SECRET_CITY, sizeof(SECRET_CITY));
strlcpy(SECRET_SSID, doc["ssid"] | SECRET_SSID, sizeof(SECRET_SSID));
strlcpy(SECRET_PASS, doc["wifi_password"] | SECRET_PASS, sizeof(SECRET_PASS));
strlcpy(FLICKR_KEY, doc["flicker_key"] | FLICKR_KEY, sizeof(FLICKR_KEY));
return true;
}
// Main function
void setup()
{
// common set up
Serial.begin(115200);
display.begin();
// check battery
if (!checkBattery()) {
return;
}
readSettings();
// Setup mcp interrupts
for (int touchPadPin = 10; touchPadPin <=12; touchPadPin++) {
display.pinModeInternal(MCP23017_INT_ADDR, display.mcpRegsInt, touchPadPin, INPUT);
display.setIntOutputInternal(MCP23017_INT_ADDR, display.mcpRegsInt, 1, false, false, HIGH);
display.setIntPinInternal(MCP23017_INT_ADDR, display.mcpRegsInt, touchPadPin, RISING);
}
readTouchPad();
refreshDisplay(false);
switch (page) {
case PAGE_WEATHER:
weather.draw();
break;
case PAGE_PHOTO:
// flickr.draw();
webPhoto();
break;
default:
localPhoto.draw();
}
// Go to sleep
Serial.println(F("Going to sleep"));
esp_sleep_enable_timer_wakeup(PHOTO_DELAY_US);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_34, 1);
(void)esp_deep_sleep_start();
}
void loop()
{
}