I want to send data from RaspberryPi to Arduino UNO using UART. I tried sending my data 10 times to Arduino but it received it once. It reads as follows.
Connected to PC
pi: ÿpi: ÿpi: ÿ
1) I modified two files:
boot/cmdline.txt: to
dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
etc/inittab: to
T0:23:respawn:/sbin/getty -L ttyAMA0 9600 vt10
2) I connected Arduino to my PC using USB UART and to Rpi
pin 10(Arduino) to pin 8(Raspberry)
pin 11(Arduino) to pin 10(Raspberry)
This is my Arduino code to receive data over UART:
/*
Connects Arduino to Raspberry Pi
Arduino: SoftSerial
Raspberry Pi: GPIO UART
Just connect pin 10(Arduino) to pin 8(Raspberry)
pin 11(Arduino) to pin 10(Raspberry)
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("Connected to PC");
mySerial.begin(9600);
}
void loop()
{
// If data is available on Raspberry Pi, print it to PC
if (mySerial.available()){
Serial.print("pi: ");
Serial.write(mySerial.read());
}
}
3) On Raspberry i use python:
import RPi.GPIO as GPIO ## Import GPIO Library
import time ## Import 'time' library. Allows us to use 'sleep'
import sys, getopt
import serial
print('Dang thuc thi')
port = serial.Serial("/dev/ttyAMA0", baudrate=9600,timeout=1)
#port.write("\r\n" + args[0])
port.open();
a = 0;
while a<10:
port.write('data ' + str(a))
a=a+1
print('times: '+ str(a))
time.sleep(1.2)
port.close()
Where am I going wrong?