so I am working on a project where I have to connect an arduino to the raspberry pi through serial and then transfer the input from the raspi to another I2C device. So, just to test things out and learn the basic principle I did som research and wrote two scripts, one for arduino and one for raspi(in python):
Arduino one:
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
void sendData(){
char data = Serial.read();
if(data > 0) {
Serial.write('a');
Wire.write(data);
}
}
void setup() {
Serial.begin(115200);
Wire.begin(SLAVE_ADDRESS);
Wire.onRequest(sendData);
}
void loop() {
sendData;
}
Python:
import serial, time
arduino = serial.Serial('/dev/ttyACM0', 115200, timeout=.1)
arduino.flushInput()
arduino.flushOutput()
arduino.write('2')
while True:
try:
data = arduino.readline()
if data:
print data #strip out the new lines for now
# (better to do .read() in the long run for this reason
except:
arduino.close()
So, I have tested the code with the reqular arduino serial monitor and it works, it transfers the data into the i2c device correctly. However, when I connnect it to the raspberry pi, it works the first time I tun the code and I can see the final output on the i2c device's screen. But then, when I run the code again, it just doesn't work. I don't even see the "a" that it is supposed to print after successful communication. Than, when I plug it back into my laptop, run the monitor again and then connect it into the raspberry pi again, it does the dame thing. Thanks in advance!
PS: I tried just unplugging and plugging the usb cable back into the raspi, It doesn't work.