Added URL ping to notify when scripts finish OK
All checks were successful
continuous-integration/drone Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
parra 2022-10-07 18:01:50 +02:00
parent 022f9df89a
commit db6b256853
3 changed files with 10 additions and 1 deletions

View file

@ -12,6 +12,7 @@ Copy the `env_file` to `.env` and open it. Complete all environment variables:
- `MQTT_SENSOR_NAME` contains the sensor name, useful to split the telemetry data if you have more than one sensors.
- `MQTT_PUBLISH_DELAY` specify, in seconds, how many time should wait since the script take the measurements to publish in the broker
- `MIJIA_BTLE_ADDRESS` constant with the BLE address of your Mijia device.. This can be retrieved activating the pairing mode in the sensor and scanning the BT devices
- `PING_URL` used to ping a monitoring service if everything was OK (Useful, for example, for monitor the script with Uptime Kuma)
## Install dependencies

View file

@ -19,4 +19,7 @@ MQTT_TOPIC_STATE=${MQTT_TELE_PREFIX}/${MQTT_SENSOR_NAME}/event
MQTT_PUBLISH_DELAY=5
# Sensor configuration
MIJIA_BTLE_ADDRESS=
MIJIA_BTLE_ADDRESS=
# Ping URL (Uptime Kuma, for example)
PING_URL=

View file

@ -5,6 +5,7 @@
import os
import re
import time
import urllib.request
from dotenv import load_dotenv
import paho.mqtt.client as mqtt
@ -12,6 +13,8 @@ from bluepy import btle
load_dotenv() # Cargamos las variables de entorno necesarias
PING_URL = os.getenv('PING_URL')
MQTT_TOPIC_HUMIDITY = os.getenv('MQTT_TOPIC_HUMIDITY')
MQTT_TOPIC_TEMPERATURE = os.getenv('MQTT_TOPIC_TEMPERATURE')
MQTT_TOPIC_BATTERY = os.getenv('MQTT_TOPIC_BATTERY')
@ -96,6 +99,8 @@ def main():
publish_sensor_data(mqttc)
last_msg_time = time.time()
reset_variables()
if PING_URL:
urllib.request.urlopen(PING_URL).read()
break
except (btle.BTLEDisconnectError, IOError):