Code Monkey home page Code Monkey logo

Comments (1)

shirish47 avatar shirish47 commented on July 20, 2024

Hi I also face similar problem. I genereally raise a flag to show that scubscription is done if not I restart the process after some tries. subscribtion call back is not called. as you post above said not to immediately publish while subscribing. so I pushed my publish call to subscribe_cb. still I don't get subscription callback this use to work before.

this is my codes out put. (sorry code is complex now )

`
[MQTT INFO] Connected to MQTT broker, create sending thread before call connected callback
[MQTT INFO] mqtt_sending_task
I (7792) BYTELENSCLIENT: Connected to BROKER. 

I (7802) BYTELENSCLIENT: 

I (7802) BYTELENSCLIENT: /27/24:a:c4:1:1b:34/auth  <----- I first create topic strings( concate string etc)

I (7812) BYTELENSCLIENT: /27/24:a:c4:1:1b:34/authdone

I (7822) BYTELENSCLIENT: /37/24:a:c4:1:1b:34/list
(then I subscribe to topics, only 2 topics for now)
[MQTT INFO] Queue subscribe, topic"/27/24:a:c4:1:1b:34/authdone", id: 1
[MQTT INFO] Sending...50 bytes
[MQTT INFO] Queue subscribe, topic"/24:a:c4:1:1b:34/list", id: 2
[MQTT INFO] Sending...49 bytes
[MQTT INFO] mqtt_start_receive_schedule
[MQTT INFO] Read len 5
[MQTT INFO] Read len 50
[MQTT INFO] Queue response QoS: 1
[MQTT INFO] Sending...4 bytes
[MQTT INFO] deliver_publish
(no subscribe_cb called)
[MQTT INFO] Data received: 1/1 bytes 
I (8072) BYTELENSCLIENT: Data cb.
I (8082) BYTELENSCLIENT: Publish topic: /27/24:a:c4:1:1b:34/authdone 
[MQTT INFO] Read len 5
I (12892) wifi: pm start, type:0

[MQTT INFO] Read len 50
[MQTT INFO] deliver_publish
[MQTT INFO] Data received: 3/3 bytes 
I (19102) BYTELENSCLIENT: Data cb.
I (19102) BYTELENSCLIENT: Publish topic: /27/24:a:c4:1:1b:34/authdone <---- but I receive data when I publish from hivemq.com try demo
I (31872) GATTC_DEMO: Ticker: conn: 0 ,Noti: 0, scan: 0
I (31872) GATTC_DEMO: Ticker: wificon: 1, mqttcon: 1, mqttsub: 0
I (31872) GATTC_DEMO: Ticker: MQTT broker not Subscribed, retrying 0
I (62872) GATTC_DEMO: Ticker: conn: 0 ,Noti: 0, scan: 0
I (62872) GATTC_DEMO: Ticker: wificon: 1, mqttcon: 0, mqttsub: 0
I (62872) GATTC_DEMO: Ticker: MQTT broker not Connected, retrying 1
[MQTT INFO] Sending pingreq
[MQTT INFO] Read len 2
[MQTT INFO] MQTT_MSG_TYPE_PINGRESP
`

my subscribe_cb and

void subscribe_cb(void *self, void *params)
{
    ESP_LOGI(BCLIENT_TAG, "Subscribed to ");
    mqtt_client *client = (mqtt_client *)self;
    mqtt_event_data_t *event_data = (mqtt_event_data_t *)params;

    char *myID=getStrHubID();
    mqtt_publish(client,MQTT_CSV_AUTH,myID,strlen(myID),0,0);
    mqttSubscribed=true;
//    mqtt_publish(client, MQTT_TOPIC_INFO,str,strlen(str), 0, 0);
}


void connected_cb(void *self, void *params)
{
   ESP_LOGI(BCLIENT_TAG,"Connected to BROKER. \n")
   mqtt_client *client = (mqtt_client *)self;
   MQTTCLIENT=client;

   mqtt_event_data_t *event_data = (mqtt_event_data_t *)params;
  //char *str="I am ESP32.";

   Broker_Connected = true;
   mqttConnected=Broker_Connected;

  create_topics();
// first subscribe to all topics
mqtt_subscribe(client,MQTT_CSV_AUTHDONE,1);
mqtt_subscribe(client,MQTT_CSV_LIST,1);
 }

//and mqtt settings 
mqtt_settings settings = {
    .host = MQTT_HOST,
#if defined(CONFIG_MQTT_SECURITY_ON)
    .port = 8883, // encrypted
#else
    .port = 1883, // unencrypted
#endif
    .client_id = MQTT_CLIENT_ID,
    .username = MQTT_USER,
    .password = MQTT_PASS,
    .clean_session = 0,
    .keepalive = 120,
    .lwt_topic = MQTT_TOPIC_LWT,
    .lwt_msg = "offline",
    .lwt_qos = 0,
    .lwt_retain = 0,
    .connected_cb = connected_cb,
    .disconnected_cb = disconnected_cb,
 //   .reconnect_cb = reconnect_cb,
    .subscribe_cb = subscribe_cb,
    .publish_cb = publish_cb,
    .data_cb = data_cb
};

from esp32-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.