[SOLVED]Recieve more than one byte with UART

Hello.

In my actual project i try to implement a serial communication betwen the ESP8266-01 and an Atmega32. The code for the Atmega is written with the Atmel Studio like i program all my controllers, but on the ESP i want to use the Blynk API, that is written for the Arduino IDE only, so right now i feel like a beginner again.
And as the beginner i am at arduino i have a beginner problem with the serial communication.

I want to send data frames with variable sizes from the Atmega to the ESP and the other way round. But the ESP always have just one sign in the serial buffer and that is always the end sign 0x0D.

Here you see my code where i gather the serial datas that are comming in.

  while (Serial.available()) {
    char inChar = Serial.read();
    RX_Data[i] = inChar;
    
    i++;
    
    if (RX_Data[i - 1] == 13 || i >= MAX_FRAME_LEN) 
    {      
      DoSerialData();
      i = 0;
      memset(&RX_Data, 0, sizeof(RX_Data));
      continue;
    }
  }

The variable RX_Data is a char type, has the size of 140 chars and is global defined. MAX_FRAME_LEN is 140 too.

If i change the while condition to Serial.available() > 10 i get the same result as always.
Frames i send are like this: 1;WiFi;pass;1234567890[0x0D]
[0x0D] is the mentioned end sign.

I send the same datas with the Atmega and directly with my PC via USB<->UART adaptor and get also the same result.

I hope you can help me.

The examples inSerial Input Basics illustrate simple reliable ways to receive data.

...R

I already found your examples, sorry i didnt said that and i found out that my code is the same as your second code to get more than one sign. I got my code from here.

In the mean time i changed the serial buffer from 64 bytes to 145 to fit in the possible maximum of data.

The link in Reply #2 is to SerialEvent(). You certainly did not get that from my Serial Input Basics. And why is explained in my link.

If you want help please post your complete program.

...R

I just used the given routine to gather the bytes from that example. But here is my whole program.

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example runs directly on ESP8266 chip.
 *
 * You need to install this for ESP8266 development:
 *   https://github.com/esp8266/Arduino
 * 
 * Please be sure to select hte right ESP8266 module
 * in the Tools -> Board menu!
 *
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 *
 **************************************************************/

 
//#########################################################
//Defines
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define SSID_LEN 33
#define PW_LEN   64
#define AUTH_LEN 33
#define MAX_FRAME_LEN SSID_LEN + PW_LEN + AUTH_LEN + 10

#define DEBUG_OUT Serial

//#########################################################
//Bibliotheken
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>


//#########################################################
//Globale Variablen
char auth[] = "32 byte auth code";
char RX_Auth[AUTH_LEN] = "";
char RX_SSID[SSID_LEN] = "";
char RX_PW[PW_LEN] = "";

SimpleTimer timer;

char TermText[] = "";
char RX_Data[MAX_FRAME_LEN] = "";
WidgetTerminal Term(0);
WidgetLED led1(1);

//#########################################################
// Prototypen

void DoSerialData(void);
void connectLED();

//#########################################################
//Programm

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "WiFi Name", "password");
  timer.setInterval(1000L, connectLED);
  
  DEBUG_OUT.print("\n\nStart");
  DEBUG_OUT.flush();
}


void loop()
{
  byte i = 0;
  
  timer.run();
  //if (Blynk.connected())
  //{
    Blynk.run();
  //}
  
  while (Serial.available()) {
    
    RX_Data[i] = Serial.read();
    
    i++;
    
    if (RX_Data[i - 1] == 13 || i >= MAX_FRAME_LEN) 
    {
      DEBUG_OUT.print("i: ");
      DEBUG_OUT.print(i);
      DEBUG_OUT.print('\n');

      for(byte j = 0; j < i; j++)
      {
        DEBUG_OUT.print(j);
        DEBUG_OUT.print(": ");
        DEBUG_OUT.print(RX_Data[j]);
        DEBUG_OUT.print('\n');
      }
      
      DoSerialData();
      DEBUG_OUT.flush();
      i = 0;
      memset(&RX_Data, 0, sizeof(RX_Data));
      continue;
    }
  }
}




