Pythonsmbusモジュールを使用してArduinoUNOからRaspberryPiにデータを読み取ろうとしています。smbusモジュールで見つけた唯一のドキュメントはここにありました。モジュールでcmdが何を意味するのかわかりません。書き込みを使用して、Arduinoにデータを送信できます。私は2つの簡単なプログラムを作成しました。1つは読み取り用、もう1つは書き込み用です。
書き込み用のもの
import smbus
b = smbus.SMBus(0)
while (0==0):
var = input("Value to Write:")
b.write_byte_data(0x10,0x00,int(var))
読むためのもの
import smbus
bus = smbus.SMBus(0)
var = bus.read_byte_data(0x10,0x00)
print(var)
Arduinoコードは
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(8,9,4,5,6,7);
int a = 7;
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
// define slave address (0x2A = 42)
#define SLAVE_ADDRESS 0x10
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
}
void loop(){
}
// callback for received data
void receiveData(int byteCount)
{
Serial.println(byteCount);
for (int i=0;i <= byteCount;i++){
char c = Wire.read();
Serial.println(c);
}
}
// callback for sending data
void sendData()
{
Wire.write(67);
lcd.println("Send Data");
}
読み取りプログラムを実行すると、毎回「33」が返されます。Arduinoは、sendData関数が呼び出されたことを返します。
私はデータレベルシフターを使用していますが、説明には少し遅いかもしれないと書かれています。
誰かがこれを機能させましたか?