[PYTHON] How to get temperature from switchBot thermo-hygrometer using raspberry Pi

As you spend more time at home, you may want to make your home more comfortable. In this article, I used Raspberry Pi from the thermo-hygrometer of SwitchBot, which is a cheap bluetooth thermometer. I would like to describe how to get the temperature and humidity.

Premise

--The settings have been completed to the point where you can log in to the raspberry Pi with ssh. --Can handle python --You can pull the code with git --vi can be used --I have a SwitchBot thermo-hygrometer

goal

--SwitchBot Obtains temperature and humidity from the thermo-hygrometer and outputs them to a text file.

Thermometer to use

The switchBot thermo-hygrometer can be purchased from Amazon. As of September 2020, it is available at a price of less than 2000 yen.

1.First of all

Make sure the raspberry Pi recognizes the switchBot thermo-hygrometer with bluetooth. You can skip this procedure.

1.1. Check the MAC address of the bluetooth of the switchBot thermo-hygrometer

Check the MAC address of bluetooth from the switchBot app.

1.1.1. Open the app and tap the device name for which you want to get the MAC address of bluetooth.

1.1.2. Tap "..." at the top right of the screen

1.1.3. The MAC address of bluetooth is displayed by BLE MAC

In the example in the image, F2: 4C: 0A: 14: 24: 13 </ b> is the Mac address of bluetooth.

1.2. Check if the rasberry Pi recognizes the switchBot thermo-hygrometer

sudo hcitool lescan

The bluetooth mac address is output as shown below. It's okay if the bluetooth MAC address of the switchbot thermo-hygrometer you want to get the temperature is listed below.

LE Scan ...
1C:C2:8E:27:DE:CF (unknown)
40:58:BF:7E:B1:22 (unknown)
40:58:BF:7E:B1:22 (unknown)
5E:3D:12:6C:E7:E6 (unknown)
5E:3D:12:6C:E7:E6 (unknown)
73:69:9F:B9:C6:8C (unknown)
73:69:9F:B9:C6:8C (unknown)
18:81:0E:EA:B6:7B (unknown)
18:81:0E:EA:B6:7B (unknown)
21:BF:DD:3B:CD:A5 (unknown)
53:15:4D:6C:AE:5C (unknown)
53:15:4D:6C:AE:5C (unknown)
5E:FC:F6:B4:7D:02 (unknown)
  • Even if it is not displayed, it may be displayed if you wait for a while.

When the scan is finished, stop the scan with ctl + c </ b>.

2. Download the source

A script that can get the temperature from the switchbot thermo-hygrometer is distributed on github. Get the source code with git clone.

#Create a folder called iot
mkdir iot
cd iot
#Download the source from github
git clone https://github.com/OpenWonderLabs/python-host.git

If you have a folder called python-host, it's ok

3. Install the modules required for execution

Install the modules needed to run the downloaded source.

sudo apt-get -y install libglib2.0-dev
sudo pip install pexpect
sudo pip install bluepy

4. Check the operation

Execute the following command to check the operation.

cd python-host
sudo python switchbot_meter.py

If the scan result is displayed as shown below, it's okay

Usage: "sudo python switchbot.py [mac_addr  cmd]" or "sudo python switchbot.py"
Start scanning...
22 16b Service Data 000d540064089a37 16
22 16b Service Data 000d540064039a39 16
scan timeout
(0, [u'f2:4c:0a:14:24:13', "Humiture:26.8'C 55%"])
(1, [u'f8:26:b7:72:bf:51', "Humiture:26.3'C 57%"])

End command execution with ctl + c </ b>.

5. Change to output the result to a text file

Modify a part of the downloaded switchbot_meter.py so that the temperature acquisition result is output to a text file. First, create the output destination file with a touch command.

touch ondo.txt

Open switchbot_meter.py in vi.

sudo vi switchbot_meter.py

Change lines 210-222 as follows.

  • Enter : 210 </ b> in vi command mode to move to line 210.

Change before

if not dev_list:
    print("No SwitchBot nearby, exit")
    sys.exit()
for idx, val in enumerate(dev_list):
    print(idx, val)

    dev_number = int(input("Input the device number to control:"))

    if dev_number >= len(dev_list) :
        print("Input error, exit")
    bluetooth_adr = dev_list[dev_number]

#Trigger the device to work
#If the SwitchBot address is known you can run this command directly without scanning

    trigger_device(bluetooth_adr)

After change (comment out line 210 and after and rewrite to read and write the file)

if not dev_list:
    print("No SwitchBot nearby, exit")
    sys.exit()
    with open("/home/pi/iot/python-host/ondo.txt","w") as f:
        f.write("")
    with open("/home/pi/iot/python-host/ondo.txt","a") as f:
        for idx, val in enumerate(dev_list):
              f.write(str(val[0])+","+str(val[1])+","+str(val[2])+"\n")

#for idx, val in enumerate(dev_list):
#    print(idx, val)

#    dev_number = int(input("Input the device number to control:"))

#    if dev_number >= len(dev_list) :
#        print("Input error, exit")
#    bluetooth_adr = dev_list[dev_number]

#Trigger the device to work
#If the SwitchBot address is known you can run this command directly without scanning

#    trigger_device(bluetooth_adr)

Rewrite line 114 Change before

elif dev_type == 'T':
    meter_list.append([mac,"Humiture:%.1f'C %d%%"%(meterTemp, meterHumi)])
if ord(dev_type) == ord('L') + 128:

After change

elif dev_type == 'T':
    meter_list.append([mac,meterTemp, meterHumi])
if ord(dev_type) == ord('L') + 128:

6. Check the operation

After changing the code, check the operation.

sudo python switchbot_meter.py 

Make sure it runs without errors.

Confirm that the result is output to ondo.txt.

cat ondo.txt 

It is okay if the MAC address, temperature, and humidity are displayed in this order as shown below.

f2:4c:0a:14:24:13,27.5,47

The above means that the f2: 4c: 0a: 14: 24: 13 thermometer recorded 27.5 degrees and 47% humidity.

7. Future

For example, you can get the temperature and humidity by executing it regularly with cron. This time I output it to a text file, but it may be good to output it to sqlite3 etc. and accumulate temperature data.

8. Search keywords

Home Iot Temperature and humidity switchbot thermometer switchbot hygrometer Iot beginner Raspberry pi Home IoT House hack

Recommended Posts