Sure, please see below the code and thanks for your help.
Again, when there is no 2G connection , we need to write the data to an SDCard (instead of using MQTT to send it to our server). We have also tried connecting and using waitFor and that does not seem to work either.
//SYSTEM_THREAD(ENABLED);
//SYSTEM_MODE(SEMI_AUTOMATIC);
#include "sd-card-library-photon-compat/sd-card-library-photon-compat.h"
#include "MQTT/MQTT.h"
#include "AssetTracker/AssetTracker.h"
#include <math.h>
#include <list>
#include <regex>
#define SENSORS_GAUSS_TO_MICROTESLA (100)
#define LEDON HIGH
#define LEDOFF LOW
using namespace std;
static float _hmc5883_Gauss_LSB_XY = 1100.0F; // Varies with gain
static float _hmc5883_Gauss_LSB_Z = 980.0F; // Varies with gain
static int address = 0x1E; //0011110b, I2C 7bit address of HMC5883
….
FuelGauge fuel;
CellularDevice device;
const uint8_t chipSelectSD = A1;
const uint8_t mosiPin = A5;
const uint8_t misoPin = A4;
const uint8_t clockPin = A3;
int tiempo;
String entry;
String coord="No hay GPS";
MQTT mqttClient(server, 1883, callback);
long dataFrameNumber = 0;
int applicationState = 1;
int batteryState = 5;
char payload[255];
char Sat[255];
String payload2;
AssetTracker t = AssetTracker();
…….
void setup() {
//pinMode(valvula1, INPUT);
// pinMode(valvula2, INPUT);
//pinMode(valvula3, INPUT);
// pinMode(motor, INPUT);
t.begin(); // Sets up all the necessary AssetTracker bits
t.gpsOn(); // Enable the GPS module. Defaults to off to save power.
Serial.begin(9600);
Serial.println("Entre en el setup");;
pinMode(ledBateria, OUTPUT);
pinMode(ledGPS, OUTPUT);
pinMode(ledGPRS, OUTPUT);
pinMode(caja_abierta, INPUT);
//delay(4000);
//Wire.begin();
//Wire.beginTransmission(address);
//Wire.write(0x02);
//Wire.write(0x00);
//Wire.endTransmission();
memset(&device, 0, sizeof(device));
device.size = sizeof(device);
cellular_device_info(&device, NULL);
SD.begin(chipSelectSD);
mqttClient.connect(server);
Particle.function("motor", encender_motor);
Particle.function("bomba", encender_bomba);
Particle.function("gps", gpsPublish);
Particle.function("tiempo1", tiempo_motor_apagado);
Particle.function("tiempo2", tiempo_motor_apagado_valula_apagada);
Particle.function("tiempo3", tiempo_motor_apagado_valvula_prendida);
Particle.variable("coordenadas",coord);
publicado = true;
Serial.println("Termine Setup");
Serial.println(device.imei);
}
void loop() {
...
transmitirGPS(tiempo2,1,0);
...
}
void transmitirGPS(int delay, int mot, int val){
t.updateGPS();
if(millis()-lastPublish > delay*1000){
Serial.println(t.preNMEA());
lastPublish = millis();
if(t.gpsFix()){
stringstream ss(t.preNMEA());
result.clear();
while( ss.good() )
{
string substr;
getline( ss, substr, ',' );
result.push_back( substr );
}
memset(payload, 0, sizeof payload);
snprintf(payload, sizeof(payload), "{ \"time\": %d, \"imei\": \"%s\", \"gps\": \"%s\", \"dir\": %f, \"motorState\": %d , \"valvulaState\": %d, \"batState\": %f, \"cantSat\": %s, \"cerrada\": %d}", Time.now() - 10800, device.imei, t.readLatLon().c_str(), desviacion_actual, mot,val, fuel.getVCell(),result[7].c_str(),digitalRead(caja_abierta));
Serial.println(payload);
digitalWrite(ledGPS,1);
gpsOk=true;
sendToIOTF(payload);
}
else
gpsOk=false;
}
}