Arduino crashes after some time even with a reset

Hi Guys,

I have a question about updating my Thinkspeak-Channel with a temperature value. As already said I have a temperature sense (DHT22) which is located outside and I want to send this signal to a Thinkspeak Channel. After one or two days the Arduino crashes. Then I re-plugged-in the Power-Supply and it works again one or two days. I can not define the exactly time when it comes to a crash. Sometimes it happens after around 24 hours, sometimes around 30 hours... Below is my code. I want to ask you where the Error might be.
What I noticed and did against it, so far:

  • First I was doing it with an ESP8266. I was thinking maybe the W-Lan-Signal crashes. To exclude this error I switched to an Arduino-Nano and wired a W5500 Ethernet-Card to it.

  • I suggested that the Stack and the Heap crashes together. To exclude this error I called a Reset with the zero-function-Pointer after each sending process to ensure there is always space between Stack and Heap. I think you know what a call of "void(* resetFunc) (void) = 0;" does. Otherwise check the code.

  • I suggested that it might be the Power-Supply. So I changed it.

Thank you so far.
Cheers
Quiddi

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h" //DHT Bibliothek laden
void(* resetFunc) (void) = 0; //declare reset function @ address 

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0x0A, 0x3A, 0x6B, 0x7C, 0x2E, 0xA2 };
#define DHTINNENPIN 5
#define DHTTYPEINNEN DHT22
DHT dhtinnen(DHTINNENPIN, DHTTYPEINNEN);
const char *host = "api.thingspeak.com";
String apiKey = "08dsfghlkdnfghtH"; 
unsigned long LetzteSendung=0;
  bool SendungBeendet = false;
// 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;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.println(Ethernet.localIP());
  dhtinnen.begin(); 

}

void loop() {
  delay(2000);
    float h = dhtinnen.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dhtinnen.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dhtinnen.readTemperature(true);


    
  bool plausible = true;
  unsigned long AktuelleZeit=0;
  AktuelleZeit = millis();


  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    plausible = false;
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dhtinnen.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dhtinnen.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
// Sende Daten

if (plausible && !SendungBeendet)
{

  
   
  const int httpPort = 80; //Port 80 is commonly used for www
 //---------------------------------------------------------------------
 //Connect to host, host(web site) is define at top 
 if(!client.connect(host, httpPort)){
   Serial.println("Connection Failed");
   delay(300);
   return; //Keep retrying until we get connected
 }
 
//---------------------------------------------------------------------
  //Make GET request as pet HTTP GET Protocol format
  String Link="GET /update?api_key="+apiKey+"&field1=";  //Requeste webpage  
  Link = Link + String(t);
  Link = Link + "&field2=";  //Requeste webpage  
  Link = Link + String(h);
  Link = Link + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n";                
  client.print(Link);
  delay(100);
  
//---------------------------------------------------------------------
 //Wait for server to respond with timeout of 5 Seconds
 int timeout=0;
 while((!client.available()) && (timeout < 1000))     //Wait 5 seconds for data
 {
   delay(10);  //Use this with time out
   timeout++;
 }

//---------------------------------------------------------------------
 //If data is available before time out read it.
 if(timeout < 500)
 {
     while(client.available()){
        Serial.println(client.readString()); //Response from ThingSpeak       
     }
 }
 else
 {
     Serial.println("Request timeout..");
 }
 LetzteSendung = AktuelleZeit;
 delay(500);
SendungBeendet = true;
 }

 if (AktuelleZeit > 360000){
SendungBeendet = false;
    resetFunc();  //call reset
}
 }
if (AktuelleZeit > 360000) {
    SendungBeendet = false;
    resetFunc();  //call reset
  }
void(* resetFunc) (void) = 0; //declare reset function @ address

So.. No matter what. After every 6 minutes you just jump to address 0?

-jim lee

So.. No matter what. After every 6 minutes you just jump to address 0?

Exactly. To reboot the Arduino.

I was thinking that I have problems with the destructors. You know that from one side of the RAM the Stack comes and from the other the Heap. If they crash together, the Sytem crashes. This is why I tried to keep the code like this:

  • Boot the System
  • Read and Send the Data to Thinkspeak
  • Reboot the System after 6 minutes
  • Read and Send the Data to Thinkspeak again
  • and so on and so on

Do you use handy telephone poles to stop your car?

-jim lee

Try the ThingSpeak Ethernet or ESP8266 library examples and see if that makes a difference. thingspeak-arduino/examples at master · mathworks/thingspeak-arduino · GitHub

Would certainly be cleaner, if nothing else.

I've had an ESP8266 and ESP32 running continuously for months using the library code.

Probably unrelated to your issue...but unless you have the paid version of ThingSpeak, you can't post more than once every 15 seconds. I didn't look all that hard at your code, but it seems you may be trying to post faster than that.

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