Serial Communication - Arduino / Raspberry pi2

Hi everyone,

I have found a similar topic

http://forum.arduino.cc/index.php?topic=45076.0

but that is 6 years old so I thought to start a new post here.
I am trying to get an Arduino UNO to talk with a Raspberry pi2 through a serial interface.
At the moment I am just trying to light ON/OFF a LED, the final project will be to control 4 motors (DC brushless) from the wifi network (possibly from remote) and manage an IP camera which has been already working.

I am writing the code on top of my head so don`t shot me if you find errors, I will post the final code tonight from home.
I got the two boards to communicate but I have realized there are some problems with the synchronization.

raspberry pi2: (written in python, "lightON.py")
import serial
import time

ser = serial.Serial('/dev/ttyACM0',9600)

ser.flushInput()
while True:
print "send ON command"
ser.write("on\n")
read_serial=ser.readline()
if read_serial == ("on\r\n")
print "ack received"
break
time.sleep(1)
ser.close()

ARDUINO UNO:
//this is just the demo software I found online, with some adjustment to fit my case
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int pinLight=7; //pin to light the LED
int synch; //synch object

void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
pinMode(pinLight, OUTPUT);
}

void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString); //send a reply to raspberry
if (inputString=="on\n")
digitalWrite(pinLight, HIGH);
if (inputString=="off\n")
digitalWrite(pinLight, LOW);
// clear the string:
inputString = "";
stringComplete = false;
}
Serial.println(synch);
delay(1000);
}

/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}

Now, I have done some testings yesterday and the code is working OK, I found some issues and was wondering if anyone could help in understanding what is causing them.
I would like to program a sort of driver to communicate between Arduino and Raspberry pi2 but I need first to understand if this serial communication is suitable for this type of communication.
NOTE: Arduino UNO is already communicating through I2C with an extra MEGA2560 so I don`t know if this would be a better choice.

Issues:

  1. Synch object.
    It looks like the whole communication need the synch object triggered from the Arduino every second. If I take off that the code does not work.
    Once I run the code:
    sudo python lightON.py
    The raspberry send print the "send ON command" but nothing after that.
    If I use the synch object, which only stream a char "0" every second, the whole thing works fine.

  2. Latency
    It looks like the response time of arduino changes, sometime the LED lights up after two commands, sometime after three/four commands.
    I guess this is because the sleeping time and the synch between the two codes?
    Is there any way to fix this?
    I would like to send just one command and not keep streaming the command until Arduino reply, I don`t like it.

Serial.serialEvent() is not an interrupt but processed just after the loop(), the serial interface has an interrupt in order to store all data in a buffer as soon as they are received.
This should mean that even is Arduino is in the loop() when the "on\n" command is received, as soon as the serialEvent() is processed, it should find the data in the buffer and turn the LED ON.
So, the latency should be at the maximum of 1s+processing time.

Could the variable latency be related with a flushing issue with the buffer?
Am I missing anything here and I should probably try to find other solutions for the communication?
Thanks for your thoughts.

Claudio

Serial communication doesn't change. The tutorial is just fine!

Yes, there are not answers to my questions though!

Have a look at this Python - Arduino demo

and at Serial Input Basics which was written more recently.

...R

Hi Robin2,
I haven`t read the Python - Arduino demo completely but this seems to be related with using python to program arduino.

The second topic Serial Input Basics is something similar like what I am trying to do but it still does not answer my questions.
I think the problem was related to the fact that the serial communication has not any mechanism for anticollision during transmission.
This means that each device will stream the data as soon as the command is triggered and if the other device is transmitting at the same time there will be a collision.
This is the reason why a proper handshaking has to be done, so I can make sure only one device is transmitting at the time.

I have fixed the issue now and the turn ON/OFF works straight forward in just one command every time.
Thank you very much for your help on this.

Claudio_Baldo:
I haven`t read the Python - Arduino demo completely but this seems to be related with using python to program arduino.

No. It is only concerned with communication.

Yes, handshaking can be necessary - more so, perhaps to prevent the Arduino being flooded with data. In another project I have a system where the PC only sends a message when requested by the Arduino.

...R