Repository navigation
Expand file tree
/
Copy pathnew_WDMS_CLOCK.ino
More file actions
300 lines (215 loc) · 6.75 KB
/
Copy pathnew_WDMS_CLOCK.ino
File metadata and controls
300 lines (215 loc) · 6.75 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
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
/**************************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/category/63_98
This example is for a 128x64 pixel display using SPI to communicate
4 or 5 pins are required to interface.
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source
hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries,
with contributions from the open source community.
BSD license, check license.txt for more information
All text above, and the splash screen below must be
included in any redistribution.
**************************************************************************/
#include <NTPClient.h>
#include <ESP8266WiFi.h> // provides ESP8266 specific Wi-Fi routines we are calling to connect to network
#include <WiFiUdp.h> //handles sending and receiving of UDP packages
#include <TimeLib.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//#include <Fonts/FreeSans9pt7b.h>
//#include <Fonts/FreeSans12pt7b.h>
//#include <Fonts/FreeMonoOblique18pt7b.h>
//#include <Fonts/FreeSerif9pt7b.h>
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI 13 //D1
#define OLED_CLK 14 //D0
#define OLED_DC 2
#define OLED_CS 15
#define OLED_RESET 16
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
int RX = 12;
int pin_state= 0;
unsigned long counter = 0;
unsigned long prev_counter = 0;
long DELAY = 0;
float freq = 0;
int i=0;
int j=0;
int x=0;
int y=0;
int a=0;
int b=0;
int c=0;
unsigned long previousMillis = 0;
const char *ssid = "sidd";
const char *password = "12345678901";
WiFiUDP ntpUDP;
// 'time.nist.gov' is used (default server) with +1 hour offset (3600 seconds) 60 seconds (60000 milliseconds) update interval
NTPClient timeClient(ntpUDP, "time.nist.gov", 19800, 60000);
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
//Serial.begin(9600);
// pinMode(RX,INPUT);
// pinMode(13,OUTPUT);
}
pinMode(RX,INPUT);
pinMode(13,OUTPUT);
display.clearDisplay();
// display.setFont(&FreeSans12pt7b);
display.setTextColor(1);
display.setCursor(0,22);
display.println(" i mpulse\n projects");
display.invertDisplay(true);
display.display();
delay(2000);
display.clearDisplay();
WiFi.begin(ssid, password);
Serial.print("Connecting.");
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println("connected");
timeClient.begin();
}
char Time[] = " : : ";
char Date[] = " - -20 ";
byte last_second, second_, minute_, hour_, wday, day_, month_, year_;
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= 100)
{
previousMillis += 100;
if (j==50){
i=0;
j=0;
x=0;
y=0;
}
j=j+1;
Serial.println(DELAY);
if(DELAY>=100 && DELAY<=350){
i=i+1;
}
Serial.println(j);
if(i>=45){
x=1;
Serial.println("in range");
}
if(i>=30){
y=1;
}
if (x==0 && y==1 && j>=49){
Serial.println("in range22");
a=1;
b=0;
c=0;
}
if(x==1 && y==1 ){
a=0;
b=1;
c=0;
}
if(y==0 && j>=45){
a=0;
b=0;
c=1;
}
}
if(digitalRead(RX) && pin_state== 0)
{
pin_state=1;
prev_counter = micros();
}
if(!digitalRead(RX) && pin_state== 1)
{
pin_state=0;
counter = micros();
DELAY = counter - prev_counter;
}
timeClient.update();
unsigned long unix_epoch = timeClient.getEpochTime(); // get UNIX Epoch time
second_ = second(unix_epoch); // get seconds from the UNIX Epoch time
if (last_second != second_)
{
minute_ = minute(unix_epoch); // get minutes (0 - 59)
hour_ = hour(unix_epoch); // get hours (0 - 23)
wday = weekday(unix_epoch); // get minutes (1 - 7 with Sunday is day 1)
day_ = day(unix_epoch); // get month day (1 - 31, depends on month)
month_ = month(unix_epoch); // get month (1 - 12 with Jan is month 1)
year_ = year(unix_epoch) - 2000; // get year with 4 digits - 2000 results 2 digits year (ex: 2018 --> 18)
Time[7] = second_ % 10 + '0';
Time[6] = second_ / 10 + '0';
Time[4] = minute_ % 10 + '0';
Time[3] = minute_ / 10 + '0';
Time[1] = hour_ % 10 + '0';
Time[0] = hour_ / 10 + '0';
Date[9] = year_ % 10 + '0';
Date[8] = year_ / 10 + '0';
Date[4] = month_ % 10 + '0';
Date[3] = month_ / 10 + '0';
Date[1] = day_ % 10 + '0';
Date[0] = day_ / 10 + '0';
display.clearDisplay();
display_wday();
draw_text(3, 3, Date, 1); // display date (format: dd-mm-yyyy)
draw_text(16, 15, Time, 2); // display time (format: hh:mm:ss)
if (a==1){
display.setFont();
display.setTextColor(1);
display.setTextSize(1);
display.setCursor(0,35);
display.println(" Device is in range 2 ");
display.display();
}
if (b==1){
display.setFont();
display.setTextColor(1);
display.setTextSize(1);
display.setCursor(0,35);
display.println(" Device is in range 1 ");
display.display();
}
if (c==1){
display.setFont();
display.setTextColor(1);
display.setTextSize(1);
display.setCursor(0,35);
display.println(" ALERT! out of range ");
display.display();
}
display.display();
last_second = second_;
}
// display.clearDisplay();
}
void display_wday()
{
switch(wday){
case 1: draw_text(80, 3, " SUNDAY ", 1); break;
case 2: draw_text(80, 3, " MONDAY ", 1); break;
case 3: draw_text(80, 3, " TUESDAY ", 1); break;
case 4: draw_text(80, 3, "WEDNESDAY", 1); break;
case 5: draw_text(80, 3, "THURSDAY ", 1); break;
case 6: draw_text(80, 3, " FRIDAY ", 1); break;
default: draw_text(80, 3, "SATURDAY ", 1);
}
}
void draw_text(byte x_pos, byte y_pos, char *text, byte text_size)
{
display.setCursor(x_pos, y_pos);
display.setTextSize(text_size);
display.print(text);
}