This article has the same content as ** Omron Environment Sensor (BAG type) article **. This article was conducted with a ** SwitchBot temperature / humidity sensor ** with a screen display at a low price.
A series of home IoT devices developed by Wonderlabs Inc., a manufacturer in Shenzhen, China. This time, the temperature and humidity sensors of the above series Use the. At a low price (about 2000 yen), the multi-functionality of data acquisition and screen display using smartphone apps and APIs is attractive.
** ・ RaspberryPi ** (Pi3Model B is used this time) **-Python execution environment ** (This time, pyenv uses Python 3.7.6) ** ・ SwitchBot temperature / humidity sensor **
** ① 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/warpzone/items/11ec9bef21f5b965bce3
** ・ Sensor setup ** Insert AAA batteries in the sensor
**-Scan for Bluetooth devices ** After checking the MAC address on SwitchBot Official App Execute the following command on Raspberry Pi
sudo hcitool lescan
LE Scan ...
AA:EE:BB:DD:55:77 (unknown)
If the MAC address confirmed by the application above appears, it is successful.
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
switchbot.py
from bluepy import btle
import struct
#Broadcast data acquisition delegate
class SwitchbotScanDelegate(btle.DefaultDelegate):
#constructor
def __init__(self, macaddr):
btle.DefaultDelegate.__init__(self)
#Variables for holding sensor data
self.sensorValue = None
self.macaddr = macaddr
#Scan handler
def handleDiscovery(self, dev, isNewDev, isNewData):
#If you find a device with the target Mac address
if dev.addr == self.macaddr:
#Extract advertisement data
for (adtype, desc, value) in dev.getScanData():
#Data retrieval is executed when the environment sensor is used.
if desc == '16b Service Data':
#Extract sensor data
self._decodeSensorData(value)
#Extract sensor data and convert to dict format
def _decodeSensorData(self, valueStr):
#Sensor data from character strings(4th and subsequent characters)Only take out and convert to binary
valueBinary = bytes.fromhex(valueStr[4:])
#Convert binary sensor data to numbers
batt = valueBinary[2] & 0b01111111
isTemperatureAboveFreezing = valueBinary[4] & 0b10000000
temp = ( valueBinary[3] & 0b00001111 ) / 10 + ( valueBinary[4] & 0b01111111 )
if not isTemperatureAboveFreezing:
temp = -temp
humid = valueBinary[5] & 0b01111111
#Store in dict type
self.sensorValue = {
'SensorType': 'SwitchBot',
'Temperature': temp,
'Humidity': humid,
'BatteryVoltage': batt
}
Same as the Omron environment sensor BAG version, the sensor value is acquired in the broadcast mode. Since the acquired data content was complicated, the above reference
Create a main script to call the sensor value acquisition script
switchbot_toSpreadSheet.py
from bluepy import btle
from switchbot import SwitchbotScanDelegate
######Get the value of SwitchBot######
#switchbot.Set the sensor value acquisition delegate of py to execute at scan time
scanner = btle.Scanner().withDelegate(SwitchbotScanDelegate())
#Scan to get sensor value (timeout 5 seconds)
scanner.scan(5.0)
#Display the temperature as a trial
print(scanner.delegate.sensorValue['Temperature'])
#Describe the process of uploading to Google Spreadsheet in ④
Try running from the console
python switchbot_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