-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultimate_js_esp8266_tft_1.8.ino
313 lines (268 loc) · 12.4 KB
/
ultimate_js_esp8266_tft_1.8.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// NodeMCU ESP8266
#include <Adafruit_ST7735.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ezTime.h>
#include <NTPClient.h>
#include <WiFiClientSecure.h>
#include <WiFiManager.h>
#include <WiFiUdp.h>
// Fixed parameters
const int time_zone = +7; // WIB (UTC + 7)
const char * ntp_pool = "id.pool.ntp.org"; // NTP Server pool address
const long ntp_update = 600000; // NTP Client update interval in millisecond (ms)
const int id_kota = 1301; // See https://api.myquran.com/v1/sholat/kota/semua
const int duty_cycle = 72; // TFT brightness using PWM duty cycle (0-255)
String new_hostname = "JamSholat";
// Buffers for JSON payload string to character conversion
char b_imsak[10];
char b_subuh[10];
char b_terbit[10];
char b_dhuha[10];
char b_dzuhur[10];
char b_ashar[10];
char b_maghrib[10];
char b_isya[10];
// Elapsed time since 1 Jan 1970 in seconds
unsigned long unix_epoch;
// Pin assignment for PWM output to set TFT backlight brightness
uint8_t led_pin = 5; // TFT LED/BL pin is connected to NodeMCU pin D1 (GPIO 5)
// Pin assignment for 1.8" TFT display with ST7735
#define TFT_A0 4 // TFT DC/A0 pin is connected to NodeMCU pin D2 (GPIO 4)
#define TFT_CS 0 // TFT CS pin is connected to NodeMCU pin D3 (GPIO 0)
#define TFT_RST 2 // TFT RST/RESET pin is connected to NodeMCU pin D4 (GPIO 2)
#define TFT_SCK 14 // TFT SCK/SCLK pin is connected to NodeMCU pin D5 (GPIO 14)
#define TFT_SDA 13 // TFT SDA/MOSI pin is connected to NodeMCU pin D7 (GPIO 13)
// 1.8" TFT display with ST7735
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_A0, TFT_RST);
// Convert RGB colors
#define RGB(r, g, b) ((( r & 0xF8 ) << 8)|(( g & 0xFC ) << 3 )|( b >> 3 ))
// TFT display colors
#define BLACK RGB( 0, 0, 0)
#define AQUAMARINE RGB(127, 255, 212)
#define GREY RGB(128, 128, 128)
#define DARKGREY RGB(169, 169, 169)
#define LIGHTGREY RGB(211, 211, 211)
#define TURQUOISE RGB( 64, 224, 208)
#define PINK RGB(255, 128, 192)
#define OLIVE RGB(128, 128, 0)
#define PURPLE RGB(128, 0, 128)
#define AZURE RGB( 0, 128, 255)
#define ORANGE RGB(255, 128, 64)
#define CYAN RGB( 0, 255, 255)
#define DARKCYAN RGB( 0, 139, 139)
#define RED RGB(255, 0, 0)
#define YELLOW RGB(255, 255, 0)
#define WHITE RGB(255, 255, 255)
#define BLUE RGB( 0, 0, 255)
#define GREEN RGB( 0, 255, 0)
#define SYNC_MARGINAL 3600 // yellow status if no sync for 1 hour
#define SYNC_LOST 86400 // red status if no sync for 1 day
// NTP client setup, this must be done before setup
int utc_offset = ( time_zone * 3600 );
WiFiUDP ntpUDP;
NTPClient timeClient( ntpUDP, ntp_pool, utc_offset, ntp_update );
// SETUP
void setup()
{
// initializing 1.8" TFT display
analogWrite(led_pin, duty_cycle); // set display brightness
tft.initR(INITR_BLACKTAB); // initialize TFT display with ST7735 chip
tft.setRotation(1); // set display orientation
tft.fillScreen(BLACK); // blanking display
// initializing Serial Port
Serial.begin(115200); // set serial port speed
delay (3000); // 3 seconds delay
// WiFiManager, Local initialization.
// Once its business is done, there is no need to keep it around
WiFiManager wfm;
// Supress Debug information
wfm.setDebugOutput(true);
// reset settings - wipe stored credentials for testing
// these are stored by the esp library
// wfm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP" ),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
Serial.println("WiFi connecting");
tft.setCursor(38, 20); // move cursor to position (38, 20) pixel
tft.print("WiFi connecting");
if (!wfm.autoConnect( "JamSholat" )) {
// Did not connect, print error message
Serial.println("failed to connect and hit timeout");
tft.setCursor(40, 30); // move cursor to position (40, 30) pixel
tft.print("failed to connect and hit timeout");
// Reset and try again
ESP.restart();
delay(1000);
}
// Connected!
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
tft.setCursor(40, 40); // move cursor to position (40, 40) pixel
tft.print("WiFi connected");
tft.setCursor(40, 60); // move cursor to position (40, 60) pixel
tft.print(WiFi.localIP());
delay(3000);
// set hostname
WiFi.hostname(new_hostname.c_str());
// Initializing NTP client
timeClient.begin();
delay (100);
tft.fillScreen(BLACK); // blanking display
// draw rectangle frames on display
tft.drawRect(0, 0, 16, 70, WHITE); // draw rectangle (x, y, w, h, color)
tft.drawRect(0, 73, 160, 55, WHITE); // draw rectangle (x, y, w, h, color)
tft.setTextSize(1); // text size = 1
tft.setTextColor(CYAN, BLACK); // set text color to cyan and black background
tft.setCursor(8, 78); // move cursor to position (8, 78) pixel
tft.print( "Imsak" );
tft.setCursor(43, 78); // move cursor to position (43, 78) pixel
tft.print( "Subuh" );
tft.setCursor(79, 78); // move cursor to position (79, 78) pixel
tft.print( "Terbit" );
tft.setCursor(120, 78); // move cursor to position (120, 78) pixel
tft.print( "Dhuha" );
tft.setCursor(6, 104); // move cursor to position (6, 104) pixel
tft.print( "Dzuhur" );
tft.setCursor(45, 104); // move cursor to position (45, 104) pixel
tft.print( "Ashar" );
tft.setCursor(79, 104); // move cursor to position (79, 104) pixel
tft.print( "Maghrib" );
tft.setCursor(124, 104); // move cursor to position (124, 104) pixel
tft.print( "Isya'" );
}
// MAIN LOOP
void loop()
{
timeClient.update(); // requesting time from NTP server
unix_epoch = timeClient.getEpochTime(); // get UNIX Epoch time from NTP server
if ( second()%1 ) return; // update every 1 second
events(); // update ntp
CST(); // requesting Clock and Salat Time
CSTATUS();
}
// CLOCK AND SALAT TIME FUNCTION
void CST()
{
// print time
tft.setTextSize(3); // text size = 3
tft.setCursor(28, 42); // move cursor to position (20, 42) pixel
tft.setTextColor(LIGHTGREY, BLACK); // set text color to lightgrey and black background
tft.printf( "%02u:%02u", hour(unix_epoch), minute(unix_epoch) );
// print seconds
tft.setTextSize(2); // text size = 2
tft.setCursor(114, 49); // move cursor to position (108, 49) pixel
tft.printf( ":%02u", second(unix_epoch) );
// creating day of week (dow) and it's corresponding absolute x position array
char dow_matrix[7][10] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu"};
byte x_pos[7] = {53, 59, 53, 65, 59, 53, 59};
static byte previous_dow = 0;
// print day of the week (dow)
if( previous_dow != weekday(unix_epoch) )
{
previous_dow = weekday(unix_epoch);
tft.fillRect(17, 0, 140, 16, BLACK); // fill rectangle (x,y,w,h,color) (erase day from the display)
tft.setTextSize(2); // text size = 2
tft.setTextColor(CYAN, BLACK); // set text color to cyan and black background
tft.setCursor(x_pos[previous_dow-1], 0); // set cursor to position (dow, 0) pixel
tft.print( dow_matrix[previous_dow-1] );
JS(); // requesting jadwal sholat
// print jadwal sholat upper row
tft.fillRect(5, 89, 150, 11, BLACK); // fill rectangle (x,y,w,h,color)
tft.setTextSize(1); // text size = 1
tft.setTextColor(YELLOW, BLACK); // set text color to red and black background
tft.setCursor(8, 90); // move cursor to position (8, 90) pixel
tft.print( b_imsak );
tft.setCursor(43, 90); // move cursor to position (43, 90) pixel
tft.print( b_subuh );
tft.setCursor(83, 90); // move cursor to position (83, 90) pixel
tft.print( b_terbit );
tft.setCursor(120, 90); // move cursor to position (120, 90) pixel
tft.print( b_dhuha );
// print jadwal sholat lower row
tft.fillRect(5, 115, 150, 11, BLACK); // fill rectangle (x,y,w,h,color)
tft.setCursor(8, 116); // move cursor to position (8, 116) pixel
tft.print( b_dzuhur );
tft.setCursor(44, 116); // move cursor to position (44, 116) pixel
tft.print( b_ashar );
tft.setCursor(84, 116); // move cursor to position (84, 116) pixel
tft.print( b_maghrib );
tft.setCursor(120, 116); // move cursor to position (120, 116) pixel
tft.print( b_isya );
}
// print date
tft.setTextSize(2); // text size = 2
tft.setCursor(28, 21); // move cursor to position (22, 21) pixel
tft.setTextColor(LIGHTGREY, BLACK); // set text color to yellow and black background
tft.printf( "%02u/%02u/%04u", day(unix_epoch), month(unix_epoch), year(unix_epoch) );
}
// JADWAL SHOLAT FUNCTION
void JS()
{
WiFiClientSecure client;
client.setInsecure(); // use with caution
client.connect ( "api.myquran.com", 80 );
// url request construct
HTTPClient https;
String url = "https://api.myquran.com/v1/sholat/jadwal/";
url = url + id_kota + "/" + year(unix_epoch) + "/" + month(unix_epoch) + "/" + day(unix_epoch);
// requesting the table
Serial.println ( url );
https.begin ( client, url );
int httpCode = https.GET();
String payload = https.getString();
Serial.print ( payload );
Serial.print ( "\r\n\r\n" );
// deserialize JSON payload
DynamicJsonDocument doc (1024);
DeserializationError error = deserializeJson(doc, payload);
JsonObject results = doc [ "data" ][ "jadwal" ];
String imsak = results[ "imsak" ];
String subuh = results[ "subuh" ];
String terbit = results[ "terbit" ];
String dhuha = results[ "dhuha" ];
String dzuhur = results[ "dzuhur" ];
String ashar = results[ "ashar" ];
String maghrib = results[ "maghrib" ];
String isya = results[ "isya" ];
// convert string to character
imsak.toCharArray (b_imsak, 10);
subuh.toCharArray (b_subuh, 10);
terbit.toCharArray (b_terbit, 10);
dhuha.toCharArray (b_dhuha, 10);
dzuhur.toCharArray (b_dzuhur, 10);
ashar.toCharArray (b_ashar, 10);
maghrib.toCharArray (b_maghrib, 10);
isya.toCharArray (b_isya, 10);
if ( error ) {
Serial.print (F( "deserializeJson() failed: " ));
Serial.println ( error.c_str() );
return;
}
}
// Clock Status Function - inspired by W8BH - Bruce E. Hall - https://github.com/bhall66/NTP-clock
void CSTATUS()
{
int color, sync_age;
if ( second()%10 ) return; // update every 10 seconds
sync_age = now() - lastNtpUpdateTime(); // how long since last sync?
if ( sync_age < SYNC_MARGINAL ) // time is good & in sync
color = GREEN;
else if ( sync_age < SYNC_LOST ) // sync is 1-24 hours old
color = YELLOW;
else color = RED; // time is stale!
tft.setTextSize(1); // text size = 1
tft.setTextColor(color, BLACK); // set text color to yellow and black background
tft.setCursor(6, 15); // move cursor to position (6, 13) pixel
tft.print( "N" );
tft.setCursor(6, 25); // move cursor to position (6, 23) pixel
tft.print( "T" );
tft.setCursor(6, 35); // move cursor to position (6, 33) pixel
tft.print( "P" );
tft.fillRoundRect( 5, 50, 7, 7, 10, color ); // show clock status as a color
}
// PROGRAM END