Make API of switchbot thermo-hygrometer with Node-RED

TL;DR --Switchbot thermo-hygrometer data acquired with Raspberry Pi --Publish the acquired data as API with Node-RED

Introduction

It has nothing to do with work.

For my home hack, I decided to start recording temperature and humidity again. In the past, I used to attach BME280 to the Rapberry Pi to measure temperature and humidity. After all, it looks bad if it is made by myself, and there was a problem that it could not be installed in the place you want to install due to the power supply of Raspberry Pi.

Therefore, I searched for a "cheap", "good-looking", and "battery-powered" thermo-hygrometer that can communicate with Bluetooth, Wifi, etc.

https://www.switchbot.jp/meter

The SwitchBot thermo-hygrometer allows you to easily check the temperature and humidity of your home. It is a convenient smart thermo-hygrometer that allows you to check alarms and history with the app. (Quote: https://www.switchbot.jp/meter)

As you can see from the page of the head family, it looks like a liquid crystal type thermo-hygrometer. Despite the price of about 2000 yen, it is compatible with Bluetooth, and you can visualize temperature and humidity information using iOS and Android apps.

But ... ** I was completely lacking in my research **, I wrote that it works with cloud services, but it means that you need to purchase Hub separately ... It seems that there is no API for acquiring temperature and humidity information even if they are linked, so I decided to create an API myself.

Acquisition of temperature and humidity information

It is necessary to obtain temperature and humidity information for both rabbits and horns. This time, I used a Raspberry Pi that has been left unused in any home.

Usage environment

--Case: Raspberry Pi 3 Model B

Check if you can see the Bluetooth device on the Raspberry Pi.

Get a list of devices using python and bluepy.


import bluepy

scanner = bluepy.btle.Scanner(0)
devices = scanner.scan(5)

for device in devices:
  print('address : %s' % device.addr)

Execution result

address : 6d:a3:xx:0a:f5:ae
address : e1:bb:xx:2a:fb:e0
address : 2c:26:xx:16:24:38
address : b8:78:xx:3d:3d:e5
address : 5e:93:xx:39:80:39
address : c8:84:xx:53:9a:52
address : 5d:e3:xx:2a:cc:76
address : d3:f2:xx:ac:97:42
address : 52:75:xx:4c:38:b5
address : 72:5f:xx:cd:b0:8d
address : 47:db:xx:64:cd:78
address : c8:84:xx:4f:71:3e
address : 4f:a5:xx:85:3a:97
address : 42:e0:xx:b9:61:5d
address : 5f:53:xx:ed:71:4b
address : 53:c6:xx:62:e5:ce
address : 68:ea:xx:4e:14:1d
address : 6b:22:xx:12:24:d1
address : 60:09:xx:37:11:f3

When I checked from the SwitchBot iOS app this time, e1: bb: xx: 2a: fb: e0 is the MAC address of the Switchbot thermo-hygrometer. Since the MAC address also exists in the acquisition result on the Raspberry Pi, it was found that the device can be recognized on the Raspberry Pi as well.

Acquisition of temperature and humidity data

Extract temperature and humidity data from BLE of SwitchBot. I was enthusiastic about packet analysis, but some people have already implemented BLE packet analysis and output, so I used it as a reference. Read SwitchBot thermo-hygrometer readings directly from BLE Advertisement packets

This time, the following changes were made to create the API for the SwitchBot thermo-hygrometer.

--Receive MAC address as an argument --Set output format to json

Execution result

# python3 switchbot-meter.py e1:bb:xx:2a:fb:e0
{
    "isHumidityLowAlert": "False",
    "isTemperatureUnitF": "False",
    "isDualStateMode": "False",
    "isHumidityHighAlert": "False",
    "isTemperatureLowAlert": "False",
    "isEncrypted": "False",
    "isStatusOff": "False",
    "temperature": "26.0", 
    "isTemperatureHighAlert": "False",
    "humidity": "29",
    "battery": "100"
}

Creating the API part

Use Node-RED to create the API part. Node-RED is a tool that can express and program the flow of data input / output and processing using GUI.

Node-RED is a flow-based programming tool, originally developed by the IBM Emerging Technology Services team and is now part of the JS Foundation.

When creating the API part, it can be implemented using Flask etc., but it is realized by Node-RED.

Node-RED flow creation

The nodes used this time are as follows.

http in http://ip/sensors/macaddressをGETする方法 How to POST {"mac": "mac address"} with json to http: // ip / sensors /

The former works, but since the MAC address contains a colon :, it is necessary to confirm the RFC to be exact. Others are as follows

--function: Store the object received by http in in msg.payload. --exec: Executing a local python script --http response: Returns json of script execution result to client --debug: for when an error occurs

Use the created API

Try hitting the API created by Node-RED from your client machine.

~ » curl -X POST -H 'Content-Type:application/json' -d '{"mac":"e1:bb:xx:2a:fb:e0"}' http://192.168.11.41:1880/sensors/             onodes@Casper3
{
    "isTemperatureLowAlert": "False",
    "isHumidityHighAlert": "False",
    "isDualStateMode": "False",
    "isTemperatureHighAlert": "False",
    "isEncrypted": "False",
    "battery": "100",
    "isTemperatureUnitF": "False",
    "humidity": "26",
    "isHumidityLowAlert": "False",
    "isStatusOff": "False",
    "temperature": "29.1"
}

Apparently it worked fine.

in conclusion

This time, I used Node-RED to create an API for the Switchbot thermo-hygrometer. For those without WebAPI, you can easily have WebAPI by inserting a tool such as Node-RED. However, since you are hitting the script directly, you need to be careful about security.

Recommended Posts

Make API of switchbot thermo-hygrometer with Node-RED
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Transcription of images with GCP's Vision API
Make common settings with subplot of matplotlib
Try to make RESTful API with MVC using Flask 1.0.2
Make Lambda Layers with Lambda
Extrude with Fusion360 API
Make slides with iPython
Make a Linux version of OpenSiv3D with find_package a little easier
Flow of extracting text in PDF with Cloud Vision API
The first API to make with python Djnago REST framework
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
Build a speed of light web API server with Falcon
Speech recognition of wav files with Google Cloud Speech API Beta
Get stock articles of infrastructure engineer yuta with Qiita API