Do you want to send the data less frequently without using delay()?
Maybe there's another option--try something like this snippet to limit the data rate to 1/sec:
void loop(void){
...
sendData();
...
}
void sendData(){
static unsigned long last = 0; // persistent storage to remeber last message time
unsigned long now = millis();
if (now - last < 1000) return; // too soon! abort!!
last = now; // update the time of the last transmission
Serial.print("time to send data");
// code to send data
// ...
}
void setup() {
// Initialize serial and wait for port to open:
// Serial.begin(9600);
//Serial1.begin(9600);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
Serial1.begin(9600);
// 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();
// Your code here
while(Serial1.available())
{
if(gps.encode(Serial1.read()))
{
String msg = Serial1.readStringUntil('\r');
Serial.println(msg);
Serial.print("LAT="); Serial.println(gps.location.lat(), 6);
Serial.print("LONG="); Serial.println(gps.location.lng(), 6);
Serial.print("ALT="); Serial.println(gps.altitude.meters(), 6);
//delay(4*1000);
}
}
double LAT;
double LONG;
location = Location(gps.location.lat(), gps.location.lng());
}
/*
Since Location is READ_WRITE variable, onLocationChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLocationChange() {
// Add your code here to act upon Location change
I am thinking getting coordinates every 15 min . what do u thinks ? this to track car location .
I am using mkr 1500 with GPS shield as a shield and not connected to 2IC cable .
I don't have your hardware, or use the IoT but consider this completely untested code:
void setup() {
// Initialize serial and wait for port to open:
// Serial.begin(9600);
//Serial1.begin(9600);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
Serial1.begin(9600);
// 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() {
// Your code here
while(Serial1.available())
{
if(gps.encode(Serial1.read()))
{
String msg = Serial1.readStringUntil('\r');
Serial.println(msg);
Serial.print("LAT="); Serial.println(gps.location.lat(), 6);
Serial.print("LONG="); Serial.println(gps.location.lng(), 6);
Serial.print("ALT="); Serial.println(gps.altitude.meters(), 6);
//delay(4*1000);
}
}
double LAT;
double LONG;
sendData();
}
void sendData(void){
static unsigned long last = 0; // persistent storage to remeber last message time
unsigned long now = millis();
if (now - last < 15*60000UL) return; // too soon! abort!!
last = now; // update the time of the last transmission
Serial.print("time to send data");
// code to send data
ArduinoCloud.update();
location = Location(gps.location.lat(), gps.location.lng());
// ...
}
/*
Since Location is READ_WRITE variable, onLocationChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLocationChange() {
// Add your code here to act upon Location change
If your code works, this attempts to limit the communications with the cloud to once every 15 minutes by moving some of the cloud bits into a millis()-based sendData() function.
If it loses the connection before 15 minues is up, you might also move this into sendData() to reconnect:
Void sendData code is working fine. I tried 2 mn timing interval yesterday and today I downloaded historic data from iot could .Historic shows 2 min interval between each sending. I am trying to understand the cost involved in receiving the coordinates and sending it to the Iot cloud . receiving coordinates through arduino GPS shield and MKR 1500 responsible for sending using GSM GPRS system .