Aggiunto supporto mqtt per la pubblicazione dello stato in formato json verso il broker

This commit is contained in:
lejubila
2018-09-20 10:53:40 +02:00
parent 0d748031fd
commit e9a676188f
7 changed files with 103 additions and 8 deletions

View File

@@ -1,3 +1,6 @@
# 0.5.9 - xx/xx/2018
- Added mqtt support for publishing status to broker
# 0.5.8 - 19/07/2018 # 0.5.8 - 19/07/2018
- Added "openweathermap" driver for impement check weather condition from openweatermap api - Added "openweathermap" driver for impement check weather condition from openweatermap api

View File

@@ -53,6 +53,11 @@ READLINK="/bin/readlink"
# Percorso stat # Percorso stat
STAT="/usr/bin/stat" STAT="/usr/bin/stat"
# Percorso mosquito_pub
# # Installare con apt install mosquitto-clients
MOSQUITTO_PUB="/usr/bin/mosquitto_pub"
# Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili, # Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili,
# se impostato a 0 il sistema gestirà elettrovalvole bisstabili # se impostato a 0 il sistema gestirà elettrovalvole bisstabili
EV_MONOSTABLE=0 EV_MONOSTABLE=0
@@ -139,3 +144,12 @@ TCPSERVER_PWD=""
# Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo # Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo
NO_SEND_IDENTIFIER=0 NO_SEND_IDENTIFIER=0
# Parametri connessione al broker mqtt
MQTT_ENABLE=0 # set 1 to enable
MQTT_HOST=you_host_mqtt
MQTT_PORT=1883
MQTT_USER=your_user_mqtt
MQTT_PWD=yout_password_mqtt
MQTT_TOPIC="pigarden/result"
MQTT_CLIENT_ID=piGarden

View File

@@ -53,6 +53,11 @@ READLINK="/bin/readlink"
# Percorso stat # Percorso stat
STAT="/usr/bin/stat" STAT="/usr/bin/stat"
# Percorso mosquito_pub
# Installare con apt install mosquitto-clients
MOSQUITTO_PUB="/usr/bin/mosquitto_pub"
# Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili, # Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili,
# se impostato a 0 il sistema gestirà elettrovalvole bisstabili # se impostato a 0 il sistema gestirà elettrovalvole bisstabili
EV_MONOSTABLE=0 EV_MONOSTABLE=0
@@ -131,6 +136,15 @@ TCPSERVER_PWD=""
# Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo # Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo
NO_SEND_IDENTIFIER=0 NO_SEND_IDENTIFIER=0
# Parametri connessione al broker mqtt
MQTT_ENABLE=0 # set 1 to enable
MQTT_HOST=you_host_mqtt
MQTT_PORT=1883
MQTT_USER=your_user_mqtt
MQTT_PWD=yout_password_mqtt
MQTT_TOPIC="pigarden/result"
MQTT_CLIENT_ID=piGarden
# #
# Configurazione piGarden remoti # Configurazione piGarden remoti

View File

@@ -53,6 +53,11 @@ READLINK="/bin/readlink"
# Percorso stat # Percorso stat
STAT="/usr/bin/stat" STAT="/usr/bin/stat"
# Percorso mosquito_pub
# # Installare con apt install mosquitto-clients
MOSQUITTO_PUB="/usr/bin/mosquitto_pub"
# Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili, # Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili,
# se impostato a 0 il sistema gestirà elettrovalvole bisstabili # se impostato a 0 il sistema gestirà elettrovalvole bisstabili
EV_MONOSTABLE=0 EV_MONOSTABLE=0
@@ -133,6 +138,15 @@ TCPSERVER_PWD=""
# Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo # Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo
NO_SEND_IDENTIFIER=0 NO_SEND_IDENTIFIER=0
# Parametri connessione al broker mqtt
MQTT_ENABLE=0 # set 1 to enable
MQTT_HOST=you_host_mqtt
MQTT_PORT=1883
MQTT_USER=your_user_mqtt
MQTT_PWD=yout_password_mqtt
MQTT_TOPIC="pigarden/result"
MQTT_CLIENT_ID=piGarden
# #
# Configurazione moduli sonoff # Configurazione moduli sonoff

