Tuesday, April 7, 2015

MQTT - Internet of Things (IoT) TimeService

I am still playing with the Internet of Things (IoT) and MQTT.

I have implemented a simple TimeService, so each of my (future) devices can subscribe to-and-know the correct current time. This script is more complex than needed, but shown here as an example of what can easily be done with a simple script.




#! /bin/bash
# A mosquitto Time Server
# Author: Eldon Brown - eldonb@ebcon.com

    # Usage: progname [-v] [-p TopicPrefix] [-h BrokerHostAddress] [other mosquitto options]

    # Check for Verbose Debug Option
    case "$1" in
        (-v):; set -xv; shift 1 ;;
    esac

    # Set Topic and Service and then add "Prefix" to Topic if desired
    TOPIC="Time/`date +%Z`"
    SERVICE="client/${TOPIC}"
    case "$1" in
        (-p):; TOPIC="$2/${TOPIC}"; SERVICE="$2/${SERVICE}"; shift 2 ;;
    esac

    HOST='' # Assume Local Host
    case "$1" in
        (-h):; HOST="$1 $2"; shift 2 ;;
    esac


    mosquitto_pub -r ${HOST} -q 1 -t "${SERVICE}" -m 1 $*
    trap "mosquitto_pub -r ${HOST} -q 1 -t \"${SERVICE}\" -m 0 $*; exit 0" SIGHUP SIGINT SIGTERM

    while true
    do

        sleep `date '+(60-%S) %% 10' | bc` # Provide on 10 sec intervals

        date '+%s %Y/%m/%d %H:%M:%S'

        sleep 2          # To avoid multiple reports for the same second

    done |

    mosquitto_pub ${HOST} -t "${TOPIC}" -l $*

# End



For a while I will "publish" this time, as a service on a the public MQTT Broker, at: iot.eclipse.org

You can connect and see the results by executing:



$ mosquitto_sub -h iot.eclipse.org -v -t 'ebcon.com/client/#' -t 'ebcon.com/Time/#' 



The -t 'client/#' topic is retained (-r) by the Broker and will report that the TimeService is active (1), or not (0).

Just for fun, You can subscribe to everything on the iot.eclipse.org MQTT Broker, but be prepared to be Over Whelmed with data!



$ mosquitto_sub -h iot.eclipse.org -v -t '#' 




So, you ask: Just how does MQTT fit into my Ham Radio plans?

I plan to implement "remote control" of radios (among many other things) using MQTT.

More information to follow.


-- Home Page: https://WA0UWH.blogspot.com

No comments:

Post a Comment