Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
card10
firmware
Commits
c63eaa95
Commit
c63eaa95
authored
Jan 02, 2022
by
schneider
Browse files
iaq: let script write directly to influxdb
parent
65e10a47
Pipeline
#5455
failed with stages
in 3 minutes and 52 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
tools/iaq/card10_iaq_notify.py
View file @
c63eaa95
#!/usr/bin/env python3
# Written 2021 by Stefan `Sec` Zehl
import
argparse
import
struct
import
os
import
select
import
sys
from
bluez
import
Manager
parser
=
argparse
.
ArgumentParser
(
description
=
'Receive IAQ values from Card10'
)
parser
.
add_argument
(
'--mac'
,
help
=
'mac of device to query'
)
parser
.
add_argument
(
'-v'
,
'--verbose'
,
help
=
'help debug'
,
action
=
'count'
,
default
=
0
)
parser
.
add_argument
(
'-m'
,
'--mac'
,
help
=
'mac of device to query'
)
parser
.
add_argument
(
'-i'
,
'--influx'
,
metavar
=
'URL'
,
help
=
'url of influxdb to push to. E.g.: http://influxdb:8086/write?db=card10'
)
args
=
parser
.
parse_args
()
mgr
=
Manager
();
if
args
.
influx
is
not
None
:
import
requests
mgr
=
Manager
()
a
=
mgr
.
get_adapter
()
#print('Using adapter', a)
# Get devices which support environmental sensing
devices
=
a
.
get_devices
(
'0000181a-0000-1000-8000-00805f9b34fb'
)
device
=
None
if
len
(
devices
)
>
1
:
print
(
"Mutiple devices found:"
)
for
d
in
devices
:
print
(
"-"
,
d
.
Name
,
d
.
Address
,
end
=
" "
)
if
d
.
Address
==
args
.
mac
:
print
(
"(selected)"
)
device
=
d
elif
args
.
mac
is
None
and
device
is
None
:
print
(
"(default)"
)
device
=
d
else
:
print
(
""
)
if
d
.
Address
==
args
.
mac
:
print
(
"(selected)"
)
device
=
d
elif
args
.
mac
is
None
and
device
is
None
:
print
(
"(default)"
)
device
=
d
else
:
print
(
""
)
elif
len
(
devices
)
==
1
:
device
=
devices
[
0
]
print
(
"Using device"
,
device
.
Name
,
device
.
Address
)
if
args
.
verbose
:
print
(
"Using device"
,
device
.
Name
,
device
.
Address
)
else
:
print
(
"No capable device found"
)
sys
.
exit
(
1
)
#print('Device:', device)
device
.
connect
()
services
=
device
.
get_gattservices
()
;
services
=
device
.
get_gattservices
()
def
p_temp
(
data
):
tmp
=
struct
.
unpack
(
'=H'
,
data
)
t
=
tmp
[
0
]
return
{
'temp'
:
tmp
[
0
]
/
100
}
tmp
=
struct
.
unpack
(
'=H'
,
data
)
return
{
'temperature'
:
tmp
[
0
]
/
100
}
def
p_humidity
(
data
):
tmp
=
struct
.
unpack
(
'H'
,
data
)
t
=
tmp
[
0
]
return
{
'humidity'
:
t
/
100
}
tmp
=
struct
.
unpack
(
'H'
,
data
)
return
{
'humidity'
:
tmp
[
0
]
/
100
}
def
p_pressure
(
data
):
tmp
=
struct
.
unpack
(
'I'
,
data
)
t
=
tmp
[
0
]
return
{
'pressure'
:
t
/
1000
}
tmp
=
struct
.
unpack
(
'I'
,
data
)
return
{
'pressure'
:
tmp
[
0
]
/
1000
}
def
p_iaq
(
data
):
names
=
[
'iaq_accuracy'
,
'iaq'
,
'co2'
]
tmp
=
struct
.
unpack
(
'=BHH'
,
data
)
tmp
=
struct
.
unpack
(
'=BHH'
,
data
)
return
dict
(
zip
(
names
,
tmp
))
characteristics
=
{
...
...
@@ -79,21 +84,42 @@ fds=[]
for
suuid
,
svc
in
services
.
items
():
chars
=
svc
.
get_gattcharacteristics
()
for
uuid
,
char
in
chars
.
items
():
# print(" - ",uuid)
# print(" - ",
uuid)
if
uuid
in
characteristics
:
print
(
"Found"
,
uuid
)
if
args
.
verbose
:
print
(
"Found"
,
uuid
)
an
=
char
.
AcquireNotify
()
# (fd, mtu)
mapping
[
an
[
0
]]
=
characteristics
[
uuid
]
fds
.
append
(
an
[
0
])
if
len
(
fds
)
<
len
(
characteristics
):
print
(
"WARN: Could only subscribe to %d characteristics"
%
len
(
fds
),
file
=
sys
.
stderr
)
else
:
print
(
"Subscribed to %d characteristics on %s (%s)"
%
(
len
(
fds
),
device
.
Name
,
device
.
Address
))
post
=
None
while
True
:
if
args
.
influx
is
not
None
:
post
=
{}
ready
,
_
,
_
=
select
.
select
(
fds
,[],[])
for
fd
in
ready
:
print
(
"notify:"
,
end
=
" "
)
data
=
os
.
read
(
fd
,
100
)
#read max 100 characters
parse
=
mapping
[
fd
](
data
)
print
(
parse
)
ble_data
=
os
.
read
(
fd
,
100
)
#read max 100 characters
parse
=
mapping
[
fd
](
ble_data
)
if
args
.
influx
is
not
None
:
post
.
update
(
parse
)
else
:
print
(
"notify:"
,
parse
)
if
args
.
influx
is
not
None
:
line
=
" "
.
join
([
"air"
,
","
.
join
([
k
+
"="
+
str
(
v
)
for
k
,
v
in
post
.
items
()])])
if
args
.
verbose
:
print
(
line
)
r
=
requests
.
post
(
args
.
influx
,
data
=
line
)
if
r
.
status_code
!=
204
:
print
(
r
.
status_code
,
r
.
reason
)
#device.disconnect()
print
(
'Exit'
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment