Hi everyone,
In my project i must use arduino1 and Ethernet Shield for send data to a server.
pseudocode of first implementation:
every 10 seconds the arduino send data to the server.
These version works, but after several hours the module freezes and does not send data to the server.
-there isnt Serial.print in the ISR
-i declared as "volatile" each variable used in ISR
-the ISR is void
#include <TimerOne.h>
#include <Ethernet.h>
#define PULSE_PORT 2
byte mac[] = { mac };
IPAddress ip(arduinoIp);
IPAddress server(serverIp);
EthernetClient client;
volatile int pulses;
String request = "";
void setup()
{
Serial.begin(9600);
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
//set a timer of length 10000000 microseconds = 10 seconds
Timer1.initialize(10000000);
//attach the service routine here
Timer1.attachInterrupt( timerIsr );
attachInterrupt(digitalPinToInterrupt(PULSE_PORT), read_pulse, FALLING);
}
void loop()
{
// DO NOTHING
}
void timerIsr()
{
//connect with server
int connectionResult = client.connect(server, 80);
if (connectionResult == 1)
{
request += "GET ";
request += "fill the request...":
client.println(request + " HTTP/1.1");
client.println("Host: iparduino");
client.println();
client.stop();
request = "";
}
else
{
pulses = 0;
client.stop();
}
}
void read_pulse()
{
// digitalRead() of PULSE_PORT and enhance the variable pulses
}
At these version i uploaded with a blinking led in the loop to debug the arduino freeze.
#define IS_ALIVE 8
void loop()
{
digitalWrite(IS_ALIVE, HIGH);
delay(1000);
digitalWrite(IS_ALIVE, LOW);
delay(1000);
}
During the freeze the led stop blinking and it remains off
then i try to change my firmware in tjese way:
-i removed the Timer1 and ISR
-every 10 second the arduino send data
#include <TimerOne.h>
#include <Ethernet.h>
#define GETMAC String(String(mac[0], HEX) + ":" + String(mac[1], HEX) + ":" + String(mac[2], HEX) + ":" + String(mac[3], HEX)+ ":" + String(mac[4], HEX) + ":" + String(mac[5], HEX))
#define PULSE_PORT 2
byte mac[] = {macArduino};
IPAddress ip(idArduino);
IPAddress server(IpServer);
EthernetClient client;
int packageNumber = 0;
int time_scale;
int watt_scale;
volatile int pulse_input;
volatile int pulses;
volatile int totalPulses;
String request = "";
int interrCount = 0;
void setup()
{
Serial.begin(9600);
time_scale = 1;
watt_scale = 1;
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
attachInterrupt(digitalPinToInterrupt(PULSE_PORT), read_pulse, FALLING);
}
void loop()
{
delay(1000);
interrCount++;
if ( interrCount == 10 )
{
int connectionResult = client.connect(server, 80);
if (connectionResult == 1)
{
request += "GET ";
request += "fill the request...with pulse and totalpulses value (in url format)";
client.println(request + " HTTP/1.1");
client.println("Host: ipArduino");
client.println();
client.stop();
request = "";
}
interrCount=0;
}
}
// these function counts the button push on a custom module attached to the eth shield
// while (i--); --> tto avoid too close input
// PULSE_PORT is pin n2
void read_pulse()
{
volatile int i = 5;
while (i--);
pulse_input = digitalRead(PULSE_PORT);
if (pulse_input == LOW)
{
pulses++;
totalPulses++;
pulse_input = digitalRead(PULSE_PORT);
while (pulse_input == LOW)
{
pulse_input = digitalRead(PULSE_PORT);
}
}
}
The latest version freeze arduino approximately after one day!!!
However, i dont understand where is the problem.
I used a binked led before the delay in loop to debug the freeze.
During the freeze:
-the led is turned off
-in eth schield the led PWD, LINK, 100M, FULLD, COLL are on; the RX and TX bink every 8/10 seconds
-Arduino ide say me:
Sketch uses 15,592 bytes (48%) of program storage space. Maximum is 32,256 bytes.
Global variables use 770 bytes (37%) of dynamic memory, leaving 1,278 bytes for local variables. Maximum is 2,048 bytes.
-furthermore i used #include <MemoryFree.h> to study the arduino memory usage and it result constant during the tests
I do not understand how to debug the freeze
Could someone help me?
Sorry for my english