Skip to content

Commit 9af4ec5

Browse files
MarcusMarcus
Marcus
authored and
Marcus
committed
First version
1 parent 27a6c48 commit 9af4ec5

File tree

6 files changed

+405
-0
lines changed

6 files changed

+405
-0
lines changed

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# backup files
2+
*~
3+
*.bak
4+
5+
#### githup standards:
6+
7+
# Prerequisites
8+
*.d
9+
10+
# Compiled Object files
11+
*.slo
12+
*.lo
13+
*.o
14+
*.obj
15+
16+
# Precompiled Headers
17+
*.gch
18+
*.pch
19+
20+
# Compiled Dynamic libraries
21+
*.so
22+
*.dylib
23+
*.dll
24+
25+
# Fortran module files
26+
*.mod
27+
*.smod
28+
29+
# Compiled Static libraries
30+
*.lai
31+
*.la
32+
*.a
33+
*.lib
34+
35+
# Executables
36+
*.exe
37+
*.out
38+
*.app

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Simple bash scripts to read temperature data from SwitchBot devices
2+
3+
This is a collection of linux bash shell scripts for reading
4+
SwitchBot's temperature sensors. The scripts use `bluetoothctl` to
5+
receive the data from the devices. Therefore the package `bluez` of
6+
your linux-distribution must be installed.
7+
8+
The scripts must either have execution rights or be started with `bash`.
9+
10+
## `scan_sensors`
11+
12+
This is a simple script to search for the sensors and get their
13+
address (MAC). It runs endless and must be aborted with `Ctrl-C`
14+
when all sensors have been found.
15+
16+
The sensors should be close to the receiver to ensure proper reception.
17+
18+
To speed up the process, you should press and hold the bluetooth
19+
button of the devices until the LED flashes or the bluetooth symbol
20+
appears on the display. This causes the devices to send data even
21+
though no value has changed.
22+
23+
Example:
24+
```
25+
$ bash scan_sensors
26+
Scanning for sensors. Use Ctrl-C to exit ...
27+
Found WoSensorTH: XX:XX:XX:XX:XX:XX
28+
Found WoIOSensorTH: YY:YY:YY:YY:YY:YY
29+
30+
```
31+
32+
You need these MAC values to configure the `sensor_config` file.
33+
34+
## Configure your sensors
35+
36+
The file `sensor_config` contains the sensors configurations. Edit the
37+
file to insert the MAC addresses of your devices. Make sure you have
38+
either modified or removed the samples in this file. Otherwise, the
39+
tools will prompt you to edit the file.
40+
41+
## ``
42+
43+
## `log_sensors`
44+
45+
This tool scans for bluetooth messages and filter out the SwitchBot
46+
temperature messages.
47+
48+
```
49+
$ bash log_sensors
50+
2023-07-08 12:01:42 EA:D8:03:13:C0:33: outdoor sensor 30.0°C / 30%
51+
2023-07-08 12:02:38 E4:16:56:54:8F:5E: room sensor 22.8°C / 47%
52+
2023-07-08 12:05:33 EA:D8:03:13:C0:33: outdoor sensor 30.1°C / 30%
53+
2023-07-08 12:11:50 E4:16:56:54:8F:5E: room sensor 22.8°C / 47%
54+
2023-07-08 12:14:46 E4:16:56:54:8F:5E: room sensor 22.8°C / 48%
55+
2023-07-08 12:21:28 EA:D8:03:13:C0:33: outdoor sensor 30.3°C / 31%
56+
```
57+
58+
A new row will appear each time one of your SwitchBot sensors sends
59+
new data. It may take a while before the first values are
60+
displayed. The tool does not actively poll the device, but waits for
61+
the sensors to send data.
62+
63+
You may want to redirect the output into a log file. Then use
64+
```
65+
bash log_sensors > sensors.log
66+
```
67+
or
68+
```
69+
bash log_sensors | tee sensors.log
70+
```
71+
72+
## `show_sensors`
73+
74+
This tool shows a table of all configured sensors with the data
75+
received from the sensors. Shortly after the start, the output looks
76+
like this:
77+
78+
```
79+
Name Temp Humidity Last Update
80+
==================== ======== ======== ===================
81+
room sensor no data no data -
82+
outdoor sensor no data no data -
83+
```
84+
85+
After a while the tool has retrieved some data and shows the last
86+
received values from the sensors:
87+
88+
```
89+
Name Temp Humidity Last Update
90+
==================== ======== ======== ===================
91+
room sensor 23.2°C 51% 2023-07-09 08:43:29
92+
outdoor sensor 25.0°C 51% 2023-07-09 08:40:33
93+
```
94+
95+
The last column shows when the last data was received. For sensors
96+
that are further away, it can take me more than an hour to receive
97+
data.

