Control Arduino Remotely over net via HTML forms

Well, for RGB colormixing I should think it best if you modify the backend.php and Arduino sketch somewhat. As it is now, php sends (depending on 'do' and 'pin') 2 bytes. The Arduino checks if the serial buffer is bigger than 2 bytes, and if it is, fetches the 2 bytes and does it's thing. One thing that dawned on me now is that I wrote "if (Serial.available() > 2)" but it should be "if (Serial.available() > 1)". After all, >2 won't trigger unless there is more than 2 bytes available, which is kinda silly as we are sending 2 byte commands.

If you want to make color-mixing, I will gladly help you with sourcecode. If you want to make a playground tutorial, Great! =D What I am thinking is to simply send 3 bytes. For colormixing you need to send 3 values of 0-255 right? Well, that is coincidentally the exact number of values covered in one byte. :wink:

backend.php:

<?php
if(isset($_REQUEST['r']) && is_numeric($_REQUEST['r']) && isset($_REQUEST['g']) && is_numeric($_REQUEST['g']) && isset($_REQUEST['b']) && is_numeric($_REQUEST['b']))
{
      $fp = @fsockopen('127.0.0.1', 1000, $errno, $errstr, 3);
      if($fp)
      {
            fputs($fp, chr($_REQUEST['r']).chr($_REQUEST['g']).chr($_REQUEST['b']));
      }
      else
      {
            die('<b>Error('.$errno.'):</b>'.$errstr);
      }
}
header('Location: index.php');
?>

That code will accept data from both a form (POST) or from the url (GET). $_REQUEST covers them both. The thought is that each color (red, green, blue) is sent to backend.php where the numeric values are replaced with their corresponding ASCII-characters using the chr()-funksjon and sent to the VB application.

Don't think you'll need to change the VB application, but the Arduino sketch needs a bit of change. Something like this:

int val;
int ledPin1 = 9;
int ledPin2 = 10;
int ledPin3 = 11;

void setup()
{
      //begin the serial communication
      Serial.begin(9600);
      pinMode(ledPin1, OUTPUT);
      pinMode(ledPin2, OUTPUT);
      pinMode(ledPin3, OUTPUT);
}

void loop()
{
      //check if data has been sent from the computer
      if (Serial.available() > 2) {
            //fetch first byte and set output (value of RED)
            val = Serial.read();
            analogWrite(ledPin1, val);

            //fetch second byte and set output (value of GREEN)
            val = Serial.read();
            analogWrite(ledPin2, val);

            //fetch third byte and set output (value of BLUE)
            val = Serial.read();
            analogWrite(ledPin3, val);
      }
}

Checks if 3 or more bytes are available on the serial buffer, fetches the 3 bytes and sets values analog pins 9, 10 and 11 to the ASCII values of the 3 bytes. If you send AbC to the Arduino, pin9 will be set to 65, pin10 to 98 and pin11 to 67.