Sending Data by SMS with Adafruit Fona GSM 800H Board

Backround:
I have a lake house in a remote location and and need a way to monitor things like temperature and water in the basement. the cellular signal is very poor. I want to send a daily SMS and emergency SMS if the temperature falls below 40F. I have a start on the code but for some reason it only goes through the loop 3 or 4 times and seems to stop. any help or advice is appreciated.

Here is my Code

#include "Adafruit_FONA.h"

// Libraries for DS18b20 temp sensor
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);


#define FONA_RX 9
#define FONA_TX 8
#define FONA_RST 4
#define FONA_RI 7

#define FloatPin 6
byte FloatAlarm = 0;
byte SMS_Flag = 0;
bool FloatState;

int MaxTemp = 0;
int MinTemp = 100;
int CurrentTemp = 0;
float AverageTemp = 0;
unsigned long CurrentTime;
unsigned long PreviousTime;

#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

#define phoneNumber "Place Phone number here"

uint8_t type;


void setup() {
  pinMode(FloatPin, INPUT_PULLUP);
  //pinMode(5,INPUT_PULLUP)
  //while (!Serial);

  Serial.begin(115200);
  Serial.println(F("FONA basic test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));

  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while (1);
  }
  type = fona.type();
  Serial.println(F("FONA is OK"));
  Serial.print(F("Found "));
  switch (type) {
    case FONA800L:
      Serial.println(F("FONA 800L")); break;
    case FONA800H:
      Serial.println(F("FONA 800H")); break;
    case FONA808_V1:
      Serial.println(F("FONA 808 (v1)")); break;
    case FONA808_V2:
      Serial.println(F("FONA 808 (v2)")); break;
    case FONA3G_A:
      Serial.println(F("FONA 3G (American)")); break;
    case FONA3G_E:
      Serial.println(F("FONA 3G (European)")); break;
    default:
      Serial.println(F("???"));
      break;
  }

  // Print module IMEI number.
  char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
  uint8_t imeiLen = fona.getIMEI(imei);
  if (imeiLen > 0) {
    Serial.print("Module IMEI: "); Serial.println(imei);
  }

  // This while is to wait till the phone is connected to the network.
  while (1) {
    uint8_t n = fona.getNetworkStatus();
    Serial.print(F("Network status"));
    Serial.print(n);
    Serial.print(F(": "));
    if (n == 0) Serial.println(F("Not registered"));
    if (n == 1) Serial.println(F("Registered (Home)"));
    if (n == 2) Serial.println(F("Not registered (searching)"));
    if (n == 3) Serial.println(F("Denied"));
    if (n == 4) Serial.println(F("Unkown"));
    if (n == 5) Serial.println(F("Registered Roaming"));
    delay(100);// Give time for connection before retry

    if (n == 1) break;
  }


  Serial.println("Ready to text");

  // Start up the Dallas library
  sensors.begin();

}

void loop() {
  Serial.println(F("Start Of the Loop"));
  delay(5000);

  String text = temp();
  String messages = text ;
  char txt[messages.length() - 1];
  messages.toCharArray(txt, messages.length());
Serial.println("Reading temp");
  for (int i = 0; i <= 10; i++) {
    sensors.requestTemperatures(); // Send the command to get temperatures
    Serial.print("i =");
    Serial.print(i);
    delay(1000);
    int CurrentTemp = (sensors.getTempFByIndex(0));
    if (CurrentTemp > MaxTemp) {
      MaxTemp = CurrentTemp;
    }
    else if (CurrentTemp < MinTemp) {
      MinTemp = CurrentTemp;
    }
    else {
      Serial.println("Not min or max");
    }
  }
Serial.println("Checking Float");
  if ((digitalRead(FloatPin) == LOW) && (FloatAlarm < 2)) {

    if (!fona.sendSMS(phoneNumber, "The crawl space has water in it!!")) {
      Serial.println(F("Failed"));
    }
    else {
      Serial.println(F(" Sent!"));
      FloatAlarm++;
      delay(5000);
    }
  }
  else {
    Serial.println(F("Float Is OK"));
  }
  
  Serial.println("Sending SMS IF Not sent");
  if (SMS_Flag < 1 ) {
    Serial.println(phoneNumber);
    Serial.println(txt);
    if (!fona.sendSMS(phoneNumber, txt)) {
      Serial.println(F("Failed"));
      delay(5000);
    }
    else {
      Serial.println(F("Sent!"));
      SMS_Flag++;
    }

    Serial.println("\nCOMPLETE!\n");
    delay(5000);
  }
  Serial.println("End of loop");
}



String temp() {
  // read the battery voltage and percentage
  uint16_t vbat = 0;
  if (!fona.getBattVoltage(&vbat)) {
    Serial.println(F("Failed to read Batt"));

  }
  else {
    Serial.print(F("VBat = "));
    Serial.print(vbat);
    Serial.println(F(" mV"));
    Serial.print("SMS_Flag = ");
    Serial.println(SMS_Flag);
    Serial.print("FloatPin = ");
    Serial.println(digitalRead(FloatPin));
    Serial.print("FloatAlarm = ");
    Serial.println(FloatAlarm);
    Serial.print("MaxTemp = ");
    Serial.println(MaxTemp);
    Serial.print("CurrentTemp = ");
    Serial.println(sensors.getTempFByIndex(0));
    Serial.print("MinTemp = ");
    Serial.println(MinTemp);
    sensors.requestTemperatures(); // Send the command to get temperatures

    String temperatureC = String(sensors.getTempCByIndex(0));
    String temperatureF = String(sensors.getTempFByIndex(0));

    return ("Temperature: C = " + temperatureC + "\nTemperature: F = " + temperatureF + "\nVBat = " + vbat + "mV ");

  }



}

Gambit120:
Backround:

void loop() {

Serial.println(F(“Start Of the Loop”));
  delay(5000);

String text = temp();
  String messages = text ;
  char txt[messages.length() - 1];

You can’t dynamically create a char array ‘txt’ like that. You should also avoid the String library and use regular char arrays. It’s quite possible that after a few times through the loop, you are stomping over memory or something.

There's no problem with creating a local char array like that. The problem is that he made it 2 characters too short to fit the String. So he is corrupting memory.

And I'll echo the comment about the String class. If you want long term stability, then avoid the String class like a plague on micro-controllers.

OK so I'm not exactly sure what is meant by "2 characters too short", but what i am hearing is that the string function is the source of my problems. I got that from an example sketch, i don't really understand it. I will take your advice and rewrite the code. Thank you for your advice. ill let you know how it gos :slight_smile:

char txt[messages.length() - 1];

I mean right there where you take a string that is length + 1 characters long and try to put it into an array that is length - 1 characters big. Ain't gonna fit.