Hello,
I have trouble with I2C communication between RaspberryPi and Arduino (tryed nano and mega).
RPi is master and Arduino is slave.
When I start my python script in Raspberry it works only for few minutes (sometimes hours). When Arduino stopped last received character is strange(wrong/different).
Thanks to everybody for any idea.
RPi python program:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import smbus
import time, datetime
bus = smbus.SMBus(1)
address = 0x2a
errorcounter = 0;
i = 0;
def toWrite(a):
time.sleep(1);
for i in a:
bus.write_byte(address, i)
while True:
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S");
print "Timestamp: |%s" %timestamp;
print "Write byte:"
print datetime.datetime.now().strftime("%m-%d %H:%M:%S!");
print map(ord, datetime.datetime.now().strftime("%m-%d %H:%M:%S!"))
toWrite(map(ord, datetime.datetime.now().strftime("ABCDEFGHIJKLMNOPQRSTUVxyz%m-%d %H:%M:%S!")))
time.sleep(1)
Arduino code:
#include <Wire.h>
#define SLAVE_ADDRESS 0x2A
char command;
void setup() {
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
Wire.onRequest(sendData);
Wire.onReceive(readData);
Serial.begin(9600);
}
void loop() {
}
int index = 0;
// callback for sending data
void sendData() {
}
// callback for receiving data.
void readData(int numbytes) {
// loop through all but the last
char c = Wire.read(); //Convert byte to char.
command = c; //Convert each character into a string
Serial.print(command);
if(command == '!'){
Serial.println(' ');
}
}