This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

mqtt simple data_publish

Hi,

I am trying to send a test message in Server through mqtt and used the mqtt_simple example for that.

For the test I insert data_publish() function in main() like following, but in my Server there are no message arrived. I tested my Server, the Server ist ok and can received mqtt message.

Did I missunderstand something. Why did't it work?

  

Regards,

Linh

Parents
  • After many time trying, I realize that the function data_publish() have to be located after the commands in While(1) Loop so that the data can send. My new Code Looks like this

    //while (1) {
                    printk("poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));\n");
    		err = poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));
    		if (err < 0) {
    			printk("ERROR: poll %d\n", errno);
    			//break;
    		}
                    printk("mqtt_live\n");
    		err = mqtt_live(&client);
    		if (err != 0) {
    			printk("ERROR: mqtt_live %d\n", err);
    			//break;
    		}
    
    		if ((fds.revents & POLLIN) == POLLIN) {
                            printk("mqtt_input\n");
    			err = mqtt_input(&client);
    			if (err != 0) {
    				printk("ERROR: mqtt_input %d\n", err);
    				//break;
    			}
    		}
    
    		if ((fds.revents & POLLERR) == POLLERR) {
    			printk("POLLERR\n");
    			//break;
    		}
    
    		if ((fds.revents & POLLNVAL) == POLLNVAL) {
    			printk("POLLNVAL\n");
    			//break;
    		}
    	//}
            data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "dataPu4", strlen("dataPu4"));
     

    But I don't really understand what the commands in while(1){..} do. Do you know, what happend in the loop? and why data_publish() have to be located after this?

    while (1) {
    
    		err = poll(&fds, 1, mqtt_keepalive_time_left(&client));
    
    		if (err < 0) {
    
    			printk("ERROR: poll %d\n", errno);
    
    			break;
    
    		}
    
    
    
    		err = mqtt_live(&client);
    
    		if ((err != 0) && (err != -EAGAIN)) {
    
    			printk("ERROR: mqtt_live %d\n", err);
    
    			break;
    
    		}
    
    
    
    		if ((fds.revents & POLLIN) == POLLIN) {
    
    			err = mqtt_input(&client);
    
    			if (err != 0) {
    
    				printk("ERROR: mqtt_input %d\n", err);
    
    				break;
    
    			}
    
    		}
    
    
    
    		if ((fds.revents & POLLERR) == POLLERR) {
    
    			printk("POLLERR\n");
    
    			break;
    
    		}
    
    
    
    		if ((fds.revents & POLLNVAL) == POLLNVAL) {
    
    			printk("POLLNVAL\n");
    
    			break;
    
    		}
    
    	}

Reply
  • After many time trying, I realize that the function data_publish() have to be located after the commands in While(1) Loop so that the data can send. My new Code Looks like this

    //while (1) {
                    printk("poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));\n");
    		err = poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));
    		if (err < 0) {
    			printk("ERROR: poll %d\n", errno);
    			//break;
    		}
                    printk("mqtt_live\n");
    		err = mqtt_live(&client);
    		if (err != 0) {
    			printk("ERROR: mqtt_live %d\n", err);
    			//break;
    		}
    
    		if ((fds.revents & POLLIN) == POLLIN) {
                            printk("mqtt_input\n");
    			err = mqtt_input(&client);
    			if (err != 0) {
    				printk("ERROR: mqtt_input %d\n", err);
    				//break;
    			}
    		}
    
    		if ((fds.revents & POLLERR) == POLLERR) {
    			printk("POLLERR\n");
    			//break;
    		}
    
    		if ((fds.revents & POLLNVAL) == POLLNVAL) {
    			printk("POLLNVAL\n");
    			//break;
    		}
    	//}
            data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "dataPu4", strlen("dataPu4"));
     

    But I don't really understand what the commands in while(1){..} do. Do you know, what happend in the loop? and why data_publish() have to be located after this?

    while (1) {
    
    		err = poll(&fds, 1, mqtt_keepalive_time_left(&client));
    
    		if (err < 0) {
    
    			printk("ERROR: poll %d\n", errno);
    
    			break;
    
    		}
    
    
    
    		err = mqtt_live(&client);
    
    		if ((err != 0) && (err != -EAGAIN)) {
    
    			printk("ERROR: mqtt_live %d\n", err);
    
    			break;
    
    		}
    
    
    
    		if ((fds.revents & POLLIN) == POLLIN) {
    
    			err = mqtt_input(&client);
    
    			if (err != 0) {
    
    				printk("ERROR: mqtt_input %d\n", err);
    
    				break;
    
    			}
    
    		}
    
    
    
    		if ((fds.revents & POLLERR) == POLLERR) {
    
    			printk("POLLERR\n");
    
    			break;
    
    		}
    
    
    
    		if ((fds.revents & POLLNVAL) == POLLNVAL) {
    
    			printk("POLLNVAL\n");
    
    			break;
    
    		}
    
    	}

Children
Related