-
Notifications
You must be signed in to change notification settings - Fork 0
/
orangepisensors.py
189 lines (162 loc) · 5.25 KB
/
orangepisensors.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
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
from datetime import datetime, timedelta
import os
import logging
import socket
import subprocess
import sqlite3
import requests
import ntplib
import logging
import yaml
import io, fcntl
class server(object):
"""Simple server connection class."""
def __init__(self, addr, port, subsite='', method='POST'):
super(server, self).__init__()
self.addr = addr
self.port = port
self.subsite = subsite
self.method = method
def __repr__(self):
return self.addr+":"+str(self.port)+'/'+self.subsite
def __str__(self):
return self.addr+":"+str(self.port)+'/'+self.subsite
class i2c(object):
def __init__(self, device, bus):
I2C_SLAVE=0x0703
self.fr = io.open("/dev/i2c-"+str(bus), "rb", buffering=0)
self.fw = io.open("/dev/i2c-"+str(bus), "wb", buffering=0)
# set device address
fcntl.ioctl(self.fr, I2C_SLAVE, device)
fcntl.ioctl(self.fw, I2C_SLAVE, device)
def write(self, bytes):
self.fw.write(bytes)
def read(self, bytes):
return self.fr.read(bytes)
def close(self):
self.fw.close()
self.fr.close()
def blink(led=0):
red = "red\:status"
green = "green\:pwr"
if led == 0:
colour = red
else:
colour = green
batcmd="cat /sys/class/leds/orangepi\:"+colour+"/brightness"
result = subprocess.check_output(batcmd, shell=True)
if (int(result) > 0):
os.system("echo 0 > /sys/class/leds/orangepi\:"+colour+"/brightness")
elif (int(result)==0):
os.system("echo 1 > /sys/class/leds/orangepi\:"+colour+"/brightness")
def checkntp(url='localhost'):
cnt = ntplib.NTPClient()
offs = 999
while offs**2>10:
try:
logging.info('NTP works fine')
response = cnt.request('localhost', version=3)
offs = response.offset
except:
logging.warning('NTP error restarting ...')
os.system('systemctl restart ntp')
time.sleep(10)
return response.offset
def readbit(inp,bit):
'''
Read bit data from SEN0177 (16-bytes)
'''
return (inp[bit] << 8) + inp[bit+1]
def checkval(inp):
'''
Calcaulate checksum for SEN0177 data
'''
val=0
for i in range(30):
val=val + inp[i]
return val
def date2matlab(dt):
ord = dt.toordinal()
mdn = dt + timedelta(days = 366)
frac = (dt-datetime(dt.year,dt.month,dt.day,0,0,0)).seconds / (24.0 * 60.0 * 60.0)
return mdn.toordinal() + frac
def filetowrite(datadir,filenm,parameters):
directory = datadir+'/' + datetime.utcnow().strftime("%Y%m")
if not os.path.exists(directory):
os.makedirs(directory)
name = "/"+filenm+'_'+datetime.utcnow().isoformat()[:10]+".csv"
fname = directory + name
if os.path.isfile(fname) == False:
f = open(fname,'w')
f.write("'STATIONID','YEAR','MONTH','DAY','HOUR','MINUTE','SECOND','TIME',"+''.join(["'"+x+"'," for x in parameters])[:-1]+'\n')
f.close()
return fname
def savetofile(datadir,filenm,id,parameters,measurements):
fname=filetowrite(datadir,filenm,parameters)
if type(measurements) is not list:
logging.warning("Measurements error, no data")
return 2
logging.warning("Writing to file %s" % fname)
with open(fname, 'a') as f:
for ms in measurements:
f.write(str(id)+','+str(ms[0].year)+','+str(ms[0].month)+','+str(ms[0].day)+','+str(ms[0].hour)+','+str(ms[0].minute)+','+str(ms[0].second)+','+str(date2matlab(ms[0]))+','+''.join([str(x)+',' for x in ms[1]])[:-1]+'\n')
return fname
def sendtodb(dbname,id,sensor,parameters,measurements):
try:
conn = sqlite3.connect(dbname)
c = conn.cursor()
except Exception as e:
logging.critical('SQLite errror: %s' % e)
if type(measurements) is not list:
logging.warning("Measurements error, no data")
return 2
logging.warning("Saving to database SQLite %s" % dbname)
savevalues=[]
for ms in measurements:
dt = ms[0].strftime('%Y-%m-%d %H:%M:%S')
for v in range(len(ms[1])):
savevalues.append((id,dt,sensor,parameters[v],ms[1][v]))
c.executemany('INSERT INTO msmt VALUES (?,?,?,?,?)', savevalues)
conn.commit()
conn.close()
return dbname
def readconf(file):
with open(file) as f:
li = yaml.load(f)
return li
def gethosint():
return int(socket.gethostname().encode('utf-8').hex(),16)
def sendtosrv(srv,id,sensor,parameters,measurements):
''' Function to calcaulate mean value and send it to server. Time resolution is minutely.'''
if type(measurements) is not list:
logging.warning("Measurements error, wrond datatype")
return 2
if len(measurements) == 0:
logging.warning("Measurements error, empty table.")
return 3
try:
msmtminute = datetime.strptime(measurements[int(len(measurements)/2)][0].strftime('%Y-%m-%d %H:%M:00'),'%Y-%m-%d %H:%M:%S').timestamp()
lenparams = len(parameters)
lenmsmt = len(measurements)
values=[]
for ms in range(lenmsmt):
for v in range(lenparams):
values.insert(ms*lenparams+v,measurements[ms][1][v])
values = ''.join([ str(round((sum(values[i::lenparams])/lenmsmt),2))+'|' for i in range(lenparams)])
parameters = ''.join(s+'|' for s in parameters)
payload = {
'station' : id,
'sensor' : sensor,
'date' : msmtminute,
'parameters' : parameters,
'values' : values,
'count' : lenparams,
## TODO: create proper CRC function or hash/oath to verify secret keys
'crc' : 13
}
logging.warning("Values: %s, Sending to server: %s" % (values,str(srv)))
r = requests.post(str(srv),params=payload)
return r.status_code
except Exception as e:
logging.critical('Sending error: %s' % e)
return 4