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