既上一篇[Raspberry Pi學習筆記]Raspberry Pi 3 CentOS 7 - Web操作RPi-GPIO,此篇要增加Mariadb,把操作過程紀錄在Mariadb中,首先進入Mariadb來增加table,輸入指令
mysql -u root -p
接下來輸入密碼進入即可
輸入指令查詢資料庫,
show databases;
切換資料庫至mysql,輸入指令
use mysql;
創造ledtable,以及此table下的各個項目,輸入指令,
create table ledtable(
id int NOT NULL AUTO_INCREMENT,
ledone varchar(11),
ledtwo varchar(11),
createtime timestamp,
primary key (id)
);
可以輸入指令查詢建立好的table裝況,
show columns from ledtable from mysql;
建立好就可以離開,輸入指令
exit;
在[Raspberry Pi學習筆記]Raspberry Pi 3 CentOS 7 - 安裝php這篇中,沒有安裝mysql相關物件,所以需要額外安裝,輸入指令
yum -y install php-mysql
接下來可額外安裝一些CMS系統需要的常見PHP模塊,如Wordpress,Joomla和Drupal,輸入指令
yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel
重新啟動Apache HTTP,輸入指令
systemctl restart httpd.service
接下來要修改在/var/www/html中的index.html與led.php這兩隻程式,輸入指令
cd /var/www/html 切換目錄
vi index.html 修改index.html
vi led.php 修改led.php
index.html程式碼如下,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery UI Button - Default functionality</title>
<link rel="stylesheet" href="./jquery-ui-1.12.1/jquery-ui.css">
<script src="./jquery-ui-1.12.1/external/jquery/jquery.js"></script>
<script src="./jquery-ui-1.12.1/jquery-ui.js"></script>
<script>
$(function(){
var led_4='off',led_17='off';
$("input").click( function(event){
var readTable='';
if(event.currentTarget.name=="x"){
if(led_4=='on'){
led_4 = 'off';
}else{
led_4 = 'on';
}
}
if(event.currentTarget.name=="y"){
if(led_17=='on'){
led_17 = 'off';
}else{
led_17 = 'on';
}
}
if(event.currentTarget.name=="read"){
readTable = 'on';
}
$.ajax({
type: 'GET',
url: 'led.php',
data: "x="+led_4+"&y="+led_17+"&read="+readTable,
dataType: 'text',
success: function(data, textStatus){
document.getElementById("message").textContent = data;
},
error: function(xhr, ajaxOptions, thrownError){
},
complete: function(XMLHttpRequest, textStatus){
},
beforeSend: function(XMLHttpRequest){
}
});
});
});
</script>
</head>
<body>
<div>
<input type="button" name="x" value="LED ONE">
<input type="button" name="y" value="LED TWO">
<input type="button" name="read" value="資料讀取">
<br>
<textarea rows="40" cols="100" id="message"></textarea>
</div>
</body>
</html>
led.php程式碼如下,
<?PHP
$host = "192.168.0.108";
$name = "root";
$password = "123456";
$mysql = "mysql";
$Link = mysql_connect($host, $name, $password);
if(!$Link){
die(' 連線失敗,輸出錯誤訊息 : ' . mysql_error());
}else{
mysql_select_db($mysql, $Link);
if($_GET["read"]=="on"){
$strSQL = "select *";
$strSQL.= " from ledtable";
$strSQL.= " order by createtime asc";
$DBList = mysql_query($strSQL);
if($DBList){
while($row = mysql_fetch_array($DBList)){
$id = $row["id"];
$ledone = $row["ledone"];
$ledtwo = $row["ledtwo"];
$createtime = $row["createtime"];
echo "id: ".$id.", ledone: ".$ledone.", ledtwo: ".$ledtwo.", createtime: ".$createtime."\r\n";
}
}
}else{
echo shell_exec("sudo /var/www/cgi-bin/cgiLed.py -x ".$_GET["x"]." -y ".$_GET["y"]);
$strSQL = "INSERT INTO ledtable";
$strSQL.= "(ledone, ledtwo)";
$strSQL.= " VALUES('".$_GET["x"]."'";
$strSQL.= ",'".$_GET["y"]."'";
$strSQL.= ")";
mysql_query($strSQL);
}
}
mysql_close($Link);
?>
按LED ONE或LED TWO來操作樹梅派LED
按"資料讀取"可顯示目前操作紀錄