ESP8266 Serial

Doing your ESP8266 C own thing? Pondering how to use Serial in ? I was until this handy chap came along..


16 thoughts on “ESP8266 Serial

      • Acme – if you’ve already got the Windows/eclipse version of the ESP8266 development system installed and working, then the following will work (I just tried it)

        – download the uart_test tar.gz file and expand it somewhere temporarily (7zip does tar and gz on Windows)
        – in the examples directory of your dev environment, clone the hello_world directory, and rename the clone uart_test. Inside this directory, delete all folders, but not the make and .project files.
        – copy or move the user directory from the DOWNLOADED uart_test to the directory you just prepared above
        – Open the .project file and change the param from hello_world to uart_test
        Open eclipse and from File, open Import, and choose General -> Existing Project into Workspace, hit Next and navigate to your new examples/uart_test directory, and hit finish

        At this point you should see the uart_test project in eclipse and you should be able to clean and build it.

        The above will work for importing most simple projects, though some might require tweaking the make file.

      • Hi Ken

        Thanks for your reply.

        I have just been reading a book written by an Scotish doctor who emigrated to Okotoks, near Calgary, in the 50s. Not exactly in your back yard 🙂

        I was working along similar lines to those you gave. As your method removes the unwanted files I think it is better.


        Bill Price

      • Hi Peter

        The author of the code is Nava Whiteford and he put the simple echo example on his blog page after I contacted him.

        He uses his uart0_rx_one_char() function in some code that uses it put data into a buffer ready for parsing.

        The code is here :-

        Look in user/user_main.c and look at line 32 : int c = uart0_rx_one_char();

        Below, on line 40, you can see that that he uses strcpy to transfer the line_buffer to a data variable (nmea_data).

        What he does with it after is beyond me, I have spent 2 days digging through the code. I am not a C coder, and we are in the land of call backs, event driven functions, and incomprehensible proceedure names, so I am well out of my depth.

        However I think the code in user/user_main.c is a good starting point for capturing esp8266 serial input.

        Bill Price

  1. Doesn’t help that I have to flu which is slowing me down. Essentially I need to get to the bit that captures a string until return – I did spot that – and call a routine – which we’ll call a callback routine. By messing about in that routine we can do things.

    • Hi Peter

      I got it working, I think

      Here is what I did :-

      1, Put the uart_test project into Eclipse and check that it builds, then flash the ESP and check that it works.

      2, In the file user_main.c make these changes :-

      2a, at line 23 insert

      int line_buffer_pos = 0;
      char line_buffer[256];

      2b, at about line 26 delete

      int c = uart0_rx_one_char();

      if(c != -1) {

      Replace with

      int c = uart0_rx_one_char();

      if(c != -1) {
      if(!((c == ‘\r’) || (c == ‘\n’))) { //Not end of line
      line_buffer[line_buffer_pos] = c;
      // if(line_buffer_pos > 255) line_buffer_pos=0; Do something here if you need to handle a buffer overflow
      else // We have an end of line
      char my_data[256];
      line_buffer_pos = 0;
      // Now do your own thing here
      // In this prototype we will just echo the string

      I hope I have copied all the braces correctly!

      Give it try.

      My thanks go to Nava Whiteford for pointing the way.

      Bill Price

    • Essentially what I THINK is needed – is for at a particular time – perhaps when a carriage return is .. detected (could be programmable) is for that interrupt routine to pass the count of characters up to the CR, to a user function (callback) – so we would not touch the code inside the interrupt routine) – we would then use the read a character routine in a loop for the number of chars we need.. or something and finish.. Any other way means sitting in a loop.

      I’m going to have to tackle this – I’m just afraid of breaking my project. I really must get to grips with GIT or similar so I can branch off with experiments without having to back up things the hard way every time.

      • If you have a time critical or low power application and you’d like not to be sitting busy waiting for input I guess the best solution would be to put the CPU to sleep in the runloop and let it wake up on the interrupt to process your input. Unfortunately, while I’ve seen references to low power modes on the esp8266, it doesn’t seem to be covered in the documentation.

        You can also do limited processing in the uart ISR itself, but the UART buffer on the esp8266 is quite small (512bytes) so I’d recommend keeping it to a minimum. I also find their UART ISR code somewhat confusing (why does it manually push data onto the buffer, and what’s the “WRITE_OVER” mode?). With limited hardware documentation it’s hard to know exactly what’s going on.

        I’d love to get a full programmers reference for this IC.

  2. I think it’s fair to say that the a WIFI board is not the best option for ultra low power. Can you clarify that..”the UART buffer on the ESP8266 is quite small” – in most of the projects I’ve seen, UART.C is there for you to alter so you could make that a larger buffer – or are you talking about another buffer that is beyond control? Yes I agree, limited documentation is a problem – you can get slightly more from ESPRESSIF if you write to them and agree to their NDA (non-disclosure agreement).

    • The specifications list low power modes down to 10uA. So, potentially there are some interesting low power things you could do, like waiting for a GPIO wakeup (and example might be when a pressure sensor triggers) and then waking up the CPU and sending some packets before going back to sleep.

      I’m not sure how much is/is not implemented in hardware/the SDK. For some reason UartDev, which is the read buffer sits in, it hardcoded to be at position 0x3fffde10. And the read buffer isn’t allocated anywhere is user code that I can see…. sooooo…. I’d like to understand better what’s going on there, hardware docs could help with that. In any case, it appears to be limited to 512 bytes according to the documentation.

      Anyway, the UART will have a small hardware FIFO, even if it’s just a couple of bytes. And if the UART ISR doesn’t return quickly enough and that buffer fills up you’ll start to lose data. Generally, my feeling is it’s best to return from ISRs as soon as possible.

  3. Agreed – which leaves the slight issue of how to arrange a callback routine for when, say there is a line of data in the incoming serial stream – without that being called from the interrupt routine that handles incoming bytes into the buffer.. without using a timer which isn’t exactly ideal…

  4. Hi Peter

    I have modified my echo code so that the incoming data is passed to a callback routine.

    At present the data is just echoed from the callback routine, but HEY you could do what you like with it in there!

    Again thanks go to Nava Whiteford for pointing the way.

    If you would like a copy of the code PM me.

    Bill Price

    • Right now my flu-bugged head is not going to swing with this – but all I can say is – guys – THANKS for this – can you suggest and keep it simple… given, say, MQTT software which currently does not use the serial input (Tuan uses serial out) – the steps to add the ability (is it a case of simply replacing the uart.c and header files and then making use of the callback routine?

      In my case -others will have their own uses, despite LUA+MQTT being around the corner, there are many instances where Lua won’t do the job. I could see many applications where you start with the basic mqtt code for communications, add in a few sensors, a few outputs… and “do stuff” – but it may be that you don’t want it all in this ESP processor – so you may decide on using serial IO to control it from, say, an Arduino. In the MQTT software the output is there already, easy to modify but right now – no serial input. I’ve ready elsewhere and I have to say I agree, you can’t go messing inside the interrupt routine – so an empty callback – (with info on how to access the data) that is not time-tied will completely the picture.

      If this makes sense – I hope it helps others, if it doesn’t make sense – it’s me, not the reader.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s