Nano with SIM808 and I2C-LCD crashes

I have a Nano, I2C LCD, SIM808 and a thermistor (47k). I am trying to make a box with a temperature probe that SMS a warning when the set limit is reached, and to reply with the current temp if asked with a codeword ("On").

It works, but chrashes. Either it just hangs, LCD freeze, LCD blacks out or any other weird thing.

The current setup blanks the LCD, bet continues to reply to SMSs.

Power source is a 9v 3A dc high quality converter. SIM808 powered directly, Nano and LCD with a ubec output 5v 3A. Thermistor by the 3.3 form nano. Using recommended resistors as needed.

Am I puzzled bu the blank LCD and the responding SIM808. The loop runs, and the lcd.clear() does not help? Nothing is displayed. It probably stops responding soon anyway. I just can't find any consistency nor predictability in its errors.

I have tried to minimise strings, and using a freemem routing did not show any decrease in memory.

#include <SoftwareSerial.h> 
#include <RunningAverage.h>
#include <LiquidCrystal_I2C.h>

#define BCOEFFICIENT 3550                      //3950 org, 3550 git korrekt temp ved 0 C.

LiquidCrystal_I2C lcd(0x27,16,2);              //SDA - A4, SCL - A5, VCC - 5V

RunningAverage avgTemp(10);                    // define the moving average object
SoftwareSerial GPSmodule(2, 3);                // RX, TX

const String varsel = "+XXXXXXXXXX";           //skriv inn telefonnummer for varsel med +XX
const int dogn = 3600000 ;                     //millisekunder i et dogn (86400000)
const int mintemp = 20;                        //setter varseltemp i C
String CellNum;
int avg;
float last2 = 0;
float lastTime = 0;
float last = 0;

void setup()
{
  analogReference(EXTERNAL);
  lcd.init();
  lcd.backlight();
  lcd.print("  Starter opp!  ");
  
  GPSmodule.begin(9600);
  
  boot();
  
  GPSmodule.println("AT");
  delay(50);    
  GPSmodule.println("AT");
  delay(50);  
  GPSmodule.println("AT+CMEE=1");
  delay(50);
  GPSmodule.println("AT+CMGF=1");              // AT command to set GPSmodule to SMS mode
  delay(50);
  GPSmodule.println("AT+CNMI=2,2,0,0,0");      // Set module to send SMS data to serial out upon receipt
  delay(5000);

  lcd.clear();
}

void loop()
{
  float t1;
  float Celcius;
  String textMessage;                          // Variable to store text message
  String CellNumtemp;
  
  if (lastTime > millis()){
    last = 0;
    last2 = 0;
  }
  
  t1 = analogRead(A3);

  if ( t1 < 1000 && (t1 > 0)) {
    Celcius = totemp(t1);
  }
  
  if (millis() - 10000 > last){                //venter "pause" på å legge inn ny verdi i average
    last = millis();
    avgTemp.addValue(Celcius);
    avg = avgTemp.getAverage();
    
    lcd.clear();

    if (avg > -20 && (avg < 40)){
    lcd.setCursor(0,0);
    lcd.print("         ");
    lcd.setCursor(0,0);
    lcd.print(avg);
    lcd.print(F(" \xDF""C"));
    } 
  }

  while (GPSmodule.available() > 0) {
      char c = GPSmodule.read();
      textMessage += c;
      delay(10);                               //small delay to allow input buffer to fill 
  }

  if (textMessage.indexOf("On") >= 0) {        //"On" er kodeordet i SMSen, bytt til noe annet
    CellNumtemp = textMessage.substring(textMessage.indexOf("+47"));
    CellNum = CellNumtemp.substring(0,11);
    String message = "Temperaturen er " + String(avg) + " C";
    sendSMS(message);
  }

  if (avg <= mintemp){
    if (millis() > dogn + last2){              //sender et varsel i dognet
      CellNum = varsel;
      String message2 = "Temperaturen er " + String(avg) + " C";
      sendSMS(message2);
      last2 = millis();
    }
  }
  
  lastTime = millis();
}

