xbee text transfer Mega

Hi

I am trying to transmit text character by character per xbee from one Mega to another via Serial.
First, characters are written in the Serial monitor or the Sender and read by the receiving board following certain push botton commands I give. Then, I set a certain switch which makes the sender open a text file stored on a micro SD card.

Generally, this works but the text file is not interpreted to the end but the process is interrupted and text file interpretation is started from the beginning.

I intend to set four stepper motors “to zero” by hand and later have them read and follow gcode commands stored in the text file.
I attach both arduino code files for the sender and the receiver. Please note the code parts interpreting gcode commands originate from marginalllyclever.com. I just use but did not develop it.

Help is appreciated concerning the question why the text transmission process is interrupted and started again.

Thanks

comsend20160818.ino (3.74 KB)

receive20160723.ino (10 KB)

Hi geebee, i am working in a project which i use an arduino mega and arduino nano, the code works for sending commands with serial.write, and some buffering thing a guy made in this code i dont understand very well but it worked for me, i will place it here, i havent look your code yet but this helped me
This is the slave code, very simple…

// Programa : Envio de texto utilizando Xbee S1
// Autor : Arduino e Cia

// Carrega a biblioteca LiquidCrystal

   
// Define os pinos que serão utilizados para ligação ao display  

const int ledPin = 13;
int incomingByte = 0;
char buf[20];     //Tamanho da mensagem - 20 caracteres
int num = 0;
String dados;

