Skip to content

Commit 06d5e9e

Browse files
authored
Add files via upload
1 parent 318867b commit 06d5e9e

File tree

3 files changed

+48
-62
lines changed

3 files changed

+48
-62
lines changed

RTC Sync with Time API/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def day_counter(string):
9999
time_fetch_flag = True
100100
time_sync_status = True
101101

102-
if(time_sync_status == True):
102+
else:
103103
if(((hour % sync_hour) == 0) and (minute == 0) and (second == 0)):
104104
time_fetch_flag = True
105105

Time Sync with Unix Time/main.py

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,18 @@
3434

3535
gc.collect()
3636
LED = Pin(2, Pin.OUT)
37-
#wdt = WDT()
3837
i2c = I2C(sda = Pin(4), scl = Pin(5), freq = 250000)
3938
oled = OLED1306(i2c)
4039
wifi_sta = network.WLAN(network.STA_IF)
4140
rtcc = RTC()
4241
rtcc.datetime((1970, 1, 1, 0, 0, 0, 0, 0))
42+
ts = unix_timestamp()
4343
unx = unix(+6)
44-
ts = unix_timestamp("Asia", "Dhaka")
4544

4645

4746
def connect_and_check_wifi_status():
4847
global ip, connection_status, LED_state
49-
50-
#
48+
5149
sleep_ms(900)
5250

5351
if(wifi_sta.isconnected()):
@@ -59,66 +57,62 @@ def connect_and_check_wifi_status():
5957
else:
6058
wifi_sta.active(True)
6159
wifi_sta.disconnect()
62-
#
6360
sleep_ms(30)
6461
wifi_sta.connect(WiFi_Credentials.SSID, WiFi_Credentials.password)
65-
#
6662
sleep_ms(60)
6763
print("Network Unavailable!" + "\r\n")
6864
LED_state ^= 0x01
6965
LED.value(LED_state)
70-
#
7166
sleep_ms(9)
7267
connection_status = False
7368

7469

70+
oled.fill(oled.BLACK)
71+
oled.show()
72+
73+
7574
while(True):
76-
try:
77-
connect_and_check_wifi_status()
78-
year, month, date, day, hour, minute, second, tz = rtcc.datetime()
79-
80-
oled.fill(oled.BLACK)
81-
oled.text("Unix Time Sync ", 12, 0, oled.WHITE)
82-
83-
if(past_second != second):
84-
LED_state ^= 0x01
85-
LED.value(LED_state)
86-
oled.text("Unix Time Sync ", 12, 0, oled.WHITE)
87-
oled.text(("Time: " + str("%02u:" %hour) + str("%02u:" %minute) + str("%02u" %second)), 0, 42, oled.WHITE)
88-
oled.text(("Date: " + str("%02u/" %date) + str("%02u/" %month) + str("%4u" %year)), 0, 56, oled.WHITE)
89-
90-
print("Time: " + str("%02u:" %hour) + str("%02u:" %minute) + str("%02u" %second)
91-
+ " Date: " + str("%02u/" %date) + str("%02u/" %month) + str("%4u" %year))
75+
connect_and_check_wifi_status()
76+
year, month, date, day, hour, minute, second, tz = rtcc.datetime()
77+
78+
oled.fill(oled.BLACK)
79+
oled.text("Unix Time Sync ", 8, 0, oled.WHITE)
80+
81+
if(second != past_second):
82+
LED_state ^= 0x01
83+
LED.value(LED_state)
84+
oled.text(("Time: " + str("%02u:" %hour) + str("%02u:" %minute) + str("%02u" %second)), 0, 42, oled.WHITE)
85+
oled.text(("Date: " + str("%02u/" %date) + str("%02u/" %month) + str("%4u" %year)), 0, 56, oled.WHITE)
86+
87+
print("Time: " + str("%02u:" %hour) + str("%02u:" %minute) + str("%02u" %second)
88+
+ " Date: " + str("%02u/" %date) + str("%02u/" %month) + str("%4u" %year))
9289

93-
if(connection_status == False):
94-
oled.text("Network Error!", 0, 14, oled.WHITE)
95-
oled.text("....", 0, 28, oled.WHITE)
96-
time_sync_status = False
90+
past_second = second
91+
92+
if(connection_status == False):
93+
oled.text("Network Error!", 0, 14, oled.WHITE)
94+
oled.text("....", 0, 28, oled.WHITE)
95+
time_sync_status = False
9796

98-
else:
99-
if(time_sync_status == False):
100-
if(second == 30):
101-
time_fetch_flag = True
102-
time_sync_status = True
103-
104-
if(time_sync_status == True):
105-
if(((hour % sync_hour) == 0) and (minute == 0) and (second == 0)):
106-
time_fetch_flag = True
107-
108-
if(time_fetch_flag == True):
109-
oled.text("*", 120, 46, oled.WHITE)
110-
unix_time_stamp = ts.fetch_data()
111-
year, month, date, hour, minute, second = unx.unix_to_date_time(unix_time_stamp)
112-
rtcc.datetime((year, month, date, day, hour, minute, second, tz))
113-
time_fetch_flag = False
114-
115-
oled.text(("WiFi: " + WiFi_Credentials.SSID), 0, 14, oled.WHITE)
116-
oled.text(("UNIX:" + str("%u" %unix_time_stamp)), 0, 28, oled.WHITE)
97+
else:
98+
if(time_sync_status == False):
99+
if(second == 30):
100+
time_fetch_flag = True
101+
time_sync_status = True
117102

118-
oled.show()
119-
103+
else:
104+
if(((hour % sync_hour) == 0) and (minute == 0) and (second == 0)):
105+
time_fetch_flag = True
106+
107+
if(time_fetch_flag == True):
108+
oled.text("*", 120, 46, oled.WHITE)
109+
unix_time_stamp = ts.fetch_data()
110+
year, month, date, hour, minute, second = unx.unix_to_date_time(unix_time_stamp)
111+
rtcc.datetime((year, month, date, day, hour, minute, second, tz))
112+
time_fetch_flag = False
120113

121-
except KeyboardInterrupt:
122-
print("End")
123-
114+
oled.text(("WiFi: " + WiFi_Credentials.SSID), 0, 14, oled.WHITE)
115+
oled.text(("UNIX: " + str("%u" %unix_time_stamp)), 0, 28, oled.WHITE)
116+
117+
oled.show()
124118

Time Sync with Unix Time/unix_timestamp.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33

44

55
class unix_timestamp():
6-
def __init__(self, _continent, _city):
7-
self.continent = _continent
8-
self.city = _city
9-
10-
116
def connect_url(self):
12-
url = 'http://api.aladhan.com/v1/currentTimestamp?zone=' + self.continent + "/" + self.city
7+
url = 'http://api.aladhan.com/v1/currentTimestamp?zone=Europe/London'
138
return url
149

1510

1611
def fetch_data(self):
1712
return_data = 0
1813
requested_data = requests.get(self.connect_url())
1914
return_data = requested_data.json().get('data')
20-
return int(return_data)
21-
22-
23-
15+
return int(return_data)

0 commit comments

Comments
 (0)