{"id":1985,"date":"2025-11-02T18:16:36","date_gmt":"2025-11-02T18:16:36","guid":{"rendered":"https:\/\/priscimon.net\/coding\/?p=1985"},"modified":"2025-11-04T14:20:39","modified_gmt":"2025-11-04T14:20:39","slug":"esp8266-espconn_regist_sentcb-must-be-called","status":"publish","type":"post","link":"https:\/\/priscimon.net\/coding\/2025\/11\/02\/esp8266-espconn_regist_sentcb-must-be-called\/","title":{"rendered":"ESP8266: espconn_regist_sentcb() must be called"},"content":{"rendered":"\n<p>Another gotcha with the ESP8266 non-OS SDK\u2014<\/p>\n\n\n\n<p>Before sending data over a TCP connection, you must register a callback function to handle &#8220;data successfully sent&#8221; events before calling <code>espconn_connect()<\/code> or <code>espconn_send()<\/code>. Although this strict requirement isn&#8217;t highlighted in the documentation, a fatal exception will reset the ESP8266 when this callback is not specified.<\/p>\n\n\n\n<p>You register the callback function with <code>espconn_regist_sentcb()<\/code>. The signature of the function is <code>void (espconn_sent_callback)(void *)<\/code>, and the parameter is a pointer to the <code>struct espconn<\/code> used for sending the data.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static ICACHE_FLASH_ATTR void espconn_sent2(void *arg)\n{\n\tstruct espconn *espconn = (struct espconn *)arg;\n\tos_printf(\"Sent data\\r\\n\");\n\tstate_current = IDLE;\n}\n\nstatic ICACHE_FLASH_ATTR void espconn_received(void *arg, char *pdata, unsigned short len)\n{\n\tstruct espconn *espconn = (struct espconn *)arg;\n\tos_printf(\"Received data: %s\\r\\n\", pdata);\n\tstate_current = IDLE;\n}\n\nvoid tcp_connect()\n{\n        struct espconn *espconn = (struct espconn *)os_zalloc(sizeof(struct espconn));\n        espconn-&gt;type = ESPCONN_TCP;\n        espconn-&gt;state = ESPCONN_NONE;\n        espconn-&gt;proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));\n        os_memcpy(espconn-&gt;proto.tcp-&gt;remote_ip, &amp;addr-&gt;addr, 4);\n        espconn-&gt;proto.tcp-&gt;remote_port = 80;\n        espconn_regist_connectcb(espconn, espconn_connected);\n        espconn_regist_reconcb(espconn, espconn_reconnect);\n        \n        \/\/ IMPORTANT! These callbacks must be registered.\n        espconn_regist_sentcb(espconn, espconn_sent2);\n        espconn_regist_recvcb(espconn, espconn_received);\n\n        espconn_set_opt(espconn, ESPCONN_NODELAY | ESPCONN_KEEPALIVE);\n        sint8 result = espconn_connect(espconn);\n        os_printf(\"Connection result: %d\\r\\n\", result);\n}\n\n\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another gotcha with the ESP8266 non-OS SDK\u2014 Before sending data over a TCP connection, you must register a callback function to handle &#8220;data successfully sent&#8221; events before calling espconn_connect() or espconn_send(). Although this strict requirement isn&#8217;t highlighted in the documentation, a fatal exception will reset the ESP8266 when this callback is not specified. You register [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1985","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3I4g9-w1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/1985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/comments?post=1985"}],"version-history":[{"count":9,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/1985\/revisions"}],"predecessor-version":[{"id":2001,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/1985\/revisions\/2001"}],"wp:attachment":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/media?parent=1985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/categories?post=1985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/tags?post=1985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}