欢迎您来到,李雷博客 | PHP博客        登录  |  注册

RS485+PHP+串口服务器,用TCP方式实现温度实时监测代码

更新:2022-01-10 08:45:48
人气:3638
来源:本站原创
A+

写这个程序是有原因的,近期天气越来越凉,估计邻居的循环泵又开始作妖了,为了摸清邻居夜间开循环泵的规律,做了这套PHP+RS485温度模块的监测程序,直接把温度传感器放到暖气上判断温度降低的时间段。

所需设备的连接方式:

一个USR-W610的串口服务器和一个温度传感器,连线方式就如下图,两个设备都需要供电。之前的博文中有介绍过这两个设备的情况包括花费的RMB

温度传感器与串口服务器连线方式

上图设备中串口服务器是需要设置参数的,通电后可以通过无线WIFI连接,进入管理界面,配置信息如下图:

注意的地方是:波特率、数据位要与温度传器一致,工作模式透明传输,网络模式、协议、端口、IP可不是服务器地址而是W610获取到的IP,这个IP就是PHP代码中发送TCP指令的目标IP。

串口服务器WIFI名称

串口服务器设置

串口服务器设置


监测页面截图:

PHP温度实时监测界面

监测页面核心代码:

<script src="../jquery.min.js"></script>
<script>
function tcp_control(zhiling,type){
		
	$("#status").html('正在读取数据,请稍后...');

	var ajaxform=$.post("jilu-ajax.php",{zhiling:zhiling,type:type},function(result){
																		  
		var give_strs= new Array(); //定义一数组
		give_strs=result.split("|"); //字符分割 

		if (give_strs[0]=="ok"){
			if (zhiling=='wendu'){
				$("#shidu").html(give_strs[1]);
				$("#wendu").html(give_strs[2]);
				$("#status").html('<span style=color:green>读取温、湿度,成功,返回值:'+give_strs[3]+'</span>');
			}if (zhiling=='light'){
				$("#status").html('<span style=color:green>执行成功,返回值:'+give_strs[1]+'</span>');
			}
		}else{
			$("#status").html('<span style=color:red>Error-'+result+'</span>')
		}
	});	

}
</script>

<div style="width:100%; margin-top:100px">
	<table width="52%" border="0" align="center" cellpadding="0" cellspacing="0">
		<tr>
			<td height="77" colspan="2" align="center" style="font-size:100px; font-family:'黑体'">实时温度</td>
		</tr>
		<tr>
			<td width="50%" height="237" align="center" valign="middle" >温度:<span id="wendu" style="font-size:86px; color:#FF6600">-</span></td>
			<td width="50%" align="center" valign="middle">湿度:<span id="shidu" style="font-size:86px;color:#FF6600">-</span></td>
		</tr>
		<tr style="display:">
			<td height="45" colspan="2" align="center" valign="middle"><span id="status">---</span></td>
		</tr>
	</table>
</div>

<div>
    <iframe src="tongji.php" id="Frame_1" name="Frame_1" frameborder="0" scrolling="no" style="width:100%;height:500px"></iframe>
</div>

<script>
$(document).ready(function(){
	tcp_control('wendu','');
	setInterval(function(){tcp_control('wendu','')},10000); 
});
</script>

RS485协议用PHP+AJAX发送指令和接收数据的核心代码:

<?
include_once("conn.php");
date_default_timezone_set ("PRC");//设置时区
set_time_limit(0);
ini_set('memory_limit','1024M');
header("Content-Type: text/html;charset=GB2312");

$zhiling=$_POST["zhiling"];
$type=$_POST["type"];

function tcp($sendStr){
	
	$host = "192.168.1.107";//串口服务器IP
	$port = 8899;//服务器端口

	$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));  // 创建Socket
	socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>3, "usec"=>0 ) );//接收超时
	socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>1, "usec"=>0 ) );//发送超时

	if (socket_connect($socket, $host, $port)) {  //连接
		socket_write($socket, $sendStr);  // 逐组数据发送
		$receiveStr = '';
		$receiveStr = socket_read($socket, 1024, PHP_BINARY_READ);  // 采用2进制方式接收数据
		$receiveStrHex = bin2hex($receiveStr);  // 将2进制数据转换成16进制
		return $receiveStrHex;//返回的值
	}
	
	socket_close($socket);// 关闭Socket
}

if ($zhiling=='wendu'){

	//要发送的指令(温度传感器发出的查询指令:01 03 00 00 00 02 C4 0B)
	$sendStr = "\x01\x03\x00\x00\x00\x02\xC4\x0B";  // 16进制数据
	
	$get_value=tcp($sendStr);
	
	$indate=date("Y-m-d H:i:s");
	
	$shidu=substr($get_value,6,4);//获取湿度信息
	$shidu_str=number_format((hexdec($shidu)/10),2);
	//echo "当前湿度:".$shidu.",转为十进制:".hexdec($shidu).",显示为:".number_format((hexdec($shidu)/10),2)."%";

	$wendu=substr($get_value,10,4);//获取温度信息
	$wendu_str=number_format((hexdec($wendu)/10),2);
	//echo "当前温度:".$shidu.",转为十进制:".hexdec($shidu).",显示为:".number_format((hexdec($shidu)/10),2)."℃";

	echo "ok|".$shidu_str."|".$wendu_str."|".$get_value.",最后返回时间:".$indate;
	
	$sql2 = "insert into wendu (indate,wendu,shidu) values ('" . $indate . "','" . $wendu_str . "','" . $shidu_str . "') ";
	$mysqli->query($sql2);
}
?>

以上放的都是主要的核心PHP代码,里面还有一个利用highchart做的统计图,这个代码就不放了,自由发挥吧。

推荐的文章
# 发表我的评论
  /     /  
# 最近评论

第一次感觉PHP与物联网也是有联系的

  Ads by Google
  联系博主
Hello,本博客系统采用PHP和MySql开发,程序开发完全是因为个人爱好,是自己纯手写PHP源代码,未采用任何PHP框架!
QQ:858353007   微信号:lileihot123
网站地图
会员服务
关于我们
QQ:858353007
 
广告服务
加我微信
移动端访问
 
 
Copyright © 2014- 2024 www.mdaima.com All Rights Reserved.
李雷博客,专注PHP经验、PHP教程及PHP源代码开源下载分享的PHP博客!   ICP备案号:京ICP备10202169号-4