View what it looks like, "App Inventor for Android Arduino Switch", on YouTube
My Arduino Sketch:
/*
Web Server
A simple web server that switches LED's on and off, based on input from an
Android App Inventor application
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* LED outputs attached to pins 5 and 6
created 13 Oct 2010
by Rogier van den Berg / rogiervandenberg.nl
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 201 };
//Settings for the two LED's
int ledPin = 5;
int ledState = LOW;
int powerLedPin = 6;
int powerLedState = LOW;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
void setup()
{
//Set the LED pins as output
pinMode(ledPin, OUTPUT);
pinMode(powerLedPin, OUTPUT);
//For debugging, set the Serial Output
Serial.begin(9600);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Turn one of the LED's on, to know it is ready to go!
digitalWrite(powerLedPin, HIGH);
}
void loop()
{
//Make sure requests are taken care of
handleIncomingInstruction();
//Make sure the power led stays on, when nothing happens.
if(powerLedState == LOW)
switchPowerLed();
}
void handleIncomingInstruction()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean newLine = true;
String line = "";
while (client.connected() && client.available()) {
char c = client.read();
//Serial.print(c);
switchPowerLed();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && newLine) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
}
if (c == '\n') {
// you're starting a new line
newLine = true;
evaluateLine(line);
line = "";
}
else if (c != '\r') {
// you've gotten a character on the current line
newLine = false;
line += c;
}
}
evaluateLine(line);
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
void evaluateLine(String line)
{
if (line.startsWith("tag", 0)) {
String instruction = line.substring(4, line.length());
Serial.println(instruction);
if (instruction == "TestOpdracht")
switchLed();
}
}
void switchLed()
{
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
Serial.println("We switchen de LED!");
}
void switchPowerLed()
{
// if the LED is off turn it on and vice-versa:
if (powerLedState == LOW)
powerLedState = HIGH;
else
powerLedState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(powerLedPin, powerLedState);
}
Was originally posted on Posterous (October 13th, 2010)