void setup()
{
  Serial.begin(9600);
  Serial.println("NANO ESTATION 1.0");
  Serial.println("\n");
  Serial.println();
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  while(Serial.available() > 0)
  {
    // Armazena os caracteres no buffer
    buf[num] = Serial.read();
    // Verifica se foi recebido o caracter de nova linha
    if (buf[num] == '\n') 
    {
      String dados = buf;
      dados.trim();
      int tamanho_string = dados.length();
      
       if (dados == "ledon") {
      digitalWrite(ledPin, HIGH);
      Serial.println("lednano1on");
      Serial.println("\n");
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (dados == "ledoff") {
      digitalWrite(ledPin, LOW);
      Serial.println("lednano1off");
      Serial.println("\n");
    }

      // Apaga o buffer para receber novo texto
      for (int ca =0; ca<20; ca++)
      {
        buf[ca]=0;
      }
      num=0;
      break;
    }
    num++;
  }
}

This worked, it receives commands when i do serial.print in the mega and does turn on the led…
It helped me for the Slave code, my problem is for the Master which is arduino mega +ethernet module with a page that controls what is sended by the serial.print , i can send the comand to the arduino nano, but arduino mega does not read yet, idk why i am looking for it yet… i think it is cause the ethernet module with the page running everytime… idk

This is also the master code if you wanna help me

//zoomkat 8-04-12
//simple button GET server code to control servo and arduino pins 5, 6, and 7


#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 102 }; // ip in lan
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString; 

//////////////////////
int conteudo = 1;
int lednano1 = 1;
void setup(){

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.begin(9600);
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  //enable serial data print 
 
  Serial.println("MASTER ONLINE"); // so I can keep track of what is loaded
}

void loop(){


  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          /////////////// parte fixa do codigo nao se altera
         
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");

          client.println(" Refresh: 1 ");

          client.println();
          client.println("<html><head><meta http-equiv='Content-Type' content='text/html; charset=windows-1252'>");
          client.println("<TITLE>NOVA AUTOMA&Ccedil;&Atilde;O</TITLE>");
          client.println("<style>");
          
          client.println("H1 { text-align:center; font-family: Cambria, Hoefler Text, Liberation Serif, Times, Times New Roman, serif;}");
          
          client.println("header {background: #979797;color:white;}");
          client.println(".flex-container > * {padding: 15px;-webkit-flex: 1 100%;flex: 1 100%;}");          
          client.println("H4 { text-align:center;}");
          client.println(".h4 {font-family: Cambria, Hoefler Text, Liberation Serif, Times, Times New Roman, serif;}    ");                
          client.println(".nav {background:#666666;text-align:center;-webkit-flex: 1 auto;flex:1 auto;-webkit-order:1;order:1;}");
          client.println(".nav2 {background:#eee;text-align:center;-webkit-flex: 1 auto;flex:1 auto;-webkit-order:1;order:1;}");
          client.println(".content {font-family: Cambria, Hoefler Text, Liberation Serif, Times, Times New Roman, serif;background:#ffffff;text-align:center;-webkit-flex: 1 auto;flex:1 auto;-webkit-order:1;order:1;}");

                              
          client.println("</style>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<div class='flex-container'>");
          client.println("<HEADER>");
          client.println("<h1 class='h1'>NOVA AUTOMA&Ccedil;&Atilde;O</h1>");
          client.println("</HEADER>");        
          client.println("<nav class='nav'>"); 

          // botoes
          client.print("<input type=submit value='LUZES' style='width:140px; height:45; -webkit-border-radius: 7;  -moz-border-radius: 7;  border-radius: 7px;  font-family: Arial;  color: #ffffff;  font-size: 20px;  background: #88b2b3;  padding: 7px 20px 10px 20px;  border: solid #7d7d7d 3px; text-decoration: none;'  onClick=location.href='/?on2'>");
          client.print("&nbsp;<input type=submit value='CONTROLES' style='width:170px; height:45; -webkit-border-radius: 7;  -moz-border-radius: 7;  border-radius: 7px;  font-family: Arial;  color: #ffffff;  font-size: 20px;  background: #88b2b3;  padding: 7px 20px 10px 20px;  border: solid #7d7d7d 3px; text-decoration: none;' onClick=location.href='/?off3'>");
          client.print("&nbsp;<input type=submit value='SEGURAN&Ccedil;A' style='width:140px; height:45; -webkit-border-radius: 7;  -moz-border-radius: 7;  border-radius: 7px;  font-family: Arial;  color: #ffffff;  font-size: 20px;  background: #88b2b3;  padding: 7px 20px 10px 20px;  border: solid #7d7d7d 3px; text-decoration: none;' onClick=location.href='/?seguranca778'>");

          client.println("<nav2 class='nav2'>");
          client.println("<p>&nbsp;</p>");
         if (conteudo==1){
         if (lednano1==1){
         client.print("<input type=submit value='ON' style='width:100px; height:30; -webkit-border-radius: 7;  -moz-border-radius: 7;  border-radius: 7px;  font-family: Arial;  color: #ffffff;  font-size: 13px;  background: #007F00;  padding: 7px 20px 10px 20px;  border: solid #007F00 3px; text-decoration: none;'  onClick=history.go(0) onClick=location.href='/?off5'>");  
         }
         if (lednano1==2){
         client.print("<input type=submit value='OFF' style='width:100px; height:30; -webkit-border-radius: 7;  -moz-border-radius: 7;  border-radius: 7px;  font-family: Arial;  color: #ffffff;  font-size: 13px;  background: #7F0000;  padding: 7px 20px 10px 20px;  border: solid #7F0000 3px; text-decoration: none;' onClick=history.go(0) onClick=location.href='/?on4'>>");
         }
         }
         else if (conteudo==2)
         {
         client.println("<p>CONTROLES</p>");  
         }
         else if(conteudo==3)
         {
         client.println("<p>SEGURAN&Ccedil;A</p>");  
         }
                   client.println("</nav>");
          client.println("<p>&nbsp;</p>");
          client.println("</content>");
          client.println("</div>");          
          
          client.println("</BODY>");
          client.println("</HTML>");
        
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf('2') >0)//checks for 2
          { 
          Serial.println("\n");
          conteudo = 1;       
          }
          if(readString.indexOf('3') >0)//checks for 3
          {            
          Serial.println("\n");   
          conteudo = 2;  
          }
           if(readString.indexOf("778") >0)//checks for 3
          {            
          Serial.println("\n");   
          conteudo = 3;  
          }
          if(readString.indexOf('4') >0)//checks for 2
          { 
          Serial.println("ledon");
          Serial.println("\n");
          lednano1 = 1;
          }
          if(readString.indexOf('5') >0)//checks for 2
          { 
          Serial.println("ledoff");
          Serial.println("\n");
          lednano1 = 2;
          }
        
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}