100 likes | 255 Views
p hp und arduino. Interface for user- arduino communication. Connection between user and arduino. Data storage: properties p roperties.json Arduino and properties control updateProperties.php sendControl.php User interface index.php settings.php. Data storage – properties.json.
E N D
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", } JSON (JavaScript Object Notation) is a lightweight data-interchange format. 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']; $json_data= json_decode(file_get_contents('properties.json'), true); $serialPort= $json_data['serialPort']; #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 ?> Example: URL to switch on plug A: http://localhost/homeAuto/sendControl.php?device=A&switchDevice=1 -> transferred into serial message 31A1 (systemCode = 31, stored in properties.json)
Main user interface – index.php • <!DOCTYPEhtml> • <html> • <head> • <title>IEEE Home Automation</title> • <linkrel="stylesheet"href="js/jquery.mobile-1.3.0.min.css"type="text/css"> • <scriptsrc="js/jquery-1.8.2.min.js"type="text/javascript"></script> • <scriptsrc="js/jquery.mobile-1.3.0.min.js"type="text/javascript"></script> • <metaname="viewport"content="width=device-width, initial-scale=1"> • </head> • <bodyonload="loadProperties()"> • <divdata-role="page"> • <divdata-role="header"> • <ahref="index.html"data-transition="slide"data-icon="home">Home</a> • <h1>Home Automation</h1> • <ahref="settings.php"data-transition="slide" • data-ajax="true"rel="external"data-icon="gear">Settings</a> • </div><!-- /header --> • <divdata-role="content"> • ….. Content ….Next slide • </div><!-- /content --> • </div><!-- /page --> • </body> • </html>
Main user interface – index.php • <h2>Light switches</h2> • <divdata-role="fieldcontain"> • <labelfor="A">A</label> • <selectname="sliderA"id="A"data-role="slider"data-mini="true"> • <optionvalue="off">Off</option> • <optionvalue="on">On</option> • </select> • </div> • <script> • $('#A').change(function (event) { • varmySwitch = event.currentTarget; • varval = 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){}); • }); • functionloadProperties(){ $.getJSON('properties.json?'+ new Date().getTime(), function(data) { $('#A').val(data.switchState_A).slider('refresh'); • }); • } • </script> … Content … For button A Test URL: http://localhost/homeAuto
Setting interface – settings.php <?php error_reporting(E_ALL); ini_set('display_errors','1'); ?> <html> <head> <title></title> <linkrel="stylesheet"href="js/jquery.mobile-1.3.0.min.css"/> <scriptsrc="js/jquery-1.8.2.min.js"></script> <scriptsrc="js/jquery.mobile-1.3.0.min.js"></script> <metaname="viewport"content="width=device-width, initial-scale=1"> </head> <bodyonload="loadProperties()"> <divdata-role="page"id="settings"> <divdata-role="header"> <ahref="index.html"data-transition="slide"data-icon="home">Home</a> <h2>Home Automation Settings</h2> </div><!-- /header --> <divdata-role="content"> …. Content …. Next slide </div><!-- /content--> </div><!-- page --> </body> </html>
Setting interface – settings.php <divdata-role="collapsible"id="serialPort"data-collapsed="true"> <h3>Serial Port</h3> <divdata-role="fieldcontain"> <labelfor="serialPortName">Serial Port:</label> <inputtype="text"name="name"id="serialPortName"value="write your serial port here..."/> </div> </div> <divdata-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> … Content … Test URL: http://localhost/homeAuto/settings.php
Setting interface – settings.php <script> <?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("Somethingchanged"); val = event.currentTarget.value; $.post("updateProperties?key=serialPort&val="+val, function(response){}); }); functionloadProperties(){ //alert ("I amhere!"); $.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> … scripting … After the content Test URL: http://localhost/homeAuto/settings.php