{"id":1899,"date":"2015-01-09T00:01:49","date_gmt":"2015-01-09T00:01:49","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1899"},"modified":"2015-03-06T04:42:28","modified_gmt":"2015-03-06T04:42:28","slug":"esp8266-serial-uart0-txrx","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/01\/esp8266-serial-uart0-txrx\/","title":{"rendered":"esp8266 serial (UART0) tx\/rx"},"content":{"rendered":"<p>The esp8266 SDK manual and examples are pretty poor. There an example uart.c knocking around, but the receive code is incomplete. I built this out a bit so I could read serial data from the circular buffer. You can download my update files here: <a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/uart.c\">uart.c<\/a>,<br \/>\n<a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/uart.h\">uart.h<\/a><br \/>\n, <a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/uart_register.h\">uart_register.h<\/a>. Only uart.c has been changed significantly. (update, I&#8217;ve put a tarball which a complete echo example <a href=\"http:\/\/41j.com\/blog\/wp-content\/uploads\/2015\/01\/uart_test.tar.gz\">here<\/a>. UART data can then be received as follows:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n  int c = uart0_rx_one_char();\r\n\r\n  if(c != -1) {\r\n    char out&#x5B;30];\r\n    os_sprintf(out,&quot;received %c&quot;,c);\r\n    debug(out);\r\n  }\r\n<\/pre>\n<p>Or if you just want to echo received characters:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n  int c = uart0_rx_one_char();\r\n\r\n  if(c != -1) {\r\n   uart_tx_one_char(c);\r\n  }\r\n<\/pre>\n<p>I&#8217;ve only built out reading a single character, but it should be easy to build more general functions on top of that. You can still use the old function to transmit:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nuart0_tx_buffer(data,strlen(data));\r\n<\/pre>\n<p>As always you need to init the UART in user_init with something like:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nuart_init(BIT_RATE_9600,BIT_RATE_9600);\r\n<\/pre>\n<p>Or the UART will be configured with 74KBPS (which is a pretty odd value).<\/p>\n<p>Update: You may also want to take a look at my <a href=\"http:\/\/41j.com\/blog\/2015\/01\/esp8266-run-loop-messaging\/\">notes on messaging system<\/a> in the esp8266 SDK, and how it can potentially be used for this application.<br \/>\n<\/p>\n<h4>This is a continuation of my notes on the esp8266 microcontroller, you can find a complete list of my esp8266 posts <a href=\"http:\/\/41j.com\/blog\/2015\/01\/esp8266-notes-summary\/\">here<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>The esp8266 SDK manual and examples are pretty poor. There an example uart.c knocking around, but the receive code is incomplete. I built this out a bit so I could read serial data from the circular buffer. You can download my update files here: uart.c, uart.h , uart_register.h. Only uart.c has been changed significantly. (update, [&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-1899","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-uD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1899","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=1899"}],"version-history":[{"count":5,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1899\/revisions"}],"predecessor-version":[{"id":2213,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1899\/revisions\/2213"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}