1

I've got an Arduino Uno and Ethernet shield, trying to test it out with a simple on off switch for the on-board LED.

My Code is this

// FILENAME = LED2.ino
// SOURCE= http://www.instructables.com/id/Control-an-LED-over-the-internet-using-the-Arduino/
// Control an LED over the internet using the Arduino Ethernet Shield and Transistor
// http://www.instructables.com/id/Control-an-LED-over-the-internet-using-the-Arduino/
// code SOURCE= http://pastebin.com/CZ6J62qJ

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

#include <Servo.h>
Servo myservo; // create servo object to control a servo

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // fixed IP addr in LAN
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString;
//////////////////////
void setup()
{
    pinMode(6, OUTPUT); //pin selected to control LED
    pinMode(13, OUTPUT);
    //start Ethernet
    Ethernet.begin(mac, ip, gateway, subnet);
    server.begin();
    //the pin for the servo co
    //enable serial data print
    Serial.begin(9600);
    Serial.println("server LED test 1.0 ---- FILENAME = LED2.ino "); // so I can keep track
}

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("HTTP/1.1 200 OK"); //send new page
                    client.println("Content-Type: text/html");
                    client.println();
                    client.println("<HTML>");
                    client.println("<HEAD>");
                    client.println("<TITLE>Home Automation</TITLE>");
                    client.println("</HEAD>");
                    client.println("<BODY bgcolor='aqua'>");
                    client.println("<H1>Home Automation</H1>");
                    client.println("<hr />");
                    client.println("<br />");

                    client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
                    client.println("<a href=\"/?lightoff\"\">Turn Off Light</a><br />");

                    client.println("</BODY>");
                    client.println("</HTML>");

                    delay(1);
                    //stopping client
                    client.stop();

                    ///////////////////// control arduino pin
                    if (readString.indexOf("?lighton") > 0) //checks for on
                    {
                        digitalWrite(13, HIGH); // set pin 6 high
                        Serial.println("LED On");
                    }
                    else {
                        if (readString.indexOf("?lightoff") > 0) //checks for off
                        {
                            digitalWrite(13, LOW); // set pin 6 low
                            Serial.println("LED Off");
                        }
                    }
                    readString = ""; //clearing string for next read
                }
            }
        }
    }
}

The problem is that when I click a button (either on or off) the LED will blink twice, and then remain off.

My serial monitor also shows 2 HTTP requests, I'm not sure if this is usual or not?

server LED test 1.0 ---- FILENAME = LED2.ino 
GET /?lighton HTTP/1.1

LED On
GET /favicon.ico HTTP/1.1

GET /?lighton HTTP/1.1

LED On
GET /?lighton HTTP/1.1

LED On
GET /favicon.ico HTTP/1.1

GET /?lighton HTTP/1.1

LED On
GET /favicon.ico HTTP/1.1

GET /?lightoff HTTP/1.1

LED Off
GET /favicon.ico HTTP/1.1

Would appreciate any help!

  • What button? What is pin 6 used for? – user31481 Aug 5 '17 at 7:33
  • Might need to reconsider using pin 6 for the LED as pin 13 is used by the Ethernet shield for SPI clock (SCK). – Mikael Patel Aug 5 '17 at 8:22
  • @lookalterno It's the button on the webserver control (html button) if you view my code – Brendan Gooden Aug 6 '17 at 2:36
  • Good problem. You only test and switch the LED after a "\n" and then clear the readString variable. – user31481 Aug 6 '17 at 9:11
  • @LookAlterno OK.. So how could I rectify this issue? – Brendan Gooden Aug 6 '17 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.