2

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

  }

}
  • What aspect of it is causing you difficulty? Where to start is moving most of it into the `loop()`, and pausing for 10s after you've done it. – Mark Smith Jan 07 '17 at 16:56
  • I tried that, but it does not work. Nothing happens after the first loop, even when I remove the client.stop() part. – Kenneth Poulsen Jan 07 '17 at 16:59
  • You need to connect to the server (`client.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 07 '17 at 17:17

1 Answers1

1

As discussed in the comments, you need to connect to the client each time, not just once.

Here's my modified version of your code, which I hope will work -- I'm not set up to test it.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 104 };
char server[] = "billigefittings.dk";

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println(Ethernet.localIP());
  Serial.println();
}

void loop()
{
  // Connect to the server
  Serial.println("connecting ...");

  EthernetClient client;
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /lan.php");
    client.println();
  } else {
    Serial.println("connection failed");
  }

  // Wait a moment for data to arrive
  // Note: This is NOT a reliable way of doing it!
  delay(1000);

  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' !");
    }
  }

  // Disconnect the client    
  if(client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // Wait another 9s, which will give us a delay of roughly 10s
  delay(9000);
}
Mark Smith
  • 2,161
  • 1
  • 9
  • 14
  • That worked perfectly, will keep in mind that you need to connect upon each try - you saved my day! thank's a lot. – Kenneth Poulsen Jan 07 '17 at 17:43
  • Glad to hear it was helpful! – Mark Smith Jan 07 '17 at 17:45
  • Got a small question related to your answer. It seems you got quite a lot of knowledge about coding this stuff. Do I need to flush cache, or something, in the code above? It's supposed to run months without stop. – Kenneth Poulsen Jan 07 '17 at 20:30
  • No, I can't see anything there which needs maintenance. The only thing that feels dodgy is that when we connect we wait 1s for data, and if there isn't any, we abandon ship. That'll probably work most of the time, but what if just occasionally the server is under high load, or your network is laggy or whatever? It'll appear to have failed. Browsers have *much* longer timeouts than this. How to deal with depends on what you're trying to achieve. – Mark Smith Jan 07 '17 at 20:47
  • Hmm... we have 3 machines each with an arduino. Each arduino check a file to see if a value, matching the machine, is active. If the value is active, then the arduino will power a motor for a short period of time. Each of the machines does this separately, checking a file every 10 seconds. – Kenneth Poulsen Jan 07 '17 at 21:14
  • What do you *want* to happen if the network is (temporarily?) slow? Would you rather it wait (how long?) or is "slow" equivalent to "not working" for the think you're trying to achieve? – Mark Smith Jan 07 '17 at 21:18
  • Thats a very good questions. Maybe add a 30 seconds timer, do a GET request to a php page with some info, so I can store some details about the delay in a database for tracking. Is it possible to add a timeout to Arduino and do a GET request to a php page? (e.g. domain.com/machine.php?status=err_delay&machine_id=3)... I need to think this over, have a feeling this will be quite important. Thanks for bringing it up :) – Kenneth Poulsen Jan 07 '17 at 21:32