control via web

Can someone point me in the direction of how to set-up a web based interface that could be used to control a bluetooth enabled arduino.

i.e. access my web server from anywhere and by means of some control buttons remotely control arduino powered robot.

I have an introductory knowledge of Processing and generally understand what must be done to send serial stream to the arduino...but don't know how to integrate that to a web based interface.

Is the web server running on your PC?

If it is, then the scripting language you are programming the web pages with (PHP, ASP.NET, etc.) will (or should) also be able to access the serial port and send data the same way that Processing does.

Aspects of what you are trying to do are covered in the "Makeing Things Talk" book. Making Things Talk [Book]

But you could just pick most of the information up here in the forum.

Matt

Thanks guys. Yes the computer running the server will be the same one running the arduino. I've no experience with any of those languages but have good aptitude for programming...I'm just starting out and really enjoy the simple yet functional arduino environment. Is there a comparable open source environment for php et al that could be recommended?

Is there a comparable open source environment for php et al that could be recommended?

Comparable to what? PHP is free, as is MySQL and Apache. Everything you need for full-blown web applications is there, along with great support - kind of like for Arduinos.

Ok..I'm making some very good progress! I've scrapped together some php code that allows me to choose from either of two buttons on a web page to blink Arduino LED either once or twice. It's an easy transition from there to add more buttons that will control the servos and the like.

Arduino Code:

 const int ledPin = 2;      // the pin that the LED is attached to
  byte brightness;
  
 void setup()
 {
   // initialize the serial communication:
   Serial.begin(9600);  
   // initialize the ledPin as an output:
   pinMode(ledPin, OUTPUT);
 }

 void loop()
{
 
 
   // check if data has been sent from the computer:
   if (Serial.available())
    {
     // read the most recent byte (which will be from 0 to 255):
     brightness = Serial.read();
     // set the brightness of the LED:
        if (brightness=='A') 
         {
           digitalWrite(ledPin, HIGH);
           delay(1000);
           digitalWrite(ledPin, LOW);
         }
        if (brightness=='B')  
          {    
           digitalWrite(ledPin, HIGH);
           delay(500);
           digitalWrite(ledPin, LOW);
           delay(500);
           digitalWrite(ledPin, HIGH);
           delay(500);
           digitalWrite(ledPin, LOW);
          }
     }  // end serialavailable loop
}

php Code Part 1:

php Code Part 2:

But I do have a few glitches.

The most glaring one is that after being powered up with the USB cable, the script will not work until after I upload the source code to the arduino. So...if I remove power and put power back to the Arduino, and then use the buttons on the web page, I get nothing. If I re-upload the source code from the arduino interface to the arduino, I then get the proper functionality with the arduino receiving the serial data.

Second problem is that upon first loading the web page, I get the error at the top of the page as shown on this screen shot:

Third problem is that upon first loading the web page, the code cycles through the "two blink" function without the button being pressed. Any help as to why that is?

FWIW...I know virtually nothing about php and an amazed that I was able to get this to work at all! I'm a little boggled by the logic/process flow of this php code.

To further troubleshoot, I modified my arduino code so that the LED blinks rapidly while there is no serial input.

When I use the php blink buttons after rebooting the arduino, the LED stops flashing for one second or so, indicating that a serial signal is coming in, but the signal apparently isn't being properly read until the code is re-uploaded into the Arduino.....strange!

 const int ledPin = 2;      // the pin that the LED is attached to
  byte brightness;
  
 void setup()
 {
   // initialize the serial communication:
   Serial.begin(9600);
   // initialize the ledPin as an output:
   pinMode(ledPin, OUTPUT);
 }

 void loop()
{
 

   // check if data has been sent from the computer:
   if (Serial.available())
    {
     // read the most recent byte (which will be from 0 to 255):
     brightness = Serial.read();
     // set the brightness of the LED:
        if (brightness=='A') 
         {
           delay(2000);
           digitalWrite(ledPin, HIGH);
           delay(1000);
           digitalWrite(ledPin, LOW);
           delay(1000);  
       }
        if (brightness=='B')  
          {    
           delay(2000);
           digitalWrite(ledPin, HIGH);
           delay(500);
           digitalWrite(ledPin, LOW);
           delay(500);
           digitalWrite(ledPin, HIGH);
           delay(500);
           digitalWrite(ledPin, LOW);
           delay(1000);  
        }
     }  // end serialavailable loop
 
    // blink LED in rapid fashion to signal no serial signal
   digitalWrite(ledPin, HIGH);
   delay(50);
   digitalWrite(ledPin, LOW);
   delay(100);
}

Here's what I got so far:

On line 3 of your PHP script, you have:

$task = $_POST['task'];

When this executes, it is asking the $_POST list to return that value whose key is 'task'. You haven't defined a value in the $_POST list with that key, yet.

You can test whether the $_POST list has a value for a given key, using:

if(isset($_POST['task']))
{
    $task= $_POST['task'];
    // Switch stuff goes here
}

The doForm call goes in an else block, after this if block.

you can use echo statements in the PHP script for debugging:

echo "<p>Some message: $someVariable</p>";

I'm pretty sure that the $serial->deviceSet and $serial->deviceOpen calls return values. You might want to store and echo them to see what is happening. I'm pretty sure, too, that the serial class has a read method, so yout could have the Arduino echo what it received:

Serial.print("I got a value of [");
Serial.print(brightness);
Serial.println(" which I stuffed in brightness");

Thanks Paul, that if statement took care of that one error and now the logic of it makes good sense.

I'm having trouble using php to receive any value from the serial port. I've got my arduino in a loop simply sending an 'A' to the serial port and am trying to capture it in php:

function doForm()
{
include "php_serial.class2.php";
$serial = new phpSerial;
$serial->deviceSet("COM4");
$serial->deviceOpen();
$read = $serial->readPort();
//$serial->deviceClose();
print"Value received from Arduino:";
print($read);

I tried both print and echo commands, they seem to do the same thing but php does not seem to be picking up any value by way of this code. This code also seems to be locking up my serial port, as I can't even upload code to the arduino without first unplugging it from power. Opening serial monitor or hitting reset button on arduino still don't free up serial port.

You've commented out the code to close the serial port connection, so that prevents other applications from being able to use the same connection.

Try putting two or three more reads and prints in a row. Maybe there is a timing issue going on. You might need to create a loop that checks for serial data many times.

Still struggling with this...I set up a loop and verified using Processing that the data is going to the serial port...it graphs the input fine.

I've tried reading serial data in php using both the class that I found and also a dll extension...both of them write to serial fine but I can't read anything. It seems like I must be doing something inherently wrong but i just dont know what.

<?php
 ser_open( "COM4", 9600, 8, "None", "1", "None" );
 

 print "
";
 if (ser_isopen()) echo "Port is open!.";
 print "
";
 ser_flush(true,true);
 
 // $nr = ser_inputcount( );
 // echo "$nr bytes available";
  
//  $b = ser_readbyte();
//  printf('  Received: %X', $b);

$counter = 0;
$start = 1;

for($start; $start < 50; $start++) {
$str = ser_read();
echo "  Received: $str";
$counter = $counter + 1;
print $start . "
";
}
$versi= ser_version();
print $versi; 


    ser_close();

  ?>

FWIW, I tried it with and without the serial flush with the same result. Here's the output I got which is just a running count of the loop and null data for the serial

Port is open!.
Received: 1
Received: 2
Received: 3
Received: 4
Received: 5
Received: 6
Received: 7
Received: 8
Received: 9
Received: 10
Received: 11
Received: 12
Received: 13
Received: 14
Received: 15
Received: 16
Received: 17
Received: 18
Received: 19
Received: 20
Received: 21
Received: 22
Received: 23
Received: 24
Received: 25
Received: 26
Received: 27
Received: 28
Received: 29
Received: 30
Received: 31
Received: 32
Received: 33
Received: 34
Received: 35
Received: 36
Received: 37
Received: 38
Received: 39
Received: 40
Received: 41
Received: 42
Received: 43
Received: 44
Received: 45
Received: 46
Received: 47
Received: 48
Received: 49
20091007.1 TRIAL 0 in 19 out

I looked at the source code for the php_serial class you were using. For the readPort function, it contains this:

elseif ($this->_os === "windows")
            {
                  /* Do nohting : not implented yet */
            }

I think this explains why the class isn't working for you. However, in that class, the deviceOpen function uses fopen to open a connection to COMn and fwrite to send data. You might be able to do the same thing, and then use fread to read the data.

Thanks Paul! You are the man!!! I'll give that a try tonight after work! I am very grateful for your help!

Still no luck. Google search seems to reveal that reading the serial port in php (with Windows) is an effort in futility. I'm going to abandon the php route and try developing something in Java instead. I've downloaded Netbeans and have been playing with it this evening...I'm finding the learning curve a bit steeper with Java and this IDE.

I hate to feel like an information leech, but I don't understand the relationship between the different files in a Java project. I see how to create an application but I don't see how to run the code in a web browser. It looks to me like you have to create a separate applet file within the project...(is the main file in the project not really needed in such a case?) with the applet file being the true meat and potatoes that can be called from a separate html file?

You might want to look at Processing, instead. It is based on Java, but has an IDE that looks a lot like the Arduino IDE. There is support for reading from, and writing to the serial port. There are drawing capabilities, so you can create a GUI, if you like. It is not easy, as there are no button, field, check box, etc. classes that do the drawing for you. But, depending on what you want to do, it might work. And it's pretty easy to use.

Or, switch to Linux.

(Still stuck using windows, myself).

Can an application made in processing be integrated onto a web page?

Can an application made in processing be integrated onto a web page?

Forgot about that requirement.

No, not that I know of.

Can an application made in processing be integrated onto a web page?

Yes that is possible but there is some functionality you can't do.
That is the way most of the Processing "art" web sites work, you don't download the sketch and run it you just visit the web page.
See the Processing web site for details.

linusb: Could you please paste the PHP code somewhere so i can try and test it? :slight_smile:

I have been trying to connect to the Arduino via PHP without any luck. So with your code i might be able to get some ideas! :sunglasses: