Last active
September 10, 2015 08:45
-
-
Save toke/6c9fec9cb6eda648fbdb to your computer and use it in GitHub Desktop.
Hacky mqtt2opensensemap bash script for http://opensensemap.org Subscribes to MQTT Topics and publishes data to opensensemap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ##################################################################################### | |
| # mqtt2sense - Publish mqtt-topics to opensensemap.org or compatible server | |
| # Toke 2015 | |
| # Public Domain - no rights reserved | |
| ##################################################################################### | |
| CURL="/usr/bin/curl" | |
| MOSQUITTO="/usr/bin/mosquitto_sub" | |
| MQTT_HOST="mqtt.server.de" | |
| MQTT_PORT="1883" | |
| BOX_ID="1234" | |
| declare -A SENSORMAP | |
| ### Format: SENSORMAP["MQTT-TOPIC"]="OPENSENSEMAP SENSOR KEY" | |
| SENSORMAP["sensors/place/1234/temp"]="1234" | |
| SENSORMAP["sensors/place/5678/temp"]="5678" | |
| SENSORMAP["sensors/place/9abc/pressure"]="9abc" | |
| BASE_URL="http://opensensemap.org:8000/boxes/" | |
| ##################################################################################### | |
| function publish { | |
| BOX_ID="$1" | |
| SENSOR_ID="$2" | |
| DATA="$3" | |
| URL="${BASE_URL}/${BOX_ID}/${SENSOR_ID}" | |
| JSON='{"value":'$DATA'}' | |
| $CURL -s -X POST --header "Content-Type: application/json" -d "$JSON" ${URL} | |
| } | |
| TOPICS="${!SENSORMAP[@]}" | |
| SUBSCRIBE=$(for t in $TOPICS ; do echo "-t $t" ; done) | |
| echo "Subscribe to $TOPICS" | |
| $MOSQUITTO -h "${MQTT_HOST}" -p "${MQTT_PORT}" -v $SUBSCRIBE | while read line | |
| do | |
| KEY=$(echo "$line" | awk '{ print $1 }') | |
| VAL=$(echo "$line" | awk '{ print $2 }') | |
| SENSOR_ID="${SENSORMAP[$KEY]}" | |
| if [ ! -z "${SENSOR_ID}" ] ; then | |
| echo -n -e "\nSend $VAL to $SENSOR_ID:\n\t" | |
| publish "$BOX_ID" "${SENSOR_ID}" "$VAL" | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment