[RESOLVED] Call a ethernet function from interrup

Hello all.
I'm JP, French and I making a part counter for a production machine.
I'm sorry but some of my code comments and variable name will be a mix of French and English. I'll do my best to rename them without destroying the code done.

This counter sends to a Mysql database on the network the counter value and the according time value.
The machine can reset the counter for a new production batch.
And finally the arduino send the time according to a time sent from the PC.

The fianl goal is to see the production figure (counter, # of stop, the reason for that, graph a production day according to these stops) but also for the production expert calculate the MTTR and MTBF (Mean time To Repare and Mean Time between Failure)...
I'll be please to send to the open source community this tool once this one will be up and ready.

I'm using an Arduino UNO and a Ethernet shield.

Before to go further, I would like to thank you all this community and the interessant thing I could have found.

Nethertheless.

#include <SoftwareSerial.h>
#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0x90, 0xA2, 0xDA, 0x0D, 0xB0, 0xD0 };   // Enter a MAC address for your controller below.
IPAddress serverIP(192,168,1,102);                      // serveur MySql
EthernetServer server(80); 				//server port
IPAddress ip(192,168,1,177); 				// =arduino


// Variables for the counter
int sensorValue ;  					// value of the counter
int ConnectedPin = 7;    
int CompteurLedPin = 9; 				// Send a data
unsigned long dernierPush; 				// LastPushTime

// Reset
int ledPin = 6; 					// LED pin Reset






////////////////////////////////////////////////////
/// Fonction Sends the data to a php file that inserts into a mysql database
////////////////////////////////////////////////////
void Envoie_data(){

  EthernetClient client;
 
  Serial.println ("Dans procedure Envoie Data");
  if (client.connect(serverIP, 80)) {
    // Make a HTTP request:
    client.println("GET /temp.php?t="+ String(now()) + "&&t1=" + String(sensorValue) + " HTTP/1.0");
    Serial.println("GET /temp.php?t="+ String(now()) + "&&t1=" + String(sensorValue) + " HTTP/1.0");
    client.println();
    client.stop();
    digitalWrite(CompteurLedPin, HIGH);   // set the LED on
    delay(500);              		  // wait for a 1/2 second
    digitalWrite(CompteurLedPin, LOW);    // set the LED off
    digitalWrite(ConnectedPin, HIGH);     // set the LED on
  } 
  else {
    // if you didn't get a connection to the server:
    digitalWrite(ConnectedPin, LOW);      // set the LED off
    Serial.println("connection failed");
  }
}


/////////////////////////////////////
// counteur interrupt
/////////////////////////////////::::
void CompteurInterrupt(){

   digitalWrite(ledPin, LOW);    	// set the LED off for the First part counted
   Serial.print(millis()); Serial.print("  "); Serial.print(dernierPush);  Serial.print("  "); Serial.println(millis()-500);

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
       Serial.println("Blabla..!!");
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

   
    if(dernierPush < (millis() - 500)){
      digitalWrite(ledPin, HIGH);    	// set the LED off
      sensorValue ++;  // Met à jour le compteur
      dernierPush = millis();		// LastPushTime
      noInterrupts();

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      Envoie_data();  			// Send The data to Mysql
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      interrupts();
      }
}


/////////////////////////
/// Procedure de setup
//////////////////////////
void setup() {

  Serial.begin(9600);
  pinMode(ConnectedPin, OUTPUT);  // pin connected to the sensor (blinks during acquisition)
  pinMode(CompteurLedPin, OUTPUT); // Pin de check de network connection
  pinMode(ledPin, OUTPUT); // pin de check reset counter


   digitalWrite(ledPin, HIGH);    // part counted (set 1)
   dernierPush =0;		  // LastPushTime

 // start the Ethernet connection:
    Ethernet.begin(mac, ip);

    Serial.println("connecting...");
    digitalWrite(ConnectedPin, HIGH);   // set the LED on
    server.begin();
    Serial.println("Server.begin");
    Serial.println(Ethernet.localIP());

   
//    interrupts();
    attachInterrupt(0, CompteurInterrupt, RISING); 
}


/////////////////////////
/// Procedure de loop
//////////////////////////
void loop() {

}

Well. Here are my two questions:

1/ Why should I connect the 2 interrupt pins (2 & 3). If I connect 1 pin only like all the exemples and the good tutorial I could have read?
But If I make a small function call, it works fine like that.

2/ In the CompteurInterrupt() liked to the interrupt, this one work fine with Serial.println and led management.. but if I call the function Envoie_data(); then the arduino is block and Serial.println("Blabla..!!"); is not displayed.
But if I comment the function Envoie_data, I get the Serial.println("Blabla..!!"); PLACED BEFORE this call

Any Help will be welcom to understand why this call blocs everything.

Please see the attached file for the connection done: (made with Fritzing)

Many thanks
JP

counter.png

It's not the ethernet call that causes the problem. It is the Serial calls. The new IDE version has a interrupt driven buffer for the Serial functions. You can't call an interrupt driven function from an interrupt.

The ethernet library doesn't use interrupts, but may take a while to complete. Not something I recommend for an interrupt function.

First of all, many thanks for this quick response.

If I comment the line Envoie_data();
I get the serial message displayed and the led on.

If I uncomment the line Envoie_data();
The arduino does not responds anymore

I place here Serial.Println() line to debug

My bad. I lied about the ethernet library not using interrupts. It doesn't directly, but it uses the delay() function in places. That requires interrupts working to update the time, and they aren't while the processor is in an interrupt.

If I uncomment the line Envoie_data();
The arduino does not responds anymore

No serial data is sent during an interrupt. There is a small buffer to hold data to be sent. As long as that buffer does not fill up, Serial.print() will return, so the ISR can end.

If the buffer gets full, Serial.print() blocks until there is room in the buffer for the data that it needs to place in the buffer.

But, since nothing leaves the buffer during an ISR, there will never be more room in the buffer, so Serial.print() will never return.

The Envoie_data() function does a LOT of Serial.print()ing. The buffer WILL fill up, and Serial.print() will then block.

Which is exactly what you see happening.

By the way, an ISR is supposed to be fast, like when the clock ticks, the value that millis() returns is updated. Sending data to the internet is NOT fast, except in geological terms. But, that is not the time scale that the Arduino uses.

Once again many thanks to all reader and to you gays who gave me some time to respond.

Unfortunuatly it's not a problem of Serial.println().

but it's due to this blody Firewall that was reset !!

I found it by using the basics and the exemple given with the dev tool.