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
  
  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)

4 comments:

  1. Very interesting project. If it isn't too much to ask, can you include a link with the app Invenntor code?

    Thank you

    ReplyDelete
  2. Hi Antonio, sure I can! Import this project in your App inventor: https://docs.google.com/open?id=13G80qipX40PaMmMDtX2ft6X5kSFTltopZMag8SrWX-ezovhWmcegBli5Xs0L It is using GET-requests to trigger things on your Arduino.

    ReplyDelete
  3. Hi! I'm trying to open the project in my App inventor but it's not working, the message that I get is: The selected project is not a project source file. Project source files are zip files...Any idea? Thank you!

    ReplyDelete
  4. Yes, you are downloading the contents of the zip file. The link points to the zip itself. Use my link above, goto file, Download. And then it should go OK :)

    ReplyDelete