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:
-
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. -
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