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