Skip to content

Instantly share code, notes, and snippets.

@jameswhite
Created May 9, 2016 17:24
Show Gist options
  • Save jameswhite/0450f952138e16fea27125e4f6551182 to your computer and use it in GitHub Desktop.
Save jameswhite/0450f952138e16fea27125e4f6551182 to your computer and use it in GitHub Desktop.
#!/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}"
@jameswhite
Copy link
Author

tyr-tbolt:~ $ mqtt bikeshed/hue@planck '{"action":"color","lights":"all","color":"white"}'
[mqtt/f11564321807b7e55c52bf2c6e4b1398be050d32] lights changed to white

@jameswhite
Copy link
Author

@jameswhite
Copy link
Author

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