Difference between revisions of "MQTT: Arduino Publish di callback"

From OnnoWiki
Jump to navigation Jump to search
(Created page with " /* Publishing in the callback - connects to an MQTT server - subscribes to the topic "inTopic" - when a message is received, republishes it to "outTopic" Th...")
 
Line 1: Line 1:
/*
+
/*
  Publishing in the callback
+
Publishing in the callback
+
 
  - connects to an MQTT server
+
  - connects to an MQTT server
  - subscribes to the topic "inTopic"
+
  - subscribes to the topic "inTopic"
  - when a message is received, republishes it to "outTopic"
+
  - when a message is received, republishes it to "outTopic"
+
 
  This example shows how to publish messages within the
+
  This example shows how to publish messages within the
  callback function. The callback function header needs to
+
  callback function. The callback function header needs to
  be declared before the PubSubClient constructor and the
+
  be declared before the PubSubClient constructor and the
  actual callback defined afterwards.
+
  actual callback defined afterwards.
  This ensures the client reference in the callback function
+
  This ensures the client reference in the callback function
  is valid.
+
  is valid.
+
 
*/
+
*/
+
 
#include <SPI.h>
+
#include <SPI.h>
#include <Ethernet.h>
+
#include <Ethernet.h>
#include <PubSubClient.h>
+
#include <PubSubClient.h>
+
 
// Update these with values suitable for your network.
+
// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
+
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 4);
+
IPAddress ip(192, 168, 0, 4);
IPAddress server(192, 168, 0, 100);
+
IPAddress server(192, 168, 0, 100);
+
 
// Callback function header
+
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
+
void callback(char* topic, byte* payload, unsigned int length);
+
 
EthernetClient ethClient;
+
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
+
PubSubClient client(server, 1883, callback, ethClient);
+
 
// Callback function
+
// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
+
void callback(char* topic, byte* payload, unsigned int length) {
  // In order to republish this payload, a copy must be made
+
  // In order to republish this payload, a copy must be made
  // as the orignal payload buffer will be overwritten whilst
+
  // as the orignal payload buffer will be overwritten whilst
  // constructing the PUBLISH packet.
+
  // constructing the PUBLISH packet.
+
 
  // Allocate the correct amount of memory for the payload copy
+
  // Allocate the correct amount of memory for the payload copy
  byte* p = (byte*)malloc(length);
+
  byte* p = (byte*)malloc(length);
  // Copy the payload to the new buffer
+
  // Copy the payload to the new buffer
  memcpy(p,payload,length);
+
  memcpy(p,payload,length);
  client.publish("outTopic", p, length);
+
  client.publish("outTopic", p, length);
  // Free the memory
+
  // Free the memory
  free(p);
+
  free(p);
}
+
}
+
 
void setup()
+
void setup()
{
+
{
+
 
  Ethernet.begin(mac, ip);
+
  Ethernet.begin(mac, ip);
  if (client.connect("arduinoClient")) {
+
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
+
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
+
    client.subscribe("inTopic");
  }
+
  }
}
+
}
+
 
void loop()
+
void loop()
{
+
{
  client.loop();
+
  client.loop();
}
+
}

Revision as of 16:29, 11 May 2018

/*

Publishing in the callback
 - connects to an MQTT server
 - subscribes to the topic "inTopic"
 - when a message is received, republishes it to "outTopic"
 This example shows how to publish messages within the
 callback function. The callback function header needs to
 be declared before the PubSubClient constructor and the
 actual callback defined afterwards.
 This ensures the client reference in the callback function
 is valid.
  • /
  1. include <SPI.h>
  2. include <Ethernet.h>
  3. include <PubSubClient.h>

// Update these with values suitable for your network. byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; IPAddress ip(192, 168, 0, 4); IPAddress server(192, 168, 0, 100);

// Callback function header void callback(char* topic, byte* payload, unsigned int length);

EthernetClient ethClient; PubSubClient client(server, 1883, callback, ethClient);

// Callback function void callback(char* topic, byte* payload, unsigned int length) {

 // In order to republish this payload, a copy must be made
 // as the orignal payload buffer will be overwritten whilst
 // constructing the PUBLISH packet.
 // Allocate the correct amount of memory for the payload copy
 byte* p = (byte*)malloc(length);
 // Copy the payload to the new buffer
 memcpy(p,payload,length);
 client.publish("outTopic", p, length);
 // Free the memory
 free(p);

}

void setup() {

 Ethernet.begin(mac, ip);
 if (client.connect("arduinoClient")) {
   client.publish("outTopic","hello world");
   client.subscribe("inTopic");
 }

}

void loop() {

 client.loop();

}