Skip to content

Instantly share code, notes, and snippets.

@jforge
Last active April 10, 2024 18:08
Show Gist options
  • Select an option

  • Save jforge/c783e47c430a897a7bddb95b64f8fcc0 to your computer and use it in GitHub Desktop.

Select an option

Save jforge/c783e47c430a897a7bddb95b64f8fcc0 to your computer and use it in GitHub Desktop.
# The upcoming curl release enables MQTT support by default
# It still has several limitations, that might be show stoppers in most MQTT environments, but it basically works.
# Major requirement for me is following the server-side security-first directive: at least simple authentication + basic TLS 1.2+ support.
# My conclusion here is to keep using the mosquitto_pub/sub tools, when doing such stuff in a terminal window.
# (and of course to try to support the respective curl contributions ;-))
# 1.) Building curl master
# - git clone https://github.com/curl/curl.git
# - Follow the guide: https://github.com/curl/curl/blob/master/GIT-INFO
autoreconf -fi
./configure
make
sudo make install
# 2.) Test setup
# - Use the Eclipse MQTT test server (mqtt.eclipse.org:1883)
# - Have an MQTT.fx instance and execute this script: https://gist.github.com/jforge/8a21eef42d35b75db3fad76172c6fbf5
# - Consider the limitations: https://github.com/curl/curl/pull/3514
Subscribe (GET) with curl:
/usr/local/bin/curl mqtt://mqtt.eclipse.org/cybus/machine/1CY816L0ⓥ3/lastMovement --output - --trace -
To subscribe with wildcards you need to url encode them (# -> %23, + -> %2B)
/usr/local/bin/curl mqtt://mqtt.eclipse.org/cybus/machine/1CY816L0ⓥ3/%2B --output - --trace -
/usr/local/bin/curl mqtt://mqtt.eclipse.org/%23 --output - --trace -
Publish (POST) with curl
/usr/local/bin/curl -d '{ "value": "scanning" }' mqtt://mqtt.eclipse.org/cybus/machine/1CY816L0ⓥ3/runMode --output - --trace -
/usr/local/bin/curl -d $SOME_PAYLOAD mqtt://mqtt.eclipse.org/cybus/machine/1CY816L0ⓥ3/lastMovement --output - --trace -
for n in {1..20}; do /usr/local/bin/curl -d $SOME_PAYLOAD mqtt://mqtt.eclipse.org/cybus/machine/1CY816L0ⓥ3/lastMovement --output - --trace -; done
with (see also the script curlMqttPublish.sh in this gist)
SOME_PAYLOAD=$(cat <<-END
{
machineId: "1CY816L0ⓥ3",
eventTimestamp: `date +%Y-%m-%dT%H:%M:%S%z`,
temperature: randomTemp,
position: {
X: randomX,
Y: randomY,
Z: randomZ
},
status: "operational"
END
)
#!/usr/bin/env bash
#
# Publish to MQTT server using curl
#
function getNewRandomFloatingPointNumber() {
local randomNumber=`printf "%02d.%04d\n" $((RANDOM%100 + 10)) $RANDOM`
echo $randomNumber
}
for n in {1..20}; do
RANDOM_TEMP=$(getNewRandomFloatingPointNumber)
RANDOM_X=$(getNewRandomFloatingPointNumber)
RANDOM_Y=$(getNewRandomFloatingPointNumber)
RANDOM_Z=$(getNewRandomFloatingPointNumber)
SOME_PAYLOAD=$(cat <<-END
{
"machineId": "1CY816L0ⓥ3",
"eventTimestamp": `date +%Y-%m-%dT%H:%M:%S%z`,
"temperature": $RANDOM_TEMP,
"position": {
"X": $RANDOM_X,
"Y": $RANDOM_Y,
"Z": $RANDOM_Z
},
"status": "operational"
}
END
)
/usr/local/bin/curl -d "$SOME_PAYLOAD" mqtt://mqtt.eclipse.org/cybus/machine/1CY816L0ⓥ3/lastMovement --output - --trace -
sleep 2;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment