0

I made an project that takes data from DHT sensor and sends it through sms if requested. I think it is some kind of memory issue but I want to know if I can do something about it. It would be very helpful if anyone can tell how can I improve my code so that arduino doesn't get freezed after sometime.

#include <DHT.h> //Include the DHT library.
#include <SoftwareSerial.h>    //Library required for serial communication

#define dataPin 4 //Define the type data pin
#define DHTType DHT22 //Define the DHT sensor (DHT11, DHT21, or DHT22)

SoftwareSerial sim800l(3, 2); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted

//Instantiate the dht class with our data pin and DHT type.
DHT dht = DHT(dataPin, DHTType);
float h;
float t;
float maxtemp;
float mintemp;
String today;
String inchar;
String a;
String d;
unsigned long previousMillis = 0;
const long interval = 50000; 
unsigned long previousMillis1 = 0;
const long interval1 = 3600000;
void setup() {
  Serial.begin(9600); //Start the serial interface on 9600
  sim800l.begin(9600);
    dht.begin();   //Call the begin class in the dht object
    h = dht.readHumidity(); //read the humidity from the sensor
    t = dht.readTemperature();  // Read temperature as Celsius (the default), insert true as a parameter for fahrenheit
    maxtemp = t;
    mintemp = t;
      // Give time to your GSM shield log on to network
  delay(2000);
  Serial.print("SIM800L ready...");
 delay(100);
  // AT command to set sim800l to SMS mode
  sim800l.println("AT+CMGF=1"); 
  //Serialcom();
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  sim800l.println("AT+CNMI=2,2,0,0,0");
  delay(100);
  serialFlush();
  sim800l.println("AT+CSMP=17,167,0,8");
  delay(100);
  serialFlush();
  sim800l.println("AT+CCLK?");
  d = sim800l.readString();
  Serial.print(d);
  d.remove(27);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    dhtgetvalue();
  }
  
//  delay(2000); // Delay for 2 seconds as the DHT22 sampling rate is 0.5Hz
 
  
  if(sim800l.available() >0) {
    inchar=sim800l.readString(); 
    a=inchar.indexOf("DHT");
  Serial.println(inchar);
  Serial.println(a);
    if (a>"50")
    {
      delay(10);
      inchar.remove(0,9);
      inchar.remove(13);
   Serial.println(inchar);
   delay(10);
      // REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER
      // USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS
      //sim800l.println("AT+CMGS=\"+919462590402\"\r");
      sim800l.println("AT+CMGS=\"" + String(inchar) + ("\"\r"));
      delay(100);
      // REPLACE WITH YOUR OWN SMS MESSAGE CONTENT
      String dataMessage = ("Temperature: " + String(t) + "*C " + " Humidity: " + String(h) + "%" + " Max Temp: " + String(maxtemp) + " Min Temp: " + String(mintemp));
      // Uncomment to change message with farenheit temperature
      // String dataMessage = ("Temperature: " + String(f) + "*F " + " Humidity: " + String(h) + "%");      
      
      // Send the SMS text message
      sim800l.print(dataMessage);
      delay(100);
      // End AT command with a ^Z, ASCII code 26
      sim800l.println((char)26); 
      delay(100);
      sim800l.println();
      // Give module time to send SMS
      delay(1000);  
    }
  } 
  
   else{
    unsigned long currentMillis1 = millis();

       if (currentMillis1 - previousMillis1 >= interval1) {
    // save the last time you blinked the LED
    previousMillis1 = currentMillis1;

    chkdt();}
 }
}

void chkdt()
{
  
  serialFlush();
  sim800l.println("AT+CCLK?");
  today = sim800l.readString();
  today.remove(27);
  if (d == today){
    Serial.print("SAME DATE");
  }
  else{
    Serial.println(d);
    Serial.println(today);
    d = today;
    maxtemp = 0;
    mintemp = 50;
  }
  
}


void serialFlush(){
  while(sim800l.available() > 0) {
    char t = sim800l.read();
  }
}
void dhtgetvalue(){
   
  h = dht.readHumidity(); //read the humidity from the sensor
  
  t = dht.readTemperature();  // Read temperature as Celsius (the default), insert true as a parameter for fahrenheit
  if (t > maxtemp) {
    maxtemp = t;
  }
  if (t < mintemp) {
    mintemp = t;
  }
}  
  • This can help confirm your diagnostic: Measuring Memory Usage. – Edgar Bonet Jun 21 at 9:09
  • 3
    You can remove all references to String and prevent heap fragmentation. – Majenko Jun 21 at 9:42
  • How long is "some time" and what happens after that? Also, if your code does not work as expected, put some extra serial.print() commands in appropriate places, allowing you to see what your code is doing and to see where/when things go wrong. – StarCat Jun 21 at 12:06
  • You could enable the watchdog timer, so the Arduino resets if your code hangs for more that, for example, 8 seconds. Adding a watchdog is something I'd advise to do anyways, even without your current problem. But I'd try to fix the underlying problem. – Gerben Jun 21 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.