Go Down

Topic: Board freezes up (Read 377 times) previous topic - next topic

Clintdm92

Jan 28, 2019, 08:13 pm Last Edit: Jan 28, 2019, 09:25 pm by Clintdm92
Hello,

I'm having an issues with my project. I believe it is an issue with my code, but it could also be an issue with my power supply. Either way, I will appreciate any C&C on my code (I am not a frequent programmer and am very rusty with C++).

But first, a little background on my project. I am using a MKR GSM 1400 board to build an ultrasonic water level sensor which will take a reading once every hour and text me the results. Additionally, I have it set up take readings on command (it will take a reading and return that reading to anybody who sends it a text). I intend to eventually set this project up in a remote location and power it with a solar panel, but for now I am just running tests in my house.

Currently, I have been able to get it to run for maximum of 24 hours (I believe this record was reached using the 5v pin to power the distance sensor) when powered via the USB port (along with a 2500mA LiPo battery of course), but oftentimes it will freeze up at around the 8 or 9 hour mark. When I have ran tests with the board powered only by the battery, it seems to last only an hour or two before freezing up (this could just be coincidence since I have only ran a couple tests using battery power only).

Also note that I am using this distance sensor from Adafruit: https://www.adafruit.com/product/4007

Below is my sketch.

Code: [Select]

// library which utilizes timekeeping hardware included with the MKR class Arduino boards
#include <RTCZero.h>

// library which manages the GSM modem included with the MKR GSM model Arduino board
#include <MKRGSM.h>

// there is no pinnumber set for this sim card, so it is left blank
#define PINNUMBER ""

// initialize GSM classes
GSM gsmAccess;
GSM_SMS sms;

// set time and date of board initialization here
RTCZero rtc;
const byte day = 1;
const byte month = 1;
const byte year = 0;

const byte hrs = 0;
const byte mins = 0;
const byte sec = 0;

// create array to store the phone number of the incoming SMS
char senderNumber[20];

// create variables to store distance sensor pin numbers
const int trigger = 0;
const int echo = 1;

// create boolean variable which will signify when it is time to send data
bool alarmTriggered = false;

// create string variable to store SMS messages which are received by the board
String command;


void setup() {
  // initialize real time clock and set time and date
  rtc.begin();
  rtc.setTime(hrs,mins,sec);
  rtc.setDate(day,month,year);

  // initialize serial connection and wait for it to connect before continuing
  Serial.begin(9600);
  //while(!Serial);     //uncomment this when using serial connection to test
 
  Serial.print("SMS water level monitor \nInitialized at: ");
  timeStamp();         // the timeStamp() function prints the time and date to the serial monitor
  Serial.println();

  // setup pins which are to be used by the distance sensor
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);

  // wait while board establishes connection with GSM network
  bool connected = false;
  while(!connected){
    if(gsmAccess.begin(PINNUMBER) == GSM_READY){
      connected = true;
    }
    else{
     Serial.println("Not Connected");
     delay(1000);
    }
  }
  Serial.println("GSM initialized");
  gsmAccess.lowPowerMode();
  Serial.println("GSM modem is in low power mode");
 
  rtc.setAlarmTime(00,01,00);
  rtc.enableAlarm(rtc.MATCH_MMSS);
  rtc.attachInterrupt(alarmMatch);
 
  Serial.println("Alarm set");
  Serial.println("Waiting for messages");
 
}

void loop() {
  // when a text is recieved, read the text and return distance reading to sender
  if (sms.available()){
    // store sender number
    Serial.println("\nrequest recieved from: ");
    sms.remoteNumber(senderNumber, 20);
    Serial.println(senderNumber);
   
    // print sms message to the serial monitor
    int c;
    int i = 0;
    char com[20] = "";
    while ((c = sms.read()) != -1) {
      Serial.print((char)c);
      com[i] = (char)c;
      i++;
    }
    Serial.println();
   
    // convert character array to a sting which can be compared later
    command = String(com);
    Serial.println(command);
   
    Serial.println(" -- END OF MESSAGE");
   
    // delete message from modem memory
    sms.flush();
    Serial.println(" -- MESSAGE DELETED");


    // shutdown gsm modem and completely stop program if texted to do so.
    if (command=="Stop"){
      gsmAccess.shutdown();
      Serial.println("GSM Modem shutdown and program stopped");
      while(1); // Comment out if you want to continue to log data after shutdown
    }
   
    // print data to sms message and send
    sms.beginSMS(senderNumber);
    sms.print("Request recieved.\r");
    sms.print(getDistance(),1);
    sms.print(" cm  @ ");
    smsTimeStamp();
    sms.endSMS();
   
    Serial.println("Data Sent");
   
  }
 
  // check if alarm has been triggered. If true, get distance reading and send to me
  if (alarmTriggered){
   
    sms.beginSMS("--MY NUMBER GOES HERE--");
    sms.print(getDistance(),1);
    sms.print(" cm  @ ");
    smsTimeStamp();
    sms.endSMS();
    Serial.println("Data Sent");
    alarmTriggered = false;
   
  }

  // Debugging code
  //Serial.print(getDistance(),1);
  //Serial.print("  @ ");
  //timeStamp();
  //Serial.println();
  //delay(5000);
 
}

//function which returns distance of object or surface from the ultrasonic sensor
float getDistance(){
  long duration;
  float cm;
  digitalWrite(trigger, LOW);
  delayMicroseconds(5);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);

  duration = pulseIn(echo,HIGH);
  cm = (duration/2)/29.1;
 
  return cm;
}

// execute this function at the specified alarm time
void alarmMatch(){
  Serial.println();
  Serial.print("Alarm Match!  @ ");
  timeStamp();
  Serial.println();
  alarmTriggered = true;
}

// prints a time-stamp to the serial monitor
void timeStamp(){
  // Print date...
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print("  ");

  // ...and time
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());
}

// formats the date and time for the serial time-stamp function
void print2digits(int number) {
  if (number < 10) {
    Serial.print("0"); // print a 0 before if the number is < than 10
  }
  Serial.print(number);
}

// prints a time-stamp to the sms mesage
void smsTimeStamp(){
  // Print date...
  smsPrint2digits(rtc.getDay());
  sms.print("/");
  smsPrint2digits(rtc.getMonth());
  sms.print("/");
  smsPrint2digits(rtc.getYear());
  sms.print("  ");

  // ...and time
  smsPrint2digits(rtc.getHours());
  sms.print(":");
  smsPrint2digits(rtc.getMinutes());
  sms.print(":");
  smsPrint2digits(rtc.getSeconds());
}

// formats the date and time for the sms time-stamp function
void smsPrint2digits(int number) {
  if (number < 10) {
    sms.print("0"); // print a 0 before if the number is < than 10
  }
  sms.print(number);
}



smgs

#1
Feb 05, 2019, 04:05 am Last Edit: Feb 05, 2019, 04:05 am by smgs
I'm a bit rusty with Arduino this week, but I thought if you wanted to turn a char array into a string there were more steps involved than this code:

Code: [Select]
// convert character array to a sting which can be compared later
   command = String(com);
   Serial.println(command);



Your code is compiling okay though. I haven't got my MKR GSM 1400 pugged in at the minute, but are you seeing the serial output you are expecting etc?

Do you have a diagram about how your unit's all connected up? I've seen some issues when people power sensors from VIO or similar, but you've indicated your's is from the 5V rail, so that should be okay....

Go Up