Particle MQTT 0.3.0 library

MQT library 0.3.0 for Particle Photon/Spark release. Already can use Web IDE. This release fixed some bug and implement QoS1|2 flag. MQTT V3.1 Protocol Specification is here. MQTT have QoS flag when it send PUBLISH/SUBSCRIBE…etc message. QoS level is following.

QoS 0
  Make the message available to any interested parties.

QoS 1
  Log the message to persistent storage, make it available to any interested parties, and return a PUBACK message to the sender.

QoS 2
  Log the message to persistent storage, do not make it available to interested parties yet, and return a PUBREC message to the sender.

Previous Photon MQTT library already use QoS 1 when Photon/Particle send message to the MQTT server, but this 0.3.0 release can control the QoS level. If application don’t want to use QoS flag on program, library use QoS 0 level(of cource don’t need to change the source code, if application use before 0.3.0 release library).

Here is How to use QoS1|2 source code.
mqttqostest.ino

#include "MQTT.h"

void callback(char* topic, byte* payload, unsigned int length);
MQTT client("www.hirotakaster.com", 1883, callback);

// recieve message from MQTT server.
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

    if (message.equals("RED"))    
        RGB.color(255, 0, 0);
    else if (message.equals("GREEN"))    
        RGB.color(0, 255, 0);
    else if (message.equals("BLUE"))    
        RGB.color(0, 0, 255);
    else    
        RGB.color(255, 255, 255);
    delay(1000);
}

// QoS ack callback.
// MQTT server sendback message id ack, if use QOS1 or QOS2.
void qoscallback(unsigned int messageid) {
    Serial.print("Ack Message Id:");
    Serial.println(messageid);
}

void setup() {
    Serial.begin(9600);
    RGB.control(true);
    
    // connect to the server
    client.connect("sparkclient");

    // add qos callback
    client.addQosCallback(qoscallback);

    // publish/subscribe
    if (client.isConnected()) {
        // get messageid parameter at 4.
        // if you want to check the QoS ack response from MQTT server,
        // store messageid(list or array) and check on "QoS ack callback".
        uint16_t messageid;

        // use QoS1
        client.publish("/outTopic", "hello world QOS1", MQTT::QOS1, &messageid);
        Serial.println(messageid);

        // use QoS2
        client.publish("/outTopic", "hello world QOS2", MQTT::QOS2, &messageid);
        Serial.println(messageid);

        client.subscribe("/inTopic");
    }
}

void loop() {
    if (client.isConnected())
        client.loop();
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください