we are trying to control our rover via wifi. the idea is motor drivers are connected to arduino mega and the mega is connected to raspberry pi 2 (raspbian os) via USB. raspberry pi is receiving command via wifi from another computer, its sending the command to arduino via USB and arduino is controlling the motors accordingly. but the problem is, after running for some times. the system starts to lag, I mean there are delay in data receiving and so transmitting to arduino. All the control command data(1 byte) we are sending is eventually received, but with a 1 or 2 secs lag. however without USB connection to Arduino system works perfectly without any kind of lagging. so we are sure there are no problems in the network, and the problem is in the USB connection to arduino.
the following code is running on raspberry pi
import serial
import socket
ip = '192.168.2.3'
port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
ser = serial.Serial('/dev/ttyACM0', 115200, timeout=0.5)
while True:
data, addr = sock.recvfrom(20)
print data
ser.write(data)
ser.flush()
and the following code is running on arduino
//Motor driver pin declaration
//....
void setup() {
Serial.begin(115200);
//Motor driver pin initialization
//...
}
void loop() {
if(Serial.available() > 0) {
while(Serial.available() > 0) { //Clearing the serial buffer
int rdBytes = Serial.read();
//motor controlling logic
//...
}
}
}