Я использую датчик потока Sensirion SFM3300 и могу прочитать правильные значения с помощью Arduino с помощью следующего кода (I2 C):
#include <Wire.h>
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(115200);
Wire.beginTransmission(byte(0x40));
Wire.write(byte(0x10));
Wire.write(byte(0x00));
Wire.endTransmission();
}
void loop() {
// put your main code here, to run repeatedly:
delay(100);
Wire.requestFrom(0x40,2);
uint16_t a = Wire.read();
uint8_t b = Wire.read();
a = (a<<8) | b;
float flow = ((float)a - 32768) / 120;
Serial.println(flow);
}
Но используя Raspberry Pi, я написал почти тот же код, надеясь, что он также будет работать. Это код:
from smbus2 import SMBus
import time
import numpy as np
address=0x40
bus = SMBus(1)
def write(value):
bus.write_byte(address,value)
write(0x10)
write(0x00)
while True:
time.sleep(0.1)
a = np.uint16(bus.read_byte(0x40))
b = np.uint8(bus.read_byte(0x40))
a = (a<<8) | b
flow = (float(a)-32768)/120
print(flow)
Код действительно выглядит так же, но я получаю только -273,06666666666 в качестве возвращаемого значения. Кто-нибудь знает, где есть различия между Raspberry Pi и Arduino I2 C и может помочь мне получить правильные значения на Pi?