Hello Guyz,
Am a newbie Arduino fan, mainly am web developer that love getting to know new technology.
My mission is to use my web development skills and combine it with Arduino to create a neat home automation project.
Currently am using Webduino and trying to tweak it to work as i want it, but i can see am using loots of memory already with simple lines of codes! and am not sure whats the problem.
Can you please advice me and help me to optimize my code ?
PS. the bottom code is using 29.186 out of 32.256 ( sure later i will need to upgrade to another arduino )
#define WEBDUINO_FAIL_MESSAGE "<h1>Request Failed</h1>"
#include "SPI.h" // new include
#include "avr/pgmspace.h" // new include
#include "SD.h"
#include "Ethernet.h"
#include "WebServer.h"
//#include "VirtualWire.h"
#define VERSION_STRING "0.1"
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static uint8_t ip[] = { 192, 168, 1, 100 };
bool repeat;
char name[16], value[16], file[30], filex[30];
File mF;
String fn, fnx, x, y;
/* This creates an instance of the webserver. By specifying a prefix
* of "", all pages will be at the root of the server. */
#define PREFIX ""
WebServer webserver(PREFIX, 80);
void helloCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
 /* this line sends the standard "we're all OK" headers back to the
  browser */
 server.httpSuccess();
/* if we're handling a GET or POST, we can output our data here.
  For a HEAD request, we just stop after outputting headers. */
 if (type == WebServer::HEAD)
  return;
//home page
}
void toggle(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
 server.httpSuccess();
if (type != WebServer::HEAD)
 {
  repeat = server.readPOSTparam(name, 16, value, 16);
  fn = String(value) + "off.txt";
  fn.toCharArray(file, 30);
  if (SD.exists(file)){
   x = "off";
   y = "on";
  }else{
   x = "on";
   y = "off";
   fnx = String(value) + "on.txt";
   fnx.toCharArray(file, 30);
  }
   SD.remove(file);
   fn = String(value) + y + ".txt";
   fn.toCharArray(file, 30);
   mF = SD.open(file, FILE_WRITE);
   mF.close();
   if(x == "off"){
   digitalWrite(9, HIGH);
   server.print(y);
   }else if(x == "on"){
   digitalWrite(9, LOW);
   server.print(y);
   }
   //asm volatile (" jmp 0");
 }
}
void bLink(){
digitalWrite(9, HIGH);
 delay(100);
 digitalWrite(9, LOW);
 delay(100);Â
}
void newDevice(WebServer &server, WebServer::ConnectionType type, char *, bool)
{
 server.httpSuccess();
if (type != WebServer::HEAD)
 {
  repeat = server.readPOSTparam(name, 16, value, 16);
 fn = String(value) + "off.txt";
 fn.toCharArray(file, 30);
 mF = SD.open(file, FILE_WRITE);
 mF.close();
 delay(100);
 for (int i=0; i <= 4; i++){
 bLink();
 delay(50);
 }
 server.print("created");
 }
}
void setup()
{
 /* initialize the Ethernet adapter */
 Ethernet.begin(mac, ip);
 pinMode(9, OUTPUT);
  if (SD.begin(4)) {
  //Serial.println("Card Ok");
  }
 /* setup our default command that will be run when the user accesses
 * the root page on the server */
 webserver.setDefaultCommand(&helloCmd);
 /* setup our default command that will be run when the user accesses
 * a page NOT on the server */
 webserver.setFailureCommand(&helloCmd);
 /* run the same command if you try to load /index.html, a common
 * default page name */
 webserver.addCommand("index.html", &helloCmd);
 webserver.addCommand("toggle", &toggle);
 webserver.addCommand("newDevice", &newDevice);
 /* start the webserver */
 webserver.begin();
}
void loop()
{
 char buff[64];
 int len = 64;
 /* process incoming connections one at a time forever */
 webserver.processConnection(buff, &len);
}