This article has the same content as ** Omron Environment Sensor (BAG type) article **. This article was carried out with the low-priced ** Inkbird IBS-TH1 **. The mini I used last time had a bad battery life, so I aimed to improve the durability with this product with AAA batteries.
A manufacturer in Shenzhen, China, which makes various home IoT sensors. It is attractive that it has all the functions such as data acquisition with smartphone application and API at a low price (about 3000 yen).
This time, it is equipped with a temperature + humidity sensor Inkbird IBS-TH1
Then, logging will be performed.
** ・ RaspberryPi ** (Pi3Model B is used this time) **-Python execution environment ** (This time, pyenv uses Python 3.7.6) ** ・ Inkbird IBS-TH1 ** (used with the probe removed)
** ① Check the Bluetooth connection between the Raspberry Pi and the sensor ** ** ② Get sensor measurements in Python ** ** ③ Hit the GAS API from Python to write data to the spreadsheet ** ** ④ Script execution regularly **
I referred to this. https://qiita.com/bon_dentetsu/items/87ed6c65640b5ba11e5c https://github.com/viyh/inkbird https://github.com/sputnikdev/eclipse-smarthome-bluetooth-binding/issues/60
** ・ Sensor setup ** Insert AAA batteries into the sensor (please note that the lid is difficult to remove)
**-Scan for Bluetooth devices ** Execute the following command on Raspberry Pi
sudo hcitool lescan
LE Scan ...
AA:CC:EE:DD:55:77 sps
If you see the name "sps", this is the MAC address of the sensor. If it does not come out, check the battery contact and the Bluetooth enable of Raspberry Pi. Depending on the model and settings, the name may not be "sps", so In that case, you can check the MAC address at Inkbird Official App.
bluepy is a library for accessing Bluetooth Low Energy (BLE) in Python (class definition)
** ・ Installation of required packages ** Install the following
sudo install libglib2.0-dev
** ・ Install bluepy **
Install with pip with the following command
pip install bluepy
** ・ Grant permissions to bluepy ** You need to give bluepy Sudo privileges for scanning.
Go to the folder where bluepy is installed and
cd ~.pyenv/versions/3.7.6/lib/python3.7/site-packages/bluepy
Grant Sudo privileges to bluepy-helper with the following command
sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper
Create the following script to get the sensor value
inkbird_ibsth1_connect.py
from bluepy import btle
import struct
def get_ibsth1_data(macaddr):
peripheral = btle.Peripheral(macaddr)
characteristic = peripheral.readCharacteristic(0x28)
(temp, humid, unknown1, unknown2, unknown3) = struct.unpack('<hhBBB', characteristic)
sensorValue = {
'Temperature': temp / 100,
'Humidity': humid / 100,
'unknown1': unknown1,
'unknown2': unknown2,
'unknown3': unknown3,
}
return sensorValue
In Inkbird IBS-TH1 mini, the handle was "0x002d", but this time it is "0x28", so please be careful. Data is acquired by communication with bluepy's Peripheral class.
The acquired characteristic data is ** ・ 1st and 2nd bytes: Temperature (0.01 ℃ unit) ** ** ・ 3rd-4th bytes: Humidity (0.01% unit) ** However, I couldn't find out even if I checked the contents of the 5th to 7th bytes, so I will log it as unknown1 ~ 3 to find out what it is. (We are aiming to get the remaining battery power)
Create a main script to call the sensor value acquisition script
inkbird_toSpreadSheet.py
from bluepy import btle
import inkbird_ibsth1_connect
######Acquisition of value of OMRON environment sensor (BAG type)######
PERIPHERAL_MAC_ADDRESS = 'MAC address obtained in ①'
sensorValue = inkbird_ibsth1_connect.get_ibsth1_data(PERIPHERAL_MAC_ADDRESS)
#Display the temperature as a trial
print(sensorValue['Temperature'])
Try running from the console
python inkbird_toSpreadSheet.py
25.49
You have now obtained the sensor readings in Python.
[Omron Environment Sensor Article](https://qiita.com/c60evaporator/items/ed2ffde4c87001111c12#python%E3%81%8B%E3%82%89gas%E3%81%AEapi%E3%82%92%E5% 8F% A9% E3% 81% 84% E3% 81% A6% E3% 82% B9% E3% 83% 97% E3% 83% AC% E3% 83% 83% E3% 83% 89% E3% 82% B7% E3% 83% BC% E3% 83% 88% E3% 81% AB% E3% 83% 87% E3% 83% BC% E3% 82% BF% E6% 9B% B8% E3% 81% 8D% E8% BE% BC% E3% 81% BF) Please refer
[Omron Environment Sensor Article](https://qiita.com/c60evaporator/items/ed2ffde4c87001111c12#%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83 % 88% E3% 81% AE% E5% AE% 9A% E6% 9C% 9F% E5% AE% 9F% E8% A1% 8C) Please refer
Recommended Posts