Showing posts with label MQTT. Show all posts
Showing posts with label MQTT. Show all posts

Sunday, April 12, 2015

More Playing with MQTT

With updated software, mosquitto version 1.4.1, it is very easy to connect two Brokers (see problem as suggested on previous post). A few new line in the configuration files is all that is needed. To publish (upload-only) my Broker's Topics to the public Broker at iot.eclipse.org, I used the following.

Content of: /etc/mosquitto/conf.d/bridge.conf


connection ebcon.com
address iot.eclipse.org
topic # out 0 "" ebcon.com/



Out-bound topics have "ebcon.com/" pre-pended, currently I do not allow or receive data from iot.eclipse.org directly by my Broker. If I want a topic, I subscribe directly with an application (mosquitto_sub).

My own local Brokers (in three different physical locations) exchange data from the two lesser Brokers in bidirectional fashion. Brokers do not have to be hierarchical, they can be peers. In my case I chose to configure the "bridge" on what I consider the lesser Brokers.



connection remote1
address <remote broker IPA>
topic # both 0



As a test, I created a "JoyStick" control publisher, when the joysticks are moved, all Brokers receive the data, much faster than I expected. Performance is very good. Eventually, I will control remote servos to direct (point) Raspberry PI Cameras.

You can subscribe to my testing with:



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




Note: Mosquitto MQTT Brokers can publish any kind of data, including Web Pages and Photos.



I am just learning and having FUN with MQTT !


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

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

Friday, April 3, 2015

The Internet-of-Things and MQTT

UPDATED: 2015/04/07
To include mosquitto2sms status via mqtt

The Internet Hobby Electronics world is ABLAZE with Esp8266 excitement.  These Ebay $3.00 modules are being used to create some very interesting Internet-of-Things (IoT) projects. Search Google and Youtube for Esp8266.

To make interesting projects, code can be downloaded into the processor on the Esp8266, or it can be connected to a processor like the Arduino. In either case, access to the Internet for a Hobby Project is very exciting.

One way to provide status and/or control to/from the Esp8266 is via a (little known) protocol called "MQTT", which provides simple and small processor code footprint. Actually, it is not unknown, Facebook uses it !

For Raspberry PI and Linux people, there are downloadable commands that implement MQTT, the package that contain these are "mosquitto" and "mosquitto-clients"




$ sudo apt-get install mosquitto mosquitto-clients




With "mosquitto" and the "curl" command (see previous post), I implemented a mosquitto2sms gateway, as:

mosquitto2sms


#! /bin/bash
# Author: Eldon R Brown - WA0UWH

 # Usage: mosquitto2sms [-h BrokerHostAddress] []


 # set -xv   # For Debug

 mosquitto_sub -v -t 'ToSMS/#' $* |

 sed -u -n 's/^ToSMS\/\([0-9][0-9]\+\) /\1 /p' |         # Extract just the phone Number and Message

 while read phone mesg
 do

  # echo -e "\n\n${phone} ${mesg}"  # For Debug
  curl http://textbelt.com/text -d "number=${phone}" -d "message=${mesg}" 2>&1 |
  grep -q true &&
  mosquitto_pub -t "ToSMS/${phone}/Stat" -m 1 $* ||
  mosquitto_pub -t "ToSMS/${phone}/Stat" -m 0 $*

 done

# End




On another system, or any MQTT device, a simple message can be easily sent to a Cell Phone. Example as shown is from Raspberry PI.




mosquitto_pub -q 1 -t ToSMS/2025551212 -m 'Test Message'




To watch (or monitor) all of the activity on the messaging system, execute the following (i.e., subscribe to all):




mosquitto_sub -v -t '+/#'




 I am anxiously awaiting the arrival of my ebay Esp8266's, . . . only a few more days !


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