This guide shows how to collect HomeKit data from a HomePod and log it into a SQLite database on a Raspberry Pi. We will create a Flask API, automate data collection using Apple Shortcuts, and ensure the API runs reliably on the Raspberry Pi using systemd
.
I wanted to enhance my HomePod's functionality beyond its typical use. The HomePod can respond to commands, play music, and monitor the environment. I realized that tapping into its temperature and humidity data could provide valuable insights for optimizing my home environment. However, there hasn't been a good way to store the environmental data until now.
This project aims to take control of HomeKit data and display it in a database for manipulation, graphing, and review. The concept of a real-time dashboard showcasing trends in temperature and humidity is both practical and exciting. This data could help make informed decisions about heating and air quality.
To bridge the gap between the HomePod’s sensors and a usable format like a database, I developed a method to collect and log this data into a SQLite database on a Raspberry Pi. This guide outlines the necessary steps, from setting up a Flask API and automating data collection using Apple Shortcuts, to ensuring the system runs reliably on the Raspberry Pi using systemd. This approach not only facilitates data visualization but also allows for integration with various applications for further analysis.
1. Setting Up the SQLite DatabaseInstall SQLite on the Raspberry Pi.
sudo apt-get install sqlite3
Create a database and table to store HomeKit metrics:
sqlite3 homekit_data.db
CREATE TABLE metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
metric_tmp REAL,
metric_hum REAL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
Close the database with:
.quit
2. Creating the Flask APIInstall Flask:
pip install Flask
Create a Flask app (app.py
) to handle POST requests:
from flask import Flask, request
import sqlite3
app = Flask(__name__)
@app.route('/log', methods=['POST'])
def log_data():
data = request.json
metric_tmp = data.get('metric_tmp')
metric_hum = data.get('metric_hum')
conn = sqlite3.connect('homekit_data.db')
cursor = conn.cursor()
cursor.execute("INSERT INTO metrics (metric_tmp, metric_hum) VALUES (?, ?)", (metric_tmp, metric_hum))
conn.commit()
conn.close()
return {'status': 'success'}, 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
- Save this file as
app.py
and start the API withpython3 app.py
.
Install Gunicorn:
pip install gunicorn
Create a new file for the service:
sudo nano /etc/systemd/system/flaskapp.service
Edit flaskapp.service:
[Unit]
Description=Flask API for HomeKit Data
After=network.target
[Service]
User=pi
WorkingDirectory=/home/pi/WeatherData
ExecStart=/home/pi/WeatherData/myenv/bin/gunicorn -w 4 -b 0.0.0.0:5000 app:app
Restart=always
[Install]
WantedBy=multi-user.target
Note: Adjust the paths according to your installation.
Start and enable the service:
sudo systemctl daemon-reload
sudo systemctl start flaskapp
sudo systemctl enable flaskapp
sudo systemctl status flaskapp
4. Setting Up Apple Shortcuts- Open the Home app on your phone. Click the Automations tab at the bottom. Click the plus icon in the top right corner and choose “Add Automation.”
- Select a trigger event. I use “A Time of Day Occurs.” Set the desired time.
- Click “Convert To Shortcut” at the bottom of the screen.
- Search for: “Get state of <yourHome>.”
- Choose your HomePod Temperature sensor and Humidity sensor (each with a new “Get state” action).
- Search for “Get Contents of URL.”
- For the URL, fill in the post URL: “http://<yourLocalRaspberryIP>:5000/log.”
- Method: POST
- Request Body: JSON
- Add a new number field: “key: metric_tmp”, and choose the current temperature variable.
- Do the same for “metric_hum”.
- Test the Flask API by sending POST requests manually.
- Check the data in the SQLite database to ensure it’s being logged correctly.
- Confirm that the service is running and restarting as needed.
This setup allows you to continuously monitor and log HomeKit data on a Raspberry Pi, ensuring that the Flask API runs reliably and efficiently.
Cover Image:
https://unsplash.com/photos/blue-and-white-round-light-lZCHy8PLyyo
Comments
Please log in or sign up to comment.