Skip to content

M5unified corrupt ntp time #288

Description

@kbencherki6-beep

Hi,
I'm using m5unified in my code and I'm synchronizing time with NTP, but the call to m5.begin() corrupts time. I need m5unified for the power management that I also use.
Here is my code :

`#include <WiFi.h>
#include "time.h"
#include "LittleFS.h"
#include "M5UnitENV.h"
#include <M5Unified.h>
#include <M5GFX.h>

M5GFX display;
M5Canvas canvas(&display);

SHT3X sht30;

RTC_DATA_ATTR int bootCount = 1;

const char* ssid = "";
const char* password = "";

int timeS = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
M5.begin();
M5.Power.begin();
display.begin();
sht30.begin();
LittleFS.begin();

display.setEpdMode(epd_fast);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}

configTzTime("WEST0WEST,M3.5.0/1,M10.5.0", "pool.ntp.org");

canvas.createSprite(540, 960);
canvas.setFreeFont(&Orbitron_Light_24);
canvas.fillScreen(WHITE);
canvas.setTextColor(BLACK, WHITE);
canvas.setTextSize(2);

printTimeDate();
printBatteryLevel();
printMeasurements();
canvas.drawPngFile(LittleFS, "/images/m5stack.png", 70, (canvas.height() - canvas.fontHeight() - 20 - 133), 400, 133);
canvas.pushSprite(0, 0);

bootCount++;

delay(5000);

WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);

canvas.fillScreen(BLACK);
display.clear();

esp_sleep_enable_ext0_wakeup(GPIO_NUM_38, LOW);
esp_deep_sleep_start();

}

void loop() {
// put your main code here, to run repeatedly:
}

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

char hour[7];
char date[7];
char year[5];  
strftime(hour, 7, "%H:%M", &timeinfo);
Serial.println(hour);
strftime(date, 7, "%d/%m", &timeinfo);
Serial.println(date);
strftime(year, 5, "%Y", &timeinfo);
Serial.println(year);

canvas.fillRect(10, 20, canvas.textWidth(hour), canvas.fontHeight(), WHITE);
canvas.drawString(hour, 10, 20);
canvas.drawLine(0, (canvas.fontHeight() + 20), canvas.width(), (canvas.fontHeight() + 20), BLACK);
canvas.drawLine(0, (canvas.height() - canvas.fontHeight() - 20 - 10), canvas.width(), (canvas.height() - canvas.fontHeight() - 20 -10), BLACK);
canvas.fillRect(10, (canvas.height() - canvas.fontHeight() - 20), canvas.textWidth(date), canvas.fontHeight(), WHITE);
canvas.drawString(date, 10, (canvas.height() - canvas.fontHeight() - 20));
canvas.fillRect((canvas.width() - canvas.textWidth(year) - 10), (canvas.height() - canvas.fontHeight() - 20), canvas.textWidth(year), canvas.fontHeight(), WHITE);
canvas.drawString(year, (canvas.width() - canvas.textWidth(year) - 10), (canvas.height() - canvas.fontHeight() - 20));
}

void printBatteryLevel() {
int32_t batteryLevel = M5.Power.getBatteryLevel();
canvas.setCursor((canvas.width() - canvas.textWidth(String(batteryLevel)) - canvas.textWidth("%") - 10), 20);
canvas.printf("%03d%%", batteryLevel);
}

void printMeasurements() {
if(sht30.update()) {
int temperature = String(sht30.cTemp).toInt();
int humidity = String(sht30.humidity).toInt();
char buffer1[10], buffer2[10];
sprintf(buffer1, "%d", temperature);
sprintf(buffer2, "%d", humidity);
const char* temp = buffer1;
const char* hum = buffer2;

canvas.setTextSize(3);
canvas.drawRect(((canvas.width() / 2) - 200), ((canvas.height() / 2) - 200 - 50), 400, 200, BLACK);
canvas.drawPngFile(LittleFS, "/images/thermometer.png", ((canvas.width() / 2) - 200 + 100), ((canvas.height() / 2) - 200 - 50 + 50), 100, 100);
canvas.drawString(String(temperature), ((canvas.width() / 2) - 200 + 400 - canvas.textWidth(String(temperature)) - 80), ((canvas.height() / 2) - 200 - 50 + 100 - (canvas.fontHeight() / 2)));
canvas.drawRect(((canvas.width() / 2) - 200), ((canvas.height() / 2) + 50), 400, 200, BLACK);
canvas.drawPngFile(LittleFS, "/images/humidity.png", ((canvas.width() / 2) - 200 + 100), ((canvas.height() / 2) + 50 + 50), 100, 100);
canvas.drawString(String(humidity), ((canvas.width() / 2) - 200 + 400 - canvas.textWidth(String(humidity)) - 80), ((canvas.height() / 2) + 50 + 100 - (canvas.fontHeight() / 2)));  

}
}`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions