MQTT pub/sub library 0.3.4

MQTT library 0.3.4 now available. On this release add Arduino platform, and this client library can use nearly same source code on Particle Photon, Core, severals Arduino Platform(UNO with Ethernet Shield…others).

Particle Photon, Core MQTT is here, and for Arduino branches is here.
BTW, Particle Photon, Core version MQTT library is used over 1,200 apps on the Particle now.

– How to use on Arduino
Particle have Web IDE with community libraries application developer easy to use several latest libraies without notifying. But on Arduino, developer have to add library by your self. here is from github readme.
Download this source code branch zip file and extract to the Arduino libraries directory or checkout MQTT repository. Here is checkout on MacOS X.

cd $HOME/Documents/Arduino/libraries/
git clone -b arduino https://github.com/hirotakaster/MQTT MQTTCilent
# restart Arduino IDE, you can find MQTTCilent examples.

here is SimplePubSub Arduino source code.

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <SPI.h>
#include <MQTT.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

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

/**
 * if want to use IP address,
 * byte server[] = { XXX,XXX,XXX,XXX };
 * MQTT client(server, 1883, callback);
 * want to use domain name,
 * MQTT client("www.sample.com", 1883, callback);
 * change "server_name".
 **/
EthernetClient ethclient;
MQTT client("server_name", 1883, callback, ethclient);

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

  Serial.println(message);
  delay(1000);
}


void setup() {
  Serial.begin(9600);

  // Get IP address from DHCP Server
  Ethernet.begin(mac);

  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte 

Following is Particle Photon, Core same version. Difference points with Arduino are include files and Ethernet connection implementation only.

#include "MQTT/MQTT.h"

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

/**
 * if want to use IP address,
 * byte server[] = { XXX,XXX,XXX,XXX };
 * MQTT client(server, 1883, callback);
 * want to use domain name,
 * MQTT client("www.sample.com", 1883, callback);
 * change "server_name".
 **/
MQTT client("server_name", 1883, callback);

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

  Serial.println(message);
  delay(1000);
}


void setup() {
  Serial.begin(9600);
    
  // connect to the server
  client.connect("mqttclient");

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

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

コメントを残す

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

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