Networkclient non blocking

Hello,

i have a problem...

I have an digital input and i want to check periodically a website for a specific string.

Everything is working fine except if there is no internet/network connection.
Then sometimes it takes several seconds till the input is recorgnized.

What can i do?

#define BLYNK_TEMPLATE_ID           ""
#define BLYNK_TEMPLATE_NAME         ""
#define BLYNK_AUTH_TOKEN            ""
char auth[] = "";

#include <Controllino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <avr/wdt.h>
#include <BlynkSimpleEthernet.h>


// MAC-Adresse
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE };
// IP-Adresse
byte ip[] = { 192, 168, 178, 178 };
// Gateway
byte gateway[] = { 192, 168, 178, 1 };
// DNS
byte dns[] = { 192, 168, 178, 1 };
// Subnetzmaske
byte subnet[] = { 255, 255, 255, 0 };


// Serverport
EthernetServer server(80);



char *host_name = "";


String request_path = "";


String http_method = "GET";


int http_port = 80;


char incomingData[80];
int dataIndex = 0;
char c;


String readString;


int taster1 = CONTROLLINO_A1;
int led1 = CONTROLLINO_D1;


int status_taster1;
int vorheriger_status_taster1 = HIGH;


long letztes_prellen1 = 0;


const long entprellzeit = 30;
const long checkTime = 5000;



unsigned long currentMillis = 0;
unsigned long startTime = 0;

bool taster_status = false;

int V1Value = 0;


EthernetClient client;


void taster() {
  if (!digitalRead(led1)) {
    digitalWrite(led1, HIGH);
    taster_status = false;
  } else {
    standby();
  }
}


void standby() {
  digitalWrite(led1, LOW);
  
  //reset V112Value and V112
  if(V1Value == 1) {
    Blynk.virtualWrite(V1, 0);
    V1Value = 0;
  }
}



void action() {
    startTime = millis();
    if (!digitalRead(led1)) {
      digitalWrite(led1, HIGH);
    }
}


//Aufruf: call_request(client, request_string)
void call_request(Client& networkclient){
    // connect to web server on port 80
    if(networkclient.connect(host_name, http_port)) {
      // if connected:
      // make a HTTP request, send HTTP header
      networkclient.println(http_method + " " + request_path + " HTTP/1.1");
      networkclient.println("Host: " + String(host_name));
      networkclient.println("Connection: close");
      networkclient.println(); // end HTTP header


      while(networkclient.connected()) {
        if(networkclient.available()){
          // read an incoming byte from the server
          c = networkclient.read();
          //dataIndex++;
          //incomingData[dataIndex] = c;
          if(dataIndex < 79) {
            incomingData[dataIndex] = c;
            dataIndex++;
            if (c == '\n') {
              incomingData[dataIndex-1] = '\0';
              dataIndex = 0;
              if (strstr(incomingData, "#1#") != NULL) {
                action();
              }
            }
          }
        }
      }
      // the server's disconnected, stop the networkclient
      networkclient.stop();
    } else {// connection failed
    
    }
}


BLYNK_WRITE(V1) {
  if(V1Value == 0) {
    V1Value = param.asInt(); // assigning incoming value from blynk-pin to a variable
    
    //if(param.asInt() == 1)
    // execute this code if the switch widget is now ON
    if(V1Value == 1) {
      action();
    }
  }
}


void setup() {
  // Start Ethernet und Server:
  Ethernet.begin(mac, ip, dns, gateway, subnet); 
  
  client.setTimeout(200);
client.setConnectionTimeout(200);

  server.begin();


  // Tasten
  pinMode(taster1, INPUT);


  // LEDs
  pinMode(led1, OUTPUT);

   // WDTO_4S, WDTO_2S, WDTO_1S
  wdt_enable(WDTO_8S);

//Blynk.config(ip);
  //Blynk.begin();
  //Blynk.begin(auth, "blynk.cloud", 80, ip, dns, gateway, subnet, mac);
  Blynk.config(auth);
  Blynk.connect(100);
  
}


void loop() {
  //Blynk.run();


  EthernetClient localClient = server.available();
  if(localClient) {
    while(localClient.connected()) {
      if(localClient.available()) {
        char c = localClient.read();
        readString.concat(c);
        if(c=='\n') {
          if(readString.indexOf("string")>0) {
            action();
          }
          
          localClient.println("HTTP/1.1 200 OK");
          localClient.println("Content-Type: text/html");
          localClient.println();
          localClient.println("<!doctype html>");
          localClient.println("<html>");
          localClient.println("<head><title>CONTROLLINO-FFL</title></head>");
          localClient.print("<!DOCTYPE HTML><html><body ></body></html>" );
          localClient.println("</html>");
          localClient.stop();
          readString="";
        }
      }
    }
  }




  int aktueller_status_taster1 = digitalRead(taster1);
 
  currentMillis = millis();


  if (aktueller_status_taster1 != vorheriger_status_taster1) {
    letztes_prellen1 = currentMillis;
  }

  if ((currentMillis - letztes_prellen1) > entprellzeit) {
    if (aktueller_status_taster1 != status_taster1) {
      status_taster1 = aktueller_status_taster1;
      if (status_taster1 == HIGH) {
        taster_status = !taster_status;
        if (status_taster1 == HIGH) {
          taster();
        } else {
          standby(); 
        }
      }
    }
  }


  vorheriger_status_taster1 = aktueller_status_taster1;
 
  //do something every x sec
  if (!(currentMillis % checkTime)) {
    call_request(client);
  }


  wdt_reset();
}

try networkclient.setConnectionTimeout(1000)

Ok.
I will test it.

Unfortunately that doesn`t change anything.
networkclient.setConnectionTimeout() is not availible, only networkclient.setTimeout()

you have some old version of the Ethernet library?

No, version 2.0.2.
In setup .setConnectionTimeout() is available, in the function not.

it is enough to set it in setup()

(setConnectionTimeout is in EthernetClient. the base class Client doesn't have it)

Ok.
But that doesn't change anything.

Any other idea?

the connect doesn't timeout in a second?

No. Sometimes it takes two seconds unil it process further.

The hostname is an URL, could it be DNS that causes the blocking?

you could try

DNSClient dns;
dns.begin(Ethernet.dnsServerIP());
dns.getHostByName(hostname, result);

Yes.
But when there is no internet, than i have the same problem.
There is no timeout for dns, or?

dns.getHostByName(hostname, result, 1000);
it tries 3 times
default timeout is 5 seconds

No change again.
Any other idea?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.