This script connects to the Zepp/MiFit/Huami/Amznfit smart watch API in order to retrieve step count and sleep information to then write onwards into an InfluxDB instance.
The underlying connectivity approach is taken from Reverse engineering of the Mi Fit API .
I put this script together in order to collect data from my Amznfit Bip 3 Pro, having found that the Bip 3 didn't work with Gadgetbridge.
Design details can be found in MISC#35.
The app seems to update values irregularly, so hourly runs should be more than sufficient.
Note: Bip 3 support has since been added to Gadgetbridge, so I've changed approach and so am unlikely to add to this codebase any further.
The script is designed to run as a docker container and so takes its configuration from environment variables
INFLUXDB_URL
: the URL of the InfluxDB server to write toINFLUXDB_TOKEN
: the token to authenticate against InfluxDB with (provide user:pass if it's a v1 instance)INFLUXDB_ORG
: The org name or ID to write intoINFLUXDB_MEASUREMENT
: The measurement name to write into (defaultzepp
)INFLUXDB_BUCKET
: The bucket/database to write into (defaulttelegraf
)QUERY_DURATION
: How many days data to fetch from the API (default 2)ZEPP_EMAIL
: The email address used to sign into Zepp/MiFit/Huami/AmznfitZEPP_PASS
: The password to your Zepp account
The simplest way to run the script is via docker container
docker run --rm \
-e INFLUXDB_URL=http://192.168.3.84:8086 \
-e INFLUXDB_TOKEN=ffffaaaaaccccc \
-e INFLUXDB_ORG=abcdef \
-e ZEPP_PASS=mypass \
-e ZEPP_EMAIL=someone@example.invalud \
bentasker12/zepp_to_influxdb:latest
To run without a container, first install dependencies
pip install -r requirements.txt
Export the necessary variables into the environment, and then trigger the script
./app/mifit_to_influxdb.py
cron
can be used to schedule runs if the container or the script are being directly called.
Kubernete's CronJob
type can be used to schedule runs of the container.
As a matter of good practice, secrets should be stored as secrets rather than being written into the job spec:
kubectl create secret generic zepp \
--from-literal='email=someone@example.invalid' \
--from-literal='pass=MyPass'
kubectl create secret generic influxdbv1 \
--from-literal=influxdb_token='ffffaaaaaccccc' \
--from-literal=influxdb_org='abcdef' \
--from-literal=influxdb_url='http://192.168.3.84:8086'
A CronJob
spec can then be created drawing from these secrets
apiVersion: batch/v1
kind: CronJob
metadata:
name: zepp-to-influxdb
spec:
schedule: "30 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: zepp-to-influxdb
image: bentasker12/zepp_to_influxdb:latest
imagePullPolicy: IfNotPresent
env:
- name: INFLUXDB_BUCKET
value: "telegraf"
- name: INFLUXDB_MEASUREMENT
value: "zepp"
- name: QUERY_DURATION
value: "2"
- name: INFLUXDB_TOKEN
valueFrom:
secretKeyRef:
name: influxdbv1
key: influxdb_token
- name: INFLUXDB_ORG
valueFrom:
secretKeyRef:
name: influxdbv1
key: influxdb_org
- name: INFLUXDB_URL
valueFrom:
secretKeyRef:
name: influxdbv1
key: influxdb_url
- name: ZEPP_EMAIL
valueFrom:
secretKeyRef:
name: zepp
key: email
- name: ZEPP_PASS
valueFrom:
secretKeyRef:
name: zepp
key: pass
restartPolicy: OnFailure
Applying the spec
kubectl apply -f zepp_cron.yml
Copyright (c) 2023 B Tasker, Michael Wyraz Released under BSD 3-Clause (see script header)