Go Down

Topic: Reading Serial Data from ESP8266 (Read 34483 times) previous topic - next topic

djred2000

Do you know if the packets are sent out by SACN View continuously or only when a setting changes?

You could try this modification of your ESP8266 code to print out the number of packets received every 100ms.  It counts the UDP packets received but discards the data.  Note: compiles OK but not tested.
SACN View does send the packets continuously even if no data has changed. The timing code does work and I get an average of five packets every 100ms.

I tried using the code below to just send the channel data bytes but it seems to crash to the ESP after a few seconds.

Code: [Select]

void loop()
{
  int packetSize = udp.parsePacket(); //parse packet  
  if(packetSize)
  {
    udp.read(E131Buffer,E131_BUFFER_SIZE); //read packet into buffer
    if(E131Buffer[1] == PacketBegin && E131Buffer[125] == StartCode) //quick check to see if packet is E1.31
   {
     for(int i = 126; i <= E131_BUFFER_SIZE; i++)
     {
      Serial.write(E131Buffer[i]); //write channel values 1-512 bytes 126-637 in packet
     }
}
}
} //end loop

Hackscribble

#16
Jul 03, 2015, 05:07 pm Last Edit: Jul 03, 2015, 05:10 pm by Hackscribble
Budgeting for a peak of 10 packets/second at 500 bytes/packet gives 5,000 bytes/second. 

Try a baud rate of 57,600 between the ESP and Arduino just in case there is some problem with the ESP running at the rate you have been using.

Also, might be worth looking through the issues on the GitHub for the core to see if anyone has reported problems with serial throughput.

Code: [Select]
for(int i = 126; i <= E131_BUFFER_SIZE; i++)

Small point - <= should be <
Hackscribble.  Writing about making things
hackscribble@outlook.com

djred2000

The issue still appears to be with the ESP. I reduced the baud rate to 115200 and 57600 and there is still latency when reading the output directly from the ESP and not even using the Mega.  I tried reducing the amount of bytes being written in the FOR loop and that removes the latency. I think the issue is with the serial buffer on the ESP.

Hackscribble

#18
Jul 04, 2015, 09:38 am Last Edit: Jul 04, 2015, 09:56 am by Hackscribble
I've run a test on serial performance on a Uno and an ESP8266 with the code below.  No UDP involved, so not a direct comparison with your program.  The code sends 125,000 bytes of data and prints the elapsed time.

On both the Uno and the ESP8266, there was no latency that I could see.  Both of them output 125,000 bytes in just over 5s at 250,000 bits/second.  So, well above the output rate that you need.

However, on the ESP8266, the program hung on about one attempt in four or five.  So, the serial (or my USB-serial converter) is not bullet proof.

But on your problem, where the amount of serial output is much less than this test, I wonder if the interaction with handling network traffic might be causing problems?  I saw this on the Github page for the ESP8266 core:

Quote
Remember that there is a lot of code that needs to run on the chip besides the sketch when WiFi is connected. WiFi and TCP/IP libraries get a chance to handle any pending events each time the loop() function completes, OR when delay(...) is called. If you have a loop somewhere in your sketch that takes a lot of time (>50ms) without calling delay(), you might consider adding a call to delay function to keep the WiFi stack running smoothly.  There is also a yield() function which is equivalent to delay(0).
Might be worth testing with a call to yield() added into the for loop in your program.


Test program:

Code: [Select]
#define BLOCK_SIZE 500UL
#define NUM_BLOCKS 250UL

char buffer[BLOCK_SIZE] = {0};

void setup()
{
  Serial.begin(250000);

  for (uint16_t i = 0; i < BLOCK_SIZE; i++)
  {
    buffer[i] = (char)('A' + (i % 16));
  }

  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  uint32_t startTime = millis();

  for (uint8_t i = 0; i < NUM_BLOCKS; i++)
  {
    for (uint16_t j = 0; j < BLOCK_SIZE; j++)
    {
      Serial.write(buffer[j]);
    }
    Serial.println();
  }

  uint32_t endTime = millis();

  digitalWrite(13, LOW);

  Serial.print(endTime - startTime);
  Serial.print("ms for ");
  Serial.print(BLOCK_SIZE * NUM_BLOCKS);
  Serial.println(" bytes");
}

void loop()
{

}
Hackscribble.  Writing about making things
hackscribble@outlook.com

djred2000

That's strange that it works for you but not me. I was able to get it working by increasing the serial baud rate to 500,000.  There is now no latency when reading on the Mega and I am sending almost the entire packet.  I changed the code around slightly to do some verification that the data is E1.31.

Here is the code on the ESP:
Code: [Select]

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

char ssid[] = "SSID"; 
char pass[] = "PASSWORD";
WiFiUDP udp;
unsigned int localPort = 5568; 

#define E131_BUFFER_SIZE 638

unsigned char E131Buffer[E131_BUFFER_SIZE];

void setup()
{
  Serial.begin(500000); //500,000 bits per second to keep up with UDP data coming in
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }
  udp.begin(localPort);
}

//checks to see if packet is E1.31 data
int checkE131Headers(unsigned char* messagein, int messagelength)
{
  if ( messagein[1] == 0x10 && messagein[4] == 0x41 && messagein[12] == 0x37)
  { 
      int ChannelCount = messagein[123] * 256 + messagein[124]; // number of values plus start code
      return ChannelCount -1; //Return how many values are in the packet.
    }
  return 0;
}

void loop()
{
  int packetSize = udp.parsePacket(); //parse packet
  if(packetSize)
  {
    udp.read(E131Buffer,E131_BUFFER_SIZE); //read packet into buffer
   
    int count = checkE131Headers(E131Buffer,E131_BUFFER_SIZE); //pass data to see if it is E1.31
    if ( count ) //if it comes back genuine
    {
      for(int i = 4; i < E131_BUFFER_SIZE; i++)
     {
      Serial.write(E131Buffer[i]); //write bytes 4-638 in packet to serial port. 
     }
     Serial.flush(); //flush transmit buffer at end of for loop
    }
  }
} //end loop


My ultimate goal is to drive RGB LED pixels using the FastLED library so that is my next step in the project.  Thanks for your help!

houssem99

hi 

i want to extract a string from serial monitor who is  sent by the esp8266 module
came frome in application android

this is the exemple of the msg received

+IPD,0,120:GET /?|avance$ HTTP/1.1
Host: 192.168.1.10
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.0.1

