-
Notifications
You must be signed in to change notification settings - Fork 7
/
AnySense_RTC.py
155 lines (127 loc) · 3.6 KB
/
AnySense_RTC.py
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
import mraa
import time
import string
import os
import random
import sys,getopt
import hmac
import hashlib
import base64
from threading import Timer
from datetime import datetime
TimeURL = "https://pm25.lass-net.org/util/timestamp.php"
DS3231_I2C_ADDR = 0x68
DS3231_TIME_CAL_ADDR = 0x00
def dec2bcd(v):
return ((v/10*16)+(v%10))
def bcd2dec(v):
return ((v/16*10)+(v%16))
def rtc_set_time(t):
rtc = mraa.I2c(0)
rtc.address(DS3231_I2C_ADDR)
tyear = t.year
tweek = dec2bcd(t.weekday())
tweek = t.weekday()
tmon = dec2bcd(t.month)
tday = dec2bcd(t.day)
thour = dec2bcd(t.hour)
tmin = dec2bcd(t.minute)
tsec = dec2bcd(t.second)
tyear_s = dec2bcd(t.year - 2000)
rtc.writeReg(0x00, tsec & 0xff)
rtc.writeReg(0x01, tmin & 0xff)
rtc.writeReg(0x02, thour & 0xff)
rtc.writeReg(0x03, tweek & 0xff)
rtc.writeReg(0x04, tday & 0xff)
rtc.writeReg(0x05, tmon & 0xff)
rtc.writeReg(0x06, tyear_s & 0xff)
def rtc_get_time():
rtc = mraa.I2c(0)
rtc.address(DS3231_I2C_ADDR)
tsec = bcd2dec(rtc.readReg(0x00))
tmin = bcd2dec(rtc.readReg(0x01))
thour = bcd2dec(rtc.readReg(0x02))
tweek = rtc.readReg(0x03)
tday = bcd2dec(rtc.readReg(0x04))
tmon = bcd2dec(rtc.readReg(0x05))
tyear_s = bcd2dec(rtc.readReg(0x06))
tyear = 2000 + tyear_s
t = str(tyear) + "-" + str(tmon) + "-" + str(tday) + " " + str(thour) + ":" + str(tmin) + ":" + str(tsec)
t = datetime.strptime(t, "%Y-%m-%d %H:%M:%S")
return t
def ntp_is_running():
try:
command = "wget -O /tmp/time1 " + TimeURL
os.system(command)
fh = open("/tmp/time1","r")
time1 = fh.read()
time1 = datetime.strptime(time1, "%Y-%m-%d %H:%M:%S")
except:
time1 = datetime.utcnow()
return 0 , time1
time2 = datetime.utcnow()
delta_time = time2 - time1
if abs(delta_time.seconds) < 30:
return 1, time2
else:
return -1, time1
def set_key(key):
mac = open('/sys/class/net/eth0/address').readline().upper().strip()
DEVICE_ID = mac.replace(':','')
dig = hmac.new(key, msg=DEVICE_ID, digestmod=hashlib.sha256).digest()
key = base64.b64encode(dig).decode() # py3k-mode
#print "key =", key[:7]
# use the first 7 bytes as the secure key
rtc = mraa.I2c(0)
rtc.address(DS3231_I2C_ADDR)
rtc.writeReg(0x07, ord(key[0]) & 0xff)
rtc.writeReg(0x08, ord(key[1]) & 0xff)
rtc.writeReg(0x09, ord(key[2]) & 0xff)
rtc.writeReg(0x0A, ord(key[3]) & 0xff)
rtc.writeReg(0x0B, ord(key[4]) & 0xff)
rtc.writeReg(0x0C, ord(key[5]) & 0xff)
rtc.writeReg(0x0D, ord(key[6]) & 0xff)
# for debug usage: print out the 7 bytes
#print chr(rtc.readReg(0x07))
#print chr(rtc.readReg(0x08))
#print chr(rtc.readReg(0x09))
#print chr(rtc.readReg(0x0A))
#print chr(rtc.readReg(0x0B))
#print chr(rtc.readReg(0x0C))
#print chr(rtc.readReg(0x0D))
def main(argv):
Valid_Command = " -k <key> -d <delay>"
DELAY = random.random()*60
try:
opts, args = getopt.getopt(argv,"hd:k:",["delay=","key="])
except getopt.GetoptError:
print str(os.path.basename(__file__)) + Valid_Command
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print str(os.path.basename(__file__)) + Valid_Command
sys.exit()
elif opt in ("-k", "--key"):
set_key(arg)
elif opt in ("-d", "--delay"):
DELAY = arg
return DELAY
if __name__ == '__main__':
delay = main(sys.argv[1:])
time.sleep(float(delay))
t = rtc_get_time()
print "RTC time: ", t
status, t = ntp_is_running()
if status == 1:
rtc_set_time(t)
elif status == -1:
t = t.strftime("%Y-%m-%d %H:%M:%S")
command = "date -u -s \"" + t + "\""
os.system(command)
elif status == 0:
t = rtc_get_time()
t = t.strftime("%Y-%m-%d %H:%M:%S")
command = "date -u -s \"" + t + "\""
os.system(command)
else:
print "Error in RTC"