Arduino IoT Cloud Float Variable Not Synchronizing s

I'm using ESP8266 and Neo-6M , the coordinate variable is sychronizing but the lati and longi variable are not synchronizing.

#include "thingProperties.h"
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>

// Define the software serial connection to the GPS module
SoftwareSerial ss(D2, D1);
TinyGPSPlus gps;


void setup() {
  // Start the hardware serial for debugging
  Serial.begin(9600);

  // Start the software serial connection to the GPS module
  ss.begin(9600);

  // Wait for the serial connection to be ready
  delay(1500);
  Serial.println("GPS Start");

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  // Set the debug message level and print debug info
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
      displayInfo();

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
  
  float  Lat = gps.location.lat() ;
  float  Long = gps.location.lng() ;

  float lati = gps.location.lat() ;
  float longi = gps.location.lng() ;

  coordinate = {Lat, Long};
  
  ArduinoCloud.update();
  
}

void displayInfo(){
  //GPS
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.println(gps.location.lng(), 6);
    
  }
  else
  {
    Serial.print(F("INVALID"));
  }
}

void onCoordinateChange() {
  Location coordinates = coordinate.getValue();
  float target_lat = coordinates.lat ;
  float target_lon = coordinates.lon ;
  Serial.println(coordinates.lat, 6);
  Serial.println(coordinates.lon, 6);
  Serial.println("Location updated from the cloud.");
}

/*
  Since Lati is READ_WRITE variable, onLatiChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLatiChange()  {
  // Add your code here to act upon Lati change
  Location coordinates = coordinate.getValue();
  float Lat = coordinates.lat;
  Serial.println(coordinates.lat, 6);
}
/*
  Since Longi is READ_WRITE variable, onLongiChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLongiChange()  {
  Location coordinates = coordinate.getValue();
  float Long = coordinates.lon ;
  Serial.println(coordinates.lon, 6);
}
// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "463e41fb-43fa-44c2-a000-38c88a588fa0";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password

void onLatiChange();
void onLongiChange();
void onCoordinateChange();

float lati;
float longi;
CloudLocation coordinate;

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(lati, READWRITE, ON_CHANGE, onLatiChange);
  ArduinoCloud.addProperty(longi, READWRITE, ON_CHANGE, onLongiChange);
  ArduinoCloud.addProperty(coordinate, READWRITE, ON_CHANGE, onCoordinateChange);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

You defined too many variables: there is a local and a global lati and longi ( updating the local the global will not change )

1 Like

no, the other sketch is thingProperties.h

those ones from the Code generated by Arduino IoT Cloud are the ones that are shared.

In your code you define new variables with the same name but not the same scope that exist just during the loop.

it's time to read about scope

➜ get rid of the type float in front of lati and longi

  lati = gps.location.lat() ;
  longi = gps.location.lng() ;

(and you don't need to duplicate those in Lat and Long, do you ???)

1 Like

Wow! Thank you so much for giving me the solution! sorry for my bad english, THANK YOU VERY MUCH!

have fun (@davidefa got it right first)

1 Like

Thank u davidefa!