Problem with connecting to a MQTT Server

I'm currently trying to communicate with my ESP32 via a MQTT Server. The issue is that the code I have only sometimes works and I don't know why. My PC is connecting just fine using Node Red.
The example code from the Library works, so it shouldn't be a hardware issue.

// This example uses an ESP32 Development Board
// to connect to shiftr.io.
//
// You can check on your device after a successful
// connection here: https://www.shiftr.io/try.
//
// by Joël Gähwiler
// https://github.com/256dpi/arduino-mqtt

#include <WiFi.h>
#include <MQTT.h>

const char ssid[] = "HTLW3R-2.5"; //Just my school WIFI
const char pass[] = "HTLW3R-2.5";

WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

IPAddress Raspi(10, 18, 2, 120); //IP of my Raspberry PI

void connect() {
  Serial.print("checking wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }


  Serial.print("\nconnecting...");
  while (!client.connect("")) {
    Serial.print(".");              //Can't connect and gets stuck here
    delay(1000);
  }

  Serial.println("\nconnected!");

  client.subscribe("testSky");
  // client.unsubscribe("/hello");
}

void messageReceived(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);

  // Note: Do not use the client in the callback to publish, subscribe or
  // unsubscribe as it may cause deadlocks when other things arrive while
  // sending and receiving acknowledgments. Instead, change a global variable,
  // or push to a queue and handle it in the loop after calling `client.loop()`.
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);

  // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
  // by Arduino. You need to set the IP address directly.
  client.begin(Raspi, 1883, net); //Two Options available: (IP, Network) or (IP, Port, Network) 1. Option seems to function better but far from consistent
  client.onMessage(messageReceived);

  connect();
}

void loop() {
  client.loop();
  delay(10);  // <- fixes some issues with WiFi stability

  if (!client.connected()) {
    connect();
  }

  // publish a message roughly every second.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    client.publish("testSky", "world1");    //This should just send itself a message. My PC is also listening for this topic
  }
}

I'm currently using Mosquitto as my server. The Settings are:
allow_anonymous true
listener 1883

Welcome to the forum

I am not familiar with the MQTT library that you are using as I normally use the PubSubClient. Where did you get the library from ?

it's in the comment at the top of the code

Thank you

What do you mean by this ?

Please describe what happens when you upload the sketch. Is a connection established with the MQTT server, does the problem occur with any/all MQTT servers or only yours ?

Turns out the School Wifi is just set up to block certain connections, and that blocking fails sometimes.

Thank you for reporting back the cause of the problem

Good luck with your project

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