ESP32-Wrover Wifi & W5500 with MQTT

#include <Arduino.h>
#include <ArduinoMqttClient.h> 
#include <WiFi.h>  
#include <WiFiClientSecure.h> 
#include <AsyncTCP.h>      
#include <AsyncWebServer_ESP32_W5500.h>

#define MISO_GPIO   19
#define MOSI_GPIO   23
#define SCK_GPIO    18
#define CS_GPIO     22
#define INT_GPIO    21
#define SPI_CLOCK_MHZ 25

#define SECRET_SSID "myssid"
#define SECRET_PASS "mypwd"

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "broker.emqx.io";
int        port     = 1883;
const char topic[]  = "W5500";

const long interval = 1000;
unsigned long previousMillis = 0;

int count = 0;

void checkWifi()
{
  if(WiFi.status() != WL_CONNECTED)
  {
    WiFi.begin(SECRET_SSID,SECRET_PASS);
  }
  if(WiFi.status() == WL_CONNECTED)
  {
    Serial.println("Wifi conncted");
  }
}

void beginWiFi()
{
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(SECRET_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(SECRET_SSID,SECRET_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(1000);
  }

  Serial.println("You're connected to the network");
  Serial.println();
}
void beginMqtt()
{
  if(!mqttClient.connect(broker,port))  
  {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());
  }
  if(mqttClient.connected())  
  {
    Serial.println("Mqtt connected");
  }
}
    
void checkMqtt()
{
  if(!mqttClient.connected())
  {
    beginMqtt();
  }
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  byte mac[6] = {0XFE,0xED,0xBE,0xEF,0xFE,0xAA};
  beginWiFi();
  checkWifi();
  ESP32_W5500_onEvent();
  ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac);
  //ESP32_W5500_waitForConnect();
   beginMqtt();
}

void loop() {
  // call poll() regularly to allow the library to send MQTT keep alives which
  // avoids being disconnected by the broker
  //checkWifi();
  checkMqtt();
  mqttClient.poll();

  // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
  // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval) {
    // save the last time a message was sent
    previousMillis = currentMillis;

    Serial.print("Sending message to topic: ");
    Serial.println(topic);
    Serial.print("hello ");
    Serial.println(count);

    // send message, the Print interface can be used to set the message contents
    if(mqttClient.connected())
    {
      mqttClient.beginMessage(topic);
      mqttClient.print("hello ");
      mqttClient.print(count);
      mqttClient.endMessage();
    
      Serial.println();
    
      count++;
    }
  }
}

Hi,

I am using ESP32-wrover with W5500. My project is to use MQTT with Wifi or LAN connection.
I want to set LAN as mqtt connection priority, also is there any way to check internet availability and make mqtt connection based on internet status? I already try http request to google.com, but sometime its not return code 200.

Thanks

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