Teensy 4.1 MQTT

JaKe101

Member
I want to upgrade my project from an ESP32 -> Teensy 4.1. Until I reached the MQTT part (pubsubclient library) everything went perfect.
I use the build-in Ethernet controller with the NativeEthernet library, Ethernet is working fine but a connection to the MQTT server cannot be made and I get the error : the network connection failed.

MQTT over Ethernet is a critical step for this project. Are the other MQTT libraries that work with this configuration or is it related to the Ethernet controller and should I use an external controller ? If so, which one? Anyone have experience with Teensy, MQTT over Ethernet?
 
Hi JaKe101,
I am trying to use Teensy 4.1 for MQTT connection in my project. I am currently using W5500 for Ethernet connection. I also use PubSubClient library.
The connection works fine at the beginning. However, I've got stability problems which I've described here: https://forum.pjrc.com/threads/61658-MQTT-TCP-connection-stability-problem-on-Teensy-4-1-with-W5500

I am waiting for MagJack to test MQTT connection with NativeEthernet. I can let you know when I will test it if you won't get answer from someone else before.
 
I've just run MQTT connection using PubSubClient library on Teensy 4.1 with Magjack and NativeEthernet. Everything works fine. It is running now for about 1.5 hour. It has reconnected to MQTT server twice in that time, but reconnection time was extremely short.
 
Hi mariuszjo, I now have the Pubsubclient working with NativeEthernet. Only 1 problem, when I stop the MQTT broker, the Pusubclient.begin does not return a false but crashes and reboots the Teensy. I have no idea why but removing the Ethernet cable and reconnect it, works fine...
 
Hi,
What did you do to get this working?
I get all the time
"MQTT connection failed, rc=-2"

Here is my code:
Code:
#include <NativeEthernet.h> //for teensy 4.1
#include <PubSubClient.h>

byte NETWORK_MAC[] = { 0xDE, 0xAA, 0xBE, 0xEF, 0xF3, 0x08 };
IPAddress NETWORK_IP      (192,168,1,31); //static IP

const char *MQTT_SERVER_IP = "192.168.1.60";
const int  MQTT_PORT = 1883;
const char *MQTT_CLIENT_ID = "heating1234";

EthernetClient eth_Client;
PubSubClient MQTT_Client(eth_Client);

void setup() {
  Serial.begin(115200);
  while (!Serial) ; // wait for Arduino Serial Monitor  
  Ethernet.begin(NETWORK_MAC,NETWORK_IP); 
  Serial.println("\nEthernet connected with IP address: ");
  Serial.println(Ethernet.localIP());    
  MQTT_Client.setServer(MQTT_SERVER_IP,MQTT_PORT);  
  while (!MQTT_Client.connected()) { // Loop until we're reconnected
    if (MQTT_Client.connect(MQTT_CLIENT_ID)) { // Attempt to connect
      Serial.print("MQTT connected");    
    }
    else {
      Serial.println("MQTT connection failed, rc=" + String(MQTT_Client.state()));      
      delay(5000);  // Wait 5 seconds before retrying
    }
  }
}
void loop() {}

I used the newest Arduino, Teensyduino and libs.
Thanks for help
 
I had the same, the solution turned that the server cannot be coded in string type...
My solution was to use the ip address, not coded into a string format.

Code:
IPAddress serv(192, 168, 0, 10);

Then later you use
Code:
mqttClient.setServer(serv, 1883);
 
Thanks I will try this :)

You may also want to try replacing NativeEthernet with QNEthernet, which is based on lwip and is now better supported. Switching to QNEthernet has been helpful in quite a few instances.

If you get it working with QNEthernet please let us know.
 
Back
Top