Giter Club home page Giter Club logo

Comments (10)

julianobst avatar julianobst commented on June 15, 2024 2

In my case, I could fix the error by enabling CONFIG_LWIP_SO_RCVBUF which results in CONFIG_LWIP_SO_RCVBUF=y in sdkconfig.

This option was not set in sdkconfig.h provided by PlatformIO. For PlatfromIO I had to add #define CONFIG_LWIP_SO_RCVBUF 1 in sdkconfig.h to get the esp-mqtt lib working.

from esp-mqtt.

256dpi avatar 256dpi commented on June 15, 2024 2

@julianobst Thanks for figuring out! This constant is automatically switched on when using the esp-idf without platform.io.

I'll check if there is a way to define constants for platform.io. In anycase I'll update the wiki to reflect this.

from esp-mqtt.

256dpi avatar 256dpi commented on June 15, 2024

Can you share the complete code? Also, can you test the provided example with mosca to rule out any broker side issues?

from esp-mqtt.

liquidpizza avatar liquidpizza commented on June 15, 2024

Here is my complete code:

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"

extern "C"
{
#include "lwmqtt.h"
#include "esp_mqtt.h"
}

#define WIFI_SSID mySsid
#define WIFI_PASSWORD myPass 

#define MQTT_HOST myBroker
#define MQTT_PORT myPort
#define MQTT_USER ""
#define MQTT_PASS ""

#define BLINK_GPIO GPIO_NUM_2

static void status_callback(esp_mqtt_status_t status) 
{
    switch (status) {
        case ESP_MQTT_STATUS_CONNECTED:
            printf("mqtt connected\n");
            break;
        case ESP_MQTT_STATUS_DISCONNECTED:
            printf("mqtt disconnected\n");
            esp_mqtt_start(
                MQTT_HOST,
                MQTT_PORT,
                "esp_mqtt_client",
                MQTT_USER,
                MQTT_PASS);
            break;
    }
}

static void message_callback(const char *topic, uint8_t *payload, size_t len) {
    printf("incoming: %s => %s (%d)\n", topic, payload, (int)len);
}

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    if (event->event_id == SYSTEM_EVENT_STA_GOT_IP)
    {
        esp_mqtt_start(
            MQTT_HOST,
            MQTT_PORT,
            "esp_mqtt_client",
            MQTT_USER,
            MQTT_PASS);
    }
    return ESP_OK;
}

static void initialise_wifi(void)
{
    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    wifi_config_t sta_config = {
        .sta = {
        WIFI_SSID,
        WIFI_PASSWORD
    }
    };
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));
    ESP_ERROR_CHECK(esp_wifi_start());
    ESP_ERROR_CHECK(esp_wifi_connect());
}

void blink_task(void *pvParameter)
{    
    /* Configure the IOMUX register for pad BLINK_GPIO (some pads are
       muxed to GPIO on reset already, but some default to other
       functions and need to be switched to GPIO. Consult the
       Technical Reference for a list of pads and their default
       functions.)
    */
    gpio_pad_select_gpio(BLINK_GPIO);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    while (1) 
    {
        /* Blink off (output low) */
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(100 / portTICK_RATE_MS);
        /* Blink on (output high) */
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(100 / portTICK_RATE_MS);
    }
}


extern "C" void app_main()
{
    nvs_flash_init();
    initialise_wifi();

    esp_mqtt_init(
        status_callback,
        message_callback,
        256,
        10);

    xTaskCreate(&blink_task, "blink_task", 512, NULL, 4, NULL);
}

I'll test the example code now and report back.

Can you tell me what the command_timeout parameter in esp_mqtt_init does? I tried different values (0-200000) but the only effect I noticed was that it does not work with 0.

// edit:
When I run the example code I still get an error but a different one:
esp_mqtt: lwmqtt_publish: -4

When I comment out this esp_mqtt_publish("hello", (uint8_t *)"world", 5, 2, false); the esp_mqtt: esp_lwmqtt_network_peek: -5 error occurs again.

In both cases Mosca only lists a keepalive timeout when I pause my ESP via the debug tools. I'll try to setup another broker and try this again.

// edit2:
I just tested the sample code with a local mosquitto broker.

The ESP throws the esp_mqtt: esp_lwmqtt_network_peek: -5 error but mosquitto tells me that there is Socket error on client esp-mqtt, disconnecting.

// edit3:
In some cases mosquitto also writes Client esp-mqtt has exceeded timeout, disconnecting.. But I am not sure in which cases.

from esp-mqtt.

liquidpizza avatar liquidpizza commented on June 15, 2024

I am not sure if this is of any use, but I debugged some more and found out, that the reason my ESP throws the esp_mqtt: esp_lwmqtt_network_peek: -5 error is that the cmd parameter in lwip_ioctl() is set to FIONREAD and the flag LWIP_FIONREAD_LINUXMODE is set to 0.

Because of this the default case in the switch-case is hit and the function returns -1.

Also I tried to skip the break in

lwmqtt_err_t err = esp_lwmqtt_network_select(&esp_mqtt_network, &available, esp_mqtt_command_timeout);
if (err != LWMQTT_SUCCESS) {
  ESP_LOGE(ESP_MQTT_LOG_TAG, "esp_lwmqtt_network_select: %d", err);
  ESP_MQTT_UNLOCK_SELECT();
  break;
}

But this lead to another error: esp_mqtt: lwmqtt_keep_alive: -13

I further looked at the MQTT traffic with wireshark and I noticed that one ping request is send to the broker and one ping response is sent back before the code crashes.

from esp-mqtt.

256dpi avatar 256dpi commented on June 15, 2024

That's weird, I tested the example multiple times with mosquitto and never ran into this situation...

The command_timeout parameter defines the max allowed time for operations by the client. A value of 2000 would result in the client trying to do something for up to 2000ms and possibly also block up to that timeout. Still it should be > 500 to accommodate WiFi delays etc.

Which IDF version are you using?

from esp-mqtt.

julianobst avatar julianobst commented on June 15, 2024

I run in the same problem after updating IDF to V3.0 and pulling your latest release V0.6.0.

Additionally to the error case mentioned before, the mqtt process exit with esp_lwmqtt_network_peek: -5 immediately when some data is received on a subscribed topic.

I'm using PlatformIO on Windows. With IDF V2.1.1 and esp-mqtt V0.5.4 everything works fine.

from esp-mqtt.

liquidpizza avatar liquidpizza commented on June 15, 2024

I am using IDF V3.0. On monday I can try it with another version.

from esp-mqtt.

liquidpizza avatar liquidpizza commented on June 15, 2024

I am using VisualGDB with Visual Studio 2015. After adding #define CONFIG_LWIP_SO_RCVBUF 1 to my sdkconfig.h it seems to work.

I'll do some more tests and report back if I notice anything.

Thanks for your help!

// edit:
I tested the lib with IDF V2.1 and IDF V2.1.1. In both cases the same error occured unless CONFIG_LWIP_SO_RCVBUF 1 is defined in the sdkconfig.h

from esp-mqtt.

256dpi avatar 256dpi commented on June 15, 2024

I added a note about this to the readme. I'll investigate how I can properly publish the library on PlatformIO so that this issues doesn't come up again. Thanks for your help!

from esp-mqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.