Topic: WolfMqtt on Arduino UNO R4 Wifi

Hello!

I am using Arduino IDE 2.3.4 and an ARDUINO UNO R4 WiFi with the Ethernet Shield. I use the example from https://github.com/wolfSSL/wolfMQTT/blo … client.ino

For the Ethernet connection I modified the setup:
void setup() {
  Serial.begin(115200);
  Serial.println("### Ethernet & MQTT Debugging ###");

  Serial.println("[Ethernet] Initialisierung...");
  Ethernet.begin(mac);
  delay(1000);

  if (Ethernet.localIP() == INADDR_NONE) {
    Serial.println("[Ethernet] DHCP fehlgeschlagen, versuche statische IP...");
    Ethernet.begin(mac, ip, dns, gateway, subnet);
  }

  Serial.print("[Ethernet] Lokale IP-Adresse: ");
  Serial.println(Ethernet.localIP());
}

For the settings:
#include <wolfMQTT.h>
#include <Ethernet.h>
#include <wolfssl.h>
#include <wolfssl/version.h>

// MAC-Adresse für das Ethernet-Modul
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
IPAddress ip(192, 168, 0, 9);  // Statische IP-Adresse
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 0, 1);

/* Konfiguration */
#define DEFAULT_MQTT_HOST       "192.168.0.4"
#define DEFAULT_CMD_TIMEOUT_MS  30000
#define DEFAULT_CON_TIMEOUT_MS  5000
#define DEFAULT_MQTT_QOS        MQTT_QOS_0
#define DEFAULT_KEEP_ALIVE_SEC  30
#define DEFAULT_CLIENT_ID       "WolfMQTTClient"
#define WOLFMQTT_TOPIC_NAME     "wfs"
#define DEFAULT_TOPIC_NAME      WOLFMQTT_TOPIC_NAME "/status"

#define MAX_BUFFER_SIZE         1024
#define TEST_MESSAGE            "test"

static word16 mPort = 1883;
static const char* mHost = DEFAULT_MQTT_HOST;
static int mStopRead = 0;

It connects to the Ethernet and gets an IP-Number view DHCP and it also connects to the mosquito Broker.

But here it comes back with rc=1 which is unknown:

rc = MqttClient_NetConnect(&client, mHost, mPort, DEFAULT_CON_TIMEOUT_MS, 0, NULL);
  Serial.print("[MQTT] Socket Connect: ");
  Serial.println(rc);

I can reach the mosquito with 2 other clients with no problem.

What could be the reason for this?

Share

Re: WolfMqtt on Arduino UNO R4 Wifi

Hi Robert,

Welcome to the wolfSSL Forums. Did you init the client structure?

https://github.com/wolfSSL/wolfMQTT/blo … #L211-L213

Could you tell us a bit about your project using wolfMQTT?

Thanks,
Eric - wolfSSL Support

Re: WolfMqtt on Arduino UNO R4 Wifi

Thanks for your swift answer.
Yes I did the initial in  rc = MqttClient_Init(&client, &net, mqttclient_message_cb, tx_buf, MAX_BUFFER_SIZE, rx_buf, MAX_BUFFER_SIZE, DEFAULT_CMD_TIMEOUT_MS);
This comes back with a 0.

My project is to send the information from an encoder via MQTT. The encoder measures the speed of a wire.

Share

Re: WolfMqtt on Arduino UNO R4 Wifi

This is my full code:
#include <wolfMQTT.h>
#include <Ethernet.h>
#include <wolfssl.h>
#include <wolfssl/version.h>

// MAC-Adresse für das Ethernet-Modul
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
IPAddress ip(192, 168, 0, 9);  // Statische IP-Adresse
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 0, 1);

/* Konfiguration */
#define DEFAULT_MQTT_HOST       "192.168.0.4"
#define DEFAULT_CMD_TIMEOUT_MS  30000
#define DEFAULT_CON_TIMEOUT_MS  5000
#define DEFAULT_MQTT_QOS        MQTT_QOS_0
#define DEFAULT_KEEP_ALIVE_SEC  30
#define DEFAULT_CLIENT_ID       "WolfMQTTClient"
#define WOLFMQTT_TOPIC_NAME     "wfs"
#define DEFAULT_TOPIC_NAME      WOLFMQTT_TOPIC_NAME "/status"
//#define wolfSSL_Debugging_ON
#define MAX_BUFFER_SIZE         1024
#define TEST_MESSAGE            "test"

static word16 mPort = 1883;
static const char* mHost = DEFAULT_MQTT_HOST;
static int mStopRead = 0;

EthernetClient ethClient;

/* Private Funktionen */
static int EthernetConnect(void *context, const char* host, word16 port, int timeout_ms)
{
  Serial.print("[Ethernet] Verbinde zu: ");
  Serial.print(host);
  Serial.print(":");
  Serial.println(port);

  int ret = ethClient.connect(host, port);
 
  if (ret) {
    Serial.println("[Ethernet] Verbindung erfolgreich!");
  } else {
    Serial.println("[Ethernet] Verbindung fehlgeschlagen!");
  }

  return ret;
}

static int EthernetRead(void *context, byte* buf, int buf_len, int timeout_ms)
{
  int recvd = 0;
  while (ethClient.available() > 0 && recvd < buf_len) {
    buf[recvd++] = ethClient.read();
  }

  Serial.print("[Ethernet] Empfangene Bytes: ");
  Serial.println(recvd);
 
  return recvd;
}

static int EthernetWrite(void *context, const byte* buf, int buf_len, int timeout_ms)
{
  int sent = ethClient.write(buf, buf_len);
 
  Serial.print("[Ethernet] Gesendete Bytes: ");
  Serial.println(sent);
 
  return sent;
}