View File

@@ -53,6 +53,11 @@ READLINK="/bin/readlink"
# Percorso stat # Percorso stat
STAT="/usr/bin/stat" STAT="/usr/bin/stat"
# Percorso mosquito_pub
# Installare con apt install mosquitto-clients
MOSQUITTO_PUB="/usr/bin/mosquitto_pub"
# Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili, # Se impostato con il valore 1, indica che il sistema gestisce elettrovalvole monostabili,
# se impostato a 0 il sistema gestirà elettrovalvole bisstabili # se impostato a 0 il sistema gestirà elettrovalvole bisstabili
EV_MONOSTABLE=1 EV_MONOSTABLE=1
@@ -503,6 +508,15 @@ TCPSERVER_PWD=""
# Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo # Con impostato il valore 1 non invia l'identificativi per statistiche di utilizzo
NO_SEND_IDENTIFIER=0 NO_SEND_IDENTIFIER=0
# Parametri connessione al broker mqtt
MQTT_ENABLE=0 # set 1 to enable
MQTT_HOST=you_host_mqtt
MQTT_PORT=1883
MQTT_USER=your_user_mqtt
MQTT_PWD=yout_password_mqtt
MQTT_TOPIC="pigarden/result"
MQTT_CLIENT_ID=piGarden
# #
# Configurazione schede spb16ch # Configurazione schede spb16ch

View File