log_sensors

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2023 [email protected]
4+
#
5+
# Permission is hereby granted, free of charge, to any person
6+
# obtaining a copy of this software and associated documentation files
7+
# (the "Software"), to deal in the Software without restriction,
8+
# including without limitation the rights to use, copy, modify, merge,
9+
# publish, distribute, sublicense, and/or sell copies of the Software,
10+
# and to permit persons to whom the Software is furnished to do so,
11+
# subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be
14+
# included in all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
. sensor_config
26+
27+
if [ "${sensors[XX:XX:XX:XX:XX:XX]}" != "" -o \
28+
"${sensors[YY:YY:YY:YY:YY:YY]}" != "" ]
29+
then
30+
echo please edit \'sensor_config\' to configure your sensors
31+
exit 1
32+
fi
33+
34+
if [ "${#sensors[@]}" -eq 0 ]
35+
then
36+
echo no sensors configured. please edit your \'sensor_config\'
37+
exit 1
38+
fi
39+
40+
data_mac=
41+
stdbuf -oL bluetoothctl scan on |
42+
while read p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 remains
43+
do
44+
if [ "$data_mac" != "" ]
45+
then
46+
# data line
47+
tstamp=$(date +"%Y-%m-%d %H:%M:%S")
48+
49+
temppt=$(( 0x$p9 & 0x0F )) # temperature after decimal point
50+
temp=$(( 0x$p10 & 0x7F )) # temperature before decimal point
51+
52+
isPos=$(( 0x$p10 & 0x80 )) # 128=positive / 0=negative
53+
if [ $isPos -eq 0 ]
54+
then
55+
temp=$((-$temp))
56+
fi
57+
58+
humidity=$(( 0x$p11 & 0x7F )) # humidity%
59+
60+
printf "%s %s %s: %-20s %3d.%1d°C / %2d%%\n" \
61+
$tstamp $data_mac "${sensors[$data_mac]}" \
62+
$temp $temppt $humidity
63+
64+
data_mac="" # search next
65+
elif [ "$p3" != "" ]
66+
then
67+
if [ "${sensors[$p3]}" != "" ]
68+
then
69+
# found a known sensor
70+
if [ "$p4" == "ManufacturerData" -a "$p5" == "Value:" ]
71+
then
72+
# next line ist sensor data
73+
data_mac=$p3
74+
fi
75+
fi
76+
fi;
77+
done

scan_sensors

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2023 [email protected]
4+
#
5+
# Permission is hereby granted, free of charge, to any person
6+
# obtaining a copy of this software and associated documentation files
7+
# (the "Software"), to deal in the Software without restriction,
8+
# including without limitation the rights to use, copy, modify, merge,
9+
# publish, distribute, sublicense, and/or sell copies of the Software,
10+
# and to permit persons to whom the Software is furnished to do so,
11+
# subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be
14+
# included in all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
declare -A sensors
26+
27+
echo Scanning for sensors. Use Ctrl-C to exit ...
28+
29+
stdbuf -oL bluetoothctl scan on |
30+
while read p1 p2 p3 remains
31+
do
32+
if [ "$p2" = "Device" ]
33+
then
34+
if [ "${sensors[$p3]}" = "" ]
35+
then
36+
name=$(
37+
bluetoothctl info $p3 |
38+
grep "Name:" |
39+
sed 's/^\tName:[ ]*\([^ ]*\)/\1/g')
40+
if [ "$name" = "WoSensorTH" -o "$name" = "WoIOSensorTH" ]
41+
then
42+
echo Found $name: $p3
43+
fi
44+
sensors["$p3"]="found"
45+
fi
46+
fi
47+
done

sensor_config

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2023 [email protected]
4+
#
5+
# Permission is hereby granted, free of charge, to any person
6+
# obtaining a copy of this software and associated documentation files
7+
# (the "Software"), to deal in the Software without restriction,
8+
# including without limitation the rights to use, copy, modify, merge,
9+
# publish, distribute, sublicense, and/or sell copies of the Software,
10+
# and to permit persons to whom the Software is furnished to do so,
11+
# subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be
14+
# included in all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20+
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21+
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
declare -A sensors
26+
27+
# Sensors that `log_sensors` and `show_sensors` should look for.
28+
# Remove or edit the examples below.
29+
30+
# sensors["<MAC address>"]="<description>"
31+
sensors["XX:XX:XX:XX:XX:XX"]="room sensor"
32+
sensors["YY:YY:YY:YY:YY:YY"]="outdoor sensor"

0 commit comments

Comments
 (0)