static int EthernetDisconnect(void *context)
{
  Serial.println("[Ethernet] Verbindung wird getrennt...");
  ethClient.stop();
  return 0;
}

#define MAX_PACKET_ID ((1 << 16) - 1)
static int mPacketIdLast;
static word16 mqttclient_get_packetid(void)
{
  mPacketIdLast = (mPacketIdLast >= MAX_PACKET_ID) ? 1 : mPacketIdLast + 1;
  return (word16)mPacketIdLast;
}

static int mqttclient_message_cb(MqttClient *client, MqttMessage *msg, byte msg_new, byte msg_done)
{
  Serial.println("[MQTT] Nachricht empfangen:");
 
  if (msg_new) {
    Serial.print("  Thema: ");
    Serial.write(msg->topic_name, msg->topic_name_len);
    Serial.println();
    Serial.print("  QoS: ");
    Serial.println(msg->qos);
  }

  Serial.print("  Payload: ");
  Serial.write(msg->buffer, msg->buffer_len);
  Serial.println();

  if (msg_done) {
    Serial.println("[MQTT] Nachricht vollständig empfangen.");
  }

  return MQTT_CODE_SUCCESS;
}

void setup() {
  Serial.begin(115200);
  Serial.println("### Ethernet & MQTT Debugging ###");

  Serial.println("[Ethernet] Initialisierung...");
  Ethernet.begin(mac);
  delay(1000);

  if (Ethernet.localIP() == INADDR_NONE) {
    Serial.println("[Ethernet] DHCP fehlgeschlagen, versuche statische IP...");
    Ethernet.begin(mac, ip, dns, gateway, subnet);
  }

  Serial.print("[Ethernet] Lokale IP-Adresse: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  int rc;
  MqttClient client;
  MqttNet net;
  MqttQoS qos = DEFAULT_MQTT_QOS;
  byte clean_session = 1;
  word16 keep_alive_sec = 30;
  const char* client_id = DEFAULT_CLIENT_ID;
  byte tx_buf[MAX_BUFFER_SIZE];
  byte rx_buf[MAX_BUFFER_SIZE];

  Serial.println("[MQTT] Initialisiere Client...");
 
  net.connect = EthernetConnect;
  net.read = EthernetRead;
  net.write = EthernetWrite;
  net.disconnect = EthernetDisconnect;
  net.context = &ethClient;

  rc = MqttClient_Init(&client, &net, mqttclient_message_cb, tx_buf, MAX_BUFFER_SIZE, rx_buf, MAX_BUFFER_SIZE, DEFAULT_CMD_TIMEOUT_MS);
  Serial.print("[MQTT] Init: ");
  Serial.println(rc);

  Serial.println("[MQTT] Verbinde mit Broker...");
  rc = MqttClient_NetConnect(&client, mHost, mPort, DEFAULT_CON_TIMEOUT_MS, 0, NULL);
  Serial.print("[MQTT] Socket Connect: ");
  Serial.println(rc);
if (rc != 0) {
    Serial.print("[MQTT] Verbindungsfehler! Code: ");
    Serial.println(MqttClient_ReturnCodeToString(rc));
}
  if (rc == 0) {
    MqttConnect connect;
    memset(&connect, 0, sizeof(MqttConnect));
    connect.keep_alive_sec = keep_alive_sec;
    connect.clean_session = clean_session;
    connect.client_id = client_id;
   
    Serial.println("[MQTT] Authentifizierung...");
    connect.username = NULL;
    connect.password = NULL;

    rc = MqttClient_Connect(&client, &connect);
    Serial.print("[MQTT] Connect: ");
    Serial.println(rc);

    if (rc == MQTT_CODE_SUCCESS) {
      Serial.println("[MQTT] Verbindung erfolgreich! Warte auf Nachrichten...");

      MqttPublish publish;
      memset(&publish, 0, sizeof(MqttPublish));
      publish.retain = 0;
      publish.qos = qos;
      publish.duplicate = 0;
      publish.topic_name = DEFAULT_TOPIC_NAME;
      publish.packet_id = mqttclient_get_packetid();
      publish.buffer = (byte*)TEST_MESSAGE;
      publish.total_len = strlen(TEST_MESSAGE);
     
      rc = MqttClient_Publish(&client, &publish);
      Serial.print("[MQTT] Publish: ");
      Serial.println(rc);

      while (mStopRead == 0) {
        rc = MqttClient_WaitMessage(&client, DEFAULT_CMD_TIMEOUT_MS);
        if (rc == MQTT_CODE_SUCCESS) {
          continue;
        } else if (rc == MQTT_CODE_ERROR_TIMEOUT) {
          Serial.println("[MQTT] Timeout! Sende Ping...");
          rc = MqttClient_Ping(&client);
          if (rc != MQTT_CODE_SUCCESS) {
            Serial.print("[MQTT] Ping fehlgeschlagen: ");
            Serial.println(rc);
            break;
          }
        } else {
          Serial.print("[MQTT] Fehler beim Warten auf Nachricht: ");
          Serial.println(rc);
          break;
        }
      }
    }

    Serial.println("[MQTT] Verbindung wird getrennt...");
    MqttClient_Disconnect(&client);
    MqttClient_NetDisconnect(&client);
  }

  Serial.println("[MQTT] Warte 5 Sekunden und starte neu...");
  delay(5000);
}

Share

Re: WolfMqtt on Arduino UNO R4 Wifi

Mosquitto reports:
1740069838: New connection from 192.168.0.200 on port 1883.
1740069843: Socket error on client <unknown>, disconnecting.

Share