I²C を使用して、 Arduino UnoからRaspberry PiにI²Cインターフェイス経由でデータを送信しようとしています。これは私が使用したコードでした。
アルドゥイーノでは:
#include <Wire.h>
unsigned int watt;
unsigned int watt1;
byte watt2;
byte watt3;
void setup()
{
Wire.begin(30);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}
void loop() {
delay(100);
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
watt = sensorValue * (5 / 1023) * 2857;
watt1 = sensorValue1 * (5 / 1023) * 2857;
watt2 = map(watt, 0, 4294967295, 0, 255);
watt3 = map(watt1, 0, 4294967295, 0, 255);
Serial.println(watt2);
Serial.println(watt3);
}
void requestEvent()
{
Wire.write(watt2);
delay(30);
Wire.write(watt3);
}
ラズベリーパイでは:
import smbus
import time
bus = smbus.SMBus(0)
address = 0x1e
while (1):
watt=bus.read_byte_data(address,1)
watt2=bus.read_byte_data(address,2)
次のエラーを受け取りました。
トレースバック (最新の呼び出しが最後):
ファイル "/home/pi/i2ctest.py" 、8 行目、<module>
watt = bus.read_byte_data(address,1)
IOError: [Errno 5] 入力/出力エラー
これを修正するにはどうすればよいですか? また、SMBus ライブラリ以外に Raspberry Pi で I²C を使用するための代替手段はありますか?