I got problems with the code and I have used most of today, trying to find a solution and it's starting to drive my crazy. I hope one of you guys will be able to help me out.
I need the code to check a specific page every 10 seconds, single character.
This is what I have now, working code: Where should I start?
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 104 };
char server[] = "billigefittings.dk";
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println(Ethernet.localIP());
Serial.println();
Serial.println("connecting ...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET /lan.php");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
if(client.available() > 0) {
char c = client.read();
Serial.print("page value:");
Serial.println(c);
if(c == '1') {
digitalWrite(8, HIGH);
Serial.println("8 HIGH ...");
delay(5000);
digitalWrite(8, LOW);
Serial.println("8 LOW ...");
}else{
Serial.println("page value does not match '1' !");
}
// The code above should run even 10 seconds
}
if(!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;);
}
}
Update: Edit the loop like the example below does not work either, it stops after the first loop (8 LOW):
void loop()
{
if(client.available() > 0) {
char c = client.read();
Serial.print("page value:");
Serial.println(c);
if(c == '1') {
digitalWrite(8, HIGH);
Serial.println("8 HIGH ...");
delay(5000);
digitalWrite(8, LOW);
Serial.println("8 LOW ...");
}else{
Serial.println("page value does not match '1' !");
}
delay(5000);
// The code above should run even 10 seconds
}
}
loop()
, and pausing for 10s after you've done it. – Mark Smith Jan 7 '17 at 16:56client.connect...
) and disconnect (client.stop();
) each time round. More generally, try to understand the problem: it isn't true that "nothing happens" - something happens, just not what you want. If you could understand what in fact happens, you'd be closer to understanding why. You can put trace (serial.prinln()
) in, find out where it's going wrong. – Mark Smith Jan 7 '17 at 17:17