Home Automation, meeting 1, 12/8/15

  • 08 Dec 2015 9:34 PM
    Message # 3688709

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

    // ethernet configuration
    byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
    IPAddress ip(192,168,0,111); // P1 --> { 10, 1, 1, 5 };
    EthernetServer server(80);              // port 80 is default for HTTP

    // initial
    const int LEDPIN = 7;         
    const int LED2PIN = 8;         
    const int THERMOPIN = A0;      // LM35 temp sensor

    char c = 0;           // received data
    char command[2] = "\0";  // command

    float t_degF = 0.0;

    void setup()
    {
        Serial.begin(9600);
        Serial.println("\nMonitor-Server 1.0");
       
      Ethernet.begin(mac);
      server.begin();
      Serial.print("Local IP: "); Serial.println(Ethernet.localIP());
     
      pinMode(LEDPIN, OUTPUT);
      pinMode(LED2PIN, OUTPUT);
      analogReference(INTERNAL);
      (void)analogRead(THERMOPIN);    // discard 1st read after ref-change
    }


    void loop()
    {
      EthernetClient client = server.available();
      // detect if current is the first line
      boolean current_line_is_first = true;

      if (client) {
        // an http request ends with a blank line
        boolean current_line_is_blank = true;
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();
            // if we've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so we can send a reply
            if (c == '\n' && current_line_is_blank) {
              // send a standard http response header
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println();

              // auto reload webpage every 5 second
              client.println("<META HTTP-EQUIV=REFRESH CONTENT=2 URL=>");
             
              // webpage title
              client.println("<center><p><h1>Arduino + Ethernet Shield Application v1.0</h1></p><center><hr><br />");

              // read analog pin 1 for the value of photocell
              t_degF = analogRead(THERMOPIN) * 110./1024.*1.8 + 32.0;
              client.print("<p><h2>Room Temperature: ");
              client.println(t_degF, 1);
              client.println("</h2></p>");
             
             
              // button functions
              client.println("<form  method=get name=form>");
              client.println("<button name=b value=1 type=submit style=height:80px;width:150px>LED1 On</button>");
              client.println("<button name=b value=2 type=submit style=height:80px;width:150px>LED1 Off</button><br><br>");
              client.println("<button name=b value=3 type=submit style=height:80px;width:150px>LED2 On</button>");
              client.println("<button name=b value=4 type=submit style=height:80px;width:150px>LED2 Off</button>");
              client.println("</form><br />");
                       
              break;
            }
            if (c == '\n') {
              // we're starting a new line
              current_line_is_first = false;
              current_line_is_blank = true;
            }
            else if (c != '\r') {
              // we've gotten a character on the current line
              current_line_is_blank = false;
            }
            // get the first http request
            if (current_line_is_first && c == '=') {
              for (int i = 0; i < 1; i++) {
                c = client.read();
                command[i] = c;
              }

              // LED control
              if (!strcmp(command, "1"))
                digitalWrite(LEDPIN, HIGH);
              else if (!strcmp(command, "2"))
                digitalWrite(LEDPIN, LOW);
              else if (!strcmp(command, "3"))
                digitalWrite(LED2PIN, HIGH);
              else if (!strcmp(command, "4"))
                digitalWrite(LED2PIN, LOW);
            }
          }
        }
        // give the web browser time to receive the data
        delay(1);
        client.stop();
      }
    }


  • 09 Dec 2015 3:41 PM
    Reply # 3690319 on 3688709

    Code from Dec 8 with comments added. No logic changes.


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

    // Ethernet configuration
    byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x06 }; // must be unique in your network
    IPAddress ip(192,168,0,111); // P1 --> { 10, 1, 1, 5 };
    EthernetServer server(80);              // port 80 is default for HTTP

    // Define I/O pins
    const int LEDPIN = 7;          // LED1
    const int LED2PIN = 8;         // LED2
    const int THERMOPIN = A0;      // LM35 temp sensor

    char c = 0;           // received data
    char command[2] = "\0";  // command

    float t_degF = 0.0;

    void setup()
    {
      // Init serial output for debugging
      Serial.begin(9600);
      Serial.println("\nMonitor-Server 1.0");

      // Initialize Ethernet & server
      Ethernet.begin(mac);
      server.begin();
      Serial.print("Local IP: "); Serial.println(Ethernet.localIP());

      // Set I/O pin modes
      pinMode(LEDPIN, OUTPUT);
      pinMode(LED2PIN, OUTPUT);
      analogReference(INTERNAL);    // Use 1.1v analog reference
      (void)analogRead(THERMOPIN);    // discard 1st read after ref-change
    }


    void loop()
    {
      EthernetClient client = server.available();
      // detect if current is the first line
      boolean current_line_is_first = true;

      if (client) {
        // an http request ends with a blank line
        boolean current_line_is_blank = true;
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();

            // If we've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so we can send a reply
            if (c == '\n' && current_line_is_blank) {

              // Send a standard http response header
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println();

              // Auto reload webpage every 2 sec
              client.println("<META HTTP-EQUIV=REFRESH CONTENT=2 URL=>");

              // Webpage title
              client.println("<center><p><h1>Arduino + Ethernet Shield Application v1.0</h1></p><center><hr><br />");

          // Calculate room temperature and display it
              t_degF = analogRead(THERMOPIN) * 110./1024.*1.8 + 32.0;
              client.print("<p><h2>Room Temperature: ");
              client.println(t_degF, 1);
              client.println("</h2></p>");

              // Define buttons and the values each sends back to us
              client.println("<form  method=get name=form>");
              client.println("<button name=b value=1 type=submit style=height:80px;width:150px>LED1 On</button>");
              client.println("<button name=b value=2 type=submit style=height:80px;width:150px>LED1 Off</button><br><br>");
              client.println("<button name=b value=3 type=submit style=height:80px;width:150px>LED2 On</button>");
              client.println("<button name=b value=4 type=submit style=height:80px;width:150px>LED2 Off</button>");
              client.println("</form><br />");

              break;
            }
            if (c == '\n') {
              // we're starting a new line
              current_line_is_first = false;
              current_line_is_blank = true;
            }
            else if (c != '\r') {
              // we've gotten a character on the current line
              current_line_is_blank = false;
            }
            // get the first http request
            if (current_line_is_first && c == '=') {
              for (int i = 0; i < 1; i++) {
                c = client.read();
                command[i] = c;
              }

              // Handle command from user clicking a displayed button
              if (!strcmp(command, "1"))
                digitalWrite(LEDPIN, HIGH);
              else if (!strcmp(command, "2"))
                digitalWrite(LEDPIN, LOW);
              else if (!strcmp(command, "3"))
                digitalWrite(LED2PIN, HIGH);
              else if (!strcmp(command, "4"))
                digitalWrite(LED2PIN, LOW);
            }
          }
        }
        // Give the web browser time to receive the data
        delay(1);
        client.stop();
      }
    }


Powered by Wild Apricot Membership Software