php und arduino

Download Report

Transcript php und arduino

php und arduino
Interface for user-arduino
communication
Connection between user and arduino
• Data storage: properties
– properties.json
• Arduino and properties control
– updateProperties.php
– sendControl.php
• User interface
– index.php
– settings.php
Data storage – properties.json
{
"serialPort":"\/dev\/ttyACM1",
"flip_1":"1",
"flip_2":"1",
"flip_3":"1",
"flip_4":"1",
"flip_5":"1",
"switchState_A":"off",
"switchState_B":"off",
"switchState_C":"off",
"switchState_D":"off",
"switchState_E":"off",
}
More information about json-data format:
http://json.org/json-nl.html
Properties control–
updateProperties.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$key = $_GET['key'];
$val = $_GET['val'];
#echo $key . "\n";
#echo $val . "\n";
$json_data = json_decode(file_get_contents('properties.json'), true);
$json_data[$key] = $val;
#echo $json_data . "\n";
file_put_contents('properties.json', json_encode($json_data));
?>
Example: URL to set your serial port
http://localhost/homeAuto/updateProperties.php?key=serialPort&val=mySerialPort
Properties control –
sendControl.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$device = $_GET['device'];
$switchDevice = $_GET['switchDevice'];
Example: URL to switch on plug A:
http://localhost/homeAuto/sendControl
.php?device=A&switchDevice=1
-> transferred into serial message
31A1
$json_data = json_decode(file_get_contents('properties.json'), true);
(systemCode = 31, stored in
$serialPort = $json_data['serialPort'];
properties.json)
#echo "Serial Port: " . $serialPort . "\n";
#echo intval($json_data['flip_1']). "\n";
$systemCode = $json_data['flip_1'] . $json_data['flip_2'] . $json_data['flip_3'] . $json_data['flip_4'] .
$json_data['flip_5'];
#echo $systemCode . "\n";
#echo bindec($systemCode) . "\n"; # function bindec = get string from binary; example 11111 -> 31
$code = bindec($systemCode). $device . $switchDevice;
$ser = fopen($serialPort,"w"); # open connection to serial port
fwrite ($ser, $code); # write command to serial port
fclose($ser); # close connection to serial port
?>
Main user interface – index.php
<!DOCTYPE html>
<html>
<head>
<title>IEEE Home Automation</title>
<link rel="stylesheet" href="js/jquery.mobile-1.3.0.min.css" type="text/css">
<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="js/jquery.mobile-1.3.0.min.js" type="text/javascript"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body onload="loadProperties()">
<div data-role="page">
<div data-role="header">
<a href="index.html" data-transition="slide" data-icon="home">Home</a>
<h1>Home Automation</h1>
<a href="settings.php" data-transition="slide"
data-ajax="true" rel="external" data-icon="gear">Settings</a>
</div><!-- /header -->
<div data-role="content">
….. Content ….Next slide
</div><!-- /content -->
</div><!-- /page -->
</body>
</html>
Main user interface – index.php
<h2>Light switches</h2>
<div data-role="fieldcontain">
<label for="A">A</label>
<select name="sliderA" id="A" data-role="slider" data-mini="true">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</div>
<script>
$('#A').change(function (event) {
var mySwitch = event.currentTarget;
var val = mySwitch.value == "on" ? 1 :0 ;
$.post('updateProperties?key=switchState_A&val='+mySwitch.value,
function(response){});
$.post('sendControl.php?device='+mySwitch.id+'&switchDevice='+val,
function (response){});
});
function loadProperties(){
$.getJSON('properties.json?'+ new Date().getTime(), function(data) {
$('#A').val(data.switchState_A).slider('refresh');
});
}
</script>
Test URL: http://localhost/homeAuto
Setting interface – settings.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="js/jquery.mobile-1.3.0.min.css" />
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/jquery.mobile-1.3.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body onload="loadProperties()">
<div data-role="page" id="settings">
<div data-role="header">
<a href="index.html" data-transition="slide" data-icon="home">Home</a>
<h2>Home Automation Settings</h2>
</div><!-- /header -->
<div data-role="content">
…. Content …. Next slide
</div><!-- /content-->
</div><!-- page -->
</body>
</html>
Setting interface – settings.php
<div data-role="collapsible" id="serialPort" data-collapsed="true">
<h3>Serial Port</h3>
<div data-role="fieldcontain">
<label for="serialPortName">Serial Port:</label>
<input type="text" name="name" id="serialPortName" value="write your serial port here..." />
</div>
</div>
<div data-role="collapsible" id="systemCode" data-collapsed="true">
<h3>System Code</h3>
<p>Set your system code here:</p>
<?php
for ($i = 1; $i <= 5; $i++) {
echo "<div data-role=\"fieldcontain\">\n";
echo "<label for=\"flip_$i\">$i</label>\n";
echo "<select name=\"flip_$i\" id=\"flip_$i\"
data-role=\"slider\“ data-mini =\" true>\" \n";
echo "<option value=\"off\">0</option>\n";
echo "<option value=\"on\">1</option>\n";
echo "</select>\n";
echo "</div>\n";
}?>
</div>
Test URL: http://localhost/homeAuto/settings.php
<script>
<?php
Setting interface – settings.php
for ($i = 1; $i <= 5; $i++) {
echo "$(\"#flip_$i\").change(function(event) {\n";
echo "val = event.currentTarget.selectedIndex;\n";
echo "$.post(\"updateProperties?key=flip_$i&val=\"+val, function(response){});\n";
echo "});\n";
}
?>
$('#serialPortName').change(function(event){
//alert("Something changed");
val = event.currentTarget.value;
$.post("updateProperties?key=serialPort&val="+val, function(response){});
});
function loadProperties(){
//alert ("I am here!");
$.getJSON('properties.json?'+ new Date().getTime(), function(data) {
$('#serialPortName').val(data.serialPort);
<?php
for ($i = 1; $i <= 5; $i++) {
echo "if (data.flip_$i == 1){\n";
echo "$(\"#flip_$i\").val(\"on\").slider('refresh');\n";
echo "} else{\n";
echo "$(\"#flip_$i\").val(\"off\").slider('refresh');\n";
echo "}\n";
}?>
});
}
</script>
Test URL: http://localhost/homeAuto/settings.php