Int to string error (serial communication)

Hi everyone, I'm new around here.

Currently, i'm developing a small project as a thesis for a control/automation subject at class and I have to connect Arduino to RaspberryPi using RX/TX. The message I want to send to the Arduino contains 3-4 numbers (199,275,194...) Being the first number (1 or 2) the mode in which the control will function (Automatic or manual).

Therefore, I would like to receive the number and split the first number so I can see if its in auto or manual and then the rest of the number to make the control action. ej. I send from RaspberryPi 123 and on the Arduino I would like to split it in: 1 (auto) 23(ºC).

This is the code I wrote to do the split but i keep getting something like this: (I enter 123)
1
9
2
0
3
1
(with random numbers in between.) I have tried several ways of spliting but it gives me strange outputs on the serial monitor.
Should get:
1
23

int byteRead, a;
String str;

void setup() {
// Turn the Serial Protocol ON
Serial.begin(9600);
}

void loop() {
/* check if data has been sent from the computer: /
if (Serial.available()) {
/
read the most recent byte */
byteRead = Serial.read();
a=byteRead;
str=String(a);
Serial.write(byteRead);
Serial.write("\n");
Serial.write(str[1]);
Serial.write("\n");
}
}

Thanks for your help, and sorry for the noob question, just getting started with Arduino.

As a, byteRead are ints, the string library converts them to strings: '1' == 49 which converts to "49"
Use char types pass in characters.

Same goes for the serial monitor.
Also use print() to print strings, use write() to write raw data.

Also there is only 1 valid char in the String, you can only use str[0] as str[1] is the terminating null ( now, your code was reading the 9 of 49, the 0 of 50 and the 1 of 51 or '1', '2', '3'

Modified code:

int byteRead, a;
String str;

void setup() {                
// Turn the Serial Protocol ON
  Serial.begin(9600);
}

void loop() {
   /*  check if data has been sent from the computer: */
  if (Serial.available()) {
    /* read the most recent byte */
    byteRead = Serial.read();
    a=byteRead;
    str=String( ( char ) a);

    Serial.print(byteRead);
    Serial.print("\n");
    Serial.print( str[0]);
    Serial.print("\n");
 }
}

typing 123 returns:

49
1
50
2
51
3

Also your sketch can become.

void setup() {                
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    char byteRead = Serial.read();
    Serial.println(byteRead, DEC);
    Serial.println(byteRead);
 }
}

You should use char arrays instead of String objects.
Try to avoid Strings because they have some known ram problems

Your solution could be:

char incomingRawData[4];//I assume you only will pass 4 values like you say -> I send from RaspberryPi 178
uint8_t i = 0;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if(Serial.available() && i <= 2){
    incomingRawData[i++] = Serial.read();
  }
  if(i== 3){
  //It means you have all bytes already in the incomingRawData array
  //lets process it
  Serial.print("The node number is ");
  Serial.println(incomingRawData[0]);
  incomingRawData[3] = '\0'; 
  char * stringValue = &incomingRawData[1];
  Serial.print("The value passed is ");
  Serial.println(stringValue);
  //reset the i variable
  i = 0;
  }
}

Thanks a lot for your time, I think this solves my problem!

I'm having some issues with the answer you gave me.

1: if I get from RaspberryPi a value less than 10, I get no answer and it gets mixed with the next entry I give. I can't make it work with 2 integer numbers...

2: Also, I get the correct answer, but I cannot get the numbers and calculate other things with it. (Once y split the mode and the temperature, I need to use this number to make calculations)

Thanks for your help and time...

I would like to receive the number and split the first number so I can see if its in auto or manual and then the rest of the number to make the control action.

The below setup for controlling servos might be of interest. I put the numeric value first, followed by the servo identifier for the servo to be controlled, then a comma , as the data packet delimiter.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}