{"id":1917,"date":"2015-01-11T00:33:32","date_gmt":"2015-01-11T00:33:32","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1917"},"modified":"2015-01-17T03:02:21","modified_gmt":"2015-01-17T03:02:21","slug":"accepting-incoming-tcp-connections-esp8266-trivial-example","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/accepting-incoming-tcp-connections-esp8266-trivial-example\/","title":{"rendered":"Accepting incoming tcp connections on the esp8266 (trivial example)"},"content":{"rendered":"<p>I&#8217;ve been working on some code that accepts incoming connections on the esp8266, if your looking for a full HTTP server take a look at <a href=\"http:\/\/www.esp8266.com\/viewtopic.php?f=6&#038;t=376\">sprite_tm&#8217;s code<\/a>. But these are my notes on accepting a TCP connection.<\/p>\n<p>As with TCP transmissions, you register a bunch of callbacks which process connections. So the first step is to initialize the TCP system with your connect callback:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;ets_sys.h&quot;\r\n#include &quot;osapi.h&quot;\r\n#include &quot;os_type.h&quot;\r\n\r\nstatic struct espconn httpconfig_conn;\r\nstatic esp_tcp httpconfig_tcp_conn;\r\n\r\nvoid ICACHE_FLASH_ATTR httpconfig_conn_init() {\r\n\r\n        httpconfig_conn.type=ESPCONN_TCP;\r\n        httpconfig_conn.state=ESPCONN_NONE;\r\n        httpconfig_tcp_conn.local_port=80;\r\n        httpconfig_conn.proto.tcp=&amp;httpconfig_tcp_conn;\r\n\r\n        espconn_regist_connectcb(&amp;httpconfig_conn, httpconfig_connected_cb);\r\n        espconn_accept(&amp;httpconfig_conn);\r\n}\r\n<\/pre>\n<p>When a connection is received on the specified port, httpconfig_connected_cb will be called. In this function you can send data to the client, and register other functions to trigger when data is received\/disconnect etc.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic void ICACHE_FLASH_ATTR httpconfig_recv_cb(void *arg, char *data, unsigned short len) {\r\n  struct espconn *conn=(struct espconn *)arg;\r\n\r\n  espconn_disconnect(conn);\r\n}\r\n\r\nstatic void ICACHE_FLASH_ATTR httpconfig_recon_cb(void *arg, sint8 err) {\r\n}\r\n\r\nstatic void ICACHE_FLASH_ATTR httpconfig_discon_cb(void *arg) {\r\n}\r\n\r\nstatic void ICACHE_FLASH_ATTR httpconfig_sent_cb(void *arg) {\r\n}\r\n\r\nstatic void ICACHE_FLASH_ATTR httpconfig_connected_cb(void *arg) {\r\n  struct espconn *conn=arg;\r\n\r\n  espconn_regist_recvcb  (conn, httpconfig_recv_cb);\r\n  espconn_regist_reconcb (conn, httpconfig_recon_cb);\r\n  espconn_regist_disconcb(conn, httpconfig_discon_cb);\r\n  espconn_regist_sentcb  (conn, httpconfig_sent_cb);\r\n\r\n  char *transmission = &quot;OK\\r\\n\\r\\nOK!\\n&quot;;\r\n  sint8 d = espconn_sent(conn,transmission,strlen(transmission));\r\n}\r\n<\/pre>\n<p>I currently don&#8217;t fully understand what the reconnect callback does. If you know, please mail me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on some code that accepts incoming connections on the esp8266, if your looking for a full HTTP server take a look at sprite_tm&#8217;s code. But these are my notes on accepting a TCP connection. As with TCP transmissions, you register a bunch of callbacks which process connections. So the first step is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1917","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-uV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1917"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1917\/revisions"}],"predecessor-version":[{"id":1918,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1917\/revisions\/1918"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}