commit 663368d197c0406d7fa75534c43e37bad8ced91c Author: Denis Nutiu Date: Mon Feb 21 22:24:05 2022 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..57abd55 --- /dev/null +++ b/Readme.md @@ -0,0 +1,57 @@ + +# Introduction + +Expose BME680 sensor readings to Apple Homekit. + +## Installing + +Ensure you are the `pi` user. Clone the repo in home and then install requirements. + +```bash +cd /home/pi && git clone git@github.com:dnutiu/bme680-homekit.git && cd bme680-homekit +sudo apt-get install libavahi-compat-libdnssd-dev +pip3 install -r requirements.txt +``` + +Run the program once to pair it with your ios. ex: + +```bash +python3 main.py +Setup payload: X-HM://0023K50QET2YB +Scan this code with your HomeKit app on your iOS device: + +Or enter this code in your HomeKit app on your iOS device: 053-86-998 + +``` + +Copy the systemd service. + +```bash +sudo cp bme680-homekit.service /etc/systemd/system +sudo systemctl status bme680-homekit +``` + +``` +● bme680-homekit.service - Bme680 Homekit service + Loaded: loaded (/etc/systemd/system/bme680-homekit.service; disabled; vendor preset: enabled) + Active: inactive (dead) +``` + +Start the service +```bash +sudo systemctl start bme680-homekit +sudo systemctl status bme680-homekit +``` + +``` +● bme680-homekit.service - Bme680 Homekit service + Loaded: loaded (/etc/systemd/system/bme680-homekit.service; disabled; vendor preset: enabled) + Active: active (running) since Mon 2022-02-21 20:10:30 GMT; 935ms ago + Main PID: 1722 (python3) + Tasks: 1 (limit: 780) + CPU: 895ms + CGroup: /system.slice/bme680-homekit.service + └─1722 /usr/bin/python3 /home/pi/bme680-homekit/main.py + +Feb 21 20:10:30 raspberrypi systemd[1]: Started Bme680 Homekit service. +``` \ No newline at end of file diff --git a/bme680-homekit.service b/bme680-homekit.service new file mode 100644 index 0000000..4bdc434 --- /dev/null +++ b/bme680-homekit.service @@ -0,0 +1,10 @@ +[Unit] +Description=Bme680 Homekit service +After=local-fs.target network-online.target + +[Service] +User=pi +ExecStart=/usr/bin/python3 /home/pi/bme680-homekit/main.py + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..f7d7abd --- /dev/null +++ b/main.py @@ -0,0 +1,63 @@ +import signal +from pyhap.accessory import Accessory, Bridge +import bme680 +from pyhap.accessory_driver import AccessoryDriver +from pyhap.const import CATEGORY_SENSOR + + +class Bme680Sensor(Accessory): + """Implementation of a mock temperature sensor accessory.""" + + category = CATEGORY_SENSOR # This is for the icon in the iOS Home app. + + def __init__(self, *args, **kwargs): + """Here, we just store a reference to the current temperature characteristic and + add a method that will be executed every time its value changes. + """ + # If overriding this method, be sure to call the super's implementation first. + super().__init__(*args, **kwargs) + + self.sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY) + + self.sensor.set_humidity_oversample(bme680.OS_2X) + self.sensor.set_pressure_oversample(bme680.OS_4X) + self.sensor.set_temperature_oversample(bme680.OS_8X) + self.sensor.set_filter(bme680.FILTER_SIZE_3) + + # Add the services that this Accessory will support with add_preload_service here + temp_service = self.add_preload_service('TemperatureSensor') + humidity_service = self.add_preload_service('HumiditySensor') + + self.temp_value = temp_service.get_characteristic('CurrentTemperature') + self.humidity_value = humidity_service.get_characteristic("CurrentRelativeHumidity") + + @Accessory.run_at_interval(3) + def run(self): + """We override this method to implement what the accessory will do when it is + started. + + We set the current temperature to a random number. The decorator runs this method + every 3 seconds. + """ + if self.sensor.get_sensor_data(): + self.temp_value.set_value(self.sensor.data.temperature) + self.humidity_value.set_value(self.sensor.data.humidity) + + def stop(self): + """We override this method to clean up any resources or perform final actions, as + this is called by the AccessoryDriver when the Accessory is being stopped. + """ + print('Stopping accessory.') + + +def get_bridge(accessory_driver): + bridge = Bridge(accessory_driver, 'Bridge') + bridge.add_accessory(Bme680Sensor(accessory_driver, 'Sensor')) + return bridge + + +if __name__ == '__main__': + driver = AccessoryDriver(port=51826, persist_file="/home/pi/bme680-homekit/accessory.state") + driver.add_accessory(accessory=get_bridge(driver)) + signal.signal(signal.SIGTERM, driver.signal_handler) + driver.start() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7fe8b29 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +bme680==1.1.1 +HAP-python==4.4.0