I'm not posting my code as I'm a newb and it's really ugly. Just want your best guess
time.h library
Why do I get 12 hour time format from this.
Serial.println(timeinfo.tm_hour);
Everything I read says it should be 24 hour
ESP32-2432S028R
Edit %I and %h return 12 hour also I need 24 hour format
This is strange, this part of my code is working????
if (timeinfo.tm_hour < 12) {
tft.setTextDatum(MC_DATUM);
tft.drawString("Good Afternoon Bob", tft.width() / 2, 20);
What is the project. Your last year's hollow clock?
You shouldn't use the time.h library or any other time library with an ESP32 any more, assuming you don't use a RTC (not needed). It's now all built into the current ESP core.
This basic example is the easy way. It gives you the local time, including daylight savings, and all the time elements. Just enter your WiFi credentials, enter your location (link is in the code), and choose your local NTP pool (or use "pool.ntp.org"). By default it will sync every 3 hours, and keeps time within one second.
Leo..
#include <WiFi.h> // ESP32
unsigned long prevTime;
time_t now; // holds epoch
tm tm; // create time struct
void setup() {
Serial.begin(115200);
WiFi.begin("SSID", "PW");
configTzTime("NZST-12NZDT,M9.5.0,M4.1.0/3", "nz.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}
void loop() {
time(&now);
if (now != prevTime) { // time has changed?
prevTime = now; // remember
localtime_r(&now, &tm); // compute local time
printf("\n%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec); // print
}
}
There is also an easy way to wait for the correct internet time, before displaying it.
I can post additional code for that if needed.
You can find a list of the time elements here (click).
The below addition should print in 12-hour format (untested).
Leo..
localtime_r(&now, &tm); // compute local time
if(tm.tm.hour > 12) tm.tm.hour -= 12; // 12-hour format
printf("\n%02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec); // print
#include "SPI.h"
#include "TFT_eSPI.h"
#include <WiFi.h>
#include "time.h"
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <XPT2046_Touchscreen.h>
int a = 0;
// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();
// Touchscreen pins
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
int tch;
int bg;
int x, y, z;
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
unsigned long drawTime = 0;
const char* ssid = "MBR";
const char* password = "cradlepoint";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 21600;
const int daylightOffset_sec = 3600;
unsigned long lastTime = 0;
unsigned long lastTime2 = 0;
unsigned long timerDelay = 20000;
unsigned long timerDelay2 = 2000;
String jsonBuffer;
struct tm timeinfo;
void setup() {
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
tft.invertDisplay( true );
// Set the Touchscreen rotation in landscape mode
// Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 3: touchscreen.setRotation(3);
touchscreen.setRotation(1);
int touchZ;
Serial.begin(115200);
tft.begin();
tft.invertDisplay( true );
tft.setRotation(3.5);
tft.fillScreen(TFT_BLACK);
tft.setTextDatum(MC_DATUM);
tft.setTextSize(2);
WiFi.begin(ssid, password);
tft.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.print(".");
}
tft.println("");
tft.print("Connected to ");
tft.println(ssid);
tft.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
}
void loop() {
//int touchZ;
Serial.println(timeinfo.tm_hour);
TS_Point p = touchscreen.getPoint();
tch = p.z;
getLocalTime(&timeinfo);
printLocalTime();
tft.invertDisplay( false );
if ((millis() - lastTime2) > timerDelay2) {
if (tch > 200) bg=bg+1;
if (bg>4) bg=0;
//Serial.println(&timeinfo, "%H");
if (bg==0) tft.fillScreen(TFT_YELLOW);
if (bg==1) tft.fillScreen(TFT_WHITE);
if (bg==2) tft.fillScreen(TFT_NAVY);
if (bg==3) tft.fillScreen(TFT_VIOLET);
if (bg==4) tft.fillScreen(TFT_RED);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setCursor(10, 10);
if (timeinfo.tm_hour > 12) {
tft.setTextDatum(MC_DATUM);
tft.drawString("Good Morning Bob", tft.width() / 2, 20);
}
if (timeinfo.tm_hour < 12) {
tft.setTextDatum(MC_DATUM);
tft.drawString("Good Afternoon Bob", tft.width() / 2, 20);
//tft.print("Good afternoon Bob");
}
//Serial.println(timeinfo.tm_hour);
tft.setCursor(90, 40);
tft.setTextSize(2);
tft.print("The time is");
tft.setTextColor(TFT_GREEN);
tft.setCursor(70, 70);
tft.setTextSize(6);
tft.println(&timeinfo, "%I:%M");
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setTextDatum(MC_DATUM);
tft.drawString("The Date is", tft.width() / 2, 130);
tft.setCursor(30, 140);
tft.print(&timeinfo, " %A,%B,%d");
//Serial.println(&timeinfo, "%A, %B %d %Y %h:%M:%S");
JSONVar myObject = JSON.parse(jsonBuffer);
tft.setCursor(15, 170);
a = (myObject["main"]["temp"]);
tft.print("Temp is: ");
tft.setTextColor(TFT_GREEN);
tft.setTextSize(6);
tft.print(a, 0);
tft.setTextSize(3);
tft.print("o ");
tft.setTextSize(6);
tft.print("F");
tft.setTextSize(2);
tft.setCursor(15, 220);
tft.print("Miller Time in");
//Serial.println(timeinfo.tm_min);
Serial.println(bg);
Serial.println(tch);
lastTime2 = millis();
}
//delay(5000);
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=rochester,mn,us&&units=imperial&appid=643b2895bb04f51dbdd5687d86203e28";
jsonBuffer = httpGETRequest(serverPath.c_str());
JSONVar myObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
//Serial.print("Temperature: ");
Serial.println(myObject["main"]["temp"]);
} else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
payload = http.getString();
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
//http.end();
printLocalTime();
return payload;}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
tft.println("Failed to obtain time");
return;
}
}
Here is my UGLY code, I know there are a million ways to make this better, but I'm damn proud of myself for getting this far. The last language I knew was Apple basic on a IIe.