mirror of
				https://github.com/gabrielkheisa/antares-esp8266-mqtt.git
				synced 2025-11-04 06:49:22 +00:00 
			
		
		
		
	Changed payload to Antares format
This commit is contained in:
		@@ -4,8 +4,8 @@
 | 
			
		||||
#define WIFISSID "your-wifi-ssid"
 | 
			
		||||
#define PASSWORD "your-wifi-password"
 | 
			
		||||
 | 
			
		||||
#define projectName = "your-project-name";
 | 
			
		||||
#define deviceName = "your-device-name";
 | 
			
		||||
#define projectName "your-project-name"
 | 
			
		||||
#define deviceName "your-device-name"
 | 
			
		||||
 | 
			
		||||
AntaresESP8266MQTT antares(ACCESSKEY);
 | 
			
		||||
 | 
			
		||||
@@ -22,6 +22,7 @@ void loop() {
 | 
			
		||||
  antares.add("humidity", 75);
 | 
			
		||||
  antares.add("message", "Hello World!");
 | 
			
		||||
  antares.add("temperature", 45);
 | 
			
		||||
  antares.printData();
 | 
			
		||||
  antares.publish(projectName, deviceName);
 | 
			
		||||
  delay(5000);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,15 +4,15 @@
 | 
			
		||||
#define WIFISSID "your-wifi-ssid"
 | 
			
		||||
#define PASSWORD "your-wifi-password"
 | 
			
		||||
 | 
			
		||||
#define projectName = "your-project-name";
 | 
			
		||||
#define deviceName = "your-device-name";
 | 
			
		||||
#define projectName "your-project-name"
 | 
			
		||||
#define deviceName "your-device-name"
 | 
			
		||||
 | 
			
		||||
AntaresESP8266MQTT antares(ACCESSKEY);
 | 
			
		||||
 | 
			
		||||
void callback(char topic[], byte payload[], unsigned int length) {
 | 
			
		||||
  String topicString = String(topic);
 | 
			
		||||
  String payloadString = antares.byteToString(payload, length);
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  Serial.println("[ANTARES] New Mesage: ");
 | 
			
		||||
  Serial.println(topicString);
 | 
			
		||||
  Serial.println(payloadString);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,43 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 Basic MQTT example with Authentication
 | 
			
		||||
 | 
			
		||||
  - connects to an MQTT server, providing username
 | 
			
		||||
    and password
 | 
			
		||||
  - publishes "hello world" to the topic "outTopic"
 | 
			
		||||
  - subscribes to the topic "inTopic"
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#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(172, 16, 0, 100);
 | 
			
		||||
IPAddress server(172, 16, 0, 2);
 | 
			
		||||
 | 
			
		||||
void callback(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
  // handle message arrived
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
EthernetClient ethClient;
 | 
			
		||||
PubSubClient client(server, 1883, callback, ethClient);
 | 
			
		||||
 | 
			
		||||
void setup()
 | 
			
		||||
{
 | 
			
		||||
  Ethernet.begin(mac, ip);
 | 
			
		||||
  // Note - the default maximum packet size is 128 bytes. If the
 | 
			
		||||
  // combined length of clientId, username and password exceed this,
 | 
			
		||||
  // you will need to increase the value of MQTT_MAX_PACKET_SIZE in
 | 
			
		||||
  // PubSubClient.h
 | 
			
		||||
  
 | 
			
		||||
  if (client.connect("arduinoClient", "testuser", "testpass")) {
 | 
			
		||||
    client.publish("outTopic","hello world");
 | 
			
		||||
    client.subscribe("inTopic");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop()
 | 
			
		||||
{
 | 
			
		||||
  client.loop();
 | 
			
		||||
}
 | 
			
		||||
@@ -1,77 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 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 "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
 | 
			
		||||
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 | 
			
		||||
 achieve the same result without blocking the main loop.
 | 
			
		||||
 
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#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(172, 16, 0, 100);
 | 
			
		||||
IPAddress server(172, 16, 0, 2);
 | 
			
		||||
 | 
			
		||||
void callback(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
  Serial.print("Message arrived [");
 | 
			
		||||
  Serial.print(topic);
 | 
			
		||||
  Serial.print("] ");
 | 
			
		||||
  for (int i=0;i<length;i++) {
 | 
			
		||||
    Serial.print((char)payload[i]);
 | 
			
		||||
  }
 | 
			
		||||
  Serial.println();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
EthernetClient ethClient;
 | 
			
		||||
PubSubClient client(ethClient);
 | 
			
		||||
 | 
			
		||||
void reconnect() {
 | 
			
		||||
  // Loop until we're reconnected
 | 
			
		||||
  while (!client.connected()) {
 | 
			
		||||
    Serial.print("Attempting MQTT connection...");
 | 
			
		||||
    // Attempt to connect
 | 
			
		||||
    if (client.connect("arduinoClient")) {
 | 
			
		||||
      Serial.println("connected");
 | 
			
		||||
      // Once connected, publish an announcement...
 | 
			
		||||
      client.publish("outTopic","hello world");
 | 
			
		||||
      // ... and resubscribe
 | 
			
		||||
      client.subscribe("inTopic");
 | 
			
		||||
    } else {
 | 
			
		||||
      Serial.print("failed, rc=");
 | 
			
		||||
      Serial.print(client.state());
 | 
			
		||||
      Serial.println(" try again in 5 seconds");
 | 
			
		||||
      // Wait 5 seconds before retrying
 | 
			
		||||
      delay(5000);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup()
 | 
			
		||||
{
 | 
			
		||||
  Serial.begin(57600);
 | 
			
		||||
 | 
			
		||||
  client.setServer(server, 1883);
 | 
			
		||||
  client.setCallback(callback);
 | 
			
		||||
 | 
			
		||||
  Ethernet.begin(mac, ip);
 | 
			
		||||
  // Allow the hardware to sort itself out
 | 
			
		||||
  delay(1500);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop()
 | 
			
		||||
{
 | 
			
		||||
  if (!client.connected()) {
 | 
			
		||||
    reconnect();
 | 
			
		||||
  }
 | 
			
		||||
  client.loop();
 | 
			
		||||
}
 | 
			
		||||
@@ -1,132 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 Basic ESP8266 MQTT example
 | 
			
		||||
 | 
			
		||||
 This sketch demonstrates the capabilities of the pubsub library in combination
 | 
			
		||||
 with the ESP8266 board/library.
 | 
			
		||||
 | 
			
		||||
 It connects to an MQTT server then:
 | 
			
		||||
  - publishes "hello world" to the topic "outTopic" every two seconds
 | 
			
		||||
  - subscribes to the topic "inTopic", printing out any messages
 | 
			
		||||
    it receives. NB - it assumes the received payloads are strings not binary
 | 
			
		||||
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
 | 
			
		||||
    else switch it off
 | 
			
		||||
 | 
			
		||||
 It will reconnect to the server if the connection is lost using a blocking
 | 
			
		||||
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 | 
			
		||||
 achieve the same result without blocking the main loop.
 | 
			
		||||
 | 
			
		||||
 To install the ESP8266 board, (using Arduino 1.6.4+):
 | 
			
		||||
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
 | 
			
		||||
       http://arduino.esp8266.com/stable/package_esp8266com_index.json
 | 
			
		||||
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
 | 
			
		||||
  - Select your ESP8266 in "Tools -> Board"
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <ESP8266WiFi.h>
 | 
			
		||||
#include <PubSubClient.h>
 | 
			
		||||
 | 
			
		||||
// Update these with values suitable for your network.
 | 
			
		||||
 | 
			
		||||
const char* ssid = "........";
 | 
			
		||||
const char* password = "........";
 | 
			
		||||
const char* mqtt_server = "broker.mqtt-dashboard.com";
 | 
			
		||||
 | 
			
		||||
WiFiClient espClient;
 | 
			
		||||
PubSubClient client(espClient);
 | 
			
		||||
long lastMsg = 0;
 | 
			
		||||
char msg[50];
 | 
			
		||||
int value = 0;
 | 
			
		||||
 | 
			
		||||
void setup_wifi() {
 | 
			
		||||
 | 
			
		||||
  delay(10);
 | 
			
		||||
  // We start by connecting to a WiFi network
 | 
			
		||||
  Serial.println();
 | 
			
		||||
  Serial.print("Connecting to ");
 | 
			
		||||
  Serial.println(ssid);
 | 
			
		||||
 | 
			
		||||
  WiFi.begin(ssid, password);
 | 
			
		||||
 | 
			
		||||
  while (WiFi.status() != WL_CONNECTED) {
 | 
			
		||||
    delay(500);
 | 
			
		||||
    Serial.print(".");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  randomSeed(micros());
 | 
			
		||||
 | 
			
		||||
  Serial.println("");
 | 
			
		||||
  Serial.println("WiFi connected");
 | 
			
		||||
  Serial.println("IP address: ");
 | 
			
		||||
  Serial.println(WiFi.localIP());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void callback(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
  Serial.print("Message arrived [");
 | 
			
		||||
  Serial.print(topic);
 | 
			
		||||
  Serial.print("] ");
 | 
			
		||||
  for (int i = 0; i < length; i++) {
 | 
			
		||||
    Serial.print((char)payload[i]);
 | 
			
		||||
  }
 | 
			
		||||
  Serial.println();
 | 
			
		||||
 | 
			
		||||
  // Switch on the LED if an 1 was received as first character
 | 
			
		||||
  if ((char)payload[0] == '1') {
 | 
			
		||||
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
 | 
			
		||||
    // but actually the LED is on; this is because
 | 
			
		||||
    // it is active low on the ESP-01)
 | 
			
		||||
  } else {
 | 
			
		||||
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void reconnect() {
 | 
			
		||||
  // Loop until we're reconnected
 | 
			
		||||
  while (!client.connected()) {
 | 
			
		||||
    Serial.print("Attempting MQTT connection...");
 | 
			
		||||
    // Create a random client ID
 | 
			
		||||
    String clientId = "ESP8266Client-";
 | 
			
		||||
    clientId += String(random(0xffff), HEX);
 | 
			
		||||
    // Attempt to connect
 | 
			
		||||
    if (client.connect(clientId.c_str())) {
 | 
			
		||||
      Serial.println("connected");
 | 
			
		||||
      // Once connected, publish an announcement...
 | 
			
		||||
      client.publish("outTopic", "hello world");
 | 
			
		||||
      // ... and resubscribe
 | 
			
		||||
      client.subscribe("inTopic");
 | 
			
		||||
    } else {
 | 
			
		||||
      Serial.print("failed, rc=");
 | 
			
		||||
      Serial.print(client.state());
 | 
			
		||||
      Serial.println(" try again in 5 seconds");
 | 
			
		||||
      // Wait 5 seconds before retrying
 | 
			
		||||
      delay(5000);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
 | 
			
		||||
  Serial.begin(115200);
 | 
			
		||||
  setup_wifi();
 | 
			
		||||
  client.setServer(mqtt_server, 1883);
 | 
			
		||||
  client.setCallback(callback);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
 | 
			
		||||
  if (!client.connected()) {
 | 
			
		||||
    reconnect();
 | 
			
		||||
  }
 | 
			
		||||
  client.loop();
 | 
			
		||||
 | 
			
		||||
  long now = millis();
 | 
			
		||||
  if (now - lastMsg > 2000) {
 | 
			
		||||
    lastMsg = now;
 | 
			
		||||
    ++value;
 | 
			
		||||
    snprintf (msg, 50, "hello world #%ld", value);
 | 
			
		||||
    Serial.print("Publish message: ");
 | 
			
		||||
    Serial.println(msg);
 | 
			
		||||
    client.publish("outTopic", msg);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,179 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 Long message ESP8266 MQTT example
 | 
			
		||||
 | 
			
		||||
 This sketch demonstrates sending arbitrarily large messages in combination
 | 
			
		||||
 with the ESP8266 board/library.
 | 
			
		||||
 | 
			
		||||
 It connects to an MQTT server then:
 | 
			
		||||
  - publishes "hello world" to the topic "outTopic"
 | 
			
		||||
  - subscribes to the topic "greenBottles/#", printing out any messages
 | 
			
		||||
    it receives. NB - it assumes the received payloads are strings not binary
 | 
			
		||||
  - If the sub-topic is a number, it publishes a "greenBottles/lyrics" message
 | 
			
		||||
    with a payload consisting of the lyrics to "10 green bottles", replacing
 | 
			
		||||
    10 with the number given in the sub-topic.
 | 
			
		||||
 | 
			
		||||
 It will reconnect to the server if the connection is lost using a blocking
 | 
			
		||||
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 | 
			
		||||
 achieve the same result without blocking the main loop.
 | 
			
		||||
 | 
			
		||||
 To install the ESP8266 board, (using Arduino 1.6.4+):
 | 
			
		||||
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
 | 
			
		||||
       http://arduino.esp8266.com/stable/package_esp8266com_index.json
 | 
			
		||||
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
 | 
			
		||||
  - Select your ESP8266 in "Tools -> Board"
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <ESP8266WiFi.h>
 | 
			
		||||
#include <PubSubClient.h>
 | 
			
		||||
 | 
			
		||||
// Update these with values suitable for your network.
 | 
			
		||||
 | 
			
		||||
const char* ssid = "........";
 | 
			
		||||
const char* password = "........";
 | 
			
		||||
const char* mqtt_server = "broker.mqtt-dashboard.com";
 | 
			
		||||
 | 
			
		||||
WiFiClient espClient;
 | 
			
		||||
PubSubClient client(espClient);
 | 
			
		||||
long lastMsg = 0;
 | 
			
		||||
char msg[50];
 | 
			
		||||
int value = 0;
 | 
			
		||||
 | 
			
		||||
void setup_wifi() {
 | 
			
		||||
 | 
			
		||||
  delay(10);
 | 
			
		||||
  // We start by connecting to a WiFi network
 | 
			
		||||
  Serial.println();
 | 
			
		||||
  Serial.print("Connecting to ");
 | 
			
		||||
  Serial.println(ssid);
 | 
			
		||||
 | 
			
		||||
  WiFi.begin(ssid, password);
 | 
			
		||||
 | 
			
		||||
  while (WiFi.status() != WL_CONNECTED) {
 | 
			
		||||
    delay(500);
 | 
			
		||||
    Serial.print(".");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  randomSeed(micros());
 | 
			
		||||
 | 
			
		||||
  Serial.println("");
 | 
			
		||||
  Serial.println("WiFi connected");
 | 
			
		||||
  Serial.println("IP address: ");
 | 
			
		||||
  Serial.println(WiFi.localIP());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void callback(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
  Serial.print("Message arrived [");
 | 
			
		||||
  Serial.print(topic);
 | 
			
		||||
  Serial.print("] ");
 | 
			
		||||
  for (int i = 0; i < length; i++) {
 | 
			
		||||
    Serial.print((char)payload[i]);
 | 
			
		||||
  }
 | 
			
		||||
  Serial.println();
 | 
			
		||||
 | 
			
		||||
  // Find out how many bottles we should generate lyrics for
 | 
			
		||||
  String topicStr(topic);
 | 
			
		||||
  int bottleCount = 0; // assume no bottles unless we correctly parse a value from the topic
 | 
			
		||||
  if (topicStr.indexOf('/') >= 0) {
 | 
			
		||||
    // The topic includes a '/', we'll try to read the number of bottles from just after that
 | 
			
		||||
    topicStr.remove(0, topicStr.indexOf('/')+1);
 | 
			
		||||
    // Now see if there's a number of bottles after the '/'
 | 
			
		||||
    bottleCount = topicStr.toInt();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (bottleCount > 0) {
 | 
			
		||||
    // Work out how big our resulting message will be
 | 
			
		||||
    int msgLen = 0;
 | 
			
		||||
    for (int i = bottleCount; i > 0; i--) {
 | 
			
		||||
      String numBottles(i);
 | 
			
		||||
      msgLen += 2*numBottles.length();
 | 
			
		||||
      if (i == 1) {
 | 
			
		||||
        msgLen += 2*String(" green bottle, standing on the wall\n").length();
 | 
			
		||||
      } else {
 | 
			
		||||
        msgLen += 2*String(" green bottles, standing on the wall\n").length();
 | 
			
		||||
      }
 | 
			
		||||
      msgLen += String("And if one green bottle should accidentally fall\nThere'll be ").length();
 | 
			
		||||
      switch (i) {
 | 
			
		||||
      case 1:
 | 
			
		||||
        msgLen += String("no green bottles, standing on the wall\n\n").length();
 | 
			
		||||
        break;
 | 
			
		||||
      case 2:
 | 
			
		||||
        msgLen += String("1 green bottle, standing on the wall\n\n").length();
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        numBottles = i-1;
 | 
			
		||||
        msgLen += numBottles.length();
 | 
			
		||||
        msgLen += String(" green bottles, standing on the wall\n\n").length();
 | 
			
		||||
        break;
 | 
			
		||||
      };
 | 
			
		||||
    }
 | 
			
		||||
  
 | 
			
		||||
    // Now we can start to publish the message
 | 
			
		||||
    client.beginPublish("greenBottles/lyrics", msgLen, false);
 | 
			
		||||
    for (int i = bottleCount; i > 0; i--) {
 | 
			
		||||
      for (int j = 0; j < 2; j++) {
 | 
			
		||||
        client.print(i);
 | 
			
		||||
        if (i == 1) {
 | 
			
		||||
          client.print(" green bottle, standing on the wall\n");
 | 
			
		||||
        } else {
 | 
			
		||||
          client.print(" green bottles, standing on the wall\n");
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      client.print("And if one green bottle should accidentally fall\nThere'll be ");
 | 
			
		||||
      switch (i) {
 | 
			
		||||
      case 1:
 | 
			
		||||
        client.print("no green bottles, standing on the wall\n\n");
 | 
			
		||||
        break;
 | 
			
		||||
      case 2:
 | 
			
		||||
        client.print("1 green bottle, standing on the wall\n\n");
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        client.print(i-1);
 | 
			
		||||
        client.print(" green bottles, standing on the wall\n\n");
 | 
			
		||||
        break;
 | 
			
		||||
      };
 | 
			
		||||
    }
 | 
			
		||||
    // Now we're done!
 | 
			
		||||
    client.endPublish();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void reconnect() {
 | 
			
		||||
  // Loop until we're reconnected
 | 
			
		||||
  while (!client.connected()) {
 | 
			
		||||
    Serial.print("Attempting MQTT connection...");
 | 
			
		||||
    // Create a random client ID
 | 
			
		||||
    String clientId = "ESP8266Client-";
 | 
			
		||||
    clientId += String(random(0xffff), HEX);
 | 
			
		||||
    // Attempt to connect
 | 
			
		||||
    if (client.connect(clientId.c_str())) {
 | 
			
		||||
      Serial.println("connected");
 | 
			
		||||
      // Once connected, publish an announcement...
 | 
			
		||||
      client.publish("outTopic", "hello world");
 | 
			
		||||
      // ... and resubscribe
 | 
			
		||||
      client.subscribe("greenBottles/#");
 | 
			
		||||
    } else {
 | 
			
		||||
      Serial.print("failed, rc=");
 | 
			
		||||
      Serial.print(client.state());
 | 
			
		||||
      Serial.println(" try again in 5 seconds");
 | 
			
		||||
      // Wait 5 seconds before retrying
 | 
			
		||||
      delay(5000);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
 | 
			
		||||
  Serial.begin(115200);
 | 
			
		||||
  setup_wifi();
 | 
			
		||||
  client.setServer(mqtt_server, 1883);
 | 
			
		||||
  client.setCallback(callback);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
 | 
			
		||||
  if (!client.connected()) {
 | 
			
		||||
    reconnect();
 | 
			
		||||
  }
 | 
			
		||||
  client.loop();
 | 
			
		||||
}
 | 
			
		||||
@@ -1,60 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 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(172, 16, 0, 100);
 | 
			
		||||
IPAddress server(172, 16, 0, 2);
 | 
			
		||||
 | 
			
		||||
// 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();
 | 
			
		||||
}
 | 
			
		||||
@@ -1,67 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 Reconnecting MQTT example - non-blocking
 | 
			
		||||
 | 
			
		||||
 This sketch demonstrates how to keep the client connected
 | 
			
		||||
 using a non-blocking reconnect function. If the client loses
 | 
			
		||||
 its connection, it attempts to reconnect every 5 seconds
 | 
			
		||||
 without blocking the main loop.
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <SPI.h>
 | 
			
		||||
#include <Ethernet.h>
 | 
			
		||||
#include <PubSubClient.h>
 | 
			
		||||
 | 
			
		||||
// Update these with values suitable for your hardware/network.
 | 
			
		||||
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
 | 
			
		||||
IPAddress ip(172, 16, 0, 100);
 | 
			
		||||
IPAddress server(172, 16, 0, 2);
 | 
			
		||||
 | 
			
		||||
void callback(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
  // handle message arrived
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
EthernetClient ethClient;
 | 
			
		||||
PubSubClient client(ethClient);
 | 
			
		||||
 | 
			
		||||
long lastReconnectAttempt = 0;
 | 
			
		||||
 | 
			
		||||
boolean reconnect() {
 | 
			
		||||
  if (client.connect("arduinoClient")) {
 | 
			
		||||
    // Once connected, publish an announcement...
 | 
			
		||||
    client.publish("outTopic","hello world");
 | 
			
		||||
    // ... and resubscribe
 | 
			
		||||
    client.subscribe("inTopic");
 | 
			
		||||
  }
 | 
			
		||||
  return client.connected();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup()
 | 
			
		||||
{
 | 
			
		||||
  client.setServer(server, 1883);
 | 
			
		||||
  client.setCallback(callback);
 | 
			
		||||
 | 
			
		||||
  Ethernet.begin(mac, ip);
 | 
			
		||||
  delay(1500);
 | 
			
		||||
  lastReconnectAttempt = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void loop()
 | 
			
		||||
{
 | 
			
		||||
  if (!client.connected()) {
 | 
			
		||||
    long now = millis();
 | 
			
		||||
    if (now - lastReconnectAttempt > 5000) {
 | 
			
		||||
      lastReconnectAttempt = now;
 | 
			
		||||
      // Attempt to reconnect
 | 
			
		||||
      if (reconnect()) {
 | 
			
		||||
        lastReconnectAttempt = 0;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    // Client connected
 | 
			
		||||
 | 
			
		||||
    client.loop();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -1,57 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 Example of using a Stream object to store the message payload
 | 
			
		||||
 | 
			
		||||
 Uses SRAM library: https://github.com/ennui2342/arduino-sram
 | 
			
		||||
 but could use any Stream based class such as SD
 | 
			
		||||
 | 
			
		||||
  - connects to an MQTT server
 | 
			
		||||
  - publishes "hello world" to the topic "outTopic"
 | 
			
		||||
  - subscribes to the topic "inTopic"
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include <SPI.h>
 | 
			
		||||
#include <Ethernet.h>
 | 
			
		||||
#include <PubSubClient.h>
 | 
			
		||||
#include <SRAM.h>
 | 
			
		||||
 | 
			
		||||
// Update these with values suitable for your network.
 | 
			
		||||
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
 | 
			
		||||
IPAddress ip(172, 16, 0, 100);
 | 
			
		||||
IPAddress server(172, 16, 0, 2);
 | 
			
		||||
 | 
			
		||||
SRAM sram(4, SRAM_1024);
 | 
			
		||||
 | 
			
		||||
void callback(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
  sram.seek(1);
 | 
			
		||||
 | 
			
		||||
  // do something with the message
 | 
			
		||||
  for(uint8_t i=0; i<length; i++) {
 | 
			
		||||
    Serial.write(sram.read());
 | 
			
		||||
  }
 | 
			
		||||
  Serial.println();
 | 
			
		||||
 | 
			
		||||
  // Reset position for the next message to be stored
 | 
			
		||||
  sram.seek(1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
EthernetClient ethClient;
 | 
			
		||||
PubSubClient client(server, 1883, callback, ethClient, sram);
 | 
			
		||||
 | 
			
		||||
void setup()
 | 
			
		||||
{
 | 
			
		||||
  Ethernet.begin(mac, ip);
 | 
			
		||||
  if (client.connect("arduinoClient")) {
 | 
			
		||||
    client.publish("outTopic","hello world");
 | 
			
		||||
    client.subscribe("inTopic");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  sram.begin();
 | 
			
		||||
  sram.seek(1);
 | 
			
		||||
 | 
			
		||||
  Serial.begin(9600);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop()
 | 
			
		||||
{
 | 
			
		||||
  client.loop();
 | 
			
		||||
}
 | 
			
		||||
@@ -125,15 +125,40 @@ void AntaresESP8266MQTT::printDebug(String text) {
 | 
			
		||||
 | 
			
		||||
void AntaresESP8266MQTT::publish(String projectName, String deviceName) {
 | 
			
		||||
    String topic = "/oneM2M/req/" + _accessKey + "/antares-cse/json";
 | 
			
		||||
    char jsonDataChar[_jsonDataString.length() + 1];
 | 
			
		||||
    String finalData;
 | 
			
		||||
 | 
			
		||||
    _jsonDataString.replace("\"", "\\\"");
 | 
			
		||||
    Serial.println(_jsonDataString);
 | 
			
		||||
 | 
			
		||||
    finalData += "{";
 | 
			
		||||
    finalData += "\"m2m:rqp\": {";
 | 
			
		||||
    finalData += "\"fr\": \"" + _accessKey +"\",";
 | 
			
		||||
    finalData += "\"to\": \"/antares-cse/antares-id/" + projectName + "/" + deviceName + "\",";
 | 
			
		||||
    finalData += "\"op\": 1,";
 | 
			
		||||
    finalData += "\"rqi\": 123456,";
 | 
			
		||||
    finalData += "\"pc\": {";
 | 
			
		||||
    finalData += "\"m2m:cin\": {";
 | 
			
		||||
    finalData += "\"cnf\": \"message\",";
 | 
			
		||||
    finalData += "\"con\": \""+ _jsonDataString + "\"";
 | 
			
		||||
    finalData += "}";
 | 
			
		||||
    finalData += "},";
 | 
			
		||||
    finalData += "\"ty\": 4";
 | 
			
		||||
    finalData += "}";
 | 
			
		||||
    finalData += "}";
 | 
			
		||||
 | 
			
		||||
    // DynamicJsonBuffer jsonBuffer;
 | 
			
		||||
    // JsonObject& object = jsonBuffer.parseObject(finalData);
 | 
			
		||||
    // object.prettyPrintTo(Serial);
 | 
			
		||||
 | 
			
		||||
    char finalDataChar[finalData.length() + 1];
 | 
			
		||||
    char topicChar[topic.length() + 1];
 | 
			
		||||
 | 
			
		||||
    _jsonDataString.toCharArray(jsonDataChar, _jsonDataString.length() + 1);
 | 
			
		||||
    finalData.toCharArray(finalDataChar, finalData.length() + 1);
 | 
			
		||||
    topic.toCharArray(topicChar, topic.length() + 1);
 | 
			
		||||
 | 
			
		||||
    _jsonDataString = "{}";
 | 
			
		||||
 | 
			
		||||
    client.publish(topicChar, jsonDataChar);
 | 
			
		||||
    client.publish(topicChar, finalDataChar);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AntaresESP8266MQTT::setCallback(std::function<void(char*, uint8_t*, unsigned int)> callbackFunc) {
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@
 | 
			
		||||
 | 
			
		||||
// MQTT_MAX_PACKET_SIZE : Maximum packet size
 | 
			
		||||
#ifndef MQTT_MAX_PACKET_SIZE
 | 
			
		||||
#define MQTT_MAX_PACKET_SIZE 128
 | 
			
		||||
#define MQTT_MAX_PACKET_SIZE 2500
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// MQTT_KEEPALIVE : keepAlive interval in Seconds
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user