-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny_receive.py
executable file
·289 lines (238 loc) · 9.14 KB
/
tiny_receive.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
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
#!/usr/bin/env python
# porttest5.py: decode binary format and clear text format. readline() does not work anymore as \n can be part of the payload. stable version.
# porttest6.py: indicate if binary or clear text format was detected (for debug purposes only)
# porttest7.py: use ISO week for file name generation, not python style week number
# porttest8.py: Error log and last log go into temporary tmpfs /var/tmp
# porttest9.py: don't log erroneous lines if status register does not contain DQD or CRL
# prttest10.py: experimental build for FEC decoding
# rename to tiny_receive.py
#
import serial
import time, datetime
import sys
import os
import string
import struct
def record_filename(loctime):
#th_week = int ( time.strftime('%W', loctime) ) + 1
th_week= datetime.date.fromtimestamp(time.time()).isocalendar()[1]
if th_week > 9:
filename = '%s%s_%i_tinyrx_log.csv' % ( verzeichnis, time.strftime('%Y', loctime), th_week )
else:
filename = '%s%s_0%i_tinyrx_log.csv' % ( verzeichnis, time.strftime('%Y', loctime), th_week )
return filename
def write_logfile(logfilename, msg):
logfile=open(logfilename,'a')
logfile.write(msg)
logfile.write('\n')
logfile.close()
def extract_data(key, results):
key_result_pair =''
if key in results.keys():
key_result_pair = ",%s,%s" % (key , str(results[key]))
return key_result_pair
hamming_codes = (0x15, 0x02, 0x49, 0x5e, 0x64, 0x73, 0x38, 0x2f, 0xd0, 0xc7, 0x8c, 0x9b, 0xa1, 0xb6, 0xfd, 0xea)
#decode a byte and return a decoded nibble - if possible
def decode_hamming(byte, codes = hamming_codes):
if byte in codes:
decoded_val = codes.index(byte)
return decoded_val, 0
else:
solution = 0
for code in codes:
if bin(code ^ byte).count("1") <= 1:
return codes.index(code), 1
if solution == 0:
return -1, 2
#decode an int and return a decoded byte
def decode_hamming_int (intval, codes = hamming_codes):
biterrors = 0
lownibble, biterror = decode_hamming(intval &0xff, codes)
biterrors += biterror
if biterror > 1:
return -1, biterrors
hinibble, biterror = decode_hamming(intval>>8, codes)
biterrors += biterror
if biterror > 1:
return -1, biterrors
return (hinibble<<4 | lownibble), (biterrors)
#get 2 ints from struct.unpack and calculate the value (16 bit int)
def decode_hamming_2ints(int_tuple, codes = hamming_codes):
biterrors =0
highbyte, biterror = decode_hamming_int (int_tuple[0]&0xffff, codes)
biterrors += biterror
lowbyte, biterror = decode_hamming_int (int_tuple[1]&0xffff, codes)
biterrors += biterror
return highbyte<<8 | lowbyte, biterrors
#give me a string with 3 bytes and decode it into a 12-bit number
def decode_hamming_3_bytes(payload, codes = hamming_codes):
val=0
biterrors_total =0
biterrors_max_per_byte = 0
for i in range(0,3):
val <<=4
dec_val, biterrors = decode_hamming (ord(payload[i]), codes) # high byte, low nibble
val = val | dec_val
biterrors_total += biterrors
if biterrors > biterrors_max_per_byte:
biterrors_max_per_byte = biterrors
return val, biterrors_total, biterrors_max_per_byte
def read_from_port (port):
raw_line = port.read(port.inWaiting())
if '\r\n' not in raw_line[-2:]:
done = False
while not done:
if port.inWaiting() > 0:
crlf = port.read(port.inWaiting())
raw_line += crlf
if '\r\n' in raw_line[-2:]: done = True
else:
time.sleep(.001) # 1 ms
line = raw_line[:-2]
return line
def decode_rfm12b_afc_drssi(sreg):
afc= sreg & 0xf
if sreg & 0x10:
afc=-((afc^0xf)+1) # 2's complement
return "a,%s,s,%s" % (afc, sreg>>8) #drssi bit
os.environ['TZ'] = 'Europe/Paris'
time.tzset()
#default filename for valid records
filename = "tinyRx_record.csv"
#filename for error packets
errorlogfile = "tinyRx_error.csv"
#filename for the last log
last_log_filename = 'last_tinytx_log.txt'
#default directory for logfiles
verzeichnis = '/var/www/logfiles/'
#default directory for temporary files
tmp_verzeichnis = '/var/tmp/'
#Oeffne Seriellen Port
port = serial.Serial('/dev/ttyAMA0',9600)
# timeout?
if not port.isOpen():
print "cannot connect to serial port."
sys.exit(1)
results={}
last_log_dict= {}
#fill last_log_dict with existing entries
fn = '%s%s' % (tmp_verzeichnis, last_log_filename)
f = open(fn, 'r')
thefile = f.read().split('\n')
f.close()
for line in thefile:
sensor = line.split(',')
if len(sensor) > 3:
last_log_dict[sensor[4]] = line
#Main loop
while (True):
if port.inWaiting() > 0:
time.sleep(0.1)
line = read_from_port(port)
else:
time.sleep(0.1) # wait 100ms
continue
#print line
loctime = time.localtime(time.time())
zeit = time.strftime('%a,%d.%m.%Y,%H:%M:%S', loctime)
results.clear()
isvalid = False
cleartext = False
# some experimental code
if "BAD-CRC," in line:
msg = line.split(',')
node = msg[1]
statreg = int(msg[2][2:], 16)
payload = msg[3][5:] # format is : "data=xxyyzz" but we only want "xxyyzz"
else:
i= string.find(line, " ")
if i>0:
node = line[:i]
msg = line[i+1:]
isvalid = True
if "v=" in msg and "&t=" in msg:
cleartext = True
i = string.rfind(msg, '&s=') # i points to '&' in '&s='
statreg = int(msg[i+3:], 16)
payload = msg[:i]
else:
node ='0'
msg=''
print "no space in string:"
continue
logstring = "%s,n,%s,%s" % (zeit, node, decode_rfm12b_afc_drssi(statreg))
is_valid_node = False
if isvalid and cleartext:
is_valid_node = True
# old verbose output, php compatible
vals = payload.split('&')
for messung in vals: # build dictionary
itemlst = messung.split('=')
if len(itemlst) > 1:
results[itemlst[0]] = itemlst[1]
logstring = "%s%s%s%s%s" % (logstring, extract_data('v', results),
extract_data('t', results),
extract_data('h', results),
extract_data('c', results))
elif not cleartext:
if node == '3' or node =='26':
is_valid_node = True
# binary mode: don't care if CRC valid or not
datalen = len(payload)
#print "Datenlaenge: %d" % datalen
biterrors_total = 0
biterrors_max_per_byte =0
if datalen >=3:
vcc, biterrors_total, biterrors_max_per_byte = decode_hamming_3_bytes(payload)
# print " VCC: %i, %i biterrors" % (vcc, biterrors_total)
logstring = "%s,v,%s" % (logstring, vcc)
if datalen >=6:
temp, biterrors, biterrors_max_pb = decode_hamming_3_bytes(payload[3:])
biterrors_total += biterrors
if biterrors_max_pb > biterrors_max_per_byte:
biterrors_max_per_byte = biterrors_max_pb
temp = (temp-1000)*4
#print "Temperature: %i, %i biterrors" % (temp, biterrors_total)
logstring = "%s,t,%s" % (logstring, temp)
if datalen >=8:
hum=0
for i in range(6,8):
hum <<=4
dec_val, biterrors = decode_hamming (ord(payload[i])) # high byte, low nibble
hum = hum | dec_val
biterrors_total += biterrors
if biterrors > biterrors_max_per_byte:
biterrors_max_per_byte = biterrors
hum = hum * 50
# print " Humidity: %i, %i biterrors" % (hum, biterrors_total)
logstring = "%s,h,%s" % (logstring, hum)
if isvalid:
if biterrors_max_per_byte == 0:
logstring += ",f,OK"
if biterrors_max_per_byte > 0: # does happen when we remove CRC completely
logstring += ",f,%i" % biterrors_total
else:
if biterrors_max_per_byte ==0: # BAD CRC but decoding was successful - the error is in the CRC!
logstring += ",f,0"
if 2 > biterrors_max_per_byte and biterrors_max_per_byte > 0:
logstring += ",f,%i" % biterrors_total
else:
logstring += ",f,fail,%i" % biterrors_total
print "LogString: %s" % logstring
if is_valid_node:
write_logfile(record_filename(loctime), logstring)
#update file with latest records for each node
last_log_dict[node] = logstring
fn = '%s%s' % (tmp_verzeichnis, last_log_filename)
last_log = open(fn,'w')
for key in last_log_dict:
last_log.write(last_log_dict[key])
last_log.write('\n')
last_log.close()
else:
if statreg & 0xC0:
errstr= "%s,%s" % (zeit,line)
logfilename = '%s%s' % (tmp_verzeichnis, errorlogfile)
write_logfile(logfilename, errstr)
else:
pass # do nothing, forget received line