@@ -89,6 +89,9 @@ function trigger_event {
local ec=$? local ec=$?
#echo "$EVENT ec=$ec" >> /tmp/piGarden.testevent #echo "$EVENT ec=$ec" >> /tmp/piGarden.testevent
mqtt_status &
if [ $ec -ne 0 ]; then if [ $ec -ne 0 ]; then
log_write "Stop events chain for exit code $ec in $current_event_dir/$f" log_write "Stop events chain for exit code $ec in $current_event_dir/$f"
return $ec return $ec

View File

@@ -492,6 +492,11 @@ function json_status {
local last_success="" local last_success=""
local with_get_cron="0" local with_get_cron="0"
local with_get_cron_open_in="0" local with_get_cron_open_in="0"
local current_pid=$!
if [ "$PARENT_PID" -gt "0" ]; then
current_pid=$PARENT_PID
fi
local vret="" local vret=""
for i in $1 $2 $3 $4 $5 $6 for i in $1 $2 $3 $4 $5 $6
@@ -516,7 +521,8 @@ function json_status {
if [ -n "$json" ]; then if [ -n "$json" ]; then
json="$json," json="$json,"
fi fi
json="$json\"$i\":{\"name\":\"$av\",\"state\":$sv}" #json="$json\"$i\":{\"name\":\"$av\",\"state\":$sv}"
json="$json\"$av\":{\"name\":\"$av\",\"state\":$sv}"
done done
json="\"zones\":{$json}" json="\"zones\":{$json}"
@@ -527,14 +533,14 @@ function json_status {
if [[ ! -z "$last_weather_online" ]]; then if [[ ! -z "$last_weather_online" ]]; then
json_last_weather_online=$last_weather_online json_last_weather_online=$last_weather_online
fi fi
if [ -f "$LAST_INFO_FILE.$!" ]; then if [ -f "$LAST_INFO_FILE.$current_pid" ]; then
last_info=`cat "$LAST_INFO_FILE.$!"` last_info=`cat "$LAST_INFO_FILE.$current_pid"`
fi fi
if [ -f "$LAST_WARNING_FILE.$!" ]; then if [ -f "$LAST_WARNING_FILE.$current_pid" ]; then
last_warning=`cat "$LAST_WARNING_FILE.$!"` last_warning=`cat "$LAST_WARNING_FILE.$current_pid"`
fi fi
if [ -f "$LAST_SUCCESS_FILE.$!" ]; then if [ -f "$LAST_SUCCESS_FILE.$current_pid" ]; then
last_success=`cat "$LAST_SUCCESS_FILE.$!"` last_success=`cat "$LAST_SUCCESS_FILE.$current_pid"`
fi fi
local json_last_weather_online="\"last_weather_online\":$json_last_weather_online" local json_last_weather_online="\"last_weather_online\":$json_last_weather_online"
local json_last_rain_sensor="\"last_rain_sensor\":\"$last_rain_sensor\"" local json_last_rain_sensor="\"last_rain_sensor\":\"$last_rain_sensor\""
@@ -617,6 +623,26 @@ function json_status {
} }
#
# Invia al broker mqtt il json contentente lo stato del sistema
#
# $1 parent pid (opzionale)
#
function mqtt_status {
if [ ! $MQTT_ENABLE -eq 1 ]; then
return
fi
if [ ! -z "$1" ]; then
PARENT_PID=$1
fi
local js=$(json_status)
$MOSQUITTO_PUB -h $MQTT_HOST -p $MQTT_PORT -u $MQTT_USER -P $MQTT_PWD -i $MQTT_CLIENT_ID -r -t "$MQTT_TOPIC" -m "$js"
}
# #
# Mostra il i parametri dello script # Mostra il i parametri dello script
# #
@@ -632,6 +658,7 @@ function show_usage {
echo -e "\t$NAME_SCRIPT ev_status alias show status solenoid" echo -e "\t$NAME_SCRIPT ev_status alias show status solenoid"
echo -e "\t$NAME_SCRIPT ev_status_all show status solenoids" echo -e "\t$NAME_SCRIPT ev_status_all show status solenoids"
echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in] show status in json format" echo -e "\t$NAME_SCRIPT json_status [get_cron|get_cron_open_in] show status in json format"
echo -e "\t$NAME_SCRIPT mqtt_status send status in json format to mqtt broker"
echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/" echo -e "\t$NAME_SCRIPT check_rain_online check rain from http://api.wunderground.com/"
echo -e "\t$NAME_SCRIPT check_rain_sensor check rain from hardware sensor" echo -e "\t$NAME_SCRIPT check_rain_sensor check rain from hardware sensor"
echo -e "\t$NAME_SCRIPT close_all_for_rain close all solenoid if it's raining" echo -e "\t$NAME_SCRIPT close_all_for_rain close all solenoid if it's raining"
@@ -880,7 +907,7 @@ function debug2 {
VERSION=0 VERSION=0
SUB_VERSION=5 SUB_VERSION=5
RELEASE_VERSION=8 RELEASE_VERSION=9
DIR_SCRIPT=`dirname $0` DIR_SCRIPT=`dirname $0`
NAME_SCRIPT=${0##*/} NAME_SCRIPT=${0##*/}
@@ -912,6 +939,8 @@ LAST_INFO_FILE="$STATUS_DIR/last_info"
LAST_WARNING_FILE="$STATUS_DIR/last_warning" LAST_WARNING_FILE="$STATUS_DIR/last_warning"
LAST_SUCCESS_FILE="$STATUS_DIR/last_success" LAST_SUCCESS_FILE="$STATUS_DIR/last_success"
PARENT_PID=0
if [ -z $LOG_OUTPUT_DRV_FILE ]; then if [ -z $LOG_OUTPUT_DRV_FILE ]; then
LOG_OUTPUT_DRV_FILE="/dev/null" LOG_OUTPUT_DRV_FILE="/dev/null"
fi fi
@@ -981,6 +1010,10 @@ case "$1" in
json_status $2 $3 $4 $5 $6 json_status $2 $3 $4 $5 $6
;; ;;
mqtt_status)
mqtt_status $2
;;
check_rain_online) check_rain_online)
check_rain_online check_rain_online
;; ;;