RS485+PHP+串口伺服器,用TCP方式實現溫度實時監測代碼

蛋蛋愛嗶嗶 發佈 2022-05-20T05:41:44.097244+00:00

寫這個程序是有原因的,近期天氣越來越涼,估計鄰居的循環泵又開始作妖了,為了摸清鄰居夜間開循環泵的規律,做了這套PHP+RS485溫度模塊的監測程序,直接把溫度傳感器放到暖氣上判斷溫度降低的時間段。

寫這個程序是有原因的,近期天氣越來越涼,估計鄰居的循環泵又開始作妖了,為了摸清鄰居夜間開循環泵的規律,做了這套PHP+RS485溫度模塊的監測程序,直接把溫度傳感器放到暖氣上判斷溫度降低的時間段。

所需設備的連接方式:

一個USR-W610的串口伺服器和一個溫度傳感器,連線方式就如下圖,兩個設備都需要供電。之前的博文中有介紹過這兩個設備的情況包括花費的RMB

上圖設備中串口伺服器是需要設置參數的,通電後可以通過無線WIFI連接,進入管理界面,配置信息如下圖:

注意的地方是:波特率、數據位要與溫度傳器一致,工作模式透明傳輸,網絡模式、協議、埠、IP可不是伺服器地址而是W610獲取到的IP,這個IP就是PHP代碼中發送TCP指令的目標IP。


監測頁面截圖:

監測頁面核心代碼:

<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做的統計圖,這個代碼就不放了,自由發揮吧。

關鍵字: