Go Down

Topic: ICMPPing interrupts MQTT connection  (Read 426 times) previous topic - next topic

kvthie0

Hello,

I've written a sketch using MQTT (PubSubClient V1.9.1 github.com/knolleary/pubsubclient/) and  ICMPPing (github.com/BlakeFoster/Arduino-Ping)

When the MQTT client receives from the broker on topic "AvSap1/00/01/TV_Clock/Cmd" a message with payload "Off", I would like to ping a device (see void STOP TV()) before publishing a response to this message. It seems that after the ping the connection is lost between the client and the broker. (the client.publish (""AvSap1/00/01/TV_Clock/Cmd/Fb", "Off") is not sent and the broker send the "will" message "Disconnected" from void connect_and_subscribe())

My question is : Is there a way to ping a device without interrupting the MQTT connection ?

I use a Arduino One R3 board with the latest internet shield. (also a RTC and IR Led connected)

Thank you in advance for your time.



Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <IRremote.h>
#include <DS3232RTC.h>    
#include <Time.h>        
#include <Wire.h>        
#include <ICMPPing.h>

/******************************************************************************/
/* Init                                                                       */
/******************************************************************************/

byte mac[]    = {  0x90, 0xA2, 0xDA, 0x0F, 0x64, 0xD1 };
byte server[] = { 192, 168, 1, 50 };
byte ip[]     = { 192, 168, 1, 40 };
IPAddress pingAddr(192,168,1,51);

IRsend irsend;

SOCKET pingSocket = 0;
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

/******************************************************************************/
/* Reaction on MQTT message                                                   */
/******************************************************************************/

void callback(char* topic, byte* payload, unsigned int length) {
  if (strcmp(topic, "AvSap1/00/01/TV_Clock/Cmd") == 0) {
    char topic_out[] = "AvSap1/00/01/TV_Clock/Cmd/Fb";

    if (memcmp("On", payload, length) == 0) {
      start_TV();
      client.publish(topic_out, "On");
    }
    if (memcmp("Off", payload, length) == 0) {
      stop_TV();
      client.publish(topic_out, "Off");
    }
    free (topic_out);
  }
}

/******************************************************************************/
/* Start TV                                                                   */
/******************************************************************************/

void start_TV () {
  digitalWrite(7, HIGH);
}

/******************************************************************************/
/* Stop TV                                                                    */
/******************************************************************************/

void stop_TV () {
  ICMPEchoReply echoReply = ping(pingAddr, 4);
  if (echoReply.status == SUCCESS) {
    for (int i = 0; i < 3; i++) {
      irsend.sendSAMSUNG(0xE0E040BF, 32); // press power on/off button IR remote
      delay(40);
    }
    delay(10000); // time TV goes in standby mode
  }
 digitalWrite(7, LOW);
}

/******************************************************************************/
/* Connect and subscribe                                                      */
/******************************************************************************/

void connect_and_subscribe () {
  if (client.connect("Arduino_TV_Clock", "AvSap1/00/01/__Arduino_TV_Clock__", 2, true, "Disconnected")) {
    char Connected[] = "Connected";
    unsigned int lengte = strlen(Connected);
    client.publish ("AvSap1/00/01/__Arduino_TV_Clock__", (byte *)Connected, lengte, true);
    client.subscribe("AvSap1/00/01/TV_Clock/Cmd");
    client.subscribe("AvSap1/00/01/RTC/Time");
    client.subscribe("AvSap1/01/01/Pi_Master/Time");
    client.subscribe("AvSap1/01/01/__Pi_Master__");
  }
  else {
    digitalWrite(7, HIGH);
  }
}

/******************************************************************************/
/* Void Setup                                                                 */
/******************************************************************************/

void setup() {
  pinMode(7, OUTPUT);
  Serial.begin(9600);

  Ethernet.begin(mac, ip);
  connect_and_subscribe ();
}

/******************************************************************************/
/* Main Loop                                                                  */
/******************************************************************************/

void loop() {
  if (!client.loop()) {
    connect_and_subscribe ();
  }
}

Go Up