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:


#! /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

  # echo -e "\n\n${phone} ${mesg}"  # For Debug
  curl -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 $*


# 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:

No comments:

Post a Comment