//#########################################################
//Zusatzfunktionen
void DoSerialData()
{
  byte mID = RX_Data[0] - '0';
  byte i = 0;
  byte j = 2; //Gleich die ersten zwei Zeichen überspringen

  DEBUG_OUT.print("\nNeue Daten\n");
  
  if (RX_Data[1] != ';') 
  {
    DEBUG_OUT.print("Falsches Zeichen: ");

    do
    {
      DEBUG_OUT.print(RX_Data[i]);
      i++;
    }while(RX_Data[i] != 0);
    
    return; //Falsch codiert
  }
   j++; //Trennzeichen überspringen
  
  switch(mID)
  {
    case 1: 
        DEBUG_OUT.print("mID = 1\n");
        
        do //SSID Auslesen
        {
          RX_SSID[i] = RX_Data[j]; 
          j++;
          i++;  
        }while(RX_Data[j] != ';' && i < SSID_LEN); //Solange Daten vorhanden. Duch \n abgeschlossen

        DEBUG_OUT.print("RX_Data: ");
        DEBUG_OUT.print(RX_Data);
        
        j++; // Das ; überspringen
        i = 0;
        
        do //Passwort auslesen 
        {
          RX_PW[i] = RX_Data[j];
          i++; 
          j++; 
        }while(RX_Data[j] != ';' && i < PW_LEN); //Solange Daten vorhanden. Duch \n abgeschlossen

        DEBUG_OUT.print("\nRX_PW: ");
        DEBUG_OUT.print(RX_PW);

        j++; //Das ; überspringen
        i = 0;
        
        do //Authentifizierungs Code auslesen
        {
          RX_Auth[i] = RX_Data[j];
          i++;  
          j++;
        }while(RX_Data[j] != 13 && i < AUTH_LEN); //Solange Daten vorhanden. Duch \n abgeschlossen

        DEBUG_OUT.print("\nRX_Auth: ");
        DEBUG_OUT.print(RX_Auth);
        DEBUG_OUT.print("\n");
        //Blynk.begin(RX_Auth, RX_SSID, RX_PW); 
        sprintf(TermText, "SSID: %s\n", RX_SSID);
        DEBUG_OUT.print(TermText); 
        
        sprintf(TermText, "PW: %s\n", RX_PW);
        DEBUG_OUT.print(TermText); 
        
        sprintf(TermText, "Auth: %s\n", RX_Auth);
        DEBUG_OUT.print(TermText); 
        //Serial.print('\n'); //Verbindung bestätigen
    break;

    default:
      DEBUG_OUT.print("Default erreicht. Gesendete ID ");
      DEBUG_OUT.print(RX_Data[0]);
    break;
    
  }


}

void connectLED()
{
  if (led1.getValue()) {
    led1.off();
  } else {
    led1.on();
  }
}

I can only refer you back to Reply #1

...R

So I figured out the bug. The count variable i, defined in the main loop needs to be defined as global. I think everytime the loop starts again it reinitialises the count variable or so, i dont know exactly why it is working now.

I also changed the code a bit so the loop isn't stucked when the transmission needs a bit more time than normal.

Here is now my complete code for everyone who have a similar problem.

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example runs directly on ESP8266 chip.
 *
 * You need to install this for ESP8266 development:
 *   https://github.com/esp8266/Arduino
 * 
 * Please be sure to select hte right ESP8266 module
 * in the Tools -> Board menu!
 *
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 *
 **************************************************************/

 
//#########################################################
//Defines
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define SSID_LEN 33
#define PW_LEN   64
#define AUTH_LEN 33
#define MAX_FRAME_LEN SSID_LEN + PW_LEN + AUTH_LEN + 10

#define DEBUG_OUT Serial

//#########################################################
//Bibliotheken
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>


//#########################################################
//Globale Variablen
char auth[] = "9205b5fa01b5461d83ff7d3ed143ac04";
char RX_Auth[AUTH_LEN] = "";
char RX_SSID[SSID_LEN] = "";
char RX_PW[PW_LEN] = "";

SimpleTimer timer;

byte i = 0;
char TermText[] = "";
char RX_Data[MAX_FRAME_LEN] = "";
WidgetTerminal Term(0);
WidgetLED led1(1);

//#########################################################
// Prototypen

void DoSerialData(void);
void connectLED();

//#########################################################
//Programm

void setup()
{
  Serial.begin(9600);
  //Blynk.begin(auth, "devolo-L", "anja2552");
  timer.setInterval(1000L, connectLED);
  
  DEBUG_OUT.print("\n\nStart");
  DEBUG_OUT.flush();
}


void loop()
{
  
  
  timer.run();
  //Blynk.run();
  
  if(Serial.available()) {
    
    RX_Data[i] = Serial.read();
    DEBUG_OUT.print(RX_Data[i]);
    if (RX_Data[i] == 13 || i >= MAX_FRAME_LEN) 
    {
      DEBUG_OUT.print("Zeichen Gesamt: ");
      DEBUG_OUT.print(i+1);
      DEBUG_OUT.print('\n');

      DEBUG_OUT.print("Einzelner Inhalt\n");
      for(byte j = 0; j < i+1; j++)
      {
        DEBUG_OUT.print(j);
        DEBUG_OUT.print(": ");
        DEBUG_OUT.print(RX_Data[j], HEX);
        DEBUG_OUT.print('\n');
      }
    
      DoSerialData();
      DEBUG_OUT.flush();
      i = 0;
      memset(&RX_Data, 0, sizeof(RX_Data));
      
    }

    i++;
  }
}




//#########################################################
//Zusatzfunktionen
void DoSerialData()
{
  byte mID = RX_Data[0] - '0';
  byte i = 0;
  byte j = 2; //Gleich die ersten zwei Zeichen überspringen

  DEBUG_OUT.print("\nNeue Daten\n");
  
  if (RX_Data[1] != ';') 
  {
    DEBUG_OUT.print("Falsches Zeichen: ");

    do
    {
      DEBUG_OUT.print(RX_Data[i]);
      i++;
    }while(RX_Data[i] != 0);
    
    return; //Falsch codiert
  }
   j++; //Trennzeichen überspringen
  
  switch(mID)
  {
    case 1: 
        DEBUG_OUT.print("mID = 1\n");
        
        do //SSID Auslesen
        {
          RX_SSID[i] = RX_Data[j]; 
          j++;
          i++;  
        }while(RX_Data[j] != ';' && i < SSID_LEN); //Solange Daten vorhanden. Duch \n abgeschlossen

        DEBUG_OUT.print("RX_Data: ");
        DEBUG_OUT.print(RX_Data);
        
        j++; // Das ; überspringen
        i = 0;
        
        do //Passwort auslesen 
        {
          RX_PW[i] = RX_Data[j];
          i++; 
          j++; 
        }while(RX_Data[j] != ';' && i < PW_LEN); //Solange Daten vorhanden. Duch \n abgeschlossen

        DEBUG_OUT.print("\nRX_PW: ");
        DEBUG_OUT.print(RX_PW);

        j++; //Das ; überspringen
        i = 0;
        
        do //Authentifizierungs Code auslesen
        {
          RX_Auth[i] = RX_Data[j];
          i++;  
          j++;
        }while(RX_Data[j] != 13 && i < AUTH_LEN); //Solange Daten vorhanden. Duch \n abgeschlossen

        DEBUG_OUT.print("\nRX_Auth: ");
        DEBUG_OUT.print(RX_Auth);
        DEBUG_OUT.print("\n");
        //Blynk.begin(RX_Auth, RX_SSID, RX_PW); 
        sprintf(TermText, "SSID: %s\n", RX_SSID);
        DEBUG_OUT.print(TermText); 
        
        sprintf(TermText, "PW: %s\n", RX_PW);
        DEBUG_OUT.print(TermText); 
        
        sprintf(TermText, "Auth: %s\n", RX_Auth);
        DEBUG_OUT.print(TermText); 
        //Serial.print('\n'); //Verbindung bestätigen
    break;

    default:
      DEBUG_OUT.print("Default erreicht. Gesendete ID ");
      DEBUG_OUT.print(RX_Data[0]);
    break;
    
  }


}

void connectLED()
{
  if (led1.getValue()) {
    led1.off();
  } else {
    led1.on();
  }
}