This article has the same content as ** Omron Environment Sensor (BAG type) article . This article was conducted with the Omron environmental sensor ( USB type **).
Omron's environment sensors include temperature, humidity, illuminance, barometric pressure, noise, etc. It is a device that combines multiple sensors. (Left: BAG type 2JCIE-BL01, Right: USB type 2JCIE-BU01)
This time, we will get the USB type data on the right side from the Raspberry Pi.
** ・ RaspberryPi ** (Pi3Model B is used this time) **-Python execution environment ** (This time, pyenv uses Python 3.7.6) ** ・ Google account ** (required to use spreadsheet) ** ・ Omron environmental sensor (USB type 2JCIE-BU01) **
** ① Check the Bluetooth connection between the Raspberry Pi and the sensor ** ** ② Get the measured value of the environmental sensor with Python ** ** ③ Hit the GAS API from Python to write data to the spreadsheet ** ** ④ Script execution regularly **
**-Connecting the environmental sensor to the USB port ** Plug the environmental sensor into the USB port and make sure the light is on for a moment
**-Scan for Bluetooth devices ** Execute the following command on Raspberry Pi
sudo hcitool lescan
LE Scan ...
CC:DD:BB:AA:66:77 Rbt
If you see the name "Rbt", this is the MAC address of the environmental sensor. If it does not come out, check the USB contact and the Bluetooth enable of the Raspberry Pi.
bluepy is a library for accessing Bluetooth Low Energy (BLE) in Python (class definition)
The USB type, which does not have a battery, does not require a change to the broadcast mode like the BAG type, so the work is less troublesome.
** ・ 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
omron_env_usb_connect.py
from bluepy import btle
import struct
def get_env_usb_data(macaddr):
peripheral = btle.Peripheral(macaddr, addrType=btle.ADDR_TYPE_RANDOM)
characteristic = peripheral.readCharacteristic(0x0059)
(seq, temp, humid, light, press, noise, eTVOC, eCO2) = struct.unpack('<Bhhhlhhh', characteristic)
sensorValue = {
'SensorType': 'Omron_USB_EP',
'Temperature': temp / 100,
'Humidity': humid / 100,
'Light': light,
'Pressure': press / 1000,
'Noise': noise / 100,
'eTVOC': eTVOC,
'eCO2': eCO2
}
return sensorValue
In the BAG type of broadcast mode, the sensor data was acquired from the advertisement data, In the USB type of connect mode, data is acquired by communication by the Peripheral class.
Create a main script to call the sensor value acquisition script
omron_env_toSpreadSheet.py
from bluepy import btle
import omron_env_usb_connect
######Acquisition of value of OMRON environment sensor (BAG type)######
PERIPHERAL_MAC_ADDRESS = 'MAC address obtained in ①'
sensorValue = omron_env_usb_connect.get_env_usb_data(PERIPHERAL_MAC_ADDRESS)
#Display the temperature as a trial
print(sensorValue['Temperature'])
Try running from the console
python omron_env_toSpreadSheet.py
26.48
You have now obtained the sensor readings in Python.
According to the User's Manual, the unit of the acquired value is as shown in the figure below.
[BAG type 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
[BAG type 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