[PYTHON] Take the value of SwitchBot thermo-hygrometer with Raspberry Pi

Purpose

As the title says. I fully refer to the following articles. Thank you very much. The code is almost the same, so if you point out, we will delete it immediately.

Log the value of SwitchBot thermo-hygrometer with Raspberry Pi https://qiita.com/c60evaporator/items/7c3156a6bbb7c6c59052

There was a part that didn't work in my environment, so I'll put the code that worked as a private log.

environment

IMG_5275.jpg

Check the Mac Address of the SwitchBot thermo-hygrometer

You can check it from the smartphone application.

IMG_5276.PNG IMG_5277.PNG

Check if the sensor is skipping data

pi@raspberrypi:~ $ sudo hcitool lescan | grep EB:F6:31:8C:1F:E9
EB:F6:31:8C:1F:E9 (unknown)
EB:F6:31:8C:1F:E9 (unknown)
EB:F6:31:8C:1F:E9 (unknown)

What is this command?

NAME
       hcitool - configure Bluetooth connections

DESCRIPTION
       hcitool  is  used to configure Bluetooth connections and send some spe-
       cial command to Bluetooth devices. If no command is given,  or  if  the
       option -h is used, hcitool prints some usage information and exits.

Get value in Python

Use bluepy. Refer to the linked URL.

pi@raspberrypi:~ $ sudo install libglib2.0-dev
pi@raspberrypi:~ $ pip3 install bluepy
pi@raspberrypi:~ $ cd .local/lib/python3.7/site-packages/bluepy
pi@raspberrypi:~ $ sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper

Python script

switchbot.py


from bluepy import btle
import struct

class SwitchbotScanDelegate(btle.DefaultDelegate):
    def __init__(self, macaddr):
        btle.DefaultDelegate.__init__(self)
        self.sensorValue = None
        self.macaddr = macaddr

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if dev.addr == self.macaddr:
            for (adtype, desc, value) in dev.getScanData():  
                if desc == '16b Service Data':
                    self._decodeSensorData(value)

    def _decodeSensorData(self, valueStr):
        valueBinary = bytes.fromhex(valueStr[4:])
        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
        self.sensorValue = {
            'SensorType': 'SwitchBot',
            'Temperature': temp,
            'Humidity': humid,
            'BatteryVoltage': batt
        }

main.py


from bluepy import btle
from switchbot import SwitchbotScanDelegate

scanner = btle.Scanner().withDelegate(SwitchbotScanDelegate('eb:f6:31:8c:1f:e9'))
scanner.scan(5.0)
print(scanner.delegate.sensorValue)
pi@raspberrypi:~ $ python3 ./main.py
{'SensorType': 'SwitchBot', 'Temperature': 25.2, 'Humidity': 68, 'BatteryVoltage': 100}

Recommended Posts

Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Periodically log the value of Omron environment sensor with Raspberry Pi
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
I tried to automate the watering of the planter with Raspberry Pi
Make API of switchbot thermo-hygrometer with Node-RED
Inkbird IBS-TH1 value logged with Raspberry Pi
CSV output of pulse data with Raspberry Pi (CSV output)
Observe the Geminids meteor shower with Raspberry Pi 4
Get CPU information of Raspberry Pi with Python
Measure CPU temperature of Raspberry Pi with Python
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
Using the digital illuminance sensor TSL2561 with Raspberry Pi
Try to visualize the room with Raspberry Pi, part 1
Take a screenshot of the LCD with Python-LEGO Mindstorms
Let's operate GPIO of Raspberry Pi with Python CGI
Mutter plants with Raspberry Pi
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)
I tried using the DS18B20 temperature sensor with Raspberry Pi
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
The true value of Terraform automation starting with Oracle Cloud
I sent the data of Raspberry Pi to GCP (free)
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 +
[Raspberry Pi] Stepping motor control with Raspberry Pi
Find the definition of the value of errno
Control the motor with a motor driver using python on Raspberry Pi 3!
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
Extract the maximum value with pandas.
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
Serial communication with Raspberry Pi + PySerial
Take the execution log of Celery
About the return value of pthread_mutex_init ()
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 1
OS setup with Raspberry Pi Imager
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 2
About the return value of the histogram.
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Real-time classification of multiple objects in the camera image with deep learning of Raspberry Pi 3 B + & PyTorch
Graph display of household power consumption with 3GPI and Raspberry Pi
Solve the initial value problem of ordinary differential equations with JModelica
Try moving 3 servos with Raspberry Pi
Tips: [Python] Calculate the average value of the specified area with bedgraph
Using a webcam with Raspberry Pi
Building a distributed environment with the Raspberry PI series (Part 1: Summary of availability of diskless clients by model)
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Display the signal strength RSSI of a specific SSID (raspberry pi (linux))
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Use Raspberry Pi to solve the problem of insufficient mobile Wi-Fi connection
[I touched the Raspberry Pi (1)] I summarized the basic operations of Minecraft Pi Edition (2015.5.23 pre-release)
CSV output of pulse data with Raspberry Pi (confirm analog input with python)