Getlocal time, doesnt get the right on arduino icloud

Hello, so i'm new to arduino and trying to build a code on arduino cloud, i have encounter a problem with time in the arduino cloud, i can't get the right gmt ouput ( i'm stuck with gtm0, i want -3 gtm), the time part seem to work on arduino ide tho, if someone can help me troubleshoot it

#include "thingProperties.h"
#include <WiFi.h>
#include "time.h"
#include <Servo.h>
#include <math.h>

const char* ssid     = 

const char* password = 

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec =-3*3600;
const int   daylightOffset_sec = 0;

static int activationHour = 0; 
static int activationMin= 0;
static int weekendHour = 0;
static int weekendMin = 0;


static bool buttonActivated = false;

static const int servoPin = 13;
Servo servo1;

void setup(){
    // Initialize serial and wait for port to open:
  Serial.begin(115200);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Connect to Wi-Fi
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  
  // Init and get the time
  configTime(-3*3600,0,"pool.ntp.org");
  printLocalTime();

 makehour();

  // 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
  delay(1000);
  printLocalTime();
}

void printLocalTime(){

   struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }


  int currentHour = timeinfo.tm_hour;
  int currentMin = timeinfo.tm_min;
  int currentDay = timeinfo.tm_wday;
  int currentSec = timeinfo.tm_sec;

  // Vérifier si l'heure actuelle correspond à l'heure d'activation du servo-moteur
  if (currentHour == activationHour && currentMin == activationMin && currentSec <=5 && currentDay != 5) {
    activateServo();
  } else if (currentDay == 6 && weekendHour == currentHour && weekendMin == currentMin) {
    activateServo();
  }

  // Autres tâches à effectuer dans la boucle loop()
}

void makehour() {
  static double x1 = week/100;
  double entierweek;
  double decimalweek = modf(x1, &entierweek);
  
  static double x2 = weekend/100;
  double entierweekend;
  double decimalweekend = modf(x2, &entierweekend);
  
  activationHour = entierweek; 
  activationMin = decimalweek * 100;
  weekendHour = entierweekend;
  weekendMin = decimalweekend * 100;  
}

void activateServo() {
  // Code pour activer le servo-moteur à l'heure souhaitée
  for (int posDegrees = 0; posDegrees <= 180; posDegrees++) {
    servo1.write(posDegrees);
    Serial.println(posDegrees);
    delay(20);
  }


  for (int posDegrees = 180; posDegrees >= 0; posDegrees--) {
    servo1.write(posDegrees);
    Serial.println(posDegrees);
    delay(20);
  }
  delay(4000); // Pause de 4 secondes
}

void onSwitchonChange() {
  if (!buttonActivated) {
    buttonActivated = true;
    activateManual();
  }else if(buttonActivated){  
    // Réinitialiser la variable buttonActivated après l'activation
  buttonActivated = false;}
}

void onSwitchoffChange() {
  if (!buttonActivated) {
    buttonActivated = true;
    activateManual1();
  
  }else if(buttonActivated){  
    // Réinitialiser la variable buttonActivated après l'activation
  buttonActivated = false;}
}

void activateManual() {
    for (int posDegrees = 0; posDegrees <= 180; posDegrees++)
    {
      servo1.write(posDegrees);
      Serial.println(posDegrees);
      delay(20);
    }
    delay(4000); // Pause de 4 secondes lorsque le servo atteint 180 degrés

    for (int posDegrees = 180; posDegrees >= 0; posDegrees--)
    {
      servo1.write(posDegrees);
      Serial.println(posDegrees);
      delay(20);
    }

}

void activateManual1() {
  
    for (int posDegrees = 0; posDegrees <= 180; posDegrees++)
    {
      servo1.write(posDegrees);
      Serial.println(posDegrees);
      delay(20);
    }
    delay(8000); // Pause de 4 secondes lorsque le servo atteint 180 degrés

    for (int posDegrees = 180; posDegrees >= 0; posDegrees--)
    {
      servo1.write(posDegrees);
      Serial.println(posDegrees);
      delay(20);
    }

  // Réinitialiser la variable buttonActivated après l'activation
 
}

void onWeekChange() {
  makehour();
}

void onWeekendChange() {
  makehour();
}

/*
  Since Currenthour is READ_WRITE variable, onCurrenthourChange() is
  executed every time a new value is received from IoT Cloud.
*/


`

Hi @piceeeee

Are you using the exact same code in Arduino IDE as you are on Arduino Cloud? Or are you using two different sketches?

I see you defined this gmtOffset_sec variable, but then you never use it in your sketch. You can see how it can be used by studying the "SimpleTime" example sketch that comes with the ESP32 boards platform:

My suggestion is to start by using simple sketch like that example first to get this specific part of your project working correctly. Once that is working, you can incorporate the working code into your Arduino IoT Cloud Thing sketch.

i hard coded it to make sur the probleme wasn't coming from the varable, it's not,

I missed that before. I see it now.

Please answer the question I asked:

Yes I am using the same. If I am not mistaking, arduino cloud link get local time to there serveur, so you have to set the thing in désire gym first, but I doesn’t have that option so

I don't think so. The "local" in the getLocalTime function is referring to the local time configuration set via the configTime call; not to the location of the computer that compiled the sketch.

I just tried the "SimpleTime" example sketch that comes with the ESP32 boards platform, but with the configTime call changed to match the configuration from your sketch:

I uploaded the sketch from Arduino Web Editor and from Arduino IDE and I got the expected results for the UTC-03:00 time zone.

Please try that "SimpleTime" example sketch (with the adjustment to the configTime call) to eliminate all the additional complexity that is introduced by the Arduino IoT Cloud and Servo code in your project.

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