Arduino Sending POST Data to Server using Ethernet Shield(w5100) will randomly stop at around 6 hours

I have two arduinos in a master slave configuration. The first arduino is connected to a plc and gets data from it then sends it to the second arduino via analog and that all works perfectly fine no issues.

But on the second arduino it receives that analog values and puts them into a char array and then sends that array to a server. That works until about 6 hours have passed then the arduino freezes. I'm new to the ethernet shield and never had to do much debugging until now so I'm lost. The data rate it sends up is every few seconds.

Code below


#include <SPI.h>
#include <Ethernet.h>
#include <avr/wdt.h>


// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(10, 115, 250, 74); //static IP for the ethernet shield
//IPAddress myDns(192, 168, 1, 1);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
//int status = WL_IDLE_STATUS;

char server[] = "xxx.xx.xx.x"; // IOT Digital Ocean

String postData;
String postVariable = "temp=";


String postData2;
String postVariable2 = "& tempc=";

String review;

//WiFiClient client;

#include <Wire.h>

unsigned long restartTime = 3600000;
unsigned long startTime;
//1 hour in milliseconds

void setup() {
  startTime= millis();
  //record start time
Wire.begin(4);                // join i2c bus with address #4

  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  Serial.println("Connecting.....");
  // else {
    //Serial.print("  DHCP assigned IP ");
    //Serial.println(Ethernet.localIP());

     // give the Ethernet shield a second to initialize:
  delay(1000);

  IPAddress ip = Ethernet.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  }
 
 char array[70] = {};





void loop() {
 if (millis() - startTime >= restartTime){
  Serial.print("Trying to reset");
  //reboot();
  Ethernet.begin(mac);
  //digitalWrite(A0, HIGH);
  reboot();
 }
Wire.onReceive(receiveEvent); // register event


  review = (postVariable+array);
  Serial.print(review+"\n");
if (review != "temp="){
  if (client.connect(server, 80)) {
    client.println("POST (this is the server url) HTTP/1.1");
    client.println("Host: 192.168.88.254");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(review.length());
    client.println();
    client.print(review);
    delay (500);
   
    delay (500);
    Serial.println("Email Sent");
    //Serial.println(array[5]+array[6]);

    //Serial.println("Email2 Sent");
    //Serial.println(array[6]);
    
    while(client.connected()){
      if(client.available()){
        //Read incoming byte from the server and print to the serial monitor
        char c = client.read();
        Serial.print(c);
      }
    }
//////////////////////////////////////////////////////////////////////
   // delay(1000);

  
delay(1000);
  }

  if (client.connected()) {
    client.stop();
  }

  delay(6000);
}

  //Serial.println(postData);

  delay(3000);
}

int i;
void receiveEvent(int howMany)
{
  
  int valueofarray = 0;
  while(1 < Wire.available()) // loop through all but the last
  {
    
    char character = Wire.read(); // receive byte as a character
    array[valueofarray] = character;        // print the character
    //Serial.println(array);
    //Serial.println(array[valueofarray]);
//Serial.println(sizeof(array));
    valueofarray++;


    
  }
  int x = Wire.read();    // receive byte as an integer
  valueofarray = 0;
    //Serial.println(x);         // print the integer

    

}

void sendMessage() {
  Serial.print("working you dumb\n");
}

void reboot() {
  wdt_disable();
  wdt_enable(WDTO_15MS);
  while (1){}
}

edit:

this is how the data looks in console

Content-Length: 62

Content-Type: text/html; charset=UTF-8

from #17 : 62 31335 0 0 0 0 0 0New record created successfullytemp=from #17 : 62 31335 0 0 0 0 0 0

Email Sent

HTTP/1.1 200 OK

Date: Mon, 15 Apr 2024 19:56:59 GMT

Server: Apache/2.4.52 (Ubuntu)

Content-Length: 62

Content-Type: text/html; charset=UTF-8

from #17 : 62 31335 0 0 0 0 0 0New record created successfullytemp=from #17 : 61 31335 0 0 0 0 0 0

Email Sent

HTTP/1.1 200 OK

Date: Mon, 15 Apr 2024 19:57:15 GMT

Server: Apache/2.4.52 (Ubuntu)

Content-Length: 62

Content-Type: text/html; charset=UTF-8

try to always do client.stop(). don't test for connected()

I am unsure what you mean with client.stop and dont test for connected()

you have

  if (client.connected()) {
    client.stop();
  }

change it to

  client.stop();

I see now my bad. I have changed that in my code. I also made more of my variables into char arrays

You are rebooting your device once every hour. And you have some endless loops on setup() on some failed checks. While some other checks just prints out errors, but continues execution without any sort of error handling.

Did you tried to monitor a Serial port for the whole time from succesful start till the "freeze"? To see what's going on during that free and what was the last message on the serial?

I suggest you checking that at first place if possible to pinpoint the part where it freezes. My bet what it happens during one of the reboots when it tries to initialize again and freezes probably because things like external device weren't resetted as Arduino. I.e. you ethernet shield was powered on and initialized. If your Arduino decided to reboot while it was processing something it might not accept initialization again and you might end up in the "Ethernet shield was not found...." endless loop or somewhere else. Even not the endless loop of your code, but in some other sort of the loop or unhandled exception of the driver library. You can't trust that "EthernetNoHardware" error code really means that "there is no hardware" in fact. All it means that error was caught by the driver. But you have to handle it too, instead of just dead-end looping.

I did monitor the whole time in the serial

HTTP/1.1 200 OK

Date: Mon, 15 Apr 2024 19:56:59 GMT

Server: Apache/2.4.52 (Ubuntu)

Content-Length: 62

Content-Type: text/html; charset=UTF-8

from #17 : 62 31335 0 0 0 0 0 0New record created successfully

This is an example of what it stops at

The reboot was an attempt to fix the solution and it didn't but that is why it is there. It had this same issue before I made it reboot.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.