Spark Core / Photon and CloudMQTT

by ToutHackAmon in Circuits > Software

25948 Views, 41 Favorites, 0 Comments

Spark Core / Photon and CloudMQTT

chs_banner2-01.png
cloud0.png
spark.jpg

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.

What Is Message Queuing?

mq.jpg

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.

WIKIPEDIA

MQTT

mqtt-hub.jpg
dbd42c343f6c4bbba564cb404e5b8994.png
cloud0.png

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

MQTT.ORG

WIKIPEDIA


CloudMQTT

cloud0.png
cloud1.png
cloud3.png
cloud5.png
cloud6.png
cloud7.png

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.

CloudMQTT

mosquitto

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.

Spark.io/build

login1.png
code4.png
code5.png
code6.png
code3.png
cloud7.png
code2.png

Starting by the Spark Programing .

  1. Point your web browser to https://www.spark.io/build
  2. 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.
  3. 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/
  4. Now chose libraries from the small icons down the left side of the website.
  5. Search for "MQTT" , you will see a library with the same name , click on it.
  6. After clicking on the library ,3 tabs will open in the IDE coding area (MQTT.cpp , MQTT.h and mqtttest.ino).
  7. 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.

  1. On the top right side of the IDE you should see a Plus sign "+" , this adds a new file to your project.
  2. Create two new files and copy the code from MQTT.cpp and MQTT.h to those new files and name them the same names.
  3. Now Verify the code.
  4. In the "mqtttest.ino" Line 29 :
    client.connect("sparkclient");
    change it to :
    client.connect("cloudmqtt-url", "userid", "password");
  5. Now put the URL , User name And password from your CloudMQTT control panel .

Spark MQTT test code and Libraries : https://github.com/hirotakaster/MQTT

Mosquitto

mosquitto.png
moqito1.png
term1.png
term2.png

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.

Testing Mosquitto

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."

Send and Receive Messages To/from Your Core.

python1.png
mosquitto.png
cloud7.png

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")

You Have Done It :)

cloud0.png
spark.jpg
mosquitto.png
chs_banner2-01.png

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.