10

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関数が呼び出されたことを返します。

私はデータレベルシフターを使用していますが、説明には少し遅いかもしれないと書かれています。

誰かがこれを機能させましたか?

4

2 に答える 2

11

Arduino と Raspberry Pi の間で通信を開始することができました。この 2 つは、2 つの 5k プルアップ抵抗を使用して接続されています (このページを参照してください)。arduinoは、リクエストごとにi2cバスにバイトを書き込みます。Raspberry Pi では、hello1 秒ごとに出力されます。

Arduino コード:

#include <Wire.h>
#define SLAVE_ADDRESS 0x2A

void setup() {
    // initialize i2c as slave
    Wire.begin(SLAVE_ADDRESS);
    Wire.onRequest(sendData); 
}

void loop() {
}

char data[] = "hello";
int index = 0;

// callback for sending data
void sendData() { 
    Wire.write(data[index]);
    ++index;
    if (index >= 5) {
         index = 0;
    }
 }

Raspberry Pi 上の Python コード:

#!/usr/bin/python

import smbus
import time
bus = smbus.SMBus(1)
address = 0x2a

while True:
    data = ""
    for i in range(0, 5):
            data += chr(bus.read_byte(address));
    print data
    time.sleep(1);

私の Raspberry Pi では、i2c バスは 1 です。コマンドi2c-detect -y 0またはを使用してi2c-detect -y 1、Raspberry Pi が Arduino を検出するかどうかを確認します。

于 2013-01-26T01:23:18.063 に答える