Mqtt library makes my photon restart?


I am new to particle and have just bought got 2 photons and have played around with a few simple demos so far ranging from leds to reading a ds18b20 temperature sensor.

Now I’ve got the real basics under my belt I wanted to start playing with some MQTT examples as this is how I plan to get all my devices talking to each other. I already have an mqtt server running and have used this with arduinos, netduinos and the like before. I also use MQTTlens to see what is going on.

So the first thing I tried was the sample app included with MQTT library, updated the settings to my server and deployed. The result was the same as my next few samples using various implementations of the same MQTT library e.g.:

I’ll explain what happens shortly…

My final attempt used the code as described in the last link which I will include below for reference.

This flashed nicely on to spark it did its usual magenta flashing, followed by what I assume is restart, green flashing light, cyan and then the light goes out (because we are controlling the RGB led).

At this point I can’t seem to attach the serial monitor and nothing really seems to happen even if I send values at the topic the photon is subscribed to (yes I’ve checked I’m publishing and subscribed to the correct topics).

After say 20-40 seconds the led fades up to bright white and off, I assume another restart, then it goes through it flashing green, cyan, off.

This repeats and I have to reset the firmware to be able to flash code again.

Any help to get this up and running will be most appreciated :smiley:

Many thanks


// This #include statement was automatically added by the Spark IDE.
#include "SparkJson/SparkJson.h"

#include "MQTT/MQTT.h"

//define your mqtt credentials
#define DEVICE_ID "spark"
#define MQTT_USER "spark"
#define MQTT_PASSWORD "core1234"
#define MQTT_SERVER ""
#define MQTT_CLIENTID "andy" //The  "clientId" given with the credentials

const int sensorPin = A0;
const int led = D7;
int ledState = LOW;
char message_buff[100];
unsigned long lastPublishTime = 0;
unsigned long lastBlinkTime = 0;

                                                // Set here the time in milliseconds between publications
int publishingPeriod = 1000;					// ATTENTION !!!
                                                // DO NOT try to set values under 200 ms or the server
                                                // will kick you out

void callback(char* topic, byte* payload, unsigned int length);

//create our instance of MQTT object
MQTT client(MQTT_SERVER, 1883, callback);

//implement our callback method thats called on receiving data from a subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
  //store the received payload and convert it to string
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  //print the topic and the payload received
  Serial.println("topic: " + String(topic));
  Serial.println("payload: " + String(p));
  //call our method to parse and use the payload received

