Introduction: Spark Core / Photon and CloudMQTT

Picture of 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?

Picture of 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.

WIKIPEDIA

Step 2: MQTT

Picture of 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

MQTT.ORG

WIKIPEDIA


Step 3: CloudMQTT

Picture of 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.

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.

Step 4: Spark.io/build

Picture of Spark.io/build

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

Step 5: Mosquitto

Picture of 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.

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

Step 6: Send and Receive Messages To/from Your Core.

Picture of 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 :)

Picture of 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.

Comments

CptanPanic (author)2015-07-15

Why do you setup cloudmqtt and then also a local mqtt server? And in your example you didn't actually set it up to use any of them. I ask because I am having problems with cloudmqtt and the particle.

marco.milone.10 (author)2015-04-22

I have the same issue!

NickM8 (author)2015-03-10

when copying the content of the spark library files verbatim ('MQTT.cpp', 'MQTT.h', 'mqtttest.ino'), the compile fails with the error below.

The same error occurs when copying the file contents verbatim directly from https://github.com/hirotakaster/MQTT files.

What gives? has the spark firmware changed enough to break the example code?

Using SPARK FIRMWARE V0.3.4. Thanks!

Error info below:

------------------------

"Error: Could not compile. Please review your code."

In file included from ../inc/spark_wiring.h:29:0,

from ../inc/spark_wiring_stream.h:36,

from ../inc/spark_wiring_client.h:24,

from ../inc/spark_wiring_tcpclient.h:29,

from MQTT.cpp:56:

../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]

#warning "Defaulting to Release Build"

^

In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,

from ../inc/spark_wlan.h:33,

from ../inc/main.h:38,

from ../inc/spark_utilities.h:30,

from ../inc/spark_wiring.h:33,

from ../inc/spark_wiring_stream.h:36,

from ../inc/spark_wiring_client.h:24,

from ../inc/spark_wiring_tcpclient.h:29,

from MQTT.cpp:56:

../../core-common-lib/CC3000_Host_Driver/socket.h:146:0: warning: "fd_set" redefined [enabled by default]

#define fd_set _types_fd_set_cc3000

^

In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,

from ../inc/spark_wiring_print.h:30,

from ../inc/spark_wiring_string.h:33,

from MQTT.cpp:55:

/opt/gcc_arm/arm-none-eabi/include/sys/types.h:256:0: note: this is the location of the previous definition

#define fd_set _types_fd_set

^

In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,

from ../inc/spark_wlan.h:33,

from ../inc/main.h:38,

from ../inc/spark_utilities.h:30,

from ../inc/spark_wiring.h:33,

from ../inc/spark_wiring_stream.h:36,

from ../inc/spark_wiring_client.h:24,

from ../inc/spark_wiring_tcpclient.h:29,

from MQTT.cpp:56:

../../core-common-lib/CC3000_Host_Driver/socket.h:162:0: warning: "FD_SET" redefined [enabled by default]

#define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp)

^

In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,

from ../inc/spark_wiring_print.h:30,

from ../inc/spark_wiring_string.h:33,

from MQTT.cpp:55:

/opt/gcc_arm/arm-none-eabi/include/sys/types.h:258:0: note: this is the location of the previous definition

# define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1L << ((n) % NFDBITS)))

^

In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,

from ../inc/spark_wlan.h:33,

from ../inc/main.h:38,

from ../inc/spark_utilities.h:30,

from ../inc/spark_wiring.h:33,

from ../inc/spark_wiring_stream.h:36,

from ../inc/spark_wiring_client.h:24,

from ../inc/spark_wiring_tcpclient.h:29,

from MQTT.cpp:56:

../../core-common-lib/CC3000_Host_Driver/socket.h:163:0: warning: "FD_CLR" redefined [enabled by default]

#define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp)

^

In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,

from ../inc/spark_wiring_print.h:30,

from ../inc/spark_wiring_string.h:33,

from MQTT.cpp:55:

/opt/gcc_arm/arm-none-eabi/include/sys/types.h:259:0: note: this is the location of the previous definition

# define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1L << ((n) % NFDBITS)))

^

In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,

from ../inc/spark_wlan.h:33,

from ../inc/main.h:38,

from ../inc/spark_utilities.h:30,

from ../inc/spark_wiring.h:33,

from ../inc/spark_wiring_stream.h:36,

from ../inc/spark_wiring_client.h:24,

from ../inc/spark_wiring_tcpclient.h:29,

from MQTT.cpp:56:

../../core-common-lib/CC3000_Host_Driver/socket.h:164:0: warning: "FD_ISSET" redefined [enabled by default]

#define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp)

^

In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,

from ../inc/spark_wiring_print.h:30,

from ../inc/spark_wiring_string.h:33,

from MQTT.cpp:55:

/opt/gcc_arm/arm-none-eabi/include/sys/types.h:260:0: note: this is the location of the previous definition

# define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1L << ((n) % NFDBITS)))

^

In file included from ../../core-common-lib/CC3000_Host_Driver/evnt_handler.h:38:0,

from ../inc/spark_wlan.h:33,

from ../inc/main.h:38,

from ../inc/spark_utilities.h:30,

from ../inc/spark_wiring.h:33,

from ../inc/spark_wiring_stream.h:36,

from ../inc/spark_wiring_client.h:24,

from ../inc/spark_wiring_tcpclient.h:29,

from MQTT.cpp:56:

../../core-common-lib/CC3000_Host_Driver/socket.h:165:0: warning: "FD_ZERO" redefined [enabled by default]

#define FD_ZERO(fdsetp) __FD_ZERO (fdsetp)

^

In file included from /opt/gcc_arm/arm-none-eabi/include/stdio.h:47:0,

from ../inc/spark_wiring_print.h:30,

from ../inc/spark_wiring_string.h:33,

from MQTT.cpp:55:

/opt/gcc_arm/arm-none-eabi/include/sys/types.h:261:0: note: this is the location of the previous definition

# define FD_ZERO(p) (__extension__ (void)({ \

^

In file included from ../inc/spark_wiring.h:29:0,

from ../inc/application.h:29,

from mqtttest.cpp:2:

../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]

#warning "Defaulting to Release Build"

^

mqtttest.cpp:5:42: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

#line 1

^

mqtttest.cpp: In function 'void callback(char*, byte*, unsigned int)':

mqtttest.cpp:11:15: warning: converting to non-pointer type 'char' from NULL [-Wconversion-null]

^

mqtttest.cpp: In function 'void setup()':

mqtttest.cpp:30:33: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

^

mqtttest.cpp:34:48: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

// connect to the server

^

mqtttest.cpp:34:48: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

mqtttest.cpp:35:35: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

client.connect("sparkclient");

^

mqtttest.o: In function `setup':

/spark/compile_server/shared/workspace/2_compile-server2/core-firmware/build/mqtttest.cpp:30: undefined reference to `MQTT::connect(char*)'

/spark/compile_server/shared/workspace/2_compile-server2/core-firmware/build/mqtttest.cpp:33: undefined reference to `MQTT::isConnected()'

/spark/compile_server/shared/workspace/2_compile-server2/core-firmware/build/mqtttest.cpp:34: undefined reference to `MQTT::publish(char*, char*)'

/spark/compile_server/shared/workspace/2_compile-server2/core-firmware/build/mqtttest.cpp:35: undefined reference to `MQTT::subscribe(char*)'

mqtttest.o: In function `loop':

/spark/compile_server/shared/workspace/2_compile-server2/core-firmware/build/mqtttest.cpp:40: undefined reference to `MQTT::isConnected()'

/spark/compile_server/shared/workspace/2_compile-server2/core-firmware/build/mqtttest.cpp:41: undefined reference to `MQTT::loop()'

mqtttest.o: In function `__static_initialization_and_destruction_0':

/spark/compile_server/shared/workspace/2_compile-server2/core-firmware/build/mqtttest.cpp:5: undefined reference to `MQTT::MQTT(char*, unsigned short, void (*)(char*, unsigned char*, unsigned int))'

collect2: error: ld returned 1 exit status

make: *** [b43d8f930b22f0b9b2e8154ddd3da59ac91e0edc109424a182be4a17143d.elf] Error 1

About This Instructable

20,798views

41favorites

License:

Bio: Hack like an Egyptian!
More by ToutHackAmon:Reading LightSpark Core / Photon and CloudMQTTEL wire motorcycle tron helmet
Add instructable to: