0

I am trying to make an arduino code to get a value from a web page to turn an led on or off. My code is working, however it will only retrieve the 1 or 0 value from the page once. I have tried messing with the loop arguments but that has just resulted in errors.

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


const int ledPin = 8;
byte mac[] = { Mac address }; 
byte ip[] = { ip };
byte server[] = { ip };

EthernetClient client;

void setup() {

  Serial.begin(9600);
   while (!Serial) {
    ;
  }

  if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
  }

  delay(1000);
  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /LEDstate.txt READ_FILE");
    client.println("Host: TestServer");
    client.println("Connection: close");
    client.println();
  }
  else {
    Serial.println("connection failed");
  }

  pinMode(ledPin, OUTPUT);
}

void loop()
{

  if (client.available()) {
char c = client.read();
Serial.print(c);

if (c == '1'){
  digitalWrite(ledPin, HIGH);
}
else if (c == '0') {
  digitalWrite(ledPin, LOW);
}

  }

  if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
while(true);
  }
}

I removed the ip and mac address for privacy reasons.

I want it to loop the GET of the value on the external page as well as as the led pin but I ma not sure how to set it up. Any help would be much appreciated.

1 Answer 1

1

Void loop() is itself an infinite loops.If you are getting value once that means your programme is correct(according to you) . But at the end you have written

 While(true); 

your programme never get out of this infinite loop since there is no condition to break it. Remove this statement and check again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.