-
-
Save jameswhite/0450f952138e16fea27125e4f6551182 to your computer and use it in GitHub Desktop.
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 | |
# Derive our topic from our some/topic@hostname | |
topic_at_host=$1; shift | |
host=$(echo ${topic_at_host} | sed -e 's/^.*@//') | |
topic=$(echo ${topic_at_host} | sed -e 's/@.*$//') | |
# Generate a random topic for reply | |
reply_topic="$(dd if=/dev/random bs=2048 count=1 2>/dev/null | shasum | awk '{print $1}')" | |
# append a 'respond':'mqtt/_random_reply_topic' to our user-provided message | |
message=$(echo "$*"|sed -e "s/}/,\"respond\":\"mqtt\/${reply_topic}@planck\"}/") | |
# send the command | |
echo "${message}" | perl -MNet::MQTT::Simple=${host} -le 'foreach $line (<STDIN>){chomp($line); publish($ARGV[0] => $line);}' "${topic}" | |
# wait for our response | |
perl -MNet::MQTT::Simple -le '$mqtt = Net::MQTT::Simple->new("planck");$mqtt->run("$ARGV[0]"=>sub{($topic,$message)=@_;print "[$topic] $message\n";exit 0;});' "mqtt/${reply_topic}" |
Author
jameswhite
commented
May 9, 2016
tyr-tbolt:~ $ mqtt bikeshed/hue@planck '{"action":"color","lights":"jfryman","color":"red"}'
[mqtt/2af1dd13bbad3ebdb72bc914b8ad399dc78520f0] lights changed to red
tyr-tbolt:~ $ mqtt bikeshed/hue@planck '{"action":"color","lights":"rick","color":"lavender"}'
[mqtt/28cd00dc47b0bf414a5bf540e73a4397f09e9d10] lights changed to lavender
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment