#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # sbm2web.py by Thomas Rudolph # Proof of concept to read values from Switchbot Meters TH S1 and write # them to an HTML file. This program can be run automatically in intervals # by using, e.g., cron # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Based in part on: # https://github.com/OpenWonderLabs/python-host # https://github.com/bbostock/Switchbot_Py_Meter # https://qiita.com/warpzone/items/11ec9bef21f5b965bce3. # https://github.com/ronschaeffer/sbm2mqtt by Ron Schaeffer # # from bluepy.btle import Scanner, DefaultDelegate, ScanEntry import datetime # SwitchBot UUID - See https://github.com/OpenWonderLabs/python-host/wiki/Meter-BLE-open-API service_uuid = "cba20d00-224d-11e6-9fb8-0002a5d5c51b" # Change the name of the HTML file according to your needs html_filename = "index.htm" class ScanDelegate(DefaultDelegate): # Scan for BLE device advertisements, filter out ones which are not SwitchBot Meters & convert the service data to binary time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Write HTML header and CSS to file f = open(html_filename, 'w') f.write("
\n\n\n") # You can define an if/elif case for every device MAC you own to give it a name instead a MAC address if dev.addr == "eb:f8:b8:03:0c:de": f.write("Studio") elif dev.addr == "dd:02:d9:f8:c3:b2": f.write("Orangerie") else: f.write(str(dev.addr)) # Write sensor measurements to HTML f.write(" | |
\n") f.write(str(temperature) + "\u00B0" + temp_scale) f.write(" | 💧️ " + str(humidity) + "% |
") f.write("🔋️ " + str(battery) + "% | |
") f.write("📡️ " + str(dev.rssi) + "dBm |