Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
firmware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
card10
firmware
Commits
c63eaa95
Commit
c63eaa95
authored
3 years ago
by
schneider
Browse files
Options
Downloads
Patches
Plain Diff
iaq: let script write directly to influxdb
parent
65e10a47
Branches
Branches containing commit
No related tags found
1 merge request
!508
tools: add IAQ example in Python
Pipeline
#5455
failed
3 years ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tools/iaq/card10_iaq_notify.py
+57
-31
57 additions, 31 deletions
tools/iaq/card10_iaq_notify.py
with
57 additions
and
31 deletions
tools/iaq/card10_iaq_notify.py
+
57
−
31
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
:
...
...
@@ -36,30 +43,28 @@ if len(devices)>1:
print
(
""
)
elif
len
(
devices
)
==
1
:
device
=
devices
[
0
]
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
}
return
{
'
temperature
'
:
tmp
[
0
]
/
100
}
def
p_humidity
(
data
):
tmp
=
struct
.
unpack
(
'
H
'
,
data
)
t
=
tmp
[
0
]
return
{
'
humidity
'
:
t
/
100
}
return
{
'
humidity
'
:
tmp
[
0
]
/
100
}
def
p_pressure
(
data
):
tmp
=
struct
.
unpack
(
'
I
'
,
data
)
t
=
tmp
[
0
]
return
{
'
pressure
'
:
t
/
1000
}
return
{
'
pressure
'
:
tmp
[
0
]
/
1000
}
def
p_iaq
(
data
):
names
=
[
'
iaq_accuracy
'
,
'
iaq
'
,
'
co2
'
]
...
...
@@ -81,19 +86,40 @@ for suuid,svc in services.items():
for
uuid
,
char
in
chars
.
items
():
# print(" - ", uuid)
if
uuid
in
characteristics
:
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
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment