Pages: [1] 2   Go Down
Author Topic: Home automation project  (Read 1659 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ? smiley

PS. the bottom code is using 29.186 out of 32.256 ( sure later i will need to upgrade to another arduino )

Code:
#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);
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 24
Posts: 1766
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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-grin
Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 =)
« Last Edit: February 13, 2013, 05:33:59 am by samehhady » Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 5
Posts: 181
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
http://maniacbug.wordpress.com/2012/03/30/rf24network/
Logged

Pages: [1] 2   Go Up
Jump to: