-
Notifications
You must be signed in to change notification settings - Fork 2
/
listen.py
35 lines (28 loc) · 1.01 KB
/
listen.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
import subprocess as sub
import re
global packet
# Line buffers the tcpdump output
def lineBuffer():
packet = 0
p = sub.Popen(('sudo', 'tcpdump', '-l', 'udp', '-X'), stdout=sub.PIPE)
for row in iter(p.stdout.readline, b''):
# Process the data here
if re.search(b'length 340', row.rstrip()):
packet = 1
continue
if packet == 1 and re.search(b'x0030', row.rstrip()):
# Data selected from output
depth = str(int(str(row.rstrip()[17:19], 'utf-8'), 16))
dep_f = str(int(str(row.rstrip()[22:24], 'utf-8'), 16))
temp = str(int(str(row.rstrip()[25:27], 'utf-8'), 16))
batt = int(str(row.rstrip()[35:37], 'utf-8'), 16)
batt_f = int(str(row.rstrip()[37:39], 'utf-8'), 16)
# Converting battery voltage into battery life
batt_volt = batt + batt_f * 0.01
batt_life = ((batt_volt-2.4)/1.8)*100
print ("Depth: " + depth + "." + dep_f)
print ("Temperature: " + temp)
print ("Battery Voltage: %.2f" % batt_volt)
print ("Battery Life: %.2f%%\n" % batt_life)
packet = 0
lineBuffer()