Arduino uno V3 / enc28j60 / UIPEthernet.h hang after few rounds post data

This sketch try to obtain data from some sensors and post them to a php program on a remote server.

The sketch compile, run ok but after 5 or 6 (almos 10) loop it hang on “client connect” on notificar() function.

I try to disconect de lcd, the sensor, etc. but every time the result is the same: it hangs on “client connect” after few loops.

The sketch :

#include  <LiquidCrystal.h>         // Include the library to use a LCD display
#include <UIPEthernet.h>

#define sensor  0   // puerto A0 lectura temperatura

int     fuego = 9;  // puerto de lectura pirografo   
int     rele  = 8;  // puerto de lectura rele


int     Vin;        // variable para leer voltage sensor temperatura
int     fallos;     // errores de la red
int     Temp;       // variable para calcular temperatura
int     MxTemp;     // temperatura maxima
int     MnTemp;     // temperatura minima
int     ExTemp;     // temperatura extremo para alerta
int     aTemp;      // temperatura anterior 
int     vlf;        // variable para lectura digital pirografo
String  car;        // caracter de estoy vivo 
int     cnt;        // contador reintento red
int     dift;       // variacion de temperatura para manejo alerta
int     ctemp;      // contador para alerta temperatura
int     luz;        // alimentacion electrica
boolean correo;     // control del correo

//LiquidCrystal  lcd    (7, 6, 5, 4, 3, 2); 
EthernetClient client;
signed long    next;

void  setup()
{
  correo = true;    // enviar inicio de operacion 
  luz = 0;          // por defecto hay luz rele en NO

  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
  IPAddress myIP(10,15,20,123);
  Ethernet.begin(mac,myIP);
  //Ethernet.begin(mac);
  
  Serial.print("localIP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: ");
  Serial.println(Ethernet.dnsServerIP());

  ExTemp = 25;                  // temperatura de alerta
  cnt = 0;
  ctemp = 5;
  car = "|";
  pinMode(fuego,INPUT);
  pinMode(rele,INPUT);
  
  Serial.begin(9600);
  MnTemp = 100;
  MxTemp = 0;
  aTemp  = 0;
  fallos = 0;
  lcd.begin(16, 2);             // 2 lineas 16 caracteres
  lcd.print("Tmp: ");           // pone etiqueta fija (Temperatura)
  lcd.setCursor(0, 1);          // posiciona el cursor en la linea 2
  lcd.print("Mx:");             // pone etiqueta fija (Temperatura Maxima)
  lcd.setCursor(7, 1);          // posiciona el cursor en la linea 2
  lcd.print("Mn:");             // pone etiqueta fija (Temperatura Maxima)
  next = 0;

}

void  loop()
{
  
  luz = digitalRead(rele);   // leer sensor presencia de tension

  
  vlf = digitalRead(fuego);   // leer sensor pirografo

  if (vlf == 1)
  {
    correo = true;    
  }

  Vin = analogRead (sensor);  // leer sensor temperatura

  Temp=(500*Vin)/1024;        // calcular temperatura en grados centigrados

  Serial.println(Temp);

  if (Temp != aTemp)          // si cambia la temperatura 
  {
    lcd.setCursor(5, 0);      // cursor a linea 1 posicion 6
    lcd.print(Temp);          // mostrar temperatura
    lcd.print(" C");          // son celcius
  }
  if (Temp > MxTemp)          // si es superior al maximo historico
  {
    MxTemp = Temp;            
    lcd.setCursor(3, 1);      // cursor a linea 2 posicion 6
    lcd.print(MxTemp);        // mostrar temperatura maxima
  }
  if (Temp < MnTemp)          // si es inferior al minimo historico
  {
    MnTemp = Temp;            
    lcd.setCursor(10, 1);      // cursor a linea 2 posicion 6
    lcd.print(MnTemp);         // mostrar temperatura maxima
  }

  if (Temp > ExTemp)           // temperatura supera extremo para notificar
  {
    dift = Temp - aTemp;       // obtener delta detemperatura
    
    if (dift < 0)              // esta bajando, bajar alerta
      {
        ctemp++;
      }
    else                       // se mantiene o sigue subiendo
      {
        ctemp--;               // nuevo nivel de alerta
      }
  }

  Serial.println(vlf);

  if ((vlf == 1) || (ctemp == 5) ||(luz == 1) || (((signed long)(millis() - next)) > 0))
  {
    if (ctemp == 5)
    {
       ctemp = 0;
       correo = true;
    }
    notificar();
    next = millis() + 10000;
  }
  
  lcd.setCursor(15, 0);       // cursor a linea 1 posicion 16 
  lcd.print(car);             // caracter "estoy vivo"

  if (car == "|")
  {
     car = "-";
  }
  else
  {
     car = "|";
  }

  lcd.setCursor(15, 1);       // cursor a linea 2 posicion 16
  lcd.print(vlf);             // mostrar valor digital pirografo
  
  delay(3000);                //  esperar 3 segundos

}

void hayluz()
{
    correo = true;
}

void notificar()
{
  String data;

  data="luz=";
  data+=luz;

  if (correo)
  {
    data+="&correo=1";
  }
  else
  {
    data+="&correo=0";
  }
  data+="&temp="; 
  data+=Temp;
  
  data+="&submit=Submit";     // Submitting data

  correo = false;             // inicializa control correo

  Serial.println(data);
  
      Serial.println("Client connect");
      lcd.setCursor(14, 0);       // cursor a linea 1 posicion 15 
      lcd.print("N");             // caracter "Trato de enviar post"


      if (client.connect(IPAddress(10,15,20,100),80))
        {
          Serial.println("Cliente conectado");
          //disconnect client
          client.println("POST /prueba_x.php HTTP/1.1");
          client.println("Host: 10.15.20.100");
//          client.println("User-Agent: Arduino/1.0");
          client.println("Content-Type: application/x-www-form-urlencoded");
          client.println("Connection: close");
          client.print("Content-Length: ");
          client.println(data.length());
          client.println();
          client.println(data);
          delay(100);
          Serial.println("Client disconnect");
          client.stop();
          correo = false;
        }
      else
        {
          Serial.println("Fallo coneccion cliente");
          Serial.print("localIP: ");
          Serial.println(Ethernet.localIP());
          Serial.print("subnetMask: ");
          Serial.println(Ethernet.subnetMask());
          Serial.print("gatewayIP: ");
          Serial.println(Ethernet.gatewayIP());
          Serial.print("dnsServerIP: ");
          Serial.println(Ethernet.dnsServerIP());
          fallos++;
          if (fallos > 6)
          {
             uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
             IPAddress myIP(10,15,20,123);
             Ethernet.begin(mac,myIP);
             fallos=0;    
          }
    }

    lcd.setCursor(14, 0);       // cursor a linea 1 posicion 15 
    lcd.print(" ");             // caracter "Trato de enviar post"
    lcd.setCursor(14, 1);       // cursor a linea 1 posicion 15 
    lcd.print(fallos);          // caracter "Trato de enviar post"

}

the php program

<?php
header("Content-type: text/html");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$hoy = date("Y-m-d H:i:s");
$bst = "--\n";
if(strlen($_POST["temp"]) > 1 )
{
          echo "1"; // if it displays 1 it worked
          $linea = $hoy." => Temp:".$_POST["temp"]." Correo : ".$_POST["correo"]." Luz: ".$_POST["luz"];
          $fp = fopen('/tmp/data.log', 'a');
          fwrite($fp, $linea);
          fclose($fp);

}
if(strlen($_POST["submit"]) > 1 )
{
          echo "1";  // if it displays 1 it worked
          $fp = fopen('/tmp/data.log', 'a');
          fwrite($fp, "\n");
          fclose($fp);
}


?>

any ideas ?
thks.
sls.
hg

try adding a call to 'Ethernet.maintain()' in the main loop so packets get processed when notificar() is not being executed. There's no need to call 'delay()' in notificar() as UIPEthernet does not do any background processing. All packet-processing is done as a side-effekt of calling into UIPEthernet methods.

  • Norbert

Ok. in motion. tkhs. sls hg

No, no change. Are there any chance to configure a timer interrupt to refresh the board to cut de hang "connect" ? thks sls hg

Use of Timer-interrupt is not implemented in UIPEthernet, but you should be able to archive this by implementing your own method that calls Ethernet.maintain() and attach that to a Timer-interrupt.

regarding hang on connect: it could be your code is leaking client-objects that are not properly closed by calling 'stop()'. If 4 client-objects are leaked you cannot connect any further. This is a 'best guess', I didn't verify your code in detail though.

You can also switch on various debug-options in UIPEthernet.h and check the output on Serial-console. Doing this and monitoring the traffic using wireshark will help finding the cause of hang. (You can past a wireshark-log on cloudshark.org if you'd like me to check this for you...).

  • Norbert

Ok. on motion. thks. sls hg

Ok, after fix some mistakes on the library's path that i'd used to use, i have activated the labels #define

This is the log of transactions until the hang.

stop(), data: NULL
Client disconnect
27
0
27
0
27
0
27
0
luz=0&correo=0&temp=27&submit=Submit
Client connect
stop(), data: NULL
UIPClient uip_newdata, uip_len:0
UIPClient state UIP_CLIENT_CLOSEUIPClient::all_data[0], state:0
packets_in: 9 0 0 0 0 
packets_out: 9 0 0 0 0 
out_pos: 179
UIPClient::all_data[1], state:0
packets_in: 9 0 0 0 0 
packets_out: 10 0 0 0 0 
out_pos: 179
UIPClient::all_data[2], state:110010
packets_in: 9 0 0 0 0 
packets_out: 13 0 0 0 0 
out_pos: 1
UIPClient::all_data[3], state:110011
packets_in: 9 0 0 0 0 
packets_out: 14 0 0 0 0 
out_pos: 1
blocks outstanding transfer -> uip_stop()connected, state: 50, first packet in: 9
Cliente conectado
UIPClient uip_acked
eatblock(13): 13 0 0 0 0 -> 0 0 0 0 0 
UIPClient state UIP_CLIENT_CLOSEUIPClient::all_data[0], state:0
packets_in: 9 0 0 0 0 
packets_out: 9 0 0 0 0 
out_pos: 179
UIPClient::all_data[1], state:0
packets_in: 9 0 0 0 0 
packets_out: 10 0 0 0 0 
out_pos: 179
UIPClient::all_data[2], state:110010
packets_in: 9 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 1
UIPClient::all_data[3], state:110011
packets_in: 9 0 0 0 0 
packets_out: 14 0 0 0 0 
out_pos: 1
no blocks out -> free userdataUIPClient::all_data[0], state:0
packets_in: 9 0 0 0 0 
packets_out: 9 0 0 0 0 
out_pos: 179
UIPClient::all_data[1], state:0
packets_in: 9 0 0 0 0 
packets_out: 10 0 0 0 0 
out_pos: 179
UIPClient::all_data[2], state:0
packets_in: 9 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 1
UIPClient::all_data[3], state:110011
packets_in: 9 0 0 0 0 
packets_out: 14 0 0 0 0 
out_pos: 1
UIPClient.write: writePacket(11) pos: 0, buf[0-27]: 'POST /prueba_x.php HTTP/1.1'
UIPClient.write: writePacket(11) pos: 27, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 28, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 29, buf[0-18]: 'Host: 10.15.20.100'
UIPClient.write: writePacket(11) pos: 47, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 48, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 49, buf[0-47]: 'Content-Type: application/x-www-form-urlencoded'
UIPClient.write: writePacket(11) pos: 96, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 97, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 98, buf[0-17]: 'Connection: close'
UIPClient.write: writePacket(11) pos: 115, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 116, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 117, buf[0-16]: 'Content-Length: '
UIPClient.write: writePacket(11) pos: 133, buf[0-2]: '36'
UIPClient.write: writePacket(11) pos: 135, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 136, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 137, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 138, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 139, buf[0-1]: 'l'
UIPClient.write: writePacket(11) pos: 140, buf[0-1]: 'u'
UIPClient.write: writePacket(11) pos: 141, buf[0-1]: 'z'
UIPClient.write: writePacket(11) pos: 142, buf[0-1]: '='
UIPClient.write: writePacket(11) pos: 143, buf[0-1]: '0'
UIPClient.write: writePacket(11) pos: 144, buf[0-1]: '&'
UIPClient.write: writePacket(11) pos: 145, buf[0-1]: 'c'
UIPClient.write: writePacket(11) pos: 146, buf[0-1]: 'o'
UIPClient.write: writePacket(11) pos: 147, buf[0-1]: 'r'
UIPClient.write: writePacket(11) pos: 148, buf[0-1]: 'r'
UIPClient.write: writePacket(11) pos: 149, buf[0-1]: 'e'
UIPClient.write: writePacket(11) pos: 150, buf[0-1]: 'o'
UIPClient.write: writePacket(11) pos: 151, buf[0-1]: '='
UIPClient.write: writePacket(11) pos: 152, buf[0-1]: '0'
UIPClient.write: writePacket(11) pos: 153, buf[0-1]: '&'
UIPClient.write: writePacket(11) pos: 154, buf[0-1]: 't'
UIPClient.write: writePacket(11) pos: 155, buf[0-1]: 'e'
UIPClient.write: writePacket(11) pos: 156, buf[0-1]: 'm'
UIPClient.write: writePacket(11) pos: 157, buf[0-1]: 'p'
UIPClient.write: writePacket(11) pos: 158, buf[0-1]: '='
UIPClient.write: writePacket(11) pos: 159, buf[0-1]: '2'
UIPClient.write: writePacket(11) pos: 160, buf[0-1]: '7'
UIPClient.write: writePacket(11) pos: 161, buf[0-1]: '&'
UIPClient.write: writePacket(11) pos: 162, buf[0-1]: 's'
UIPClient.write: writePacket(11) pos: 163, buf[0-1]: 'u'
UIPClient.write: writePacket(11) pos: 164, buf[0-1]: 'b'
UIPClient.write: writePacket(11) pos: 165, buf[0-1]: 'm'
UIPClient.write: writePacket(11) pos: 166, buf[0-1]: 'i'
UIPClient.write: writePacket(11) pos: 167, buf[0-1]: 't'
UIPClient.write: writePacket(11) pos: 168, buf[0-1]: '='
UIPClient.write: writePacket(11) pos: 169, buf[0-1]: 'S'
UIPClient.write: writePacket(11) pos: 170, buf[0-1]: 'u'
UIPClient.write: writePacket(11) pos: 171, buf[0-1]: 'b'
UIPClient.write: writePacket(11) pos: 172, buf[0-1]: 'm'
UIPClient.write: writePacket(11) pos: 173, buf[0-1]: 'i'
UIPClient.write: writePacket(11) pos: 174, buf[0-1]: 't'
UIPClient.write: writePacket(11) pos: 175, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 176, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 177, buf[0-1]: '
'
UIPClient.write: writePacket(11) pos: 178, buf[0-1]: '
'
stop(), data: NULL
Client disconnect
27
0
27
0
27
0
27
0
luz=0&correo=0&temp=27&submit=Submit
Client connect
stop(), data: NULL
UIPClient uip_newdata, uip_len:0
UIPClient state UIP_CLIENT_CLOSEUIPClient::all_data[0], state:0
packets_in: 9 0 0 0 0 
packets_out: 9 0 0 0 0 
out_pos: 179
UIPClient::all_data[1], state:0
packets_in: 9 0 0 0 0 
packets_out: 10 0 0 0 0 
out_pos: 179
UIPClient::all_data[2], state:0
packets_in: 9 0 0 0 0 
packets_out: 11 0 0 0 0 
out_pos: 179
UIPClient::all_data[3], state:110011
packets_in: 9 0 0 0 0 
packets_out: 14 0 0 0 0 
out_pos: 1
blocks outstanding transfer -> uip_stop()connected, state: 51, first packet in: 9
Cliente conectado
UIPClient uip_acked
eatblock(14): 14 0 0 0 0 -> 0 0 0 0 0 
UIPClient state UIP_CLIENT_CLOSEUIPClient::all_data[0], state:0
packets_in: 9 0 0 0 0 
packets_out: 9 0 0 0 0 
out_pos: 179
UIPClient::all_data[1], state:0
packets_in: 9 0 0 0 0 
packets_out: 10 0 0 0 0 
out_pos: 179
UIPClient::all_data[2], state:0
packets_in: 9 0 0 0 0 
packets_out: 11 0 0 0 0 
out_pos: 179
UIPClient::all_data[3], state:110011
packets_in: 9 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 1
no blocks out -> free userdataUIPClient::all_data[0], state:0
packets_in: 9 0 0 0 0 
packets_out: 9 0 0 0 0 
out_pos: 179
UIPClient::all_data[1], state:0
packets_in: 9 0 0 0 0 
packets_out: 10 0 0 0 0 
out_pos: 179
UIPClient::all_data[2], state:0
packets_in: 9 0 0 0 0 
packets_out: 11 0 0 0 0 
out_pos: 179
UIPClient::all_data[3], state:0
packets_in: 9 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 1

I'll try to understand what is the meaning of this, but any way, i hope help us to fix or explain what is happening here.

thks sls hg.

New log after remove all delays :

luz=0&correo=0&temp=27&submit=Submit
Client connect
stop(), data: NULL
UIPClient uip_connected
UIPClient::all_data[0], state:1110000
packets_in: 1 0 0 0 0 
state remote closed, local port: 1027
UIPClient::all_data[1], state:1110001
packets_in: 1 0 0 0 0 
state remote closed, local port: 1028
UIPClient::all_data[2], state:1110010
packets_in: 1 0 0 0 0 
state remote closed, local port: 1032
UIPClient::all_data[3], state:1110011
packets_in: 1 0 0 0 0 
state remote closed, local port: 1033
UIPClient allocation failed
connected, state: 192, first packet in: 0
Cliente conectado
stop(), data: NULL
Client disconnect

It always failed after 4 transmissions

the message "UIPClient allocation failed" is in the UIPClient.cpp

The hang condition has been released but, after failing, the server doesn't receive any thing at all.

why have been probed all this ports (1027,1028, 1032, 1033 ) until fail? is it trying on the 4 possible sockets ?

another log

blocks outstanding transfer -> uip_stop()luz=0&correo=0&temp=27&submit=Submit
Client connect
stop(), data: NULL
Fallo coneccion cliente
localIP: 10.15.20.160
subnetMask: 255.255.255.0
gatewayIP: 10.15.20.254
dnsServerIP: 200.42.4.204
UIPClient uip_closed
UIPClient::all_data[0], state:110000
packets_in: 1 0 0 0 0 
packets_out: 3 0 0 0 0 
out_pos: 1
UIPClient::all_data[1], state:110001
packets_in: 1 0 0 0 0 
packets_out: 4 0 0 0 0 
out_pos: 1
UIPClient::all_data[2], state:110010
packets_in: 1 0 0 0 0 
packets_out: 5 0 0 0 0 
out_pos: 1
UIPClient::all_data[3], state:110011
packets_in: 1 0 0 0 0 
packets_out: 6 0 0 0 0 
out_pos: 1
after UIPClient uip_closed
UIPClient::all_data[0], state:1110000
packets_in: 1 0 0 0 0 
state remote closed, local port: 1026
UIPClient::all_data[1], state:110001
packets_in: 1 0 0 0 0 
packets_out: 4 0 0 0 0 
out_pos: 1
UIPClient::all_data[2], state:110010
packets_in: 1 0 0 0 0 
packets_out: 5 0 0 0 0 
out_pos: 1
UIPClient::all_data[3], state:110011
packets_in: 1 0 0 0 0 
packets_out: 6 0 0 0 0 
out_pos: 1
luz=0&correo=0&temp=27&submit=Submit
Client connect
stop(), data: NULL
UIPClient uip_connected
UIPClient::all_data[0], state:1110000
packets_in: 1 0 0 0 0 
state remote closed, local port: 1026
UIPClient::all_data[1], state:110001
packets_in: 1 0 0 0 0 
packets_out: 4 0 0 0 0 
out_pos: 1
UIPClient::all_data[2], state:110010
packets_in: 1 0 0 0 0 
packets_out: 5 0 0 0 0 
out_pos: 1
UIPClient::all_data[3], state:110011
packets_in: 1 0 0 0 0 
packets_out: 6 0 0 0 0 
out_pos: 1
UIPClient allocation failed
connected, state: 192, first packet in: 0
Cliente conectado
stop(), data: NULL
Client disconnect

It seems that "client.connect()" returns a true value, but there is a fail on the UIPClient.cpp routine operation, so there is no connection. Even with a restart of "Ethernet.begin()" the situation persist.

on motion.

thks. sls hg

hg_xyz: It always failed after 4 transmissions the message "UIPClient allocation failed" is in the UIPClient.cpp

that means your code does not read all incomming data and is not calling client.stop() to release this unread data either to cleanup after the remote host did close his side. By default there are 4 slots for connection-data.

The local port is incremented with each new connection so the identifier (the tuple of local+remote-ip and port) is unique for every new connection. That is required for tcp to work.

  • Norbert

Ok.

The night have been very useful, the sketch is running since 5 hours by now. The situation is that i not use the serial access to monitor and i inserted some flags over the lcd to do the evolution control of the job.

Are any posibilities that the time or resources of the serial link have been the core of the problem?

Any way almost every 5 o 6 minutes the conecction is lost but with a call to the object “Ethernet.begin()” the link is renewed, and (most important) NO MORE hangs.

I will verify the sketch looking for the missed or delayed call to the object “client.stop()” as you have suggested. .

your concern is remarkable, the library is awesome.

Thks.
sls
hg