Skip to main content
edited tags
Link
Source Link

Arduino loop/setup issue

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.