void handlePayload(char* payload) {
  StaticJsonBuffer<200> jsonBuffer;
  //convert payload to json
  JsonObject& json = jsonBuffer.parseObject(payload);
  if (!json.success()) {
    Serial.println("json parsing failed");
  //get value of the key "command"
  const char* command = json["command"];
  Serial.println("parsed command: " + String(command));
  if (String(command).equals("color"))
    const char* color = json["value"];
    Serial.println("parsed color: " + String(color));
    String s(color);
    if (s.equals("red"))
      RGB.color(255, 0, 0);
    else if (s.equals("blue"))
      RGB.color(0, 0, 255);
    else if (s.equals("green"))
      RGB.color(0, 255, 0);

void mqtt_connect() {
  Serial.println("Connecting to mqtt server");
  if (client.connect(MQTT_CLIENTID, MQTT_USER, MQTT_PASSWORD)) {
    Serial.println("Connection success, subscribing to topic");
    //subscribe to a topic
  else {
    Serial.println("Connection failed, check your credentials or wifi");

void setup() {
  Serial.println("Hello There, I'm your photon!");
  //setup our LED pin and connect to mqtt broker
  pinMode(led, OUTPUT);
  //set 200ms as minimum publishing period
  publishingPeriod = publishingPeriod > 200 ? publishingPeriod : 200;

void loop() {
  if (client.isConnected()) {
    //publish within publishing period
    if (millis() - lastPublishTime > publishingPeriod) {
      lastPublishTime = millis();
    blink(publishingPeriod / 2);
  else {
    //if connection lost, reconnect

void publish() {
  //create our json payload as {"meaning":"moisture","value":sensorvalue}
  StaticJsonBuffer<100> pubJsonBuffer;
  JsonObject& pubJson = pubJsonBuffer.createObject();
  //define a meaning for what we're sending
  pubJson["meaning"] = "moisture";
  //set our value key to the sensor's reading
  pubJson["value"] = analogRead(sensorPin);
  //copy our json object as a char array and publish it via mqtt
  char message_buff[100];
  pubJson.printTo(message_buff, sizeof(message_buff));
  client.publish("/v1/"DEVICE_ID"/data", message_buff);
  Serial.println("Publishing " + String(message_buff));

void blink(int interval) {
  if (millis() - lastBlinkTime > interval) {
    // save the last time you blinked the LED
    lastBlinkTime = millis();
    if (ledState == LOW)
      ledState = HIGH;
      ledState = LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(led, ledState);

Just wondered if anyone can offer me any suggestions? :slight_smile:

Please update to the latest firmware release to see if that makes a difference.

Hi @mdma

I’m having issues updating the firmware, I think I’ve followed all the information I can find on building the firmware but when I do I get the following errors :frowning:

C:\Users\amcinnes\Documents\GitHub\photon\modules>make clean all program-dfu PLATFORM=photon
’head’ is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the file specified.
The system cannot find the file specified.
clean all program-dfu
make -C C:/Users/amcinnes/Documents/GitHub/photon/modules/photon/system-part1/ clean all program-dfu PLATFORM=photon
The system cannot find the path specified.
The system cannot find the path specified.
‘head’ is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the file specified.
The system cannot find the file specified.
make[1]: Entering directory C:/Users/amcinnes/Documents/GitHub/photon/modules/photon/system-part1' make -C ../../../communication clean The system cannot find the path specified. The system cannot find the path specified. 'head' is not recognized as an internal or external command, operable program or batch file. The system cannot find the file specified. The system cannot find the file specified. make[2]: Entering directoryC:/Users/amcinnes/Documents/GitHub/photon/communication’
rm -f …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/aes.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/bignum.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/padlock.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/rsa.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/sha1.o …/build/target/communication/platform-6-m-prod-6/src/coap.o …/build/target/communication/platform-6-m-prod-6/src/handshake.o …/build/target/communication/platform-6-m-prod-6/src/spark_protocol.o …/build/target/communication/platform-6-m-prod-6/src/events.o …/build/target/communication/platform-6-m-prod-6/src/spark_protocol_functions.o …/build/target/communication/platform-6-m-prod-6/src/communication_dynalib.o …/build/target/communication/platform-6-m-prod-6/src/dsakeygen.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/aes.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/bignum.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/padlock.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/rsa.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/sha1.o.d …/build/target/communication/platform-6-m-prod-6/src/coap.o.d …/build/target/communication/platform-6-m-prod-6/src/handshake.o.d …/build/target/communication/platform-6-m-prod-6/src/spark_protocol.o.d …/build/target/communication/platform-6-m-prod-6/src/events.o.d …/build/target/communication/platform-6-m-prod-6/src/spark_protocol_functions.o.d …/build/target/communication/platform-6-m-prod-6/src/communication_dynalib.o.d …/build/target/communication/platform-6-m-prod-6/src/dsakeygen.o.d …/build/target/communication/platform-6-m-prod-6/libcommunication.a
process_begin: CreateProcess(NULL, rm -f …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/aes.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/bignum.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/padlock.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/rsa.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/sha1.o …/build/target/communication/platform-6-m-prod-6/src/coap.o …/build/target/communication/platform-6-m-prod-6/src/handshake.o …/build/target/communication/platform-6-m-prod-6/src/spark_protocol.o …/build/target/communication/platform-6-m-prod-6/src/events.o …/build/target/communication/platform-6-m-prod-6/src/spark_protocol_functions.o …/build/target/communication/platform-6-m-prod-6/src/communication_dynalib.o …/build/target/communication/platform-6-m-prod-6/src/dsakeygen.o …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/aes.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/bignum.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/padlock.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/rsa.o.d …/build/target/communication/platform-6-m-prod-6/lib/tropicssl/library/sha1.o.d …/build/target/communication/platform-6-m-prod-6/src/coap.o.d …/build/target/communication/platform-6-m-prod-6/src/handshake.o.d …/build/target/communication/platform-6-m-prod-6/src/spark_protocol.o.d …/build/target/communication/platform-6-m-prod-6/src/events.o.d …/build/target/communication/platform-6-m-prod-6/src/spark_protocol_functions.o.d …/build/target/communication/platform-6-m-prod-6/src/communication_dynalib.o.d …/build/target/communication/platform-6-m-prod-6/src/dsakeygen.o.d …/build/target/communication/platform-6-m-prod-6/libcommunication.a, …) failed.
make (e=2): The system cannot find the file specified.
make[2]: *** [clean] Error 2
make[2]: Leaving directory C:/Users/amcinnes/Documents/GitHub/photon/communication' make[1]: *** [clean_communication] Error 2 make[1]: Leaving directoryC:/Users/amcinnes/Documents/GitHub/photon/modules/photon/system-part1’
make: *** [C:/Users/amcinnes/Documents/GitHub/photon/modules/photon/system-part1/makefile] Error 2

Any ideas where I am going wrong?

Many thanks


Hi @mdma

I still have the issues I mentioned but wanted to confirm I am on firmware 0.4.4, is it the latest 0.4.5 you want me to update to? With that in mind, I’m still having issues building the firmware :frowning:

Any help would be much appreciated.

Many thanks


To update to the latest firmware, you don’t need to build locally. You can put your device in DFU mode, then run

npm install -g particle-cli
particle update

That will install 0.4.4 to your device.

The build problem is that you don’t have needed tools in your path. The Toolchain for Windows Installer is the simplest way to get started.

Hi @mdma

I am already on firmware 0.4.4 and am having the issue with mqtt.

Should I try to get the toolchain working and build 0.4.5? Or should this be working with 0.4.4 that I have?

Also are the firmware files available prebuilt for download?

Many thanks


I just tried the example, but of course the test was only going to be limited since I don’t have a MQTT server running.

But I’m seeing serial output working. As a start, you should take down the MQTT server and verify you’re seeing failed connection messages. Since you’re on Windows, be sure you are fully disconnecting the device and closing your serial program (e.g. PuTTY.) Windows is finnicky with serial connections.

Once that’s working, add more Serial output to your program so you can see exactly where the code is hanging.