Function that calls web service does not work outside setup()

I have created my own dynamic dns service. My code works when I send data to WS inside setup() function, but not when I create a its own function. I need to run it at startup and periodically to keep my dynamic ip updated. I have tried Interrupts, SimpleTimer and millis control. Any help is welcomed.

#include <SPI.h>
#include <Ethernet.h>
#include <SimpleTimer.h>
#include "DHT.h"
#define DHTPIN A7 // pino que estamos conectado
#define DHTTYPE DHT11 // DHT 11

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress ip(192,168,1,177);                 //Define o endereco IP
IPAddress gateway(192,168,1,1);              //Define o gateway
IPAddress subnet(255, 255, 255, 0);           //Define a máscara de rede
char webServer[] = "";   // webservice for current ethernet ip

// port you want to use
EthernetServer server(4162);
// web service to receive data
EthernetClient wsClient;

// the sensor temperature and humidity object

// the timer object
SimpleTimer timer;

void setup() {
  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.print("server is at ");
  timer.setInterval(10000, noIPService);

void loop() {
  // start timer;

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c =;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 60");  // refresh the page automatically every 60 sec
          client.println("<!DOCTYPE HTML>");
          // output the value of each analog input pin
          float h = dht.readHumidity();
          float t = dht.readTemperature();
            client.print("Temperatura:  ");
            client.print("Humidade: ");
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
    // give the web browser time to receive the data
    // close the connection:
    Serial.println("client disconnected");

void noIPService(){
  // get current ip  
    if (wsClient.connect(webServer, 80)){
      Serial.println("Connected to web service");
      // Make a HTTP request:
      wsClient.println("GET /myip.asp HTTP/1.1");
      wsClient.println("Connection: close");
    } else {
      Serial.println("No web service available");
      if (wsClient.available())
        char c =;

I have tried Interrupts, SimpleTimer and millis control.

You showed us the least efficient method, but failed to explain exactly what the code does, or does not do.

"It doesn't work" is useless complaining.

You appear to be attempting to do 2 completely separate activities: (1) Connecting to an external web server as a web client and issuing a command: "GET /myip.asp HTTP/1.1". I presume this is determine the external IP address of your local Arduino client which you are going to use somehow to update a dynamic DNS service. (2) Running a web server on the arduino which publishes temperature and humidity information.

Assuming these 2 activities work separately and do not interfere with each other, you could try the following in your loop():

void loop() {

    static unsigned long lastUpdateAtMs = 0 ;
    if (millis() - lastUpdateAtMs > 10000UL ) {   // 10 seconds
        lastUpdateAtMs = millis() ;
        noIPService() ;

    // your loop code here


6v6gt sorry for no response, vacations... I tried this approach also. But it appears that I cannot run a server and make calls to the web service in the same program. My work around will be try to configure my router to update a third part ddns service.