树莓派远程温度采集

先在http://www.rpilink.com网站上注册用户和下载客户端,详细见树莓派引脚远程控制

在树莓派上启动客户端,使树莓派处于在线状态。

配置好树莓派的I2C接口,使接口处于可用状态,详情见树莓派i2c配置

选择菜单“数据采集”,新建一条数据采集。

然后按照下图配置好,以下3个设备,温度传感器(DS18B20),arduino uno,树莓派。(可以按照自己有的设备接入)

然后点击数据查看的“实时曲线”即可看到实时数据。

将传感器处于不同的环境中,可以看到数据变化。

以下是arduino的程序示例(采用官方例子改写),需要注意的是 OneWire.h 需要手动下载官方lib(到这里下载)包并添加到arduino IDE中(1.5.5版本加到IDE目录下面的libraries下面):

#include
#include

int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
String strValue=”";
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2

void setup(void) {
Wire.begin(4);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop(void) {
float temperature = getTemp();
strValue=String(temperature);
//char charBuf[strValue.length()+1];
// strValue.toCharArray(charBuf, strValue.length()+1);
//Serial.println(charBuf);

delay(2000); //just here to slow down the output so it is easier to read

}
void requestEvent()
{
char charBuf[strValue.length()+1];
strValue.toCharArray(charBuf, strValue.length()+1);
Wire.write(charBuf);
}
void receiveEvent(int howMany)
{
while (1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.print(x); // print the integer
}

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return -1000;
}

if ( addr[0] != 0×10 && addr[0] != 0×28) {
Serial.print(“Device is not recognized”);
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0×44,1); // start conversion, with parasite power on at the end

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two’s compliment
float TemperatureSum = tempRead / 16;

return TemperatureSum;

}


你可能感兴趣的:(树莓派远程温度采集)