Go Down

Topic: Home automation with Arduino Uno and Ethernet Shield (Read 10491 times) previous topic - next topic

rbright

Paradigm, good to see you made it...
could you for the benifit of others watching these posts send your final code & screen dumps.
Regards

Paradigm

Hi rbright,

             Sure. Below is the final code and the output captured sent out to the serial port

Hi Paul,

            Thanks for your help. Is there anything I can do better regarding the coding? Pls advise. Thanks

Code: [Select]

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xB0, 0xB2 }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
EthernetServer server(80); //server port

String readString;
char Area[4] = "\0";
char AreaDim[6] = "\0";

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

void setup(){
 
  //start Ethernet
  Ethernet.begin(mac, ip);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
}

void loop(){
  // Create a client connection
  EthernetClient 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

          client.println(F("HTTP/1.1 200 OK")); //send new page
          client.println(F("Content-Type: text/html"));
          client.println();

          client.println(F("<HTML>"));
          client.println(F("<HEAD>"));
          client.println(F("<TITLE>Home Automation System</TITLE>"));
          client.println(F("</HEAD>"));
          client.println(F("<BODY>"));

          client.println(F("<H1>Home Automation System</H1><br />"));

          client.println(F("<form name=input method=get>"));
          client.println(F("Select Room:"));
          client.println(F("<select name=Room>"));
          client.println(F("<option value=R1>Room 1</option>"));
          client.println(F("<option value=R2>Room 2</option>"));
          client.println(F("<option value=R3>Room 3</option>"));
          client.println(F("<option value=R4>Room 4</option>"));
          client.println(F("</select><br /><br />"));
         
          client.println(F("Light: <INPUT type=submit name=Toggle value=L1>"));
          client.println(F("Dimmable: <INPUT type=submit name=Dim value=D1><br /><br />"));
          client.println(F("Light: <INPUT type=submit name=Toggle value=L2>"));
          client.println(F("Dimmable: <INPUT type=submit name=Dim value=D2><br /><br />"));
          client.println(F("Light: <INPUT type=submit name=Toggle value=L3>"));
          client.println(F("Dimmable: <INPUT type=submit name=Dim value=D3><br /><br />"));
          client.println(F("Light: <INPUT type=submit name=Toggle value=L4>"));
          client.println(F("Dimmable: <INPUT type=submit name=Dim value=D4><br /><br />"));
          client.println(F("Light: <INPUT type=submit name=Toggle value=L5>"));
          client.println(F("Dimmable: <INPUT type=submit name=Dim value=D5><br /><br /><br />"));
         
          client.println(F("Select Dim Level:<select name=Brightness>"));
          client.println(F("<option value=_1>10%</option>"));
          client.println(F("<option value=_2>20%</option>"));
          client.println(F("<option value=_3>30%</option>"));
          client.println(F("<option value=_4>40%</option>"));
          client.println(F("<option value=_5>50%</option>"));
          client.println(F("<option value=_6>60%</option>"));
          client.println(F("<option value=_7>70%</option>"));
          client.println(F("<option value=_8>80%</option>"));
          client.println(F("<option value=_9>90%</option>"));
          client.println(F("<option value=_A>100%</option>"));         
          client.println(F("</select><br /><br />"));
          client.println(F("</form>"));
          client.println(F("</BODY>"));
          client.println(F("</HTML>"));

          delay(1);
          //stopping client
          client.stop();
         
          if(readString.indexOf("Toggle") >0)
          {
          int area_room = readString.lastIndexOf("Room=");
          for (int i=0; i<2; i++)
            {
              Area[i] = readString[area_room + i + 5];
            }
          int area_Light = readString.lastIndexOf("Toggle=");
          for (int i=2; i<4; i++)
            {
              Area[i] = readString[area_Light + i + 5];
            }
            Serial.print("AT+SCAST:");
            Serial.println(Area);
          }
          if(readString.indexOf("Dim") >0)
          {
          int areadim_room = readString.lastIndexOf("Room=");
          for (int i=0; i<2; i++)
            {
              AreaDim[i] = readString[areadim_room + i + 5];
            }
          int areadim_Light = readString.lastIndexOf("Dim=");
          for (int i=2; i<4; i++)
            {
              AreaDim[i] = readString[areadim_Light + i + 2];
            }
          int areadim_bright = readString.lastIndexOf("Brightness=");
          for (int i=4; i<6; i++)
            {
              AreaDim[i] = readString[areadim_bright + i + 7];
            }
            Serial.print("AT+SCAST:");
            Serial.println(AreaDim); 
          }   
          //clearing string for next read
          readString="";
          for(int j=0; j<6; j++)
           {
             Area[j] = 0;
             AreaDim[j] = 0;
           }       
        }
      }
    }
  }
}


The Webpage

Go Up