Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Ethernet Issue? on: June 17, 2013, 06:08:57 am
In your first code, you haven't defined a DHCP server or an IP address. Where do you expect the IP address to come from?

In the second code, no gateway or netmask are defined. Is the IP address known by your router to be the Arduino's IP address?

The first code at the moment spits out the IP via the serial monitor and I have that working, but will try at adding it in manually, little clueless!

It seems that pins 0 + 1 are constantly high, and cannot be set with a digitalWrite LOW in the setup which is annoying, on top of that when I plug in my relays two will always activate for no apparent reason, If I unplug the headers and use a jumper wire and touch the pin to the relay with just one it doesn't go HIGH. very odd.
2  Using Arduino / Programming Questions / Ethernet Issue? on: June 17, 2013, 05:33:21 am
EDIT- main issue fixed, however ....

Is it easy enough to modify this code for a static IP?

Code:
/*********************** Marque's Arduino Controller Example ***********************/
/***********************  for questions: marque.l@gmail.com  ***********************/
/*
In the android app settingsscreen you can set up you IP and port.
For this example you need to call prefix 1 "B" and prefix 2 "C".
*/
#include <Ethernet.h>
#include <HTTPClient.h>
#include <SPI.h>

int RecievedString;
int valueB;
int valueC;
String readString = String(20);

int Relay240v1 = 1;
int Relay240v2 = 2;
int Relay24v1 = 5;
int Relay24v2 = 6;
int Relay24v3 = 7;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);   // port to listen on
EthernetClient client;

char content_main_top[] = "<body bgcolor=#101010><font color=white><center>";
char S1[] = "240v Relay 1 is On" ;
char S2[] = "240v Relay 1 is oOff" ;
char S3[] = "240v Relay 2 is On";
char S4[] = "240v Relay 2 is Off";
char S5[] = "24v Relay 1 is On";
char S6[] = "24v Relay 1 is Off";
char S7[] = "24v Relay 2 is Off";
char S8[] = "24v Relay 2 is Off";
char S9[] = "24v Relay 3 is Off";
char S10[] = "24v Relay 3 is Off";
char S404[] = "Not Found";

/************************** Setup **********************/
void setup()
{
  Serial.begin(9600);
  Serial.println("Getting IP......");
  Ethernet.begin(mac);
  Serial.print("My IP address: ");
  Ethernet.localIP().printTo(Serial);
  Serial.println();
  Serial.print("Gateway IP address is ");
  Ethernet.gatewayIP().printTo(Serial);
  Serial.println();
  Serial.print("DNS IP address is ");
  Ethernet.dnsServerIP().printTo(Serial);
  Serial.println();
 
  pinMode(Relay240v1, OUTPUT);
  pinMode(Relay240v2, OUTPUT);
  pinMode(Relay24v1, OUTPUT);
  pinMode(Relay24v2, OUTPUT);
  pinMode(Relay24v3, OUTPUT);
 
  digitalWrite(Relay240v1, LOW);
  digitalWrite(Relay240v2, LOW);
  digitalWrite(Relay24v1, LOW);
  digitalWrite(Relay24v2, LOW);
  digitalWrite(Relay24v3, LOW);
}
void loop()
{

checkclient();
}

void checkclient()
{
  EthernetClient client = server.available();
  if (client)   
  {
    boolean sentHeader = false;
    while (client.connected())
    {
      if (client.available())
      {
        if(!sentHeader){
        client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }
         char c = client.read();
         if (readString.length() < 20)
         Serial.print(c);
         {readString.concat(c);}
         if (c == 'H')
         {
           Serial.println();
           int Is = readString.indexOf("/");
           int Iq = readString.indexOf("?");
           int Ib = readString.indexOf("b");
           int Ic = readString.indexOf("c");
           if(readString.indexOf("?") > 1)
           {
             if (Ib == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ib+1));
               Serial.println(carray);
                valueB = atof(carray);
               Serial.print("B is now: ");
               Serial.println(valueB);
               client.print (content_main_top);
               client.print("B is now: ");
               client.print(valueB);
             }
            else if (Ic == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ic+1));
               Serial.println(carray);
                valueC = atof(carray);
               Serial.print("C is now: ");
               Serial.println(valueC);
               client.print (content_main_top);
               client.print("C is now: ");
               client.print(valueC);
             }
             else
             {
               char carray[2];
               readString.toCharArray( carray,2,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {
               case 1: action(1, client);digitalWrite(Relay240v1, HIGH);break;
               case 2: action(2, client);digitalWrite(Relay240v1, LOW);break;
               case 3: action(3, client);digitalWrite(Relay240v2, HIGH);break;
               case 4: action(4, client);digitalWrite(Relay240v2, LOW);break;
               case 5: action(5, client);digitalWrite(Relay24v1, HIGH);break;
               case 6: action(6, client);digitalWrite(Relay24v1, LOW);break;
               case 7: action(7, client);digitalWrite(Relay24v2, HIGH);break;
               case 8: action(8, client);digitalWrite(Relay24v2, LOW);break;
               case 9: action(9, client);digitalWrite(Relay24v3, HIGH);break;
               case 10: action(10, client);digitalWrite(Relay24v3, LOW);break;
               default: action(404, client);
               }
             }
          delay(1);
             client.stop();
             readString="";
             client.read(); client.read();
           }
         if (Iq < 0)
         {
         action(404, client);
         delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
         }
          delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
       }
     }
   }
 }
}

void action(int x, EthernetClient client)

  if (x == 1)
  {client.print (content_main_top);
   client.println(S1);
   Serial.println(S1);
  }
if (x == 2)
  {client.print (content_main_top);
   client.println(S2);
   Serial.println(S2);
  }
if (x == 3)
  {client.print (content_main_top);
   client.println(S3);
   Serial.println(S3);
  }
if (x == 4)
  {client.print (content_main_top);
   client.println(S4);
   Serial.println(S4);
  }
if (x == 5)
  {client.print (content_main_top);
   client.println(S5);
   Serial.println(S5);
  }
if (x == 6)
  {client.print (content_main_top);
   client.println(S6);
   Serial.println(S6);
  }
  if (x == 7)
  {client.print (content_main_top);
   client.println(S7);
   Serial.println(S7);
  }
  if (x == 8)
  {client.print (content_main_top);
   client.println(S8);
   Serial.println(S8);
  }
  if (x == 9)
  {client.print (content_main_top);
   client.println(S9);
   Serial.println(S9);
  }
  if (x == 10)
  {client.print (content_main_top);
   client.println(S10);
   Serial.println(S10);
  }
if (x == 404)
  {client.print (content_main_top);
   client.println(S404);
   Serial.println(S404);
  }
x=0;
}
3  Topics / Home Automation and Networked Objects / Re: My Domotic Home on: November 24, 2012, 03:04:29 am
Sure mate, sales@reanimate.com.au
4  Using Arduino / Programming Questions / Re: Program Alteration on: November 23, 2012, 01:03:06 am
Only just noticed that the picture didn't attach, but either way it turns out I fried my pin6 so I'm moved to pin 8 and changed the code to get it working which it now does. So all sweet. I think I'll worry about adding the RFID codes into the code manually later down the track. Out to buy a doorstrike now.
5  Using Arduino / Programming Questions / Re: Program Alteration on: November 20, 2012, 11:44:40 pm
Hi Paul,

I came across an old post of yours from a while back which might help however, my code is a little more complex to start with the void loop and void writechip. Do I need to remove more of the if / else statements from my above code to implement.

Code:
int validSiteCodes[] = {196, 452};
int validSerialNumbers[] = {15809, 3285};

Code:
boolean IsTagValid(int siteCode, int serialNumber)
{
   boolean valid = false;

   // Determine how many valid tags there are
   int validTagCount = sizeof(validSiteCodes)/sizeof(int);

   // Loop through the arrays to see if siteCode
   // and serialNumber are present

   for(int t=0; t<validTagCount; t++)
   {
      if(validSiteCode[t] == siteCode &&
         validSerialNumber == serialNumber)
      {
          valid = true;
          break;
      }
   }
 
   return valid;
}

Code:
if(IsTagValid(siteCode, serialNumber)
{
   // Open the door. It's cold out here!
}
6  Using Arduino / Programming Questions / Re: Program Alteration on: November 20, 2012, 08:41:55 pm
Well my Serial gives out usually correct information, this is from this morning:

All recorded Chips Erased! (Took three resets while holding pin6 low, after the fourth time and 30 second it registered)
All recorded Chips Erased!
Added Tag : 244A2B7 (Again had to reset a few times before the reader would see the card)
244A2B7
Matched Tag: 0 Access Granted (Took another board reset until the reader grabbed the id and about 10 seconds)

I am currently using a HID MiniProx but I have swapped the reader out with a new one and still no difference. The only thing I stupidly did was while building my button for pin6 (see attached pic) I shorted 5v to a Pin, so I'm wondering if I may have possibly buggered pin6, will run a simple code now to find out if its reading and outputting okay.

Sorry about the lack of info off the first post, end of a 19 hour day, brain wasn't working smiley-sad
7  Using Arduino / Programming Questions / Program Alteration on: November 20, 2012, 11:53:38 am
Hello again, yet another thing I am working on is RFID for my door and am using the current code. However while it does run, it is very buggy when adding and removing cards for some reason, can anyone see an easy fix to remove the button sections and just manually add my RFID code 244A2B7 directly to the code?

Code:
 
#include <EEPROM.h>
 
volatile unsigned long tagID = 32; 
//32 = bitwise 100000 - Set an initial mask to keep all tags 4 bytes long. WG26 (bits) plus another 6 = 32 bits or 4 bytes.

volatile unsigned long tmptagID;

volatile unsigned long lastBitArrivalTime;

volatile int bitCount = 0;
int buttonState = 0;
int rec_chips = 0;
int MAX_CARDS = 10;
byte CardBytes[4];
int MatchedTag = -1;
 
void ISRone(void)
{
  lastBitArrivalTime = millis();
 
  bitCount++;
  tagID <<= 1;
  tagID |= 1;
}
 
 
void ISRzero(void)
{
  lastBitArrivalTime = millis();
  bitCount++;
  tagID <<= 1;
}
 
void setup()
{
  // Arduino connections:
  pinMode(2, INPUT);   // Data0
  pinMode(3, INPUT);   // Data1
  pinMode(4, OUTPUT);  // Beep
  pinMode(5, OUTPUT);  // Green LED
  pinMode(6, INPUT);   // Programming switch
  pinMode(13, OUTPUT);   // To Transistor for lock output


  digitalWrite(4,HIGH);// Turns off beep
  digitalWrite(5,HIGH);// Turns off green led
  digitalWrite(6,HIGH);// Enable Pullup on switch
  digitalWrite(13,LOW); // Make sure the door is locked by default
 
  Serial.begin(57600); // Serial Start
 
  delay(1000); //Give the reader some time to settle so we don't fire interrupts yet.
 
  buttonState = digitalRead(6);
  if(buttonState == LOW){
  //If the button is held on power up, clear the EEPROM
  for(int i = 0; i < 40; i++){
      EEPROM.write(i, 0xFF);
      }
   Serial.println("All recorded Chips Erased!");   
   }
 
// --- Data0 / Green Wire ----------------------------
  digitalWrite(2, HIGH);  // Enable pull-up resistor
  attachInterrupt(0, ISRzero, FALLING);
//----------------------------------------------------
 
// --- Data1 / White Wire ----------------------------
  digitalWrite(3, HIGH);  // Enable pull-up resistor
  attachInterrupt(1, ISRone,  FALLING);
//----------------------------------------------------

  tagID = 32;
  bitCount = 0;
 
}
 
void loop()
{
  if(bitCount > 0 && millis() - lastBitArrivalTime >  250)
    {
    //convert the 32 bits into 4 byte arrays
    CardBytes[0] = lowByte(tagID);
    CardBytes[1] = highByte(tagID);
    tmptagID = tagID >> 16;
    CardBytes[2] = lowByte(tmptagID);
    CardBytes[3] = highByte(tmptagID);
   
    buttonState = digitalRead(6);
    if(buttonState == LOW){
      //Programming mode
      WriteChip();
      }
    else {
      //Normal mode
      rec_chips = 0;
      Serial.print(CardBytes[0],HEX);
      Serial.print(CardBytes[1],HEX);
      Serial.print(CardBytes[2],HEX);
      Serial.println(CardBytes[3],HEX);
      MatchedTag = Checkchip(CardBytes);
      if (MatchedTag != -1){
        Serial.print("Matched Tag: ");
        Serial.print(MatchedTag);
        Serial.println(" Access Granted");
        delay (200);
        digitalWrite(4,LOW);// Turns on beep
        delay (500);
        digitalWrite(4,HIGH);// Turns off beep
        delay (200);
        digitalWrite(4,LOW);// Turns on beep
        delay (500);
        digitalWrite(4,HIGH);// Turns off beep
        digitalWrite(5,LOW); // Turns on green led
        digitalWrite(13,HIGH); // Unlock the door     
        delay (5000);
        digitalWrite(5,HIGH); // Turns off green led
        digitalWrite(13,LOW); // Lock the door again
      }
      else {
        Serial.println("Access denied");
        digitalWrite(4,LOW);// Turns on beep
        delay (5000);
        digitalWrite(4,HIGH);// Turns off beep
     }
  }
  tagID = 32;
  bitCount = 0;
  }
}

void WriteChip(){
  if (rec_chips < MAX_CARDS){
    Serial.print("Added Tag : ");
    for(int i = 0; i < 4; i++){
      EEPROM.write(4*rec_chips + i, CardBytes[i]);
      Serial.print(CardBytes[i],HEX);
      }
      //give some feedback that the card was added by beeping weird.
      delay(200);
      digitalWrite(4,LOW);// Turns on beep
      delay(20);
      digitalWrite(4,HIGH);// Turns off beep
      delay(20);
      digitalWrite(4,LOW);// Turns on beep
      delay(20);
      digitalWrite(4,HIGH);// Turns off beep
      delay(20);
      digitalWrite(4,LOW);// Turns on beep
      delay(20);
      digitalWrite(4,HIGH);// Turns off beep
  }
  Serial.println("");
  ++rec_chips;
}
 
int Checkchip(byte TagData[]){
  int count = 0;
  byte tmpByte[4];
  while (count < 10){
    for(int i = 0; i < 4; i++){
      tmpByte[i] = EEPROM.read(4*count + i);
      // Serial.print(tmpByte[i], HEX);
      // Serial.print(" - ");
      // Serial.println(TagData[i], HEX);
    }
      if (tmpByte[0] == TagData[0] && tmpByte[1] == TagData[1] && tmpByte[2] == TagData[2] && tmpByte[3] == TagData[3])
        {
        // We matched all 4 bytes of data to one in EEPROM
        //Serial.print("Matched tag ");
        //Serial.println(count);
        return count;
        }
      else
        {
          //Serial.print("Did not match tag : ");
          //Serial.println(count);
          count++;
        }
  }
 // If we got here, no tags matched - Access denied!
 //Serial.println("No match found");
 return -1;
}
8  Using Arduino / Project Guidance / Re: Home Automation Possibilities on: November 20, 2012, 11:48:58 am
Hi Paul,

My PHP is certainly limited, but of C, Python, Processing etc I'm thinking its the quickest for me to continue with. For now my server is running win7, but I have a few machines I have lying around I can turn in linux boxes. As for relays, they will be controlling light rails, appliances and Solenoids.

The {} was just an example to break it up so its easier to read here. I knew I should have clarified that one when writing!!

The idea of the sensors attached to the controller is so the database has info on every room, I can then work on getting the server to trigger the relays depending on certain actions, ie dont turn off the lights when PIR detects movement. Turn on fan if over 30c etc etc.

Hope this helps to clarify. I currently have an arduino flicking of several relays around my house, but its a messy setup and I currently trigger my lights with 192.168.1.1/?2=on etc smiley
9  Using Arduino / Project Guidance / Home Automation Possibilities on: November 20, 2012, 09:17:18 am
Hello all,

I have been racking my brains for the 'best' solution to my problem but each has its merits and disadvantages. I am developing a home automation system for my home and would like to get some feedback, so anyway, here I go. (This is my rough idea)

As per the picture I want to have a 'control' module in each doorway which consists of:
- Etherten utilizing POE and ethernet for communications
- Temp Sensor
- Photocell
- 4 sets of on/off buttons (8 buttons total)
- PIR
- RGB Status LED

1. These modules will grab the pin status on a button press, PIR movement and every 30 seconds (for light and temp measurements) this is then pumps through to the server onto a database in a string format
{Controller-ID}{1:off,2:on,3:off,4:off,}{PIR:LOW}{LIGHT:643}{TEMP:32}

2. The server accepts data and checks to see if database holds the same values, if not update db and turn on relevant relays via secondary arduino, then send confirmation message back to original controler which flashes status ok LED.

I only have limited php experience so this is going to be a tricky one, any idea's of practical ways to do this would be appreciated as there is so many options.


10  Topics / Home Automation and Networked Objects / Re: Arduino Controller android app on: November 18, 2012, 09:25:50 pm
Howdy Marque,

Any idea why my other pins are high? I can pop a led into any of my unused pins and the led lights up?

Very soon I'm going to buy another etherten and use the first one as a lightswitch that will send the status of the pins through to another etherten connected to my server. Server will use php to write values to a db, then switch relays accordlingly. As I'm not the best coder its a daunting task!

Code:
/*********************** Marque's Arduino Controller Example ***********************/
/***********************  for questions: marque.l@gmail.com  ***********************/
/*
In the android app settingsscreen you can set up you IP and port.
For this example you need to call prefix 1 "B" and prefix 2 "C".
*/
#include <Ethernet.h>
#include <HTTPClient.h>
#include <SPI.h>

int RecievedString;
int valueB;
int valueC;
String readString = String(20);

int rm2light1 = 4;
int fishlight = 2;
int fishpump = 3;


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);   // port to listen on
EthernetClient client;

char content_main_top[] = "<body bgcolor=black><font color=white><center>";
char S1[] = "Room 2, Light 1 is on " ;
char S2[] = "Room 2, Light 1 is off" ;
char S3[] = "Fish Light is on";
char S4[] = "Fish Light is off";
char S5[] = "Fish Pump is on";
char S6[] = "Fish Pump is off";
char S404[] = "Not Found";

/************************** Setup **********************/
void setup()
{
  Serial.begin(9600);
  Serial.println("Getting IP......");
  Ethernet.begin(mac);
  Serial.print("My IP address: ");
  Ethernet.localIP().printTo(Serial);
  Serial.println();
  Serial.print("Gateway IP address is ");
  Ethernet.gatewayIP().printTo(Serial);
  Serial.println();
  Serial.print("DNS IP address is ");
  Ethernet.dnsServerIP().printTo(Serial);
  Serial.println();
  pinMode(rm2light1, OUTPUT);
  pinMode(fishlight, OUTPUT);
  pinMode(fishpump, OUTPUT);
  Serial.print("Initialising rm2light1 ");
  Serial.print("Initialising fishlight ");
  Serial.print("Initialising fishpump ");
  Serial.println();
  digitalWrite(rm2light1, LOW);
  digitalWrite(fishlight, LOW);
  digitalWrite(fishpump, LOW);
}
void loop()
{

checkclient();
}

void checkclient()
{
  EthernetClient client = server.available();
  if (client)   
  {
    boolean sentHeader = false;
    while (client.connected())
    {
      if (client.available())
      {
        if(!sentHeader){
        client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }
         char c = client.read();
         if (readString.length() < 20)
         Serial.print(c);
         {readString.concat(c);}
         if (c == 'H')
         {
           Serial.println();
           int Is = readString.indexOf("/");
           int Iq = readString.indexOf("?");
           int Ib = readString.indexOf("b");
           int Ic = readString.indexOf("c");
           if(readString.indexOf("?") > 1)
           {
             if (Ib == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ib+1));
               Serial.println(carray);
                valueB = atof(carray);
               Serial.print("B is now: ");
               Serial.println(valueB);
               client.print (content_main_top);
               client.print("B is now: ");
               client.print(valueB);
             }
            else if (Ic == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ic+1));
               Serial.println(carray);
                valueC = atof(carray);
               Serial.print("C is now: ");
               Serial.println(valueC);
               client.print (content_main_top);
               client.print("C is now: ");
               client.print(valueC);
             }
             else
             {
               char carray[2];
               readString.toCharArray( carray,2,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {
               case 1: action(1, client);digitalWrite(rm2light1, HIGH);break;
               case 2: action(2, client);digitalWrite(rm2light1, LOW);break;
               case 3: action(3, client);digitalWrite(fishlight, HIGH);break;
               case 4: action(4, client);digitalWrite(fishlight, LOW);break;
               case 5: action(5, client);digitalWrite(fishpump, HIGH);break;
               case 6: action(6, client);digitalWrite(fishpump, LOW);break;
               default: action(404, client);
               }
             }
          delay(1);
             client.stop();
             readString="";
             client.read(); client.read();
           }
         if (Iq < 0)
         {
         action(404, client);
         delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
         }
          delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
       }
     }
   }
 }
}

void action(int x, EthernetClient client)

  if (x == 1)
  {client.print (content_main_top);
   client.println(S1);
   Serial.println(S1);
  }
if (x == 2)
  {client.print (content_main_top);
   client.println(S2);
   Serial.println(S2);
  }
if (x == 3)
  {client.print (content_main_top);
   client.println(S3);
   Serial.println(S3);
  }
if (x == 4)
  {client.print (content_main_top);
   client.println(S4);
   Serial.println(S4);
  }
if (x == 5)
  {client.print (content_main_top);
   client.println(S5);
   Serial.println(S5);
  }
if (x == 6)
  {client.print (content_main_top);
   client.println(S6);
   Serial.println(S6);
  }
if (x == 404)
  {client.print (content_main_top);
   client.println(S404);
   Serial.println(S404);
  }
x=0;
}
11  Using Arduino / Programming Questions / All pins unused pins HIGH? on: November 14, 2012, 04:27:17 am
Hi guys,

I am using a bit of modified code, but for the life of me I cannot work out why all my pins are set to high, pins 2,3,4 start off as usual, but I can poke a LED into any other and its on smiley-grin Any ideas?

(code tags added by Moderator)
Code:
/*********************** Marque's Arduino Controller Example ***********************/
/***********************  for questions: marque.l@gmail.com  ***********************/
/*
In the android app settingsscreen you can set up you IP and port.
For this example you need to call prefix 1 "B" and prefix 2 "C".
*/
#include <Ethernet.h>
#include <HTTPClient.h>
#include <SPI.h>

int RecievedString;
int valueB;
int valueC;
String readString = String(20);

int rm2light1 = 4;
int fishlight = 2;
int fishpump = 3;


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);   // port to listen on
EthernetClient client;

char content_main_top[] = "<body bgcolor=black><font color=white><center>";
char S1[] = "Room 2, Light 1 is on " ;
char S2[] = "Room 2, Light 1 is off" ;
char S3[] = "Fish Light is on";
char S4[] = "Fish Light is off";
char S5[] = "Fish Pump is on";
char S6[] = "Fish Pump is off";
char S404[] = "Not Found";

/************************** Setup **********************/
void setup()
{
  Serial.begin(9600);
  Serial.println("Getting IP......");
  Ethernet.begin(mac);
  Serial.print("My IP address: ");
  Ethernet.localIP().printTo(Serial);
  Serial.println();
  Serial.print("Gateway IP address is ");
  Ethernet.gatewayIP().printTo(Serial);
  Serial.println();
  Serial.print("DNS IP address is ");
  Ethernet.dnsServerIP().printTo(Serial);
  Serial.println();
  pinMode(rm2light1, OUTPUT);
  pinMode(fishlight, OUTPUT);
  pinMode(fishpump, OUTPUT);
  Serial.print("Initialising rm2light1 ");
  Serial.print("Initialising fishlight ");
  Serial.print("Initialising fishpump ");
  Serial.println();
  digitalWrite(rm2light1, LOW);
  digitalWrite(fishlight, LOW);
  digitalWrite(fishpump, LOW);
}
void loop()
{

checkclient();
}

void checkclient()
{
  EthernetClient client = server.available();
  if (client)  
  {
    boolean sentHeader = false;
    while (client.connected())
    {
      if (client.available())
      {
        if(!sentHeader){
        client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }
         char c = client.read();
         if (readString.length() < 20)
         Serial.print(c);
         {readString.concat(c);}
         if (c == 'H')
         {
           Serial.println();
           int Is = readString.indexOf("/");
           int Iq = readString.indexOf("?");
           int Ib = readString.indexOf("b");
           int Ic = readString.indexOf("c");
           if(readString.indexOf("?") > 1)
           {
             if (Ib == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ib+1));
               Serial.println(carray);
                valueB = atof(carray);
               Serial.print("B is now: ");
               Serial.println(valueB);
               client.print (content_main_top);
               client.print("B is now: ");
               client.print(valueB);
             }
            else if (Ic == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ic+1));
               Serial.println(carray);
                valueC = atof(carray);
               Serial.print("C is now: ");
               Serial.println(valueC);
               client.print (content_main_top);
               client.print("C is now: ");
               client.print(valueC);
             }
             else
             {
               char carray[2];
               readString.toCharArray( carray,2,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {
               case 1: action(1, client);digitalWrite(rm2light1, HIGH);break;
               case 2: action(2, client);digitalWrite(rm2light1, LOW);break;
               case 3: action(3, client);digitalWrite(fishlight, HIGH);break;
               case 4: action(4, client);digitalWrite(fishlight, LOW);break;
               case 5: action(5, client);digitalWrite(fishpump, HIGH);break;
               case 6: action(6, client);digitalWrite(fishpump, LOW);break;
               default: action(404, client);
               }
             }
          delay(1);
             client.stop();
             readString="";
             client.read(); client.read();
           }
         if (Iq < 0)
         {
         action(404, client);
         delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
         }
          delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
       }
     }
   }
 }
}

void action(int x, EthernetClient client)
{  
  if (x == 1)
  {client.print (content_main_top);
   client.println(S1);
   Serial.println(S1);
  }
if (x == 2)
  {client.print (content_main_top);
   client.println(S2);
   Serial.println(S2);
  }
if (x == 3)
  {client.print (content_main_top);
   client.println(S3);
   Serial.println(S3);
  }
if (x == 4)
  {client.print (content_main_top);
   client.println(S4);
   Serial.println(S4);
  }
if (x == 5)
  {client.print (content_main_top);
   client.println(S5);
   Serial.println(S5);
  }
if (x == 6)
  {client.print (content_main_top);
   client.println(S6);
   Serial.println(S6);
  }
if (x == 404)
  {client.print (content_main_top);
   client.println(S404);
   Serial.println(S404);
  }
x=0;
}
12  Topics / Home Automation and Networked Objects / Re: Arduino Controller android app on: November 12, 2012, 07:48:07 am
Hi Marque,

Wonderful combo you have written here, has become very handy to me. I would love to know how you went about coding the android part. Anyway I showed my code mods and what not here http://arduino.cc/forum/index.php/topic,132021.0.html

I'd like to control my relay setup with buttons also, would you have any tips for integrating this?

Thank you again!
13  Topics / Home Automation and Networked Objects / My Domotic Home on: November 12, 2012, 05:12:40 am
Hi guys,

Been a while since I posted but I thought I would throw this up. I started off last night with Marque's excellent little script to allow for android, then built a little site for iPad access. As I am in a rented house this is all done with modified GPO's and old wireless double plug housings (see pic). Anyhoo, Once I move into my own house and tear the walls out the wiring will be a bit neater, but until then...

I've thrown the site mockup online @ www.reanimate.com.au/domotic/index.html (will need to sexify this with something nice and pretty)

Finally on the scheduling page I need to figure out how to get that working with php, cron and a flatfile, record the time and relay down and then open the url when the time is now. But a lack of php is causing a few headaches.

Code:
/*********************** Marque's Arduino Controller Example ***********************/
/***********************  for questions: marque.l@gmail.com  ***********************/
/*
In the android app settingsscreen you can set up you IP and port.
For this example you need to call prefix 1 "B" and prefix 2 "C".

*/
#include <Ethernet.h>
#include <HTTPClient.h>
#include <SPI.h>

int RecievedString;
int valueB;
int valueC;
String readString = String(20);

int rm2light1 = 4;
int fishlight = 2;
int fishpump = 3;


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);   // port to listen on
EthernetClient client;

char content_main_top[] = "<body bgcolor=black><font color=white><center>";
char S1[] = "Room 2, Light 1 is on " ;
char S2[] = "Room 2, Light 1 is off" ;
char S3[] = "Fish Light is on";
char S4[] = "Fish Light is off";
char S5[] = "Fish Pump is on";
char S6[] = "Fish Pump is off";
char S404[] = "Not Found";

/************************** Setup **********************/
void setup()
{
  Serial.begin(9600);
  Serial.println("Getting IP......");
  Ethernet.begin(mac);
  Serial.print("My IP address: ");
  Ethernet.localIP().printTo(Serial);
  Serial.println();
  Serial.print("Gateway IP address is ");
  Ethernet.gatewayIP().printTo(Serial);
  Serial.println();
  Serial.print("DNS IP address is ");
  Ethernet.dnsServerIP().printTo(Serial);
  Serial.println();
  pinMode(rm2light1, OUTPUT);
  pinMode(fishlight, OUTPUT);
  pinMode(fishpump, OUTPUT);
  Serial.print("Initialising rm2light1 ");
  Serial.print("Initialising fishlight ");
  Serial.print("Initialising fishpump ");
  Serial.println();
  digitalWrite(rm2light1, LOW);
  digitalWrite(fishlight, LOW);
  digitalWrite(fishpump, LOW);
}
void loop()
{

checkclient();
}

void checkclient()
{
  EthernetClient client = server.available();
  if (client)  
  {
    boolean sentHeader = false;
    while (client.connected())
    {
      if (client.available())
      {
        if(!sentHeader){
        client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }
         char c = client.read();
         if (readString.length() < 20)
         Serial.print(c);
         {readString.concat(c);}
         if (c == 'H')
         {
           Serial.println();
           int Is = readString.indexOf("/");
           int Iq = readString.indexOf("?");
           int Ib = readString.indexOf("b");
           int Ic = readString.indexOf("c");
           if(readString.indexOf("?") > 1)
           {
             if (Ib == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ib+1));
               Serial.println(carray);
                valueB = atof(carray);
               Serial.print("B is now: ");
               Serial.println(valueB);
               client.print (content_main_top);
               client.print("B is now: ");
               client.print(valueB);
             }
            else if (Ic == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ic+1));
               Serial.println(carray);
                valueC = atof(carray);
               Serial.print("C is now: ");
               Serial.println(valueC);
               client.print (content_main_top);
               client.print("C is now: ");
               client.print(valueC);
             }
             else
             {
               char carray[2];
               readString.toCharArray( carray,2,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {
               case 1: action(1, client);digitalWrite(rm2light1, HIGH);break;
               case 2: action(2, client);digitalWrite(rm2light1, LOW);break;
               case 3: action(3, client);digitalWrite(fishlight, HIGH);break;
               case 4: action(4, client);digitalWrite(fishlight, LOW);break;
               case 5: action(5, client);digitalWrite(fishpump, HIGH);break;
               case 6: action(6, client);digitalWrite(fishpump, LOW);break;
               default: action(404, client);
               }
             }
          delay(1);
             client.stop();
             readString="";
             client.read(); client.read();
           }
         if (Iq < 0)
         {
         action(404, client);
         delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
         }
          delay(1);
         client.stop();
         readString="";
         client.read(); client.read();
       }
     }
   }
 }
}

void action(int x, EthernetClient client)
{  
  if (x == 1)
  {client.print (content_main_top);
   client.println(S1);
   Serial.println(S1);
  }
if (x == 2)
  {client.print (content_main_top);
   client.println(S2);
   Serial.println(S2);
  }
if (x == 3)
  {client.print (content_main_top);
   client.println(S3);
   Serial.println(S3);
  }
if (x == 4)
  {client.print (content_main_top);
   client.println(S4);
   Serial.println(S4);
  }
if (x == 5)
  {client.print (content_main_top);
   client.println(S5);
   Serial.println(S5);
  }
if (x == 6)
  {client.print (content_main_top);
   client.println(S6);
   Serial.println(S6);
  }
if (x == 404)
  {client.print (content_main_top);
   client.println(S404);
   Serial.println(S404);
  }
x=0;
}

14  Using Arduino / Project Guidance / Arduino Ethernet + Code on: February 20, 2012, 05:08:13 am
Not sure if this is the more appropriate section as its still in progress (reposted from Home Automation, couldn't find a delete button)

Hi Guys,

Been working for a few weeks on a project to turn on/off some power points via relays and using the EtherTen's ethernet features, so to start with I started with a basic bit of code to start a webserver on the arduino with a simple bit of text to click on and off the lights. However I didn't want to use the actual arduino hosted page due to its not so pretty looks, so I hosted a website on my XAMPP server that looks a bit nice and created a link to the arduino ie 192.168.2.102:84/?on1. The problem was that it navigated away from my pretty page and after three days I couldn't get an iframe to work so I have to use Jquery and JSON, see code below.

The XAMPP side code
Code:
<span style="float:right;padding-top:10px;padding-right:12px;font-size:18px; font-weight:bold;"><span onclick="javascript:{$.get('http://192.168.2.102:84/?on2');}">ON</span> | <span onclick="javascript:{$.get('http://192.168.2.102:84/?off2');}">OFF</span></li></span>

The Ardunio Code
Code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 2, 102 }; // ip in lan
byte gateway[] = { 192, 168, 2, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString;

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  pinMode(2, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("SFAP Running"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          ///////////////////// control arduino pin
          if(readString.indexOf("on4") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf("off4") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led 4 Off");
          }
          
          if(readString.indexOf("on2") >0)//checks for on
          {
            digitalWrite(2, HIGH);    // set pin 4 high
            Serial.println("Led 2 On");
          }
          if(readString.indexOf("off2") >0)//checks for off
          {
            digitalWrite(2, LOW);    // set pin 4 low
            Serial.println("Led 2 Off");
          }
          
          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Jotaris");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: application/json");
          client.println();

client.print("{\"content\":\"");
if(readString.indexOf("on2")>0)
{
client.print("Switch 1 = ON");
}
else if(readString.indexOf("off2")>0)
{
client.print("Switch 1 = OFF");
}
        else
                {
                client.print("Switch 1 = UNKNOWN");
                }
client.print("; ");
if(readString.indexOf("on4")>0)
{
client.print("Switch 2 = ON");
}
else if(readString.indexOf("off4")>0)
{
client.print("Switch 2 = OFF");
}
        else
                {
                client.print("Switch 2 = UNKNOWN");
                }
client.print("\"}");

             }

          delay(1);
          //stopping client
          client.stop();
          
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

The questions are...

1. Is there a way to for me to read the state of the relay (its not double pole so it would have to remember if its on or off) and request the information from my XAMPP site so when I go the page it can tell me if my lights are on?

2. I have a PIR that I would like to activate one of the relays by motion sensing as well as by my website? Ie I have a light outside plugged into one of the powerpoints with a relay and I would like it to turn on when I walk past. As well as being able to manually turn in on?

I can run a mysql database and php off my server to hold values if there is a way for the arduino to read commands from a database. That way the DB could remember the relay state.

Idea's and thoughts would be much appreciated.
15  Topics / Home Automation and Networked Objects / EtherTen + Ridiculous code on: February 19, 2012, 01:23:04 am
Hi Guys,

Been working for a few weeks on a project to turn on/off some power points via relays and using the EtherTen's ethernet features, so to start with I started with a basic bit of code to start a webserver on the arduino with a simple bit of text to click on and off the lights. However I didn't want to use the actual arduino hosted page due to its not so pretty looks, so I hosted a website on my XAMPP server that looks a bit nice and created a link to the arduino ie 192.168.2.102:84/?on1. The problem was that it navigated away from my pretty page and after three days I couldn't get an iframe to work so I have to use Jquery and JSON, see code below.

The XAMPP side code
Code:
<span style="float:right;padding-top:10px;padding-right:12px;font-size:18px; font-weight:bold;"><span onclick="javascript:{$.get('http://192.168.2.102:84/?on2');}">ON</span> | <span onclick="javascript:{$.get('http://192.168.2.102:84/?off2');}">OFF</span></li></span>

The Ardunio Code
Code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 2, 102 }; // ip in lan
byte gateway[] = { 192, 168, 2, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString;

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  pinMode(2, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("SFAP Running"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging

          ///////////////////// control arduino pin
          if(readString.indexOf("on4") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf("off4") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led 4 Off");
          }
          
          if(readString.indexOf("on2") >0)//checks for on
          {
            digitalWrite(2, HIGH);    // set pin 4 high
            Serial.println("Led 2 On");
          }
          if(readString.indexOf("off2") >0)//checks for off
          {
            digitalWrite(2, LOW);    // set pin 4 low
            Serial.println("Led 2 Off");
          }
          
          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Jotaris");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: application/json");
          client.println();

client.print("{\"content\":\"");
if(readString.indexOf("on2")>0)
{
client.print("Switch 1 = ON");
}
else if(readString.indexOf("off2")>0)
{
client.print("Switch 1 = OFF");
}
        else
                {
                client.print("Switch 1 = UNKNOWN");
                }
client.print("; ");
if(readString.indexOf("on4")>0)
{
client.print("Switch 2 = ON");
}
else if(readString.indexOf("off4")>0)
{
client.print("Switch 2 = OFF");
}
        else
                {
                client.print("Switch 2 = UNKNOWN");
                }
client.print("\"}");

             }

          delay(1);
          //stopping client
          client.stop();
          
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

The questions are...

1. Is there a way to for me to read the state of the relay (its not double pole so it would have to remember if its on or off) and request the information from my XAMPP site so when I go the page it can tell me if my lights are on?

2. I have a PIR that I would like to activate one of the relays by motion sensing as well as by my website? Ie I have a light outside plugged into one of the powerpoints with a relay and I would like it to turn on when I walk past. As well as being able to manually turn in on?

I can run a mysql database and php off my server to hold values if there is a way for the arduino to read commands from a database. That way the DB could remember the relay state.

Idea's and thoughts would be much appreciated.
Pages: [1] 2