UDP Ethernet Firewall Problem with MQTT

I want to send data using PubSubClient and Ethernet libraries. My current Code is using TCP protocol, but what do I need to change in my code to send from UDP and 5001 port to pass through the firewall.

////////////////////////  Library  ////////////////////////

#include <Ethernet.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include "EmonLib.h"
#include <WiFi.h>

////////////////////////  Ethernet Mac  ///////////////////

byte mac[] = {0x9C, 0x9C, 0x1F, 0xC8, 0x6D, 0x6D};

////////////////////////  CT Config  ///////////////////

EnergyMonitor emonA;
EnergyMonitor emonB;
EnergyMonitor emonC;

//////////////////////// LED Input ////////////////////////

byte green = 13;

//////////////////////// Global Args //////////////////////

unsigned long lastMillis = 0;
unsigned long lastMillis1 = 0;
unsigned long lastMillis2 = 0;
unsigned long lastMillis3 = 0;


byte TryCount;
byte r;
byte i;
float IrmsA;
float IrmsB;
float IrmsC;

//////////////////////// Client Config //////////////////////

EthernetClient net;
PubSubClient client("xx.xxx.xxx.xxx", 1883, net);


void setup_eth() {
  Ethernet.maintain ();
  // Serial.println("setup_eth()");
  Ethernet.init(5);

  while (Ethernet.begin(mac) == 0) {
    // Serial.println("Failed to configure Ethernet using DHCP");
digitalWrite(green,HIGH);
    if (millis() - lastMillis1 > 100) {

      lastMillis1 = millis();
      net.stop();
      TryCount++;

    }

    //  Serial.print("setup_eth() > Ethernet.begin(mac) == 0 > lastMillis1: ");
    //  Serial.println(TryCount);
    if ( TryCount == 10 )
    {
      //Serial.println("Ethernet Restart !");
      ESP.restart();
    }
  }
digitalWrite(green,LOW);
  Ethernet.begin(mac);
  //Serial.println("Ethernet Connected !");
}

void connect() {
  // Serial.println("connect()");

  String clientId = "ESP32Client-";
  clientId += String(random(0xffff), HEX);

  while (!client.connected()) {

    //Serial.println("connect() > !client.connected()");

    if (client.connect(clientId.c_str(), "mqtt-test", "mqtt-test", NULL , 1, true, NULL)) {

      // Serial.println("connect() > !client.connected() > client.connect");


    }
    else {
      if (millis() - lastMillis2 > 100) {
        lastMillis2 = millis();

        r++;

      }

      //Serial.print("connect() > !client.connected() Else !: ");
      //Serial.println(r);
      if (r == 50) {
        // Serial.println("MQTT Restart !");
        ESP.restart();
      }
    }
  }
  //Serial.println("MQTT Connected !");
}

void setup() {
  // Serial.begin(115200);

  emonA.current(25, 180.0);
  emonB.current(26, 220.0);
  emonC.current(27,  222.0);

  pinMode(green, OUTPUT);

}

void loop() {
  if ( (net.connected()) && (Ethernet.linkStatus() == LinkON) )
  { //Serial.println("net.connected()) && (Ethernet.linkStatus() == LinkON");
    client.setKeepAlive(90);
    client.setBufferSize(256);

    if (!client.connected()) {
      //Serial.println("net.connected()) && (Ethernet.linkStatus() == LinkON > !client.connected() ");
      connect();
    }

    if (millis() - lastMillis1 > 310) {

      IrmsA = IrmsA + emonA.calcIrms(1676);
      IrmsB = IrmsB + emonB.calcIrms(1676);
      IrmsC = IrmsC + emonC.calcIrms(1676);

      i++;

      digitalWrite(green, LOW);
      lastMillis1 = millis();
    }

    if (millis() - lastMillis3 > 5000) {
      digitalWrite(green, HIGH);

      lastMillis3 = millis();
    }

    //Serial.print("CT Collect Data: ");
    //  Serial.println(i);

    if (i == 8) {

      StaticJsonBuffer<800> JSON;
      JsonObject& JSONencoder = JSON.createObject();

      //Serial.println("MQTT Publish");


      IrmsA = IrmsA / 8;
      IrmsB = IrmsB / 8;
      IrmsC = IrmsC / 8;

      JSONencoder["DEVICE = "]  =     WiFi.macAddress();
      JSONencoder["PHASE A = "] =     IrmsA;
      JSONencoder["PHASE B = "] =     IrmsB;
      JSONencoder["PHASE C = "] =     IrmsC;
      JSONencoder["STATE = "]   =     true;
      JSONencoder["RAM = "]     =     esp_get_free_heap_size();
      JSONencoder["RAM2 = "]    =     uxTaskGetStackHighWaterMark(NULL);
      JSONencoder["RAM3 = "]    =     ESP.getFreeHeap();
      JSONencoder["RAM4 = "]    =     xPortGetFreeHeapSize();
      char JSONmessageBuffer[800];
      JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));

      client.publish("24vEthernet/data", JSONmessageBuffer);



      IrmsA = 0;
      IrmsB = 0;
      IrmsC = 0;
      i = 0;
    }
    client.loop();
  }
  else {
    //Serial.println("net.connected()) && (Ethernet.linkStatus() == LinkON Else !");
    if ( !(net.connected()) || (Ethernet.linkStatus() == LinkOFF) )
    { //Serial.print("!net.connected()) && (Ethernet.linkStatus() == LinkON: ");

      //Serial.println("Try Again Ethernet");
      setup_eth();

    }

    //Serial.println("Try Again MQTT");
    connect();

  }
}

My first instinct would be to say that you can't because of the need to keep a TCP connection up to permit the keep alive callback to function.

It turns out though that there is a UDP variant called MQTT-SN. You will need to check to see whether the pubsub library you're using supports it.

When I connect to my home modem, there is no problem with my code, but when I connect it to my workplace, I think I cannot connect because of the firewall.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.