During my researches for my home automation project I also stumbled across the MQTT protocol for sensor-server-communication. A matching arduino library was found quickly here on Nick's blog. An arduino sketch was quickly copied from here and modified. Download my version from here.
For testing purposes I decided to use 2lemetry - the blog author of the arduino sample uses it also - but - the interface of 2lemetry has changed a lot. It was really hard to get a simple publish/subscribe scenario up and running.
So find below a short step-by-step example how I get it up and running:
Create a new user at 2lemetry
After completing this step you already have your default project created.
Open it by clicking on it
Now you see the first relevant information - the domain. Insert the domain into the Sketch-constant MQTT_DOMAIN.
To allow external users/devices access to your domain for posting data, you must create a credential. Give it a meaningful name.
After the credential is created, you see two more relevant informations.
Username - enter this string into the Sketch-constant MQTT_USER_NAME.
Token - this is the password for the user. MQTT requires an MD5 encryption of the password, so that no plain-text passwords are transmitted. The MD5 encryption can be done via web-supplied encrypters. Search for "md5 encrypter". The encrypted password is a 32-digit string. Copy the encrypted password into the Sketch-constant MQTT_USER_PASS.
Now your arduino sketch is ready to be downloaded and started. In the arduino-IDE-terminal you should see these messages if everything starts up successful.
Local IP=192.168.2.120
Reconnect to mqtt
Connect client [Arduino_MEGA] OK
Publishing alive message to [domainxx/Arduino_MEGA/light] OK
Subscribe to [domainxx/Arduino_MEGA/control] OK
Now we can start, configuring 2lemetry so we can see the published data from our arduino MQTT-Client. For this we have to open a stream from your projects homepage. Click on Stream.
The logon to your domain is prefilled with the data of your default credential. So no need to change anything on this screen. Simply click Connect. Now the stream screen appears with no subscriptions.
Now create a subscription on the topic, on which the arduino publishes it's data "domainxx/Arduino_MEGA/light". You can copy and paste this from the arduino-console-log. If everythings went right, you should see the messages published from arduino in the right column of the stream window.
Until now, the dataflow from your arduino to MQTT-server at 2lemetry works. Now we check the way from 2lemetry back to your arduino. For this enter a test message in the lower-left panel on the stream window:
After clicking on Publish you should the the payload in the arduino-console-window like this:
Local IP=192.168.2.120
Reconnect to mqtt
Connect client [Arduino_MEGA] OK
Publishing alive message to [c6runvsu/Arduino_MEGA/light] OK
Subscribe to [c6runvsu/Arduino_MEGA/control] OK
Message arrived: topic: c6runvsu/Arduino_MEGA/control
Length: 22
Payload: This is a test message
Congrats, your first steps with MQTT based communications are successfully completed!
Next steps I'll try are
- running a local Mosquitto MQTT server
- rules engine on 2lemetry
- running a private MQT&T server on my Synology NAS box
- ...