Introduction: Spark Core / Photon and CloudMQTT
In this instructables you will learn about "message queuing" specially "MQTT" protocol that specially used for IoT applications , in our case the Spark Core / Photon .
We will be using the default "MQTT" Example and library supplied by Spark Cloud IDE.
This intractable is made for the Spark core build night At Cairo Hackerspace, Egypt.
Step 1: What Is Message Queuing?
Message queues provide an asynchronous communications protocol,
meaning that the sender and receiver of the message do not need to interact with the message queue at the same time. Messages placed onto the queue are stored until the recipient retrieves them. Message queues have implicit or explicit limits on the size of data that may be transmitted in a single message and the number of messages that may remain outstanding on the queue.
Other implementations allow the passing of messages between different computer systems, potentially connecting multiple applications and multiple operating systems. These message queuing systems typically provide enhanced resilience functionality to ensure that messages do not get "lost" in the event of a system failure.
There are a number of open source choices of messaging middleware systems, including JBoss Messaging, JORAM, Apache ActiveMQ, Sun Open Message Queue, Apache Qpid, RabbitMQ, Beanstalkd, Tarantool and HTTPSQS.
Step 2: MQTT
MQTT stands for Message Queuing Telemetry Transport. It is a publish/subscribe,
extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.
The Publish-Subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message
Step 3: CloudMQTT
CloudMQTT Is a message broker service , Meaning : To create
distributed queues or topics we need to have the message brokers communicate with each other.
CloudMQTT are managed Mosquitto servers in the cloud.
Mosquitto implements the MQ Telemetry Transport protocol, MQTT, which provides lightweight methods of carrying out messaging using a publish/subscribe model.
Create a CloudMQTT instance
Create an account and login to the control panel and press Create+ to create a new instance. Choose a name for the instance and the datacenter to host the instance. To get started you need to sign up for a customer plan. What plan you want to use depend of your needs. CloudMQTT offer four different plans for different needs.
We will be using the free plan CuteCat in this instructables .
Please refer to CloudMQTT documentation for for further help.
Step 4: Spark.io/build
Starting by the Spark Programing .
- Point your web browser to https://www.spark.io/build
- Login with your account that you already created using the mobile APP or create a new one from the same login windeow , you should see "Sing Up" link.
- After logging in successfully you should be already claimed your core through the mobile app or manually by following the steps here: http://docs.spark.io/connect/
- Now chose libraries from the small icons down the left side of the website.
- Search for "MQTT" , you will see a library with the same name , click on it.
- After clicking on the library ,3 tabs will open in the IDE coding area (MQTT.cpp , MQTT.h and mqtttest.ino).
- Click on "mqtttest.ino" and on the left panel you should see "Use this example" , Click it.
Editing the code:
Usually when I rely on what the spark web IDE say on the left "Included Libraries " It never work , may be I am missing something , You need to copy the files manually every time.
- On the top right side of the IDE you should see a Plus sign "+" , this adds a new file to your project.
- Create two new files and copy the code from MQTT.cpp and MQTT.h to those new files and name them the same names.
- Now Verify the code.
- In the "mqtttest.ino" Line 29 :
change it to :
client.connect("cloudmqtt-url", "userid", "password");
- Now put the URL , User name And password from your CloudMQTT control panel .
Spark MQTT test code and Libraries : https://github.com/hirotakaster/MQTT
Step 5: Mosquitto
Mosquitto is an open source message broker that implements the MQ Telemetry Transport
protocol . MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for "machine to machine" messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino. A good example of this is all of the work that Andy Stanford-Clark (one of the originators of MQTT) has done in home monitoring and automation with his twittering house and twittering ferry.
Download mosquitto from this page: http://mosquitto.org/download
You should find almost all OSs.
Prerequisites and installation instruction should be not complicated for most OSs , you should read it next to your OS download link.
The broker should be up and running on port 1883. You will find out if it is running when you try the following tests.
Open 2 terminals. In the first run this command:
<em>mosquitto_sub -d -t hello/world</em>
Here we are subscribing to all messages with the topic "hello/world".
Publishing under a Topic
To publish a message under a specific topic you can run the following in your thus far unused terminal:
mosquitto_pub -d -t hello/world -m "Hello, MQTT. This is my first message."
Step 6: Send and Receive Messages To/from Your Core.
Now after preparing and testing everything and sure that everything works fine , The core, the broker "CloudMQTT" and mosquitto ,we will try this sample python program provided by CloudMQTT documentation .
The prerequisite for this program is mosquitto because it use some libraries from it to connect to MQTT protocol.
You can download the program from this link: https://github.com/CloudMQTT/python-mqtt-example
Change this line with you CloudMQTT url:
# Parse CLOUDMQTT_URL (or fallback to localhost) url_str = os.environ.get('CLOUDMQTT_URL', 'mqtt://localhost:1883') url = urlparse.urlparse(url_str).
Change my message with what you send to the core , In our case (RED,GREE or BLUE).
# Publish a message
mqttc.publish("hello/world", "my message")
Step 7: You Have Done It :)
Now after you made a successful communication to /from your core through MQTT try to play with it more and share you projects with us.
We have a be nice policy.
Please be positive and constructive.