{"id":103,"date":"2021-09-20T09:08:38","date_gmt":"2021-09-20T01:08:38","guid":{"rendered":"https:\/\/www.sy-blog.moe\/?p=103"},"modified":"2025-05-26T10:18:00","modified_gmt":"2025-05-26T02:18:00","slug":"esp8266-%e4%bd%bf%e7%94%a8-pubsubclient-%e6%8e%a8%e9%80%81%ef%bc%88publish%ef%bc%89%e4%b8%8d%e5%8f%8a%e6%97%b6%e9%97%ae%e9%a2%98%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/sy-blog.moe\/en\/103.html","title":{"rendered":"Solution to ESP8266 PubSubClient Publish Delay Issue"},"content":{"rendered":"<p>I updated the power-meter project two days ago and planned to use PubSubClient library to connect to homeassistant<\/p>\n<p>But when running the project, I found that esp8266 mqtt push is extremely unstable<\/p>\n<p>I checked the mqtt server and the client that receives the message, but no problems were found.<\/p>\n<p>I wrote a loop push using esp8266, which is also very stable, eliminating the problem of the board itself<code><\/code><\/p>\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\"><code lang=\"c\" class=\"language-c line-numbers\">for(int i = 1; i &lt;= 100; i++){\n    sprintf(message, \"%d\", i);\n    client.publish(topic, message);\n    yield();\n    delay(500);\n}<\/code><\/pre>\n\n\n\n<p>Code snippet:<\/p>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\"><code lang=\"c\" class=\"language-c line-numbers\">void loop(){\n    while(1){\n        light1 = analogRead(A0);\n        if(light1 &gt; powerledPin_average){\n            \/\/ led on\n            timeA = micros();\n\n            delay(300);\n            break;\n        }\n        yield();\n        client.loop();\n    }\n    \n\n    time_light = timeA - timeB;\n\n    power = 1. \/ powerledPin_rate * 1000 * 3600 * 1000000 \/ time_light;\n\n    Serial.println(power);\n    sprintf(message, \"%f\", power);\n    client.publish(topic, message);\n\n    delay(100); \/\/ \u52a0\u5165\u5ef6\u65f6\n}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">I guess the last line of publish code is not processed in time and continues to loop.<strong>So add a delay after pushing the code, and the problem is solved<\/strong>.<\/p>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"Explore the mystery of ESP8266 PubSubClient publishing delay. Why is MQTT push always half a beat late? After checking that the board and the server are both fine, the key is the lack of buffering after publish in the loop. After increasing the delay, the publishing takes effect immediately!","protected":false},"author":1,"featured_media":104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2],"tags":[4,12],"class_list":["post-103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-esp8266","tag-12"],"_links":{"self":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":1,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":749,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/posts\/103\/revisions\/749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sy-blog.moe\/en\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}