[PYTHON] Logging the value of Omron environment sensor with Raspberry Pi (USB type)

Introduction

This article has the same content as ** Omron Environment Sensor (BAG type) article . This article was conducted with the Omron environmental sensor ( USB type **).

What is an environmental sensor?

Omron's environment sensors include temperature, humidity, illuminance, barometric pressure, noise, etc. It is a device that combines multiple sensors. オムロン環境センサ.png (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.

Things necessary

** ・ 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) **

procedure

** ① 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 **

Details

① Check the Bluetooth connection between the Raspberry pi and the sensor

Confirmation of recognition of environmental sensor

**-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.

** ② Get the measured value of the environmental sensor with Python **

Recognition confirmation with bluepy

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

Creating a sensor value acquisition script

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 main script

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. usb_env.png

③ Hit the GAS API from Python to write data to the spreadsheet

[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

④ Periodic execution of script

[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

Logging the value of Omron environment sensor with Raspberry Pi (USB type)
Periodically log the value of Omron environment sensor with Raspberry Pi
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried to automate the watering of the planter with Raspberry Pi
Build a Tensorflow environment with Raspberry Pi [2020]
Use the Grove sensor on the Raspberry Pi
Improved motion sensor made with Raspberry Pi
Use PIR motion sensor with raspberry Pi
Inkbird IBS-TH1 value logged with Raspberry Pi
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
Building a distributed environment with the Raspberry PI series (Part 1: Summary of availability of diskless clients by model)
Note: Prepare the environment of CmdStanPy with docker
Prepare the execution environment of Python3 with Docker
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
CSV output of pulse data with Raspberry Pi (CSV output)
Observe the Geminids meteor shower with Raspberry Pi 4
Logging Inkbird IBS-TH1 mini values with Raspberry Pi
[Python] Determine the type of iris with SVM
Measure CPU temperature of Raspberry Pi with Python
Realize a super IoT house by acquiring sensor data in the house with Raspberry Pi
Get the value of a specific key in a list from the dictionary type in the list with Python
I want to be notified of the connection environment when the Raspberry Pi connects to the network
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Display images taken with the Raspberry Pi camera module
The story of sharing the pyenv environment with multiple users
Try using the temperature sensor (LM75B) on the Raspberry Pi.
[C language] [Linux] Get the value of environment variable
Preparing the execution environment of PyTorch with Docker November 2019
Control power on / off of USB port of Raspberry Pi
Display USB camera video with Python OpenCV with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Make a note of what you want to do in the future with Raspberry Pi
Control the display of RGB LED matirix electric bulletin board freely with Raspberry Pi 3B +
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
Switch the setting value of setting.py according to the development environment
Unify the environment of the Python development team starting with Poetry
Play with the Raspberry Pi Zero WH camera module Part 1
I tried running Movidius NCS with python of Raspberry Pi3
Your URL didn't respond with the value of the challenge parameter.
Production of temperature control system with Raspberry Pi and ESP32 (1)
Change the message displayed when logging in to Raspberry Pi
Construction of Cortex-M development environment for TOPPERS using Raspberry Pi
The true value of Terraform automation starting with Oracle Cloud
I sent the data of Raspberry Pi to GCP (free)
Try to extract the features of the sensor data with CNN
Building a distributed environment with the Raspberry PI series (Part 2: PiServer analysis and alternative system design)
Real-time classification of multiple objects in the camera image with deep learning of Raspberry Pi 3 B + & PyTorch
Control the motor with a motor driver using python on Raspberry Pi 3!
Find the optimal value of a function with a genetic algorithm (Part 2)
March 14th is Pi Day. The story of calculating pi with python
I learned how the infrared remote control works with Raspberry Pi
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 1
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 2
Graph display of household power consumption with 3GPI and Raspberry Pi