0

Problems withFeather Fona and GSM
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Problems withFeather Fona and GSM

by bauerbyter on Thu Jun 24, 2021 2:03 am

Hey,
so I created a door watcher. The DS3231 turns it on every 12 hours to send status update( in code at the moment every 30 min for testing) or when the door opens.
But I have the problem that the "fona.enableGPRS(true)" randomly fails and I absolutely don't know why. I don't think it has something to do with the schematics because it happens also with the feather fona standalone.
I added some delays, because I find out, that this helps, but is there maybe another way to say: Wait for the fona to be ready?
What can I do better?
Thanks in advance.

Screenshot_20210624_075134.png
Screenshot_20210624_075134.png (68.8 KiB) Viewed 47 times


Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_FONA.h"
#include <ArduinoJson.h>
#include "RTClib.h"

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

// this is a large buffer for replies
char replybuffer[255];

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

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
RTC_DS3231 rtc;

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

uint8_t type;

char url[80] = "http://foobar.com"; //REMOVED
StaticJsonDocument<200> doc;

int dsPowerPin = 10;
int door = 0;

void (*resetFunc)(void) = 0;
void shutdown()
{
  Serial.println("SHUTDOWN!");
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);
}

void tryAgain()
{
  Serial.print("No Succed: ");
  int minutes = rtc.now().minute();
  if ((minutes > 5 && minutes < 29) || (minutes > 35))
  {
    Serial.println("shutdown");
    shutdown();
  }
  else
  {
    Serial.println("restart");
    resetFunc();
  }
}

void setupGSM()
{
  Serial.println(F("Initializing....(May take 3 seconds)"));

  fonaSerial->begin(4800);
  if (!fona.begin(*fonaSerial))
  {
    Serial.println(F("Couldn't find FONA"));
    shutdown();
    while (1)
      ;
  }
  Serial.println(F("FONA is OK"));
  digitalWrite(13, LOW);
  delay(5000);

  fona.setGPRSNetworkSettings(F("internet.eplus.de"), F("blau"), F("blau"));

  delay(10000);

  digitalWrite(13, HIGH);
  // turn GPRS on
  if (!fona.enableGPRS(true))
  {
    Serial.println(F("Failed to turn on"));
    Serial.println(F("RESET!"));
    tryAgain();
  }
}

void setupRTC()
{
  if (!rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    return;
  }
  else
  {
    rtc.writeSqwPinMode(DS3231_OFF);
    Serial.println("RTC READY");
  }

  if (rtc.lostPower())
  {
    Serial.println("RTC lost power, let's set the time!");

    //GET GSM TIME
    if (!fona.enableNTPTimeSync(true, F("pool.ntp.org")))
      Serial.println(F("Failed to enable"));

    delay(3000);

    char buffer[23];

    fona.getTime(buffer, 23); // make sure replybuffer is at least 23 bytes!
    Serial.print(F("Time from GSM= "));
    Serial.println(buffer);
    String date = buffer;

    DateTime dateTime = DateTime(date.substring(1, 3).toInt(), date.substring(4, 6).toInt(), date.substring(7, 9).toInt(), date.substring(10, 12).toInt() + 2, date.substring(13, 15).toInt(), date.substring(16, 18).toInt());

    rtc.adjust(dateTime);
    Serial.print(F("Time from RTC= "));
    Serial.println(rtc.now().timestamp());
    //------
    if (!rtc.setAlarm1(
            DateTime(0, 0, 0, 0, 0, 0),
            DS3231_A1_Minute))
    {
      Serial.println("Error, alarm1 wasn't set!");
    }
    else
    {
      Serial.println("New Alarm1 set");
    }
    if (!rtc.setAlarm2(
            DateTime(0, 0, 0, 0, 30, 0),
            DS3231_A2_Minute))
    {
      Serial.println("Error, alarm2 wasn't set!");
    }
    else
    {
      Serial.println("New Alarm2 set");
    }
  }
}

void setup()
{
  Serial.begin(115200);

  pinMode(dsPowerPin, OUTPUT);
  pinMode(13, OUTPUT);

  digitalWrite(dsPowerPin, HIGH);
  digitalWrite(13, HIGH);

  delay(3000);

  setupGSM();
  setupRTC();

  Serial.println(F("Setup finished"));
}

void post()
{

  delay(10000);
  //get battery
  uint16_t vbat;
  uint16_t per;
  fona.getBattVoltage(&vbat);
  fona.getBattPercent(&per);

  if (rtc.alarmFired(1) || rtc.alarmFired(2))
  {
    door = 0;
  }
  else
  {
    door = 1;
  }

  // Create Json
  char temp[200];
  doc["door"] = door;
  doc["volt"] = vbat;
  doc["per"] = per;
  serializeJson(doc, temp);
  Serial.print("JSON: ");
  Serial.println(temp);

  // Post data to website
  uint16_t statuscode;
  int16_t length;

  Serial.println(F("****"));
  if (!fona.HTTP_POST_start(url, F("application/json"), (uint8_t *)temp, strlen(temp), &statuscode, (uint16_t *)&length))
  {
    Serial.println("Failed!");
    Serial.print("Statuscode: ");
    Serial.println(statuscode);
    tryAgain();
    return;
  }
  while (length > 0)
  {
    while (fona.available())
    {
      char c = fona.read();

      Serial.write(c);
      length--;
      if (!length)
        break;
    }
  }
  Serial.println(F("\n****"));
  fona.HTTP_POST_end();
  Serial.println(F("send DONE"));

  if (!fona.enableGPRS(false))
  {
    Serial.println(F("Failed to turn GSM off"));
  }
  else
  {
    Serial.println(F("GSM turned off"));
  }
}
char date[10] = "hh:mm:ss";
void loop()
{
  while (Serial.available())
    Serial.read();
  post();

  digitalWrite(13, LOW);
  delay(1000);

  while (1)
  {
    shutdown();
    delay(900000);
    Serial.println("RESTART!!!");
    resetFunc();
  }
}

bauerbyter
 
Posts: 6
Joined: Mon Mar 17, 2014 4:44 am

Re: Problems withFeather Fona and GSM

by bauerbyter on Sat Jun 26, 2021 4:24 am

nobody?

bauerbyter
 
Posts: 6
Joined: Mon Mar 17, 2014 4:44 am

Re: Problems withFeather Fona and GSM

by adafruit_support_mike on Sun Jun 27, 2021 10:42 pm

Add some code to check your connection before calling any code that needs a working connection.

With wireless signals, the question isn't whether the connection will drop, but when. For anything that needs to run reliably and long-term, you need code that checks the connection and re-establishes a connection when necessary.

adafruit_support_mike
 
Posts: 63059
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.