Home automation project

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 ? :slight_smile:

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

Any help guyz ? is it normally to take that much of memory?

All the libraries that you are including are jacking up your memory usage. While hosting a web server on an arduino is possible, from what I have read you may be better off using something like a raspberry pi to host the web server and having the arduino connect to the hardware. Just my 2 cents

is it normally to take that much of memory?

what do you mean?
I'd guess that the SD takes a sector's worth of RAM (512 bytes) at least.
You've got a few strings which could go into PROGMEM.

laadams85:
All the libraries that you are including are jacking up your memory usage. While hosting a web server on an arduino is possible, from what I have read you may be better off using something like a raspberry pi to host the web server and having the arduino connect to the hardware. Just my 2 cents

When I Just include the libraries and remove all my codes it only takes 18 kbs but when i put my codes back I get 29 +

You think that's normal ?

AWOL:

is it normally to take that much of memory?

what do you mean?
I'd guess that the SD takes a sector's worth of RAM (512 bytes) at least.
You've got a few strings which could go into PROGMEM.

What i mean is that i saw many sketches out there that are muuch longer than mine and still use almost 31 kbs
These sketches contain like 4 files each of them full of codes!, that's why i felt something is wrong with my code and somehow eating the memory.

PS by removing the toggle() and newDevice() functions i get extra 9 kbs!

If that's normal its ok for me to upgrade to Arduino Mega or something with 256kb, cause definitely i need more space for my project.

Thx for your help.

samehhady:
Any help guyz ? is it normally to take that much of memory?

The figures you posted refer to the size of the code, not the amount of RAM that the sketch uses. Almost all the code size is caused by the libraries you're using. As you add more code to your sketch you'll find the total size does increase, but bear in mind that your own code is (currently) only a very small part of the overall sketch so don't expect the total size to leap up due to small changes in your code.

If you do eventually exceed the program size for your Arduino, you have the option of migrating to an Arduino with more space available, or trying to optimise your code and minimise the libraries it uses.

You haven't mentioned the amount of RAM your sketch uses, and that is a separate constraint which you could easily hit before you run out of program space. Given the variety of libraries you're using, each of which will have their own memory requirements, you might already be close to or even past the amount of RAM available. In that case you will have the same options - optimise your code and use of libraries to reduce the RAM requirements, or migrate to an Arduino with more RAM available.

PeterH:
You haven't mentioned the amount of RAM your sketch uses, and that is a separate constraint which you could easily hit before you run out of program space. Given the variety of libraries you're using, each of which will have their own memory requirements, you might already be close to or even past the amount of RAM available. In that case you will have the same options - optimise your code and use of libraries to reduce the RAM requirements, or migrate to an Arduino with more RAM available.

Interesting point, I never tried to check the free ram available but it worth trying to use the free_memory function.
For now my sketch works perfectly without single error so i guess as for now am not having any issue with Ram right?
My only issue was the sketch size and how it ate the flash memory.

Thx for your help Peter, I will do more debugging as you said and if needed i will do Arduino upgrade.

Hi,
I expect a MEGA 2560 is in your future. Looking at a lot of Home Automation stuff there are too many limitations with just a regular 328 Arduino type.

And based on many years of doing home automation, I think you will keep adding stuff and sensors are relays for a long time. So give yourself some headroom.

You will also end up connecting LOTS of cables! It can be a rats nest. See: http://arduino-info.wikispaces.com/SensorShield
for a nice way to provide individual 3-pin connectors for each cable.

For a look at many cable solutions, see: http://arduino-info.wikispaces.com/Cables

DISCLAIMER: Mentioned stuff from my own shop...

terryking228:
I expect a MEGA 2560 is in your future. Looking at a lot of Home Automation stuff there are too many limitations with just a regular 328 Arduino type.

Yup i was just looking into it, and checking if the Ethernet shield is compatible with it or not.

Overall i wont face much issues with wires, this will be the base station for my home automation, it will send RF commands to other small nodes or points. So mainly i will replace wires with RF connection :slight_smile:

I got soo much in my mind and don't want to be limited with memory in my project.

Currently i did massive progress, I can save new devices by ID, save these devices in the Base SD card & save it to database and control the lights on and off through web interface.

Next mission to accept remote controls and connect them to specific nodes through web and let the base station ( learn ) new remote commands.

Once I complete the base station i should be able to control lights, sockets, rgb leds, cheap rf switches, TV's and all IR controlled appliance.

With roughly calculations the nodes would cost me like $30 which would be much cheaper than any other Home automation system.

Wish me luck guys, am getting more excited everyday and really want to finish this project and implement it in my new home :smiley:

You should be able to make your rf nodes for much cheaper than that. What RF modules are you using? Are you gonna use an arduino for every node?

laadams85:
You should be able to make your rf nodes for much cheaper than that. What RF modules are you using? Are you gonna use an arduino for every node?

I am using sparkfun RF Link Receiver - 4800bps (315MHz), they are the most expensive pieces in my system. , $9 for pair, $5 for receiver.
I wanted to make sure getting the best RF connection and not to get a cheap RF connection with poor signal like the cheap switches.

Yes i am going to use arduino on every node, Mini arduino pro to be specific.

I have 3 directions to choose from:

  • Arduino Mini ( Original ) --> expensive
  • Arduino Mini ( china ) -->$6
  • Micro controller board that act as Arduino programmed and connected to a receiver.

I think i will go with the second one at least till i get more experience with the third option.

Do you have a better, cheap & more efficient idea ? remember i will need to use Virtual wire in these nodes and will need to program each node to have its own unique ID using EEPROM.

Base station will send RF command 3453-on / 3453-off ( all nodes will receive command and the one with that ID will respond )

Thanks for your help =)

samehhady:
I have 3 directions to choose from:

There was an advert posted here recently announcing a rather neat looking Arduino clone that had an integrated radio transceiver - the whole thing was tiny. I'm not going to post a link, but you should be able to find it under goods and services.

PeterH:
There was an advert posted here recently announcing a rather neat looking Arduino clone that had an integrated radio transceiver

Peter that sounds very interesting!, I am searching the products and services forum but with no luck.

It would be great if you can help me with the Topic title or any searchable query that leads me to this topic.

Thx for your help.

This guy has done a lot of work with using the nRF24L01 modules. Depending on what you want at each node you can build them for ~$10-$15.

Hi, the nRF24L01 type modules work well and and low cost. Also have optional Power amplifier / Low Noise Preamp

How-To on the http://ArduinoInfo.Info WIKI here: http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
(also links to ManiacBug's work)

Some modules here: http://goo.gl/50h5H

DISCLAIMER: Mentioned stuff from my own shop...

samehhady:
Do you have a better, cheap & more efficient idea ? remember i will need to use Virtual wire in these nodes and will need to program each node to have its own unique ID using EEPROM.

I would use a transceiver instead so that you can ensure transmission with an acknowledge packet. Also, take a look at the JeeNode for a good example of a cheap wireless system with good software and community support. I use their libraries with a device similar to this one: http://arduino.cc/forum/index.php/topic,104203.0.html

I like the JeeNodes but they use the RFM12B transcievers which are more expensive ~$5-$8. You can get the nRF24L01 transcievers for ~$1-$2 off of ebay, and with the rf24 library they seem to work really well. The big question is what frequency you want to use 915MHz or 2.4GHz.

laadams85:
I like the JeeNodes but they use the RFM12B transcievers which are more expensive ~$5-$8. You can get the nRF24L01 transcievers for ~$1-$2 off of ebay, and with the rf24 library they seem to work really well. The big question is what frequency you want to use 915MHz or 2.4GHz.

Never been a fan of the form factor, but the fact that it's cheaper is nice. I haven't gotten the chance to test if the libraries work for the ATTinys.

Guys really thanks for your valuable information and your sharing!

Am in love with the nRF24L01 chip, it is VERY cheap and seems reliable and been used in many other projects.
The only odd thing about it is that there is no much information about connecting its pins with Arduino Nano, Uno, Mega, atTiny
I can see It needs 8 wires to be connected to Arduino. but i guess being a member in this lovely community will help me much to figure it out.

In this case I will leave the idea of using 315 MHz connection and will go with the 2.4, I wanted to use the 315 MHZ to be able to use the cheap Chinese remote controls with it, but in this case I can make cheaper remote controls using the nRF24L01 chip or using AtTiny micro controller.

This chip will totally help me reduce the node price, would be great if you can share with me step by step connection to Arduino.

Thx again, you guys are the best! :slight_smile: