Arduino Uno Freezes after a while consistently

Hi,
I realize this is a common topic and some of the suggestions but this sketch has string variables and I don't know how to eliminate them (many have suggested this as a possible problem)

The sketch uses an Arduino UNO and a DHT temp/humidity sensor to send that data via WiFi to a local Coldfusion Server but it makes no difference where the data is sent (locally or to an Internet address), it still locks up after about the same length of time. Maybe it's the HEAP and memory fragmentation? How do I check that? It seems the WiFi is still connected but the sketch just stops looping.

Thank you for your suggestions


#include <DHT.h>
#include <DHT_U.h>
#include <DHT.h>;
#include<Timer.h>
Timer t;
#include <SoftwareSerial.h>
SoftwareSerial Serial1(2, 3);

//#define dht_dpin 12 
#define DHTPIN 12
#define heart 13
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

const char *workaround="OK";
static char postUrl[150];
float humi;
float tem;
//int tem,humi;
float temConv;
float humiConv;
void httpGet(String ip, String path, int port=80);

void setup()
{
 Serial1.begin(57600);
 Serial.begin(57600);
 Serial.println("Delay: 5Min(300000ms), ESP8266-10, DHT22-2"); 
 Serial.println("Connecting Wifi....");

 connect_wifi("AT",1000);
 connect_wifi("AT+CWMODE=1",1000);
 connect_wifi("AT+CIPSTA=\"192.168.xxx.xxx\",\"192.168.xxx.xxx\",\"255.255.255.000\"",10000);
 connect_wifi("AT+CWQAP",1000);  
 connect_wifi("AT+RST",5000);
 connect_wifi("AT+CWJAP=\"SSID\",\"PIN\"",10000);

 
 Serial.println("Wifi Connected"); 
 pinMode(heart, OUTPUT);
 delay(2000);
 t.oscillate(heart, 1000, LOW);
 t.every(300000, send2server);  // 50000 = 1 minute
}

void loop()
{

    humi = dht.readHumidity();
    tem= dht.readTemperature();
 // DHT.read11(DHTPIN);
  //humi=DHT.humidity;
 // tem=DHT.temperature;
  delay(2000);
  t.update();
}

void send2server()
{
  char tempStr[8];
  char humidStr[8];
  char idStr[8];  
  temConv=(tem*1.8)+32;
  temConv=temConv-3.5; //Correction for DHT22-2

   // Humidity Calibrations
   // if ((humi <= 47) && (humi > 40)) humiConv=humi+1;
   // if ((humi <= 40) && (humi > 39)) humiConv=humi+2;
   // if ((humi <= 39) && (humi > 36)) humiConv=humi+3;
   // if ((humi <= 36) && (humi > 34)) humiConv=humi+4;
   // if (humi <= 34) humiConv=humi+5;
   humiConv=humi;  //For DHT22 -2
   
  dtostrf(temConv, 5, 3, tempStr);
  dtostrf(humiConv, 5, 3, humidStr);
  
  sprintf(postUrl, "temphumid/temphumid.cfm?humidity=%s&temperature=%s&ID=TH-1",humidStr,tempStr,idStr);
     httpGet("192.168.xxx.xxx", postUrl, 80);
}


void httpGet(String ip, String path, int port)
{
  int resp;
  String atHttpGetCmd = "GET /"+path+" HTTP/1.0\r\n\r\n";
   String atTcpPortConnectCmd = "AT+CIPSTART=\"TCP\",\""+ip+"\","+port+"";
  connect_wifi(atTcpPortConnectCmd,2000);
  int len = atHttpGetCmd.length();
  String atSendCmd = "AT+CIPSEND=";
  atSendCmd+=len;
  
  connect_wifi(atSendCmd,3000);
  connect_wifi(atHttpGetCmd,3000);
}

void connect_wifi(String cmd, int t)
{
  int temp=0,i=0;
  while(1)
  {
    Serial.println(cmd);
    Serial1.println(cmd); 
    while(Serial1.available())
    {
      if(Serial1.find("OK"))

      i=8;
    }
    delay(t);
    if(i>5)
    break;
    i++;
  }
  if(i==8)
  {
   Serial.println("OK");
  }
  else
  {
   Serial.println("Error");
  }
}

Remove String type, it doesn't work on an Uno.

A bit harsh.

Simply removing the lines declaring string variable will render the sketch broken. Do you have any suggestions on how to replace these lines with equivalents that do not use the STRING type?

@quamikazee Installation and Troubleshooting is for Problems with the Arduino itself NOT your project. It says so in the description of the section. Therefore I have moved your post here. Please be more careful where you post in future.

You may want to read this before you proceed:-
how to get the best out of this forum

It's called cstrings, character arrays terminated with '\0'.

Some tips on String (capital S); not saying that it will solve your issue.
1)
Don't use plus to concatenate String objects. Use concat() instead. Each time you use plus, a new temporary String object is created.
2)

void httpGet(String ip, String path, int port)

When calling httpGet, String objects get copied which costs memory. That can be prevented by using references.

void httpGet(String &ip, String &path, int port)
1 Like

Thanks for this.
Very helpful.

Would these lines be examples where the plus sign should be replaced?

 connect_wifi("AT",1000);
 connect_wifi("AT+CWMODE=1",1000);
 connect_wifi("AT+CIPSTA=\"192.168.000.57\",\"192.168.000.002\",\"255.255.255.000\"",10000);
 connect_wifi("AT+CWQAP",1000);  
 connect_wifi("AT+RST",5000);
 connect_wifi("AT+CWJAP=\"RomeoWhisky\",\"11507185\"",10000);

No, that contains just a literal plus in the text. You use e.g. ...+path+... in you code; that is a concatenation that you have to change.

1 Like

I'm sure I'm just slow, but when I try that I get an error. Am I misunderstanding this?

Original


  String atHttpGetCmd = "GET /"+path+" HTTP/1.0\r\n\r\n";

Changed To


  String atHttpGetCmd = "GET /"concat(path)concat(" HTTP/1.0\r\n\r\n");`

Did you try to compile it?

  String atHttpGetCmd = "GET /";
  atHttpGetCmd.concat(path);
  atHttpGetCmd.concat(" HTTP/1.0\r\n\r\n");
1 Like

Your version worked.
Apparently I'll need to read the concat() documentation again.
There's a lot there I just don't understand.

Thanks for your patience and help.

A few things I've since learned after making the code changes recommended to save memory.

1). Breadboards and the associated wire connectors are not stable and are prone to interruptions.

2). When using WiFi devices such as the ESP8266 it is very possible that some of the interruptions in code processing loops could actually originate in the WiFi router you have. Apparently, rebooting the WiFi router often seems to keep the Arduino device going. I use a TP-WR940N which is a low end router. Maybe a high end router would do better so when someone says they've been running an Arduino sketch on WiFi for a long time with no problems, get them to tell you what WiFi router they are using.

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