Ardunio not getting string over serial

Hello.
I have been working on getting the Ardunio to revieve a string over serial from a raspberry pi using TX/RX

Im not good with eletrionics but here is the basic wireing

ArdTX -> piRX
piTX -> 1.4kohm resistor -> ArdRX
/
3.3kohm resistor
/
piGND <- ->ArdGND

the ardunio code

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

void loop() {
  String msg = "";
  if(Serial1.available() > 0)
  {
    while(Serial1.available() > 0)
    {
      msg += char(Serial1.read());
      Serial.print(msg);
      delay(100);
    }
    Keyboard.print(msg);
  }
}

pi code

import serial
import time

ser = serial.Serial(
    port = '/dev/ttyAMA0',
    baudrate = 9600,
    parity = serial.PARITY_NONE,
    stopbits = serial.STOPBITS_ONE,
    bytesize = serial.EIGHTBITS,
    timeout = 1
)

while True:
    msg = "send message"
    print (msg)
    ser.write('msg')
    time.sleep(5)

The problem is the Ardunio is not recieveing the message, either that or the pi is not sending it.
Have gone through all the steps setting up the pi to send via serial by removing console output to serial.

is it because im sending more than 8 bits ?

Why do you think you are sending more than 8 bits ?

Are you using a Leonardo ?

Try one of the examples in serial input basics for receiving your data. It would be best to use the 3rd example and get your Python program to send ""

I suggest you leave the Keyboard() stuff out until you get the communication working.

...R

I am using a Ardunio Pro Micro (was suggested on this forum because of its HID compatibility).

Im also asking on the Pi forums about the pi code and setup and they say its fine.

and i assume 8 bits is 01010101 (binary) which is equle to a single character, not 100% sure on how serial works but im sending an entire string across "msg" which would be 24 bits. either that or i missed a memo on how bits work...

I did some test on Serial.print however that was on the USB serial port not the RX/TX serial port, which was referred to as Serial1 if im not mistaken, which i could not test without using a pi.

Why do you have resistors between the PI and the Arduino? ASCII art sucks. Post a fritzing diagram or a hand-drawn schematic.

http://blog.oscarliang.net/ctt/uploads/2013/05/arduino-raspberry-pi-serial-connect-schematics.jpg

Thats the same diagram i used just for a Ardunio Pro Micro.

Ok i fixed some code, bug tested some stuff, threw in a magic spell and got it to send over serial (yey)

Raspberry Pi is set to send '1' over serial every few seconds.
Ardunio reads '1' and translates it as ÿ ....

So the serial connection is now working just the imput-output is wrong.

Ardunio reads '1' and translates it as ÿ ....

Nonsense. The ÿ is what you get when you print what you read from the serial port, when there was nothing to read.

now im writing "12345" to the serial and the ardunio is reading "24940"

Im completely lost ...

and i read about that right after i posed that message, sorry.

void loop() {
    if(Serial1.available() > 0)
  {
    while(Serial1.available() > 0)
    {
    String msg = "";
    msg +=char(Serial1.read());
    Serial.println(msg);
    delay(1000);
    }
   }
   else
   {
    Serial.println('no bytes in Serial');
   }
}

pi

ser=serial.Serial("/dev/ttyAMA0", 9600, timeout=1)
ser.open

while True:
    ser.write("12345")
    time.sleep(0.2)
ser.close()

AceScottie:
now im writing "12345" to the serial and the ardunio is reading "24940"

Im completely lost ...

    Serial.println('no bytes in Serial');

This was covered in another thread of yours.

Use double quotes!

void loop() {
    if(Serial1.available() > 0)
  {
    while(Serial1.available() > 0)
...
   else
   {
    Serial.println('no bytes in Serial');
   }
}

This is just going to spam "no bytes in Serial" to the serial port, once you fix up those quotes. I wouldn't be doing that.

    String msg = "";
    msg +=char(Serial1.read());
    Serial.println(msg);

What does all that String stuff achieve? How about:

    Serial.println(Serial1.read());

the only reason i put the else statement there was so i could get an output saying nothing is happening.
If its not there i only ever get a black space.

Edited it as per you suggested however still no change at all, just blank.

And yes i get confused between " and ' as they mean different things in different languages.

new code

    if(Serial1.available() > 0)
  {
    while(Serial1.available() > 0)
    {
    Serial.println(Serial1.read());
    delay(1000);
    }
   }

AceScottie:
now im writing "12345" to the serial and the ardunio is reading "24940"

Im completely lost ...

I guess you did not try the examples I linked to in Reply #1

...R

Ok some success, I didnt notice last nigh (being about 2-3am) but one of the solderd wires had come loose on the pi TX - ard RX pins and was probably why it wasnt working very well.

So resoldured it today (myself this time) and hurah its working ... ish

so first of all 2 things

"Serial.println(Serial1.read());" just prints a value not a string eg:
109
115
103

just repeting that every time

And Robin2, I did look at the examples. Most of it i did not understand, and quite a lot of it is stuff i dont need.
I came up with the following code hopefully stripping out the stuff i didnt need (this is suppose to be 1 way communication)

const byte numChars = 32;
char receivedChars[numChars];


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

}

void loop() {
  String msg = "";
  char endMarker = '\n';
  char rc;
  if(Serial1.available() > 0)
  {
    while(Serial1.available() > 0)
    {
      static byte ndx = 0;
      rc = Serial1.read();
      rc = Serial.read();

      if (rc != endMarker) {
       receivedChars[ndx] = rc;
       ndx++;
          if (ndx >= numChars) {
          ndx = numChars - 1;
          Serial.println(ndx);
          }
      }
    }
  }
  
}

but as with before it just came up with a random number:
31
31
31

my python code is set to sleep for 1 second after writing. When the Ardunio recieves the numbers its in sets of 3 followed by a 1 second pause then repetes.

Simple serial test code that will capture an incoming string of bytes on the arduino rx pin and echo them back out the tx pin.

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}

zoomkat:
Simple serial test code that will capture an incoming string of bytes on the arduino rx pin and echo them back out the tx pin.

//zoomkat 6-29-14 Simple serial echo test

//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String
    readString="";
  }
}

Thank you, this worked fine with a little adjustment in setting Serial to Serial1.

I was putting
if (Serilal1.availble) {
while (serial1.availble) {
read
print
}
{

so i now know why it was printing them on separate lines ( as it was printing 1 character at a time as the came in)

AceScottie:
And Robin2, I did look at the examples. Most of it i did not understand, and quite a lot of it is stuff i dont need.

It is time to say you don't need stuff after you have a system working reliably, not before.

Try one or more of the examples. They do work :slight_smile:

...R

    delay(2);  //slow looping to allow buffer to fill with next character

There must be a way of coding this without a delay, musn't there?

You would laugh if people did this, right?

int a = b + 3;
delay (2);   // allow CPU time to finish the addition

There must be a way of coding this without a delay, musn't there?

I'm sad you don't understand the code as written and under the conditions it is being used for demonstration purposes.

You would laugh if people did this, right?

I chuckle at people who appear to be jealous of simple code that that is somewhat effective in answering questions.

I'm sad you don't understand the code as written

I'm sad that you keep defending that crappy code.