Afficheur IV Bus via MQTT
Rédigé par Master
Aucun commentaire
Classé dans : Non classé
- ESPeasy sur ESP8266 avec afficheur OLED 128x64 et abonnement MQTT
- Broker MQTT (un mosquitto)
- script bash pour utilisation API PRIM et publication MQTT
Installation
- Installer ESPeasy sur votre ESP8266
- Installer un broker MQTT (une mosquitto sous docker fera l'affaire) et le configurer
- Machine Linux avec script bash et API PRIM valide
Configuration
Faire fonctionner l'écran OLED:
Les paramètres dépendent de votre écran.
Activer NTP, Rules, et MQTT (Domoticz MQTT par exemple, en le configurant sur votre broker)
Activer un device Generic - MQTT Import et s'abonner sur les Topics IV
Faire les règles d'affichage sur réception des informations MQTT
on System#boot do
oledframedcmd,userDef1,'G. Leclerc'
oledframedcmd,userDef2,'Bus'
endon
on MQTTimport#ligne0 do
logentry,"Ligne0 = %eventvalue%"
oledframedcmd,1,"Bus {substring:0:3:%eventvalue%}<|>{substring:4:6:%eventvalue%}"
endon
on MQTTimport#ligne1 do
logentry,"Ligne1 = %eventvalue%"
oledframedcmd,2,"Bus {substring:0:3:%eventvalue%}<|>{substring:4:6:%eventvalue%}"
endon
on MQTTimport#ligne2 do
logentry,"Ligne2 = %eventvalue%"
oledframedcmd,3,"Bus {substring:0:3:%eventvalue%}<|>{substring:4:6:%eventvalue%}"
endonUtilisation
Sur une machine Linux, implanter le script suivant:
#!/usr/bin/env bash
# BIV@home
#
# (c) Yves Lavanant - 2024
#
# Debug
#set -x
# Enable xtrace if the DEBUG environment variable is set
if [[ ${DEBUG-} =~ ^1|yes|true$ ]]; then
set -o xtrace # Trace the execution of the script (debug)
fi
# Only enable these shell behaviours if we're not being sourced
# Approach via: https://stackoverflow.com/a/28776166/8787985
if ! (return 0 2> /dev/null); then
# A better class of script...
set -o errexit # Exit on most errors (see the manual)
set -o nounset # Disallow expansion of unset variables
set -o pipefail # Use last non-zero exit code in a pipeline
fi
# Enable errtrace or the error trap handler will not work as expected
set -o errtrace # Ensure the error trap handler is inherited
# Variables
TMP=$(mktemp)
#StopPoint="STIF:StopPoint:Q:22578:" # Point d'arret choisi (exemple 113)
StopPoint="STIF:StopPoint:Q:26023:" # devant Duvelleroy
# Map sur
# https://prim.iledefrance-mobilites.fr/fr/jeux-de-donnees/
# zones-d-arrets?tab=vue_personnalisee
APIKEY="apiKey: PRIM_key"
# MQTT
BROKER=192.168.1.300
MQTT_USER=user
MQTT_PASSWORD=password
# Tableaux
declare -A DEST=()
declare -A LINE=()
declare -A TIME=()
declare -A JSON=()
# DESC: Usage help
# ARGS: None
# OUTS: None
function script_usage() {
cat << EOF
Usage:
-h|--help Displays this help
-v|--verbose Displays verbose output
-nc|--no-colour Disables colour output
-cr|--cron Run silently unless we encounter an error
EOF
}
# DESC: Parameter parser
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: Variables indicating command-line parameters and options
function parse_params() {
local param
while [[ $# -gt 0 ]]; do
param="$1"
shift
case $param in
-h | --help)
script_usage
exit 0
;;
-v | --verbose)
verbose=true
;;
-nc | --no-colour)
no_colour=true
;;
-cr | --cron)
cron=true
;;
*)
script_exit "Invalid parameter was provided: $param" 1
;;
esac
done
}
# DESC: Get IVTR from PRIM
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: None
function get_IVTR() {
if [[ -n ${verbose-} ]]; then
echo
echo "URL https://prim.iledefrance-mobilites.fr/marketplace/stop-monitoring?MonitoringRef=${StopPoint}"
fi
curl --silent --header "$APIKEY"
"https://prim.iledefrance-mobilites.fr/marketplace/stop-monitoring?MonitoringRef=${StopPoint}"
-o $TMP
}
# DESC: Get LINE name from PRIM
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: None
function get_LINE() {
echo $(grep $1 ~/bin/referentiel-des-lignes.csv | cut -d";" -f 2)
}
# DESC: send IVTR to the PIQ
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: None
function send_IVTR() {
for i in $(seq 0 2); do
if [[ $(jq -r .Siri.ServiceDelivery.StopMonitoringDelivery[].MonitoredStopVisit[$i].MonitoredVehicleJourney.DestinationName[].value $TMP 2>/dev/null ) ]]; then
eval "DEST[$i]"'=$(jq -r .Siri.ServiceDelivery.StopMonitoringDelivery[].MonitoredStopVisit[$i].MonitoredVehicleJourney.DestinationName[].value $TMP | iconv -t ASCII//TRANSLIT)'
eval "LINE[$i]"'=$(get_LINE $(jq -r .Siri.ServiceDelivery.StopMonitoringDelivery[].MonitoredStopVisit[$i].MonitoredVehicleJourney.LineRef[] $TMP | cut -d":" -f 4 ))'
eval "TIME[$i]"'=$(echo "($(date -d $(jq -r .Siri.ServiceDelivery.StopMonitoringDelivery[].MonitoredStopVisit[$i].MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime $TMP) +%s) - $(date +%s)) / 60" | bc)'
if [ $(eval echo ${TIME[$i]}) -lt 0 ]; then eval "TIME[$i]"'="0"';fi
else
eval "DEST[$i]"'=""'
eval "LINE[$i]"'=""'
eval "TIME[$i]"'=""'
fi
if [[ -n ${verbose-} ]]; then
echo "Passage dans ${TIME[$i]} minutes à destination de ${DEST[$i]} sur la ligne ${LINE[$i]}."
fi
# Envoi
#eval "DEST[$i]=$(echo ${DEST[$i]} | tr " " "+")"
JSON[$i]="${LINE[$i]}.$(printf "%.2d" ${TIME[$i]})"
if [[ -n ${verbose-} ]]; then
echo ${JSON[$i]}
fi
done
if [[ -n ${verbose-} ]]; then
echo mosquitto_pub -h $BROKER -u $MQTT_USER -P $MQTT_PASSWORD -t IV/$(echo $StopPoint | cut -d":" -f4)
-m "$(jq -n --arg attente0 "${JSON[0]}"
--arg attente1 "${JSON[1]}"
--arg attente2 "${JSON[2]}"
'$ARGS.named' )"
fi
mosquitto_pub -h $BROKER -u $MQTT_USER -P $MQTT_PASSWORD -t IV/$(echo $StopPoint | cut -d":" -f4)
-m "$(jq -n --arg attente0 "${JSON[0]}"
--arg attente1 "${JSON[1]}"
--arg attente2 "${JSON[2]}"
'$ARGS.named' )"
}
# DESC: Main control flow
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: None
function main() {
trap script_trap_err ERR
trap script_trap_exit EXIT
#trap script_trap_exit SIGHUP SIGINT SIGQUIT SIGABRT
script_init "$@"
parse_params "$@"
cron_init
colour_init
#lock_init system
# On boucle
while :
do
# Recuperation des infos
get_IVTR
# Calcul et envoi des infos au PIQ
send_IVTR
# Attente
if [[ -n ${verbose-} ]]; then
echo "On attend !"
fi
# Boucle sur 30 s
sleep 30
done
}
# shellcheck source=source.sh
if [[ -x $(dirname "${BASH_SOURCE[0]}")/source_$(basename $0) ]]; then
source "$(dirname "${BASH_SOURCE[0]}")/source_$(basename $0)"
else
source "$(dirname "${BASH_SOURCE[0]}")/source.sh"
fi
# Invoke main with args if not sourced
# Approach via: https://stackoverflow.com/a/28776166/8787985
if ! (return 0 2> /dev/null); then
main "$@"
fi
Changer user et mot de passe pour MQTT et insérer votre clé d'API PRIMAdapter le StopPoint à votre besoin (par défaut, un arrêt à l'est de Paris avec 3 Bus).
Lancer le script, et voilà!