<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MyDarkMaterials.co.uk &#187; PHP</title>
	<atom:link href="http://www.mydarkmaterials.co.uk/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mydarkmaterials.co.uk</link>
	<description></description>
	<lastBuildDate>Sat, 01 May 2010 12:49:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>DOOMWATCH</title>
		<link>http://www.mydarkmaterials.co.uk/2009/07/doomwatch/</link>
		<comments>http://www.mydarkmaterials.co.uk/2009/07/doomwatch/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 22:30:16 +0000</pubDate>
		<dc:creator>Tinlad</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[B3ta]]></category>
		<category><![CDATA[Daily Mail]]></category>
		<category><![CDATA[DOOMWATCH]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mydarkmaterials.co.uk/?p=125</guid>
		<description><![CDATA[Short of time? Life too hectic? Remove the daily chore of reading the news; simply use DOOMWATCH. DOOMWATCH removes the hassle of having to actually read the news to find out what you need to be afraid of. Using state-of-the-art &#8230; <a href="http://www.mydarkmaterials.co.uk/2009/07/doomwatch/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Short of time? Life too hectic? Remove the daily chore of reading the news; simply use <a href="http://www.mydarkmaterials.co.uk/doom/">DOOMWATCH</a>.</p>
<p>DOOMWATCH removes the hassle of having to actually read the news to find out what you need to be afraid of. Using state-of-the-art Science™ it analyses various news sources to determine exactly what will cause the downfall of society today.</p>
<p>By default it uses the BBC News service, but if you need an extra dose of pessimism, it also include a <a href="http://www.mydarkmaterials.co.uk/doom/">Daily Mail mode</a> &#8211; guaranteed to included 150% extra baseless panic!</p>
<p><strong>Update:</strong> Now with extra features, and default Daily Mail. Woo!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mydarkmaterials.co.uk/2009/07/doomwatch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP and the Arduino &#8211; Follow-up</title>
		<link>http://www.mydarkmaterials.co.uk/2009/04/php-and-the-arduino-followup/</link>
		<comments>http://www.mydarkmaterials.co.uk/2009/04/php-and-the-arduino-followup/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 20:31:31 +0000</pubDate>
		<dc:creator>Tinlad</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mydarkmaterials.co.uk/?p=98</guid>
		<description><![CDATA[I&#8217;ve had a bit of interest on the original entry, asking for more detail on the code used in the video demonstation at the end. Download the relevent files here: arduinophp.zip The zip file contains three files: index.php, serial.php and &#8230; <a href="http://www.mydarkmaterials.co.uk/2009/04/php-and-the-arduino-followup/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had a bit of interest on the <a href="http://www.mydarkmaterials.co.uk/2008/11/interfacing-php-with-the-arduino/">original entry</a>, asking for more detail on the code used in the video demonstation at the end.</p>
<p>Download the relevent files here: <a href="http://www.mydarkmaterials.co.uk/wp-content/uploads/2009/04/arduinophp.zip">arduinophp.zip</a></p>
<p>The zip file contains three files: index.php, serial.php and arduino_serial.pde. Two more files are required: the <a href="http://jquery.com/">jQuery</a> JavaScript library, and the <a href="http://www.phpclasses.org/browse/package/3679.html">PHP Serial</a> class.</p>
<p>The code isn&#8217;t exactly elegant. It was just something I was playing around with, so there is probably scope for a lot of improvement. I&#8217;m not always great at explaining things like this, but I&#8217;ll give it a crack:</p>
<h3>index.php</h3>
<p>This doesn&#8217;t actually <em>need</em> to be a PHP file, since it only contains HTML and JavaScript. It&#8217;s just force of habit that caused me to save it as .php instead of .html.</p>
<p>This is the file I navigated to on my iPod touch. It consists of a HTML form containing two option boxes; one to select the LED and another to select the brightness. The value attributes on the options contains the actual data that&#8217;s sent to the Arduino.</p>
<p>An onClick event is added to the submit button that triggers the AJAXness, which is done using jQuery&#8217;s getJSON function. It calls serial.php with the values from the option boxes as GET variables. It also displays the returned values from serial.php on the page.</p>
<h3>serial.php</h3>
<p>This file does the actual communication with the Arduino. If first checks that the necessary GET variables are set, then does a bit of input sanitising and then constructs a packet to send via the serial port to the Arduino. The packet is three bytes:</p>
<ul>
<li>The first byte is an @ symbol, which denotes the start of the packet to the Arduino.</li>
<li>The second byte is the index (0, 1, 2) of the LED to adjust, encoded to ASCII using PHP&#8217;s <em>chr</em> function.</li>
<li>The third byte is the desired brightness (0 &#8211; 255), similarly encoded using <em>chr</em>.</li>
</ul>
<p>This is then sent, and the response from the Arduino is echoed.</p>
<h3>arduino_serial.pde</h3>
<p>This is the code loaded onto the Arduino itself. The most important part of it is the nest of ifs that check for and interpret the data packet sent by serial.php:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Serial.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// if there are at least 3 bytes in the serial buffer</span>
  <span style="color: #993333;">int</span> p<span style="color: #339933;">,</span>i<span style="color: #339933;">;</span>
  incomingByte <span style="color: #339933;">=</span> Serial.<span style="color: #202020;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read first byte</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>incomingByte <span style="color: #339933;">==</span> <span style="color: #0000dd;">64</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// check if it is packet start (@ = 64 in ASCII)</span>
    p <span style="color: #339933;">=</span> Serial.<span style="color: #202020;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read second byte</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>p <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> p <span style="color: #339933;">&lt;</span> numPins<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// check it is within a valid range</span>
      i <span style="color: #339933;">=</span> Serial.<span style="color: #202020;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// read the third byte</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&gt;=</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">&amp;&amp;</span> i <span style="color: #339933;">&lt;=</span><span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// check it is within a valid range</span>
        set<span style="color: #009900;">&#40;</span>p<span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// set LED p to brightness i</span>
        printVals<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// respond with status</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s basically it. Hopfully that makes sense&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mydarkmaterials.co.uk/2009/04/php-and-the-arduino-followup/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Interfacing PHP with the Arduino (with a splash OS X)</title>
		<link>http://www.mydarkmaterials.co.uk/2008/11/interfacing-php-with-the-arduino/</link>
		<comments>http://www.mydarkmaterials.co.uk/2008/11/interfacing-php-with-the-arduino/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 18:29:25 +0000</pubDate>
		<dc:creator>Tinlad</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mydarkmaterials.co.uk/?p=16</guid>
		<description><![CDATA[A while ago I bought an Arduino Diecimila, which is an open-source electronics prototyping platform. It&#8217;s a very easy to use programmable micro-controller with a bevy of digital and analogue inputs and outputs. It is programmed via USB using the &#8230; <a href="http://www.mydarkmaterials.co.uk/2008/11/interfacing-php-with-the-arduino/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A while ago I bought an <a href="http://www.arduino.cc/">Arduino</a> Diecimila, which is an open-source electronics prototyping platform. It&#8217;s a very easy to use programmable micro-controller with a bevy of digital and analogue inputs and outputs. It is programmed via USB using the cross-platform Arduino software. The language is C-like with plenty of documentation available online. The Arduino appears to the OS as a serial device which makes two-way communication very easy indeed.</p>
<p>Whilst I really haven&#8217;t got a clue what I might use it for, I thought it would be cool if I could create a simple web-based system to control hardware attached to the Arduino. My server-side language of choice is PHP, so I looked for a way to allow it to send and receive data from a serial port. I didn&#8217;t have to look very far before I found the <a href="http://www.phpclasses.org/browse/package/3679.html">PHP Serial class</a>, which looked just the ticket. Unfortunately whilst it caters for Windows and Linux, it doesn&#8217;t work under OS X. Fortunately it was just a matter of adding a few more <em>else if</em>&#8216;s to allow for the slight differences in the way <em>stty</em> works in OS X.</p>
<p>I thought it would all be plain sailing from here, but this was not the case. The Diecimila resets whenever a serial connection is opened, so it can be ready to receive a new program. However this can be a problem; suppose I open a serial connection to the Arduino and tell it to light up an LED at 50% brightness. Then later I decide I want it at 100% brightness, so I open up a serial connection again, at which point the Arduino resets and the LED goes out. Not very desirable, and positively useless if the Arduino has been collecting data that you wanted to retrieve.</p>
<p>After some Googling it became apparent that I wasn&#8217;t the only one with this issue. Most suggestions were to either remove a <em>tiny</em> SMT capacitor from the board to permanently disable the auto-reset function, or to suppress the behaviour by disabling DTR (in Windows) or HUPCL (in *nix). That sounded better, so I tried it out. The commands required were:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">nhup <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">999</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>cu.usbserial-A7004WqM <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #c20cb9; font-weight: bold;">stty</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>cu.usbserial-A7004WqM <span style="color: #660033;">-hupcl</span></pre></div></div>

<p>Now, I&#8217;m no command-line/serial expert, so I only have the slightest understanding about what goes on when those two commands are run. What I do know is that it solves the problem <em>so long as the sleep process is still running</em>. It&#8217;s hardly an ideal solution (in my opinion). I suspect this might be a problem unique to OS X, as it seemed to work much better when I tried it in Debian.
</p>
<p>Then! I found a <a href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213719666/28#28" >forum post</a> lurking at the bottom of a thread on the Arduino Forum. It suggests putting a 110 ohm resistor between the 5V and Reset pins, or a 47 ohm between the 3.3V and Reset pins. I had a 47 ohm resistor handy so I whacking it in there and lo! No more automatic resets. Brilliant. The post does go on to say that using the reset button with the 47 ohm resistor is place may be risky, so I&#8217;ll have to try and remember to remove it if I ever need to press it.</p>
<p>So now the PHP to Arduino link is useable. Here&#8217;s an example of some PHP code to send a character to the Arduino:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #666666; font-style: italic;">//Include the PHP Serial class</span>
    <span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;php_serial.class.php&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//Define the serial port to use (in Windows something like 'COM1')</span>
    <span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SERIALPORT'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/dev/cu.usbserial-A7004WqM'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//The character to send</span>
    <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$serial</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> phpSerial<span style="color: #339933;">;</span> 
    <span style="color: #000088;">$serial</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deviceSet</span><span style="color: #009900;">&#40;</span>SERIALPORT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$serial</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deviceOpen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
    <span style="color: #000088;">$serial</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendMessage</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&#8230; and an example of the Arduino code to interpret this and do something:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> incomingByte <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Start serial connection, 9600 baud</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>Serial.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        incomingByte <span style="color: #339933;">=</span> Serial.<span style="color: #202020;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>incomingByte <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            analogWrite<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">128</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Pin 9 to 50% if 0 received</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>incomingByte <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            analogWrite<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Pin 9 to 100% if 1 received</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This works brilliantly. With only slightly more complex code (thanks to <a href="http://principialabs.com/arduino-python-4-axis-servo-control/">a post on Principia Labs</a> for a great explanation on effective serial commands), and a bit of AJAXy goodness, I have something like this:</p>
<p>
    <object width="400" height="225" data="http://vimeo.com/moogaloop.swf?clip_id=2387120&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=2387120&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /></object>
</p>
<p>Now all I have to do if think of a use for it&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mydarkmaterials.co.uk/2008/11/interfacing-php-with-the-arduino/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
<enclosure url="http://www.mydarkmaterials.co.uk/video/arduino/Arduino.mp4" length="5199166" type="audio/mp4" />
<enclosure url="http://www.mydarkmaterials.co.uk/video/arduino/Arduino.ogv" length="3916486" type="video/ogg" />
		</item>
	</channel>
</rss>
