Difference between revisions of "MQTT: Arduino Simple"

From OnnoWiki
Jump to navigation Jump to search
(Created page with " /* Basic MQTT example This sketch demonstrates the basic capabilities of the library. It connects to an MQTT server then: - publishes "hello world" to the topic "o...")
 
Line 1: Line 1:
 
  /*
 
  /*
   Basic MQTT example
+
   Publishing in the callback
 
   
 
   
  This sketch demonstrates the basic capabilities of the library.
+
  - connects to an MQTT server
  It connects to an MQTT server then:
+
   - subscribes to the topic "inTopic"
   - publishes "hello world" to the topic "outTopic"
+
   - when a message is received, republishes it to "outTopic"  
   - subscribes to the topic "inTopic", printing out any messages
 
    it receives. NB - it assumes the received payloads are strings not binary
 
 
   
 
   
  It will reconnect to the server if the connection is lost using a blocking
+
  This example shows how to publish messages within the
  reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
+
  callback function. The callback function header needs to
  achieve the same result without blocking the main loop.
+
  be declared before the PubSubClient constructor and the
 
+
  actual callback defined afterwards.
 +
  This ensures the client reference in the callback function
 +
  is valid.
 
  */
 
  */
 
   
 
   
Line 23: Line 23:
 
  IPAddress server(192, 168, 0, 100);
 
  IPAddress server(192, 168, 0, 100);
 
   
 
   
  void callback(char* topic, byte* payload, unsigned int length) {
+
// Callback function header
  Serial.print("Message arrived [");
+
  void callback(char* topic, byte* payload, unsigned int length);
  Serial.print(topic);
 
  Serial.print("] ");
 
  for (int i=0;i<length;i++) {
 
    Serial.print((char)payload[i]);
 
  }
 
  Serial.println();
 
}
 
 
   
 
   
 
  EthernetClient ethClient;
 
  EthernetClient ethClient;
  PubSubClient client(ethClient);
+
  PubSubClient client(server, 1883, callback, ethClient);  
 
   
 
   
  void reconnect() {
+
// Callback function
   // Loop until we're reconnected
+
  void callback(char* topic, byte* payload, unsigned int length) {
   while (!client.connected()) {
+
   // In order to republish this payload, a copy must be made
    Serial.print("Attempting MQTT connection...");
+
   // as the orignal payload buffer will be overwritten whilst
    // Attempt to connect
+
  // constructing the PUBLISH packet.  
    if (client.connect("arduinoClient")) {
+
      Serial.println("connected");
+
  // Allocate the correct amount of memory for the payload copy
      // Once connected, publish an announcement...
+
  byte* p = (byte*)malloc(length);
      client.publish("outTopic","hello world");
+
  // Copy the payload to the new buffer
      // ... and resubscribe
+
  memcpy(p,payload,length);
      client.subscribe("inTopic");
+
  client.publish("outTopic", p, length);
    } else {
+
  // Free the memory
      Serial.print("failed, rc=");
+
  free(p);
      Serial.print(client.state());
 
      Serial.println(" try again in 5 seconds");
 
      // Wait 5 seconds before retrying
 
      delay(5000);
 
    }
 
  }
 
 
  }
 
  }
 
   
 
   
 
  void setup()
 
  void setup()
 
  {
 
  {
  Serial.begin(9600);
 
 
  client.setServer(server, 1883);
 
  client.setCallback(callback);
 
 
   
 
   
 
   Ethernet.begin(mac, ip);
 
   Ethernet.begin(mac, ip);
   // Allow the hardware to sort itself out
+
   if (client.connect("arduinoClient")) {
  delay(1500);
+
    client.publish("outTopic","hello world");
 +
    client.subscribe("inTopic");
 +
  }
 
  }
 
  }
 
   
 
   
 
  void loop()
 
  void loop()
 
  {
 
  {
  if (!client.connected()) {
 
    reconnect();
 
  }
 
 
   client.loop();
 
   client.loop();
 
  }
 
  }

Revision as of 16:31, 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.
*/

#include <SPI.h>
#include <Ethernet.h>
#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();
}