<?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>The project pages</title>
	<atom:link href="http://projects.sindrelindstad.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://projects.sindrelindstad.com</link>
	<description>sindrelindstad.com</description>
	<lastBuildDate>Sun, 22 Jan 2012 15:38:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to remotely enable/disable a LED on your Arduino using PHP and Processing</title>
		<link>http://projects.sindrelindstad.com/how-to-led-arduino-php-proc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-led-arduino-php-proc</link>
		<comments>http://projects.sindrelindstad.com/how-to-led-arduino-php-proc/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 11:12:42 +0000</pubDate>
		<dc:creator>sindre</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://projects.sindrelindstad.com/?p=4</guid>
		<description><![CDATA[<p></p> <p>Just got your first Arduino, but you don&#8217;t have any additional hardware? Gotten a little tired of watching the LED blink by itself, with no interaction? Check this out.</p> <p>This is the first thing I ever made for Arduino, as I had just received my first Uno and didn&#8217;t have any additional hardware. After [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://projects.sindrelindstad.com/wp-content/uploads/2012/01/ledphp2.jpg" alt="" title="ledphp2" width="675" height="300" class="alignnone size-full wp-image-99" /></p>
<p>Just got your first Arduino, but you don&#8217;t have any additional hardware? Gotten a little tired of watching the LED blink by itself, with no interaction? Check this out.</p>
<p>This is the first thing I ever made for Arduino, as I had just received my first Uno and didn&#8217;t have any additional hardware. After playing with the included examples and some tutorials I got bored after about an hour (making the internal LED blink is only exciting for so long!).</p>
<p>So I decided to make it a bit more interesting; i wanted to be able to enable and disable the internal LED using a web interface. This is actually quite simple, and could easily be altered to enable remote control of relays or other different types of outputs. The great thing about using a web interface is that it is cross-platform and pretty much enables you to also control your Arduino using most devices with web browsing capabilities, such as smartphones and tablets.</p>
<p>Well, enough talk. Let&#8217;s get to it! Here&#8217;s what you need:</p>
<blockquote><p>• An Arduino board with an internal LED (usually on pin 13) or a separate LED<br />
• A webhosting service with PHP support<br />
• The <a title="Arduino IDE" href="http://arduino.cc/en/Main/Software" target="_blank">Arduino IDE</a><br />
• The <a title="Processing IDE" href="http://processing.org/download/" target="_blank">Processing IDE</a><br />
• A text editor (e.g. Notepad++, Geany, or whatever you prefer)</p></blockquote>
<p>First, we&#8217;ll start off with the web interface. It&#8217;s function is to give the user the choice to either switch the LED on or off. When either one of them is clicked, the command is written to a .txt-file as a 1 (ON) or 0 (OFF) value.<br />
The interface consists of three files; a HTML frontend, a PHP document, and a .txt-file for storing values. So, firstly create these files using a text editor:</p>
<ul>
• index.html<br />
• led.php<br />
• LEDstate.txt
</ul>
<p><strong>Index.html should contain the following:</strong></p>
<pre class="brush: xml; title: index.html; notranslate">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;LED ON/OFF&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!-- This is just for aesthetics, centering the
content within the paragraph --&gt;
&lt;p align=&quot;center&quot;&gt;
&lt;font size=&quot;8&quot;&gt;

&lt;!-- This part is a a link, which also sends a request to the php-document,
telling it to write 1 or 0 to LEDstate.txt --&gt;
&lt;b&gt;&lt;a href=&quot;led.php?state=1&quot;&gt;ON&lt;/a&gt;&lt;/b&gt; /
&lt;b&gt;&lt;a href=&quot;led.php?state=0&quot;&gt;OFF&lt;/a&gt;&lt;/b&gt;&lt;/font&gt;
&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>&nbsp;<br />
<a href="http://projects.sindrelindstad.com/demo/ardled/" target="_blank">Here&#8217;s an example</a> of what it should look like.</p>
<p><strong>Now for the PHP file </strong><strong>:</strong></p>
<pre class="brush: php; title: led.php; notranslate">
&lt;?php
$onoroff = $_GET[&quot;state&quot;]; // Declares the request from index.html as a variable
$textfile = &quot;LEDstate.txt&quot;; // Declares the name and location of the .txt file

$fileLocation = &quot;$textfile&quot;;
$fh = fopen($fileLocation, 'w	') or die(&quot;Something went wrong!&quot;); // Opens up the .txt file for writing and replaces any previous content
$stringToWrite = &quot;$onoroff&quot;; // Write either 1 or 0 depending on request from index.html
fwrite($fh, $stringToWrite); // Writes it to the .txt file
fclose($fh); 

header(&quot;Location: index.html&quot;); // Return to frontend (index.html)
?&gt;
</pre>
<p><strong>The .txt file (LEDstate.txt) can be left empty.</strong></p>
<p>Upload these three files to your website, and remember the location. We&#8217;re gonna need it for later.</p>
<p><strong>Now it&#8217;s time to code the Arduino!</strong> For this, we can simply use <a href="http://arduino.cc/en/Tutorial/PhysicalPixel" target="_blank">the &#8220;Physical Pixel&#8221; example code from the Arduino website</a>, written by David A. Mellis, Tom Igoe and Scott Fitzgerald. We only need the Arduino part of it though, so I&#8217;ll copy it onto here. What it does is to open up a serial connection to the host computer and listens for serial inputs. If the Arduino receives the command &#8220;H&#8221; it sets the LED to &#8220;HIGH&#8221; (ON), or &#8220;LOW&#8221; (OFF) if it receives the command &#8220;L&#8221;. (For more information on this, check out <a href="http://arduino.cc/en/Tutorial/Blink" target="_blank">the &#8220;blink&#8221; tutorial</a>).</p>
<pre class="brush: arduino; title: Arduino sketch; notranslate">
const int ledPin = 13; // the pin that the LED is attached to - change this if you have a separate LED connected to another pin
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() &gt; 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}
</pre>
<p>Upload the sketch to your Arduino using the Arduino IDE. To test if it works, open up the serial port monitor and type either &#8220;H&#8221; or &#8220;L&#8221; (without quotation marks), then click send. If the LED turns on and off, you&#8217;re good to go! If not, check if the pin variable is set to the actual pin your LED is connected to (if you&#8217;re not using the internal one &#8211; which usually is at pin 13).</p>
<p><strong>Now for the last part that ties it all together! Open up the Processing IDE and enter the following:</strong></p>
<pre class="brush: arduino; highlight: [18,23]; title: Processing sketch; notranslate">
/*

A simple Processing script for enabling or disabling the LED on an Arduino using a web interface and serial communication.
Author: Sindre Lindstad
Created: 19th of January 2011

http://projects.sindrelindstad.com

*/

 import processing.serial.*;
 Serial port;

 void setup()  {

   /* This part must be altered to fit your local settings. The number in brackets after &quot;Serial.list()&quot; is where you declare what COM port your Arduino is connected to.
      If you get error messages, try a different number starting from 0 (e.g. 0, 1, 2, 3...) . */
    port = new Serial(this, Serial.list()[1], 9600);  // Open the port that the Arduino board is connected to, at 9600 baud

}
 void draw() {

  String onoroff[] = loadStrings(&quot;http://YOURDOMAIN.COM/LEDstate.txt&quot;); // Insert the location of your .txt file
  print(onoroff[0]);  // Prints whatever is in the file (&quot;1&quot; or &quot;0&quot;)

  if (onoroff[0].equals(&quot;1&quot;) == true) {
    println(&quot; - TELLING ARDUINO TO TURN LED ON&quot;);
    port.write('H'); // Send &quot;H&quot; over serial to set LED to HIGH

  } else {

    println(&quot; - TELLING ARDUINO TO TURN LED OFF&quot;);
    port.write('L');  // Send &quot;L&quot; over serial to set LED to LOW
 }

  delay(7000); // Set your desired interval here, in milliseconds
 }
</pre>
<p>Remember to edit it according to the comments. Now you should be all set to hit the Run/Play button! A small empty window will pop up, but your sketch should be running properly and posting messages in the Processing terminal window.</p>
<p>If you haven&#8217;t set it to connect to the right serial COM-port it will post an error to the terminal window. If you haven&#8217;t set the correct path to your .txt file, it will post &#8220;html&#8221; instead of &#8220;1&#8243; or &#8220;0&#8243;. Also note that you cannot use the Arduino IDE serial port monitor (or any other serial monitor) while running the sketch.</p>
<p>If everything works; &#8211; <strong>hooray!</strong> Show your friends, and have them turn your LED on and off from whereever they might be in the world. If it doesn&#8217;t work, review everything and make sure it&#8217;s all set up properly. If it still won&#8217;t work; leave a comment.</p>
<p><strong>-</p>
<p>Reddit user <a href="http://www.reddit.com/user/tenbatsu" target="_blank">tenbatsu</a> jazzed up the Arduino sketch a bit, and made it a little sexier. It&#8217;s designed for the use of three (external) LEDs. A blue LED to fade in and out, a green LED that indicates a &#8220;ready&#8221; state, and a red LED that indicates a &#8220;change&#8221; state. Have a look!</strong></p>
<pre class="brush: arduino; collapse: true; light: false; title: Tenbatsu&#039;s Arduino sketch; toolbar: true; notranslate">
const int fadePin = 9;    // The pretty blue LED
const int redPin = 10;    // Red LED
const int greenPin = 11;  // Green LED
const int fadeAmount = 5; // Increment to adjust brightness

int incomingByte;   // A variable to read incoming serial data into
int brightness = 0; // How bright the LED is

void setup()  {
  // Initialize serial communication:
  Serial.begin(9600);

  // Declare outputs:
  pinMode(fadePin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
} 

void loop()  {
  // Indicate ready state
  digitalWrite(greenPin, HIGH);
  digitalWrite(redPin, LOW);

  if (Serial.available() &gt; 0) {
    // Indicate change state
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);

    // See if there's incoming serial data:
    incomingByte = Serial.read();

    if (incomingByte == 'H' &amp;&amp; brightness &lt; 255) {
      // Cycle the blue LED from dim to bright
      for (brightness = 0; brightness &lt; 255; brightness += fadeAmount) {
        analogWrite(fadePin, brightness);
        delay(30);
      }
    } else if (incomingByte == 'L' &amp;&amp; brightness &gt; 0) {
      // Cycle the blue LED from bright to dim
      for (brightness = 255; brightness &gt; 0; brightness -= fadeAmount) {
        analogWrite(fadePin, brightness);
        delay(30);
      }
    }
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://projects.sindrelindstad.com/how-to-led-arduino-php-proc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