void sendSMS(String message3) {
  String sende;
  sende = "AT+CMGS=\"" + CellNum + "\"";
  GPSmodule.println(sende);
  delay(100);
  GPSmodule.print(message3);
  delay(100);

  GPSmodule.println((char)26);
  delay(50);
  GPSmodule.println();

  CellNum = "";
  
  delay(5000);
}

void boot(){
    
  delay(500);
  
  if (analogRead(A1) > 700){
  }
  else{ 
    digitalWrite(5, HIGH);                     //til D9 på SIM808, skrur den på
    delay(1200);
    digitalWrite(5, LOW);
    
    delay(500);
  }
  
  delay(5000);
}

float totemp(float i){
  i = 1023 / i - 1;
  i = 47000 / i;                               //RTS300R47K3.81A 47k thermoresistor
  
  float steinhart;
  steinhart = i / 47000;                       // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (25 + 273.15);            // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert absolute temp to C
  return steinhart;
}

What kind of Nano?

On classic Nano 3, int can hold numbers only up to 32000. For 3600000 you must use long.

No fancy version of Nano, don't know what version.

Thank you for a quick reply, I will test this now.

As for last, last2 and lastTime, should I use long in stead of float? They just store current mills().

Well, the LCD still blanks out after sending "On" to the setup, even with correcting "const long dogn"

How are you able to upload your code to the Nano if you don't know what kind? What board are you selecting in the IDE?

For variables that hold values of millis() use unsigned long.

Time to read the forum guide in the sticky post at the top of most forum sections. Post your schematic. Post links to the components you are using.

Nano with old bootloader works in IDE.

I can somewhat consistently crash it now, see modified code and serial monitor.

Freeze after "193" after receiving and replying to a SMS. Could be a timing issue, can't see anything wrong with lcd.setCursor(0,1); between 193 and 194

Serial monitor:

20:06:53.287 -> 1
20:06:53.287 -> 3
20:06:53.287 -> 11
20:06:53.777 -> 12
20:06:53.777 -> 13
20:06:53.777 -> 24
20:06:54.056 -> 25
20:06:59.081 -> 14
20:06:59.081 -> 15
20:06:59.081 -> 19
20:06:59.081 -> 191
20:06:59.081 -> 193

Modified code:

#include <SoftwareSerial.h> 
#include <RunningAverage.h>
#include <LiquidCrystal_I2C.h>

#define BCOEFFICIENT 3550                      //3950 org, 3550 git korrekt temp ved 0 C.

LiquidCrystal_I2C lcd(0x27,16,2);              //SDA - A4, SCL - A5, VCC - 5V

RunningAverage avgTemp(10);                    // define the moving average object
SoftwareSerial GPSmodule(2, 3);                // RX, TX

const String varsel = "+4741411883";           //skriv inn telefonnummer for varsel med +47
const long dogn = 3600000 ;                     //millisekunder i et dogn (86400000)
const int mintemp = 20;                        //setter varseltemp i C
String CellNum;
int avg;
byte activity = 0;
unsigned long lastTime = 0;
unsigned long last = 0;
unsigned long last2 = 0;
unsigned long last3 = 0;


void setup()
{
  analogReference(EXTERNAL);
  lcd.init();
  lcd.backlight();
  lcd.print("  Starter opp!  ");

  Serial.begin(9600);
  GPSmodule.begin(9600);
  
  boot();
  
  GPSmodule.println("AT");
  delay(50);    
  GPSmodule.println("AT");
  delay(50);  
  GPSmodule.println("AT+CMEE=1");
  delay(50);
  GPSmodule.println("AT+CMGF=1");              // AT command to set GPSmodule to SMS mode
  delay(50);
  GPSmodule.println("AT+CNMI=2,2,0,0,0");      // Set module to send SMS data to serial out upon receipt
  delay(5000);

  lcd.clear();
}

