Giter VIP home page Giter VIP logo

mqtt's Introduction

MQTT for Photon, Spark Core

MQTT publish/subscribe library for Photon, Argon, Tracker One...etc version 0.4.31.

Source Code

This lightweight library source code is only 2 files. firmware -> MQTT.cpp, MQTT.h.

The application can use QoS 0, 1, 2 and the retain flag when publishing a message.

Example

Some sample sketches for Spark Core and Photon included (firmware/examples/).

  • mqtttest.ino : simple pub/sub sample.
  • mqttqostest.ino : QoS1, QoS2 publish and callback sample.
  • mqttSwitchBroker.ino : Example of how to switch to different brokers.
  • threadtest.ino : Example of SYSTEM_THREAD(ENABLED).

developer examples

some applications use MQTT with Photon. here are developer's reference examples.

sample source

#include "application.h"
#include "MQTT.h"

void callback(char* topic, byte* payload, unsigned int length);
MQTT client("iot.eclipse.org", 1883, callback);

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

    if (!strcmp(p, "RED"))
        RGB.color(255, 0, 0);
    else if (!strcmp(p, "GREEN"))
        RGB.color(0, 255, 0);
    else if (!strcmp(p, "BLUE"))
        RGB.color(0, 0, 255);
    else
        RGB.color(255, 255, 255);
    delay(1000);
}


void setup() {
    RGB.control(true);

    // connect to the server(unique id by Time.now())
    client.connect("sparkclient_" + String(Time.now()));

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("outTopic/message","hello world");
        client.subscribe("inTopic/message");
    }
}

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

FAQ

Can't connect/publish/subscribe to the MQTT server?

  • Test your MQTT server and port (default 1883) with the mosquitto_pub/sub command.
  • Check your network environments. Make sure your MQTT server can reach the Internet through your firewall.
  • Verify your subscribe/publish topic name is correct.
  • Perhaps device firmware network stack is failed. check your firmware version and bugs.
  • If you are using MQTT-TLS, make sure your RooT CA pem file, client key, certifications is valid.
  • Several MQTT servers will disconnect the first connection when you use the same user_id. When the application calls the connect method, use a different user_id on every device as connect method's second argument. Using the MAC address as a user_id is suggested.
   // device.1
   client.connect("spark-client", "user_1", "password1");
   // other devices...
   client.connect("spark-client", "user_others", "password1");

I want to change MQTT keepalive timeout.

MQTT keepalive timeout is defined "MQTT_DEFAULT_KEEPALIVE 15"(15 sec) in header file. You can change the keepalive timeout in constructor.

    MQTT client("server_name", 1883, callback); // default: send keepalive packet to MQTT server every 15sec.
    MQTT client("server_name", 1883, 256, 30, callback); // keepalive timeout is 30 seconds, default message size also added to be able to access correct constructor 

Want to use over the 255 byte message size.

In this library, the maximum MQTT message size is defined as "MQTT_MAX_PACKET_SIZE 255" in the header file. If you want to use over 255 bytes, use the constructor's last argument.

    MQTT client("server_name", 1883, callback);      // default 255 bytes
    MQTT client("server_name", 1883, 512, callback); // max 512 bytes

Can I use on old firmware?

No, use the default latest firmware. I tested this library on the default latest firmware or the latest pre-release version. If you use an old firmware it may not work well and is not supported.

Bug or Problem?

First, check the Particle community site. But if your problem is not resolved, please create an issue with the problem details.

Pull Request

If you have a bug fix or feature, please send a pull request. Thanks for all developers' pull requests!

mqtt's People

Contributors

hirotakaster avatar nitej avatar ganehag avatar imaustink avatar dakl avatar dguerizec avatar jw3 avatar khaledosman avatar martycochrane avatar orienfish avatar jenesaisdiq avatar smauer avatar technobly avatar kalkov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.