查看: 4592|回复: 2
收起左侧

树莓派通过水位传感器远程监控水位变化 (转载)

2014-9-29 11:31:16 | 显示全部楼层 |阅读模式
先在http://www.rpilink.com网站上注册用户和下载客户端,详细见树莓派引脚远程控制

在树莓派上启动客户端,使树莓派处于在线状态。
配置好树莓派的I2C接口,使接口处于可用状态,详情见树莓派i2c配置

选择菜单“数据采集”,新建一条数据采集。
water11-293x300.png
然后按照下图配置好,以下3个设备,水位传感器,arduino uno,树莓派。(可以按照自己有的设备接入)
water3-300x189.png
然后点击数据查看的“实时曲线”即可看到实时数据。
water2-300x147.png
将传感器放入水中,可以看到数据变化。注意该数据只是读取的模拟量,可以自行换算成水位信息。
water4-225x300.png
以下是arduino的程序示例(注意直接发送int型值可能会有问题,需换成byte[] 发送):
#include <Wire.h>
int sensorPin = A0;
int sensorValue = 0;
void setup()
{
Wire.begin(4); // join i2c bus with address #2
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent); // register event
Serial.begin(9600);
}

void loop()
{
sensorValue = analogRead(sensorPin);
//Serial.print(sensorValue);
delay(2000);
}
byte* getByteFromInt(int value){
if(value<256){
byte re[2]={00,value};
return re;
}else{
int low=value&0xFF;
int h=value>>8;
int high=h&0xFF;
byte re[2]={high,low};
return re;
}
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
byte* bb=getByteFromInt(sensorValue);
Wire.write(bb,2);
}
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
}

原文地址:http://blog.csdn.net/rpilink/article/details/19123935
回复

使用道具 举报

2014-12-3 00:09:53 | 显示全部楼层
请问下,水位传感器可以直接连到树莓派上而不是通过arduino? 我arduino不懂啊,这个程序是什么语言写的?然后烧到arduino里吗?
回复 支持 反对

使用道具 举报

2016-1-5 15:55:34 | 显示全部楼层
http://www.rpilink.com 打不开 ,国外的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部