i want to extact the word (''avance") 

can you please help me
and this is my code :

#include <SoftwareSerial.h>

SoftwareSerial ESP8266(10, 11);

String NomduReseauWifi = "Novel-Ti"; // Garder les guillements
String MotDePasse      = "CeciestlacleWPA2deNovelti"; // Garder les guillements
String voice;
/****************************************************************/
/*                             INIT                             */
/****************************************************************/
void setup()
{
  Serial.begin(9600);
  ESP8266.begin(9600); 
  initESP8266();
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
}
/****************************************************************/
/*                        BOUCLE INFINIE                        */
/****************************************************************/
void loop()
{
   while(ESP8266.available())
   {   
     Serial.println(ESP8266.readString());
   }   
}
/****************************************************************/
/*                Fonction qui initialise l'ESP8266             */
/****************************************************************/
void initESP8266()

  Serial.println("**********************************************************"); 
  Serial.println("**************** DEBUT DE L'INITIALISATION ***************");
  Serial.println("**********************************************************"); 
  //envoieAuESP8266("AT+RST");
  //recoitDuESP8266(2000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CWMODE=3");
  recoitDuESP8266(5000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIOBAUD=9600");
  recoitDuESP8266(10000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIFSR");
  recoitDuESP8266(1000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIPMUX=1");   
  recoitDuESP8266(1000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIPSERVER=1,80");
  recoitDuESP8266(1000);
  Serial.println("**********************************************************");
  Serial.println("***************** INITIALISATION TERMINEE ****************");
  Serial.println("**********************************************************");
  Serial.println(""); 
}

/****************************************************************/
/*        Fonction qui envoie une commande à l'ESP8266          */
/****************************************************************/
void envoieAuESP8266(String commande)

  ESP8266.println(commande);
}
/****************************************************************/
/*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/
/****************************************************************/
void recoitDuESP8266(const int timeout)
{
  String reponse = "";
  long int time = millis();
  while( (time+timeout) > millis())
  {
    while(ESP8266.available())
    {
      char c = ESP8266.read();
      reponse+=c;
    }
  }
  Serial.print(reponse);
  //------------------------------------------------
 
  while (Serial.available()){ //Check if there is an available byte to read
     delay(10); //Delay added to make thing stable
    char c = Serial.read(); //Conduct a serial read
   if (c=='|');
    voice += c; //Shorthand for voice = voice + c
    if (c == '$') {break;} //Exit the loop when the # is detected after the word
   
  }
  if (voice.length() > 0){
    if(voice == "avance"){
      Serial.print("done");
      digitalWrite(4,1);
      }
    else if(voice == "recule"){
      digitalWrite(5,1);
      delay(1000);
      digitalWrite(5,0);
      }
    else if(voice == "droite") {
      digitalWrite(6,1);
      delay(10000);
      digitalWrite(6,0);
    }
    else if(voice == "gauche") {
     digitalWrite(7,1);
     delay(1000);
      digitalWrite(7,0);
    }

   
  voice=""; //Reset the variable after initiating
  } 
}

houssem99

hi

i want to extract a string from serial monitor who is  sent by the esp8266 module
came frome in application android

this is the exemple of the msg received

+IPD,0,120:GET /?|avance$ HTTP/1.1
Host: 192.168.1.10
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.0.1

i want to extact the word (''avance") 

can you please help me
and this is my code :

#include <SoftwareSerial.h>

SoftwareSerial ESP8266(10, 11);

String NomduReseauWifi = "Novel-Ti"; // Garder les guillements
String MotDePasse      = "CeciestlacleWPA2deNovelti"; // Garder les guillements
String voice;
/****************************************************************/
/*                             INIT                             */
/****************************************************************/
void setup()
{
  Serial.begin(9600);
  ESP8266.begin(9600); 
  initESP8266();
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
}
/****************************************************************/
/*                        BOUCLE INFINIE                        */
/****************************************************************/
void loop()
{
   while(ESP8266.available())
   {   
     Serial.println(ESP8266.readString());
   }   
}
/****************************************************************/
/*                Fonction qui initialise l'ESP8266             */
/****************************************************************/
void initESP8266()

  Serial.println("**********************************************************"); 
  Serial.println("**************** DEBUT DE L'INITIALISATION ***************");
  Serial.println("**********************************************************"); 
  //envoieAuESP8266("AT+RST");
  //recoitDuESP8266(2000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CWMODE=3");
  recoitDuESP8266(5000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIOBAUD=9600");
  recoitDuESP8266(10000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIFSR");
  recoitDuESP8266(1000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIPMUX=1");   
  recoitDuESP8266(1000);
  Serial.println("**********************************************************");
  envoieAuESP8266("AT+CIPSERVER=1,80");
  recoitDuESP8266(1000);
  Serial.println("**********************************************************");
  Serial.println("***************** INITIALISATION TERMINEE ****************");
  Serial.println("**********************************************************");
  Serial.println(""); 
}

/****************************************************************/
/*        Fonction qui envoie une commande à l'ESP8266          */
/****************************************************************/
void envoieAuESP8266(String commande)

  ESP8266.println(commande);
}
/****************************************************************/
/*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/
/****************************************************************/
void recoitDuESP8266(const int timeout)
{
  String reponse = "";
  long int time = millis();
  while( (time+timeout) > millis())
  {
    while(ESP8266.available())
    {
      char c = ESP8266.read();
      reponse+=c;
    }
  }
  Serial.print(reponse);
  //------------------------------------------------
 
  while (Serial.available()){ //Check if there is an available byte to read
     delay(10); //Delay added to make thing stable
    char c = Serial.read(); //Conduct a serial read
   if (c=='|');
    voice += c; //Shorthand for voice = voice + c
    if (c == '$') {break;} //Exit the loop when the # is detected after the word
   
  }
  if (voice.length() > 0){
    if(voice == "avance"){
      Serial.print("done");
      digitalWrite(4,1);
      }
    else if(voice == "recule"){
      digitalWrite(5,1);
      delay(1000);
      digitalWrite(5,0);
      }
    else if(voice == "droite") {
      digitalWrite(6,1);
      delay(10000);
      digitalWrite(6,0);
    }
    else if(voice == "gauche") {
     digitalWrite(7,1);
     delay(1000);
      digitalWrite(7,0);
    }

   
  voice=""; //Reset the variable after initiating
  } 
}

Go Up