Netduino home hardware projects downloads community

Jump to content


Photo

SerialPort ESP 8266 ESP8266 Wifi

ESP 8266 ESP 8266 WiFi

  • Please log in to reply
10 replies to this topic

#1 JohanV

JohanV

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationAmsterdam

Posted 18 May 2015 - 12:47 PM

Hello I'm trying to interface the ESP8266 WiFi with AT commands.

 

 

Also any one intresting in WiFi for the Netduino,

( better just buy the new Netduino 3 with out of the box Wifi !!! :D )

 

 

At the moment i'm using the module, with this firmware (0018000902-AI03).

At 9600 bit ps.

 

 

most functions are working now.

 

my problem is the ESP 8266 only have 256 byte buffer. so to read the 256 byte buffer ( 2048 bit. ).

after about 200 ms you start to loose data.

 

At the moment i spend quite some time to get me familiar to the .net micro framework serial port.

 

The serial port is read with an interupt.

This interupt is started in a new thread. so this is triggered after some data is recieved.

somtimes 2 bytes somtimes 10 or more.

 

serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceived_Interrupt);

serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorReceived_Interrupt);

 

Also at first i did not have an Error Revieved interrupt, and was unable to find the problems i had with this module.

turns out it's an buffer input overflow. ( 256 bytes max ).

 

I changed the handling of the recieved data, and turns out, my approach is to slowy ( or the neduino ? ). For this module.

I'm using some string functions that i suspect are to slowly ?

 

static System.String sRecieveData = "";

static SerialPort serialPort;

 

//* Data reception interrupt handler */

internal void DataReceived_Interrupt(object com, SerialDataReceivedEventArgs arg)

{

  byte[] bytes = new byte[serialPort.BytesToRead];

  // read the bytes

  serialPort.Read(bytes, 0, bytes.Length);

  // convert the bytes into a string

  String strLine = new String(System.Text.Encoding.UTF8.GetChars(bytes, 0, bytes.Length));

  sRecieveData += strLine;

}

 

 

/* Data reception interrupt handler */

internal void DataReceived_Interrupt(object com, SerialDataReceivedEventArgs arg)

{

  byte[] buf = new byte[1];

  System.String sRecievePart = "";

  while (serialPort.BytesToRead > 0)

  {

    serialPort.Read(buf, 0, 1);

    sRecievePart += (char)buf[0];

  }

  sRecieveData += sRecievePart;

}

 

So my question is what is the best way to handle the recieved serial data ? so i not get the buffer overflow error. 

 

 

Thanks



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 18 May 2015 - 06:20 PM

Hi JohanV,

Reading data when the interrupt is fired, as you have done, is probably your best option there.

Can you run the module at a higher speed? 9600 bps (~960 characters per second) seems really, really slow. You could overflow your buffer in a quarter of a second (i.e. data is coming in too fast for the serial transport itself). What is the total size of buffers on the module?

Chris

#3 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1070 posts
  • LocationNorth Yorkshire, UK

Posted 19 May 2015 - 05:48 AM

Can you run the module at a higher speed? 9600 bps (~960 characters per second) seems really, really slow. You could overflow your buffer in a quarter of a second (i.e. data is coming in too fast for the serial transport itself). What is the total size of buffers on the module?

 

There is an AT command to change the baud rate, AT+CIOBAUD.  There's a list here.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#4 JohanV

JohanV

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationAmsterdam

Posted 19 May 2015 - 11:26 PM

This module has only 256 bytes cache. as far as i can test, and read on the internet.

 

also i did not think about that the data rate could be to slow, 

during testing i saw with the interrupt routine many times only get 2 to 4 characters.

but i will change it and see what happens.

 

i know the baud rate can be changed of this module. it started with 115200, and i thought it would be to fast for netmf.

 

so it's not the string manipulation that is slow but rather the interupt trigger fired many times ?

 

if i get serial data back bigger than about 256 bytes the stream is choped at irregular places

 

 

thanks

 

i will try it tomorrow.



#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 19 May 2015 - 11:41 PM

Hi JohanV,

I would increase the speed to 57600 or 115200 and then we can go from there.

Chris

#6 JohanV

JohanV

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationAmsterdam

Posted 21 May 2015 - 08:41 AM

Hi Chris,

 

Actualy, i changed the speed to 115200, and get about 1450 bytes at each http response.

so quite an improvement. Different than i expected....

But some data is still choped, at the end, this is not a real problem, for only sending some simple sensor data to a webserver or database.

 

I get the "RXOver" type in ErrorReceived_Interrupt(object sender, SerialErrorReceivedEventArgs e)

This is an input buffer overflow ?

 

So to fix this i need to speed up reading and processing data from the input buffer ?

 

I'm still wondering what the slow part is of this code (DataReceived_Interrupt) ?

there is an example of the esp8266 driver ( https://github.com/brusdev/ATModem )

this code handles the return data of the module in arrays, i think to avoid slow string processing ?



#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 21 May 2015 - 06:51 PM

Hi JohanV,

Progress :) Yay!

I'd start by processing with byte arrays (preferably with a pre-allocated array that you use over and over). Strings are created/destroyed on the fly in memory which requires a lot of RAM and extra processor cycles.

For instance, if you want to add something to string a like this...
string a = "First part of messa";
string b = "ge.";
a = a + b;
...then what is actually happening in the background is something like this...
string a = "First part of messa";
string b = "ge.";
string c = ... /* creates new string, concatenating "First part of messa" and "ge." */
a = null;
a = c; /* copy string c or assign its pointer to string a */
/* NOTE: b is still hanging around for a while, taking up RAM until it is out of scope and GC'd */
Putting your buffers into byte arrays cuts down on MCU cycles and memory requirements.

Chris

#8 JohanV

JohanV

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationAmsterdam

Posted 22 May 2015 - 07:13 AM

Hi Chris,

 

Thanks for the support, 

I did some string tests.

 

                this.SendTimeOut = System.DateTime.Now;
                System.String test = "";
                for (int i1 = 0; i1 < 10000; ++i1)
                {
                    test += ":" + i1.ToString();
                }
                Debug.Print(this.SendTimeOut +"::"+ System.DateTime.Now);
 
this itteration takes about 68 seconds for 10000 repeats.
And turns out it does only 147 times per second the ( += string function ).
so this is only 1200 bytes per second, not fast enough for the 9600 baud rate.
 
I will look into some array functions to do similar functionality.
 
I know the STM32F427 is quite fast with array functions.
 
 
Any one have some nice example that will run fast ?


#9 JohanV

JohanV

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationAmsterdam

Posted 22 May 2015 - 07:26 AM

Also after getting some serial port data back, and do some string searching.

I did not check how long this takes. an array search will probably be a lot faster ?

 

                this.SendTimeOut = System.DateTime.Now;

                // search string
                for (int i1 = 0; i1 < 10000; ++i1)
                {
                    test.IndexOf(":" + i1.ToString());
                }
                Debug.Print(this.SendTimeOut +"::"+ System.DateTime.Now);


#10 JohanV

JohanV

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationAmsterdam

Posted 22 May 2015 - 02:57 PM

After some testing i reccomend for serial data processing, 

some short code that quickly handles the datarecieved interrupt.

 

and about a 100 or 150 msec handling the data, but this is al depending on your project off-course.

 

       private int recBufferLength = 0;
       private byte[] recBuffer = new byte[2048];
 
            /* Data reception interrupt handler */
       internal void DataReceived_Interrupt(object com, SerialDataReceivedEventArgs arg)
       {
             // byte[] buffer = new byte[serialPort.BytesToRead];
             Int32 curRead = serialPort.Read(recBuffer, recBufferLength , serialPort.BytesToRead);
             recBufferLength += curRead;
        }

 

// i use some sort of data process moment, each 100 or 150 msec.

// at this moment i turn the data into a string.

 

 sRecieveData += new String(System.Text.Encoding.UTF8.GetChars(recBuffer, 0, recBufferLength));
 recBuffer = new byte[2048];
 
 
Also if there is an incomplete http response from the ESP8266, remember how the request was made.
Http1.0 or HTTP1.1.
testing against a linux apache server the HTTP1.1 could use Chunked responses.
( there will be some strange identifier ?? 8d4 in this case.) http://en.wikipedia....ansfer_encoding
 
+IPD,1059:
1412
HTTP/1.1 200 OK
Date: Fri, 22 May 2015 14:54:29 GMT
Server: Apache/2.2.29 (Unix)
X-Powered-By: PHP/5.4.40
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
 
8d4
 
 
 
for a different request i get a response of 39342 bytes.
But still my response with the ESP8266 is choped at around 1450 bytes.
 
Host: www.netduino.com
Connection: close
Accept: text/html
User-Agent: ESP8266
 
 
 
 
+IPD,1412:HTTP/1.1 200 OK
Content-Length: 39342
Content-Type: text/html
Last-Modified: Sun, 10 May 2015 19:52:13 GMT
Accept-Ranges: bytes
ETag: "2d348ccf5a8bd01:0"
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=0fc3cc817ad58a64bbaff8bc51eb48d3224777aeb37b322d17e1369cad947f99;Path=/;Domain=www.netduino.com
Date: Fri, 22 May 2015 14:51:24 GMT
Connection: close
 
?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
 
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-e


#11 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 10 June 2015 - 01:45 AM

Out of curiosity, how are you powering your setup currently?

I'm just getting started with the ESP and I don't think the 3.3v line on the netduino can power the unit can it?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.