13 October 2010

Android Arduino Switch with a TinyWebDB hack

You can easily control your Arduino with an Android App Inventor App. Make a basic app, using the TinyWebDB in the Android App Inventor. If you specify the IP of the Arduino as a server, the Android app things it is storing values in a WebDB. But basically, it is just triggering a command on the Arduino.

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
  * 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
   // start the Ethernet connection and the server:
   Ethernet.begin(mac, ip);
   //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
   //Make sure the power led stays on, when nothing happens.
   if(powerLedState == LOW)
 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();
         // 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");
         if (c == '\n') {
           // you're starting a new line
           newLine = true;
           line = "";
         else if (c != '\r') {
           // you've gotten a character on the current line
           newLine = false;
           line += c; 
      // give the web browser time to receive the data
     // close the connection:
 void evaluateLine(String line)
   if (line.startsWith("tag", 0)) {
     String instruction = line.substring(4, line.length());
     if (instruction == "TestOpdracht")
  void switchLed()
    // if the LED is off turn it on and vice-versa:
     if (ledState == LOW)
       ledState = HIGH;
       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;
       powerLedState = LOW;
       // set the LED with the ledState of the variable:
     digitalWrite(powerLedPin, powerLedState);
Was originally posted on Posterous (October 13th, 2010)