Thursday, July 14, 2011

Arduino - Web server controlled LED using serial connection between the Arduino and web server

I was chatting to a friend who was working on home automation.  Controlling lights, heating and other stuff around the house using a touch screen controller and I thought if you used a web site as the interface then connected to the Arduino over a serial link it would be possible to switch things on and off from any devlice that could display a web page and access the web server.

Personally, I would not recommend using a serial link from the server to the Arduino as the Arduino resets itself everytime something is sent over serial,  More correctly an ethernet shield would be great.  Giving more scalability as the web server can talk to mutliple Arduinos with no great extra effort.

I installed XAMPP on my netbook as the webserver and used PHP code on the server to present the web interface and send the command to the Arduino over serial.  The Arduino is waiting for a serial command and then depending on what it is will turn on or off the mounted LED.

So, not the most impressive looking demo, but as a proof of concept if I can turn on a LED I can control a relay to turn on an kind of device.


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.


PHP on the server:

<?php

exec('mode com4: baud=9600 data=8 stop=1 parity=n xon=no');

$switch1 = "";

/* Serial script for pan/tilt Camera with servos */
/* Script by Aneal Khimani, 2-12-10 */

//check the GET action SuperGlobal var to see if an
//action is to be performed

if (isset($_GET['action'])) {

$switch1 = $_GET['action'];

//Action required

switch ($switch1) {
case "on":
$fp = fopen("com4", "w");
fwrite($fp, chr(97));
fclose($fp);
break;

case "off":
$fp = fopen("com4", "w");
fwrite($fp, chr(98));
fclose($fp);
break;


}
}

?>

<html>
<body>
<center>

<p>
<font size="4">Flick Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on" ?>">On</a></td><td><?php if($switch1 == 'on'){echo "<b>ON</b>";} ?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off" ?>">Off</a></td><td><?php if($switch1 == 'off'){echo "<b>OFF</b>";} ?></td></tr>
</table>
</p>

</center>
</body>
</html>


Arduino Code:

/*

  Used with the PHP code
  http://localhost/arduino.php
 */

void setup() {               

  Serial.begin(9600); // initialize serial communication:

  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {

// read the serial port:

if (Serial.available() > 0) {
int inByte = Serial.read();

switch (inByte) {
case 'a':
  digitalWrite(13, HIGH);   // set the LED on
  Serial.println("HIGH");
 
break;

case 'b':
  digitalWrite(13, LOW);   // set the LED off
  Serial.
  println("LOW");

break;
}

}

}

Wednesday, June 8, 2011

Arduino Servo mounted maze controlled by 4 buttons

OK, this one is a bit less impressive than I wanted it to be.  Then I found out that really cheap servos cannot take the weight of a decent sized maze.  So, plan B was to use a small maze as a 'proof of concept'.

Using 4 buttons for up, down, left and right to move the maze to get the bead from the start to the finish.

The code (which is below) is actually quite straight forward.  Most of my time was spend gluing bits of cardbaord together to make the various bits that are needed to mount the servos and the maze itself..

In case you need recommends on cardbaord the box that the bumper packs of pampers nappies come in are ideal.  (yes I have small kids)


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.


// Balance Table for Maze
// Winkleink
// 2011

// Pin 7 Left
// Pin 6 Right

//Pin 5 Up
//Pin 4 Down

// Pin 10 Servo - Up/Down
// Pin 9 Servo - Left/Right

// Library that makes working with Servos easier
#include <Servo.h>

// Set Button variables
int bLeft = 5;  // Left Button
int bRight = 4;  // Right Button
int bUp = 7;  // Up Button
int bDown = 6;  // Down Button

// Set Servo variables
Servo servoUD;  // Servo Up/Down on Pin 10 
Servo servoLR;  // Servo Left/Right on Pin 9

//Set Servo position variables
int posUD = 100;    // variable to store the Up/Down servo position
int posLR = 100;    // variable to store the Left/Right servo position

void setup()
{
  servoUD.attach(10);    // attaches the servo on pin 10 to the servo object
  servoLR.attach(9);    // attaches the servo on pin 9 to the servo object

  pinMode(bLeft,INPUT);  // bLeft - Left Button
  pinMode(bRight,INPUT);  // bRight - Right Button
  pinMode(bUp,INPUT);  // bUp - Up Button
  pinMode(bDown,INPUT);  // bDown - Down Button
} 

void loop()
{
// Position the Serrvo
// As the initial variable are center this also centers the table when it starts
servoUD.write(posUD);
servoLR.write(posLR);
 
if (digitalRead(bLeft) == HIGH){
posLR--;
}

if (digitalRead(bRight) == HIGH){
posLR++;
}

if (digitalRead(bUp) == HIGH){
posUD--;
}

if (digitalRead(bDown) == HIGH){
posUD++;
}

delay(50);
}

Sunday, May 22, 2011

Arduino Servo Catapult

Decided to put my cardboard gluing skills to a new use and tried to build a catapult.
Did the whole cutting and gluing the tower and the arm,  Mounted the servo and did the coding.

Everything worked - except the servo is too slow so the projectile (the bead) just kind of falls out.

The code is very simple.
Read a button.
Move the servo and the arm
Pause
Reset the servo and the arm


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.

// Catapult
// Read a button press on pin 4
// and Sweep the servo on pin 10

#include <Servo.h>

int button4 = 4;  // Used to start servo10
Servo myservo10;  // create servo object to control a servo on pin10

void setup()
{
myservo10.attach(10);  // attaches the servo on pin 10 to the servo object
pinMode(button4,INPUT);  // button4 used to start myservo10
myservo10.write(180); // Put servo all the say back
}

void loop()
{
  // Check if button2 has been pressed
  if (digitalRead(button4) == HIGH){

    // Fire catapult - fully
  myservo10.write(80); // Put servo all the say back

  // Pause after firing
  delay(500);

  // Reset to all the way back
  myservo10.write(180); // Put servo all the say back
  }
}