Iot cloud plus SMS

Is it possible to be connected to the arduino iot cloud and simultaneously send an SMS? Alternatively is there a way to disconnect from the cloud, send an SMS, then reconnect to the cloud. Something like ArduinoCloud.end(); or ArduinoCloud.disconnect();

This code compiles fine and runs fine untill the void SMS code runs, then the board reboots.

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/160cc0f5-2ece-41eb-86a5-373f1f10e69e 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  int temperature;
  bool alarmStatus;
  bool engineOff;
  bool engineStart;
  bool engineStatus;
  bool lock;
  bool lockStatus;
  bool unlock;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/
//adjustments
int reconnectionTimer = 10000;
int watchDogTime = 60000;
///////////////////////////////////////////////////////////////////////////////////////////

bool connectionAttempt = true;
unsigned long currentReconnectionTimer;
unsigned long previousReconnectionTimer;
unsigned long currentWatchDogTime;
unsigned long previousWatchDogTime;

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
bool connected = false;
bool alarmStatusFlag = false;
    
#include "thingProperties.h"
#include <MKRNB.h>

NB nbAccess;
NB_SMS sms;

String serialMessageReceived;
const unsigned int MAX_MESSAGE_LENGTH = 20;


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(115200);
  Serial1.begin(9600);

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  

}

void loop() {
  ArduinoCloud.update();
  // reconnect to cloud //////////////////////////////////////////////////////////////////////////////////////
  if(!ArduinoCloud.connected() && connectionAttempt == false){
    Serial.println("Not connected to cloud");
      ArduinoCloud.begin(ArduinoIoTPreferredConnection);
      connectionAttempt = true;
  }
    currentReconnectionTimer = millis();
  if (currentReconnectionTimer - previousReconnectionTimer > reconnectionTimer){
    connectionAttempt = false;
    previousReconnectionTimer = currentReconnectionTimer;
  }
  //serial recieve///////////////////////////////////////////////////////////////////////////////////////////
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial1.available() > 0 && newData == false) {
        rc = Serial1.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }


    if (newData == true) {
      if (strcmp(receivedChars, "unlocked") == 0) {
        Serial.println("lockStatus = false");
        lockStatus = false;
      }
      if (strcmp(receivedChars, "locked") == 0) {
        Serial.println("lockStatus = true");
        lockStatus = true;
      }
      if (strcmp(receivedChars, "eRun") == 0) {
        Serial.println("engineStatus = true");
        engineStatus = true;
      }
      if (strcmp(receivedChars, "eOff") == 0) {
        Serial.println("engineStatus = false");
        engineStatus = false;
      }
      if (receivedChars[2] == 'F') {
        temperature = atoi(receivedChars);
        Serial.println(temperature);
      }
      if (strcmp(receivedChars, "alarmOff") == 0) {
        Serial.println("alarmStatus = false");
        alarmStatus = false;
        alarmStatusFlag = false;
      }
      if (strcmp(receivedChars, "alarmOn") == 0) {
        Serial.println("alarmStatus = true");
        alarmStatus = true;
        if(alarmStatusFlag == false){
                SMS();
                alarmStatusFlag = true;
        }
      }
        newData = false;
    }
watchDog();
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////
}//end void loop
void SMS(){

  while (!connected) {
    if (nbAccess.begin("") == NB_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
        sms.beginSMS("+1336XXXXXXX");
        sms.print("Alarm 4 Runner");
        sms.endSMS();
}

void watchDog(){
  currentWatchDogTime = millis();
  
if(currentWatchDogTime - previousWatchDogTime > watchDogTime && ArduinoCloud.connected()){
  Serial1.print("alive");
  previousWatchDogTime = currentWatchDogTime;
}
}


/*
  Since Unlock is READ_WRITE variable, onUnlockChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onUnlockChange()  {
  if(unlock == true){
    Serial1.print("UnlockDoor");
    Serial.println("UnlockDoor");
    unlock = false;
  }
}

/*
  Since Lock is READ_WRITE variable, onLockChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLockChange()  {
  if(lock == true){
    Serial1.print("LockDoor");
    Serial.println("LockDoor");
    lock = false;
  }
}


/*
  Since EngineStart is READ_WRITE variable, onEngineStartChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onEngineStartChange()  {
if(engineStart == true){
  Serial1.print("StartEngine");
  Serial.println("Start Engine");
  engineStart = false;
  }
}

/*
  Since EngineOff is READ_WRITE variable, onEngineOffChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onEngineOffChange()  {
if(engineOff == true){
  Serial1.print("StopEngine");
  Serial.println("Stop Engine");
  engineOff = false;
  }
}

/*
  Since Temperature is READ_WRITE variable, onTemperatureChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTemperatureChange()  {
  // Add your code here to act upon Temperature change
}




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