Arduino IoT Cloud doesn't update my variables when using ArduinoCloud.update()

Hi there,

I'm trying to set up a project that gets the location of the MKR GSM 1400 by means of the location service "CellLocate", and use the information for projecting the latitude and longitude into the Arduino IoT Cloud widget "Map".

This widget uses a variable called "CloudLocation" for work, and has to be updated like this <Variable_name> = {<Latitude_Double_format>, <Longitude_Double_format>} . For example (assuming my variable is named 'coordinates') :

coordinates = {5.5307279, -87.0592366};

When using the loop() function with relative simple code, I don't get any problems. For example:

void loop() {
  ArduinoCloud.update();

  LatD  = LatD  + 0.0001000; //Double variable containing 7 decimals
  LongD = LongD + 0.0001000; //Double variable containing 7 decimals
    
  coordinates = {LatD, LongD};
}

But, my problem is, when I try to make a more complicated code, the ArduinoCloud.update() function stops updating the CloudLocation variable. So, the code keeps running, but the variables in the cloud and in the map widget stay static.

And, for the record, I'm aware of the watchdog timer that reeboots the board after 16 seconds or so without using the ArduinoCloud.update() function. So, I disable it.

Without further waiting, here's my code at this moment (Some parts are in spanish, sorry):

#include "thingProperties.h"

#include <MKRGSM.h>

#define LF  0x0A

//Constantes secretas a utilizar en el código:
const char UBLOX_APIKEY[]   = SECRET_UBLOX_APIKEY;    //Rellenar con el Token para hacer uso de CellLocate.

//Constantes a utilizar en el código:
unsigned long baud = 115200;      //Tasa de baudios usada para ambos puertos seriales.

//Variables:
char    respuesta_char[10]; //Variable para guardar la respuesta recibida del módulo.
int     indx = 0;           //Índice para concadenar la respuesta recibida del módulo.
int     lastCommaIndex;     //Va a ser el índice que permita cortar el string del URC.
String  Respuesta_str;      //Variable para guardar la respuesta del módulo.
String  Incertidumbre;      //Variables que guardan info del URC dado por el módulo.
String  Latitud, Longitud;  //Variables que guardan info del URC dado por el módulo.
double  LatD, LongD;        //Variables para almacenar los strings de Latitud y Longitud en número.


void setup() {
  
  // Initialize serial and wait for port to open:
  Serial.begin(baud);     //"Serial" es para el Monitor Serial de las plataformas de Arduino.
  SerialGSM.begin(baud);  //"SerialGSM" está definido para comunicarse con el módulo celular.
  // 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();
  
  while (!ArduinoCloud.connected()) {
    Serial.println("Waiting for connection to Arduino IoT Cloud");
    ArduinoCloud.update();
  }
	Serial.println("Connected to cloud");
  
  MODEM.send("AT+UGSRV=\"cell-live1.services.u-blox.com\",\"cell-live2.services.u-blox.com\",\"" + String(UBLOX_APIKEY) + "\"");
  MODEM.waitForResponse(100);
  MODEM.send("AT+ULOCCELL=1");
  MODEM.waitForResponse(100);
  //MODEM.send("AT+ULOC=2,2,2,20,1");
  //MODEM.waitForResponse(100);
  Serial.println("Configuración del módulo lista...");
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  
  if (ArduinoCloud.connected() == 0) {
    //not connected do nothing or whatever
    Serial.println("Se desconectó, restableciendo conexión.");
    ArduinoCloud.update();
  }
  
  else {
    //I'm now connected, put code here that should only run while connected.
    
    //delay(200);
    //MODEM.send("AT+ULOC=2,2,2,10,1"); //Ver si lo cambio por AT+ULOC=2,2,2,60,1
    SerialGSM.write("AT+ULOC=2,2,2,20,10\r\n");
    
    while(true){
      
      if (SerialGSM.available() > 0) {
        respuesta_char[indx] = SerialGSM.read();
        if (respuesta_char[indx] == LF) {
          Serial.print("Recibí: ");
          respuesta_char[indx-1] = 0;
          Serial.println(respuesta_char);
          indx = -1;
          
          //ArduinoCloud.update();
          
          Respuesta_str = String(respuesta_char);
          if (Respuesta_str.startsWith("+UULOC: ") && Respuesta_str.endsWith(",95")) {
            
            Respuesta_str.remove(Respuesta_str.lastIndexOf(','));
            Respuesta_str.remove(Respuesta_str.lastIndexOf(','));
            Respuesta_str.remove(Respuesta_str.lastIndexOf(','));
            
            lastCommaIndex = Respuesta_str.lastIndexOf(',');
            Incertidumbre = Respuesta_str.substring(lastCommaIndex + 1);
            Respuesta_str.remove(lastCommaIndex);
            
            lastCommaIndex = Respuesta_str.lastIndexOf(',');
            Longitud = Respuesta_str.substring(lastCommaIndex + 1);
            Respuesta_str.remove(lastCommaIndex);
            
            lastCommaIndex = Respuesta_str.lastIndexOf(',');
            Latitud = Respuesta_str.substring(lastCommaIndex + 1);
            Respuesta_str.remove(lastCommaIndex);
            
            LatD  = Latitud.toDouble();
            LongD = Longitud.toDouble();
            
            coordenadas = {LatD, LongD};
            
            Serial.println();
            Serial.println("Latitud(95%): "  +Latitud);
            Serial.println("Longitud(95%): " +Longitud);
            Serial.println("Incertidumbre(95%): " + Incertidumbre + "m");
            Serial.print("Latitud(#): ");
            Serial.println(LatD,7);
            Serial.print("Longitud(#): ");
            Serial.println(LongD,7);
            Serial.println();
            
            indx++;
            break;
            
            //ArduinoCloud.update();
            
          }
          else if (Respuesta_str.startsWith("+UULOC")){
            
            indx++;
            break;
            
          }
        }
        indx++;
      }
    }
  }
}

I would appreciate any advice.

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