void loop()
{
  float t1;
  float Celcius;
  String textMessage;                          // Variable to store text message
  String CellNumtemp;
  
  Serial.println("1");
  
  if (lastTime > millis()){
    Serial.println("2");
    last = 0;
    last2 = 0;
    
  }

  Serial.println("3");
    
  if (millis() - 10000 > last){                //venter "pause" på å legge inn ny verdi i average
    Serial.println("4");
    t1 = analogRead(A3);

    if ( t1 < 1000 && (t1 > 0)) {
      Serial.println("5");
      Celcius = totemp(t1);
    }
    
    Serial.println("6");
    last = millis();
    avgTemp.addValue(Celcius);
    avg = avgTemp.getAverage();
    Serial.println("7");
    lcd.clear();
    Serial.println("8");
    
    if (avg > -20 && (avg < 40)){
      Serial.println("9");
      lcd.setCursor(0,0);
      lcd.print("         ");
      lcd.setCursor(0,0);
      lcd.print(avg);
      lcd.print(F(" \xDF""C"));
      Serial.println("10");
    } 
  }

  Serial.println("11");

  while (GPSmodule.available() > 0) {
    delay(10);                               //small delay to allow input buffer to fill 
    char c = GPSmodule.read();
    textMessage += c;
  }

  Serial.println("12");

  if (textMessage.indexOf("On") >= 0) {        //"On" er kodeordet i SMSen, bytt til noe annet
    Serial.println("13");
    CellNumtemp = textMessage.substring(textMessage.indexOf("+47"));
    textMessage = "";
    CellNum = CellNumtemp.substring(0,11);
    CellNumtemp = "";
    String message = "Temperaturen er " + String(avg) + " C";
    sendSMS(message);
    message = "";
    CellNum = "";
    Serial.println("14");
  }

  Serial.println("15");

  if (avg <= mintemp){
    Serial.println("16");
    if (millis() > dogn + last2){              //sender et varsel i dognet
      Serial.println("17");
      CellNum = varsel;
      String message2 = "Temperaturen er " + String(avg) + " C";
      sendSMS(message2);
      CellNum = "";
      message2 = "";
      last2 = millis();
      Serial.println("18");
    }
  }

  Serial.println("19");

 if (millis() - 1000 > last3){
     Serial.println("191");
     if (activity == 0){
      Serial.println("20");
      lcd.setCursor(0,1);
      lcd.print("*");
      activity = 1;
      Serial.println("192");
    }
    else{
      Serial.println("193");
      lcd.setCursor(0,1);
      Serial.println("194");
      lcd.print(" ");
      Serial.println("195");
      activity = 0;
      Serial.println("21");
    }
    last3 = millis();
    Serial.println("196");
  }
  
  Serial.println("22");

  lastTime = millis();
  Serial.println("23");
}

void sendSMS(String message3) {
  Serial.println("24");
  String sende;
  sende = "AT+CMGS=\"" + CellNum + "\"";
  GPSmodule.println(sende);
  delay(100);
  GPSmodule.print(message3);
  delay(100);

  GPSmodule.println((char)26);
  delay(50);
  GPSmodule.println();

  CellNum = "";
  Serial.println("25");
  delay(5000);
}

void boot(){
    
  delay(500);
  Serial.println("26");
  if (analogRead(A1) > 700){
  }
  else{ 
    digitalWrite(5, HIGH);                     //til D9 på SIM808, skrur den på
    delay(1200);
    digitalWrite(5, LOW);
    
    delay(500);
  }
  Serial.println("27");
  delay(5000);
}

float totemp(float i){
  Serial.println("28");
  i = 1023 / i - 1;
  i = 47000 / i;                               //RTS300R47K3.81A 47k thermoresistor
  
  float steinhart;
  steinhart = i / 47000;                       // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (25 + 273.15);            // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert absolute temp to C
  Serial.println("29");
  return steinhart;
}

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