How to display live location from gps to IOT Cloud dashboard?

hi, im using an Mkr 1010 board with a bn-220 GPS. I want to display my live location from GPS to map widget on the Iot cloud dashboard.

This is my first building something with Arduino, with that in mind, here is my code:

/*
  CloudLocation boardGPS;

*/

#include <Arduino.h>
#include "TinyGPS++.h"

TinyGPSPlus gps;

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
}

void loop() {

    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);
        }
    }  
}

You just need to add a Location cloud variable to your IoT cloud thing, and assign values like

location = Location(gps.location.lat(), gps.location.lng());

in your loop() cycle.

Additionally, this could help:

Okay i did it but it still doesnt show the location on map widget.

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/d6e2bdcf-ebab-4c09-9272-39f0d83d3f97 

  Arduino IoT Cloud Variables description

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

  CloudLocation boardGPS;

  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.
*/

#include "thingProperties.h"
#include "TinyGPS++.h"

TinyGPSPlus gps;


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
    Serial1.begin(9600);
    
  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();
  // 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;

boardGPS = Location(gps.location.lat(), gps.location.lng());
}

Please don't use delay() function in IoT Cloud sketches because it can interfere with the internal state machine of ArduinoCloud . More information here.

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