Arduino UNO R3 as webclient restarting repeatedly

Hello everyone!

I have connected my arduino uno R3 to intern site in small factory. I go pulses from PLC to SQL (always every 100 sec. i got one pulse).

My code work perfectly but once per 14 hours arduino restarting itself (my prediction).
Because second part (with temp variable) didnt process.

HERE IS AN EXAMPLE OF CODE:

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

byte mac[] = {
// 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};

IPAddress ip(xx);
IPAddress gateway(xx);
IPAddress dns(xx);
IPAddress subnet(xx);

EthernetClient client;

 const int InputSignal[6] = {2,3,4,5,6,7}; // InputSignal[0] = 2
 const bool Validation[6] = {true, true, true, true, true, true};
 bool val = true;
 
void setup() { 
 Serial.begin(9600);
 Ethernet.begin(mac, ip, dns, gateway, subnet);
 Serial.println(F("Inicializace proměnných."));
 delay(100); // Bezpečnostní čas pro nastartování čipu
 pinMode(7, INPUT);
 //Serial.println(Ethernet.localIP());  
}


void loop(){
 // I know i can use loop with other arguments, but this is more lovely solution
 ProcessData("Machine=1&Validation=secret",7,val);
}

void ProcessData(String InputString, int InputSignal, bool &temp) {

if(digitalRead(InputSignal) == HIGH) {
   if(temp == true){

     if (client.connect("(my secret ip)",80)) { // 443 = HTTPS
     client.println("GET (my secret file)"); 
     client.println(" HTTP/1.1");
     client.println("Host: (my secret ip)");
     client.println("User-Agent: Arduino");
     client.println("Accept: text/html");  
     client.println(InputString.length()); 
     client.println(); 
     } 
     if (client.connected()) { 
     client.stop();
     }
     temp = false;
   }
 }

 if(digitalRead(InputSignal)==LOW && temp==false){
     if (client.connect("(my secret ip)",80)) {
     client.println("GET (my secret file)"); 
     client.println(" HTTP/1.1");
     client.println("Host: (my secret ip)");
     client.println("Accept: text/html");  
     client.println(InputString.length()); 
     client.println(); 
     } 

     if (client.connected()) { 
     client.stop();
     }
     temp = true;     
 }
 
}

The second part starting with:
if(digitalRead(InputSignal)==LOW && temp==false) once per 14 hours is killed. Only one reason is why, because help variable is restarted by starting a chip.

Im thinking about add to every client.println(F(

Please can you help me why it is possible? I can filter the one bad input per this long period, but im lil bit nervous. Thanks guys!!!

I’m not sure what you are saying is the issue

I Would start by getting rid of the String class that’s absolutely non necessary in your code, just do avoid ProcessData(const char * InputString, int InputSignal, bool &temp) {and replaceclient.println(InputString.length());withclient.println(strlen(InputString));that will possibly help with memory

J-M-L: void ProcessData(const char * InputString, int InputSignal, bool &temp) {

Yeah you are absolutly right. This is type of message what i need. I had a lot of Serial.println() commands in my code and restart period was about 3 hours. After deleting this one im getting 14 hours period. I test your idea with other my conditions like if(client.connected()) to restart helping variable.

Thanks for your time!! +1 KARMA

Check also the usage of Ethernet.maintain() to ensure you don’t get kicked out (see sample code here)

J-M-L:
Check also the usage of Ethernet.maintain() to ensure you don’t get kicked out (see sample code here)

Thanks man for your time!! :slight_smile:

Im also thinking about connecting to server always on start of function… What you whink about connect in void setup() ? Because chip working 24/7. So i think its not effective connect and disconnect always when function starts.

emololftw: Thanks man for your time!! :)

Im also thinking about connecting to server always on start of function.. What you whink about connect in void setup() ? Because chip working 24/7. So i think its not effective connect and disconnect always when function starts.

yes - connecting in setup is probably the way to go. just ensure you maintain the connection if you use DHCP and try to handle errors properly if things fail.