Wednesday, March 18, 2015

HotPaw Basic now supports MQTT messaging

MQTT support has been added to HotPaw Basic version 1.7.1 for iOS.

What is MQTT? It's one of the hottest new technologies for the Internet of Things. MQTT is a lightweight messaging protocol, where short messages, such as number from a temperature sensor, can get relayed though servers in the cloud, called Brokers. These messages can be relayed to an app somewhere else, such as the iPhone of someone on the road who want to check the latest home temperature reading. IBM is one of the major vendors supporting MQTT.

MQTT is "light-weight", meaning there's a lot less bits and bytes of overhead that need to get passed back and forth to send a message, much less than needed for accessing even a tiny web page. This makes MQTT more suitable for button-sized devices which can run off a tiny battery for over a year.

To send a message using MQTT, one needs to initialize a few details. You need to know at least two things so that the message knows where to start heading: the name of a MQTT host server in cloud and a port number for that broker. Optionally you will need a username and password a private broker, or for one of many MQTT brokers run as commercial services. IBM, cloudmqtt.com and hivemq.com provide commercial MQTT broker services. You can use Google to find "public MQTT brokers", or you can run a broker on your own server (even a Raspberry Pi). There are currently a few public brokers available for testing which need no password. One such is: "iot.eclipse.org", port 1883. Please don't use public brokers for more than a small amount of basic testing. Note that any the data one might send thru these public brokers is, well, completely public.

One needs to know 2 more things to identify the sending device and the message. A unique client ID helps the broker to not get confused between zillions of device trying to talk to it. IBM uses a MAC address as a unique ID. You can look up the MAC address for your iPhone in Settings:General:About:WiFi Address. You will also need a identifying topic so that a subscriber can find your devices messages out of the zillions of other messages sent by zillions of other devices. This is a short hierarchical string separated by slashes, such as "sample.corp/london.office/building.3/room.4/temperature".

And last, there is your short message itself, usually just a few characters, although IBM would like it packed into a JSON formatted string.

Here's an example HotPaw Basic program to send a message to an MQTT server.

100 rem MQTT test send
200 host$ = "iot.eclipse.org" :rem a public MQTT broker
210 port = 1883
220 client$ = "12345678" :rem Please put you MAC ADDR here.
300 fn mqtt.connect(host$,port,client$)
310 fn sleep(1)
320 print fn mqtt.connected()
400 topic$ = "hotpaw/test/randomdata/0"
410 msg$ = "hello 123"
500 fn mqtt.publish(topic$,msg$,1)
900 fn mqtt.disconnect()
910 print "done"
990 end

The JSON formatted version of the message would look more like this:

410 temperature = 22.5
412 msg$ = '{ "d": {"myName":"Arduino","temperature":'
414 msg$ = msg$ + str$(temperature) + " } }"

There are a few MQTT apps in the App store that can be used to check your message.

To see the message, you could run this HotPaw Basic program on another iOS device. Since these messages are relayed though a remote broker, the 2 iOS devices don't need to be on the same local network.

100 rem MQTT test subscribe
200 host$ = "iot.eclipse.org"
210 port = 1883
220 client$ = "12345678" :rem Please put you MAC ADDR here.
300 fn mqtt.connect(host$,port,client$)
310 fn sleep(1)
320 print fn mqtt.connected()
400 topic$ = "hotpaw/test/randomdata/0"
500 fn mqtt.subscribe(topic$)
520 for i = 1 to 10
530   fn sleep(10) : rem waits for message
535   print ".";
540   r$ = fn mqtt.message$()
560   if len(r$) > 0 then print : print "message = ", r$
570   if len(r$) > 0 then exit for
590 next i
900 fn mqtt.disconnect()
910 print "done"
990 end

No comments: