/*
Chapter 54 of Arduino from scratch in Spanish.
TFT SPI bus ILI9341 display of number of seconds elapsed
since the program started. Requires Adafruit ILI9341 and Adafruit GFX libraries
Autor: bitwiseAr
Complete parameters
http://www.cplusplus.com/reference/ctime/strftime/
*/
#include <SPI.h> // incluye libreria bus SPI
#include <Adafruit_GFX.h> // incluye libreria para manejo de graficos
#include <Adafruit_ILI9341.h> // incluye libreria para controlador ILI9341
#include <Fonts/FreeMonoBoldOblique12pt7b.h>
#include "FUTURAM20pt7b.h"
#include "FUTURAM40pt7b.h"
//#include "FUTURAM80pt7b.h"
//#include "FUTURAM60pt7b.h"
#define TFT_DC 2 // define constante TFT_DC con numero 9
#define TFT_CS 5 // define constante TFT_CS con numero 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); // crea objeto
#include <WiFi.h>
#include <TimeLib.h>
#include <Wire.h>
const char* ntpServer = "pool.ntp.org"; //NTP host address
const long gmtOffset_sec = 28800; //Time zone (in seconds) 8*60*60 //https://time.artjoey.com/
const int daylightOffset_sec = 0; //Define the offset for daylight saving time (in seconds)
// char *weekdayStr[7] = {"日", "一", "二", "三", "四", "五", "六"};
uint32_t targetTime = 0; // for next 1 second timeout
char dateStringBuff[50]; //date
char timeStringBuff[50]; //time
char WeekStringBuff[50]; //Week
char hhStringBuff[10]; //hour
char mmStringBuff[10]; //minute
char ssStringBuff[10]; //second
uint8_t hh, mm, ss;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
//tft.setFont(&FreeMonoBoldOblique12pt7b);
tft.setFont(&FUTURAM20pt7b);
tft.setCursor(10, 30); // locate cursor
tft.setTextColor(ILI9341_YELLOW );
tft.println("Starting ILI9341");
tft.setTextColor(ILI9341_GREEN );
tft.println("The quick brown");
delay(5000);
tft.fillScreen(ILI9341_BLACK);
tft.setFont(&FUTURAM40pt7b);
tft.setTextColor(ILI9341_CYAN);
tft.setCursor(10, 80); // locate cursor
tft.println("561.12");
while(1);
delay(10000);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
targetTime = millis() + 1000;
}
void loop() {
printLocalTime();
// Serial.print("time:");
// Serial.println(timeStringBuff);
}
//------Network ntpServer gets time-------------
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to get time");
return;
}
strftime(dateStringBuff, sizeof(dateStringBuff), "%F", &timeinfo); //year-month-day
strftime(timeStringBuff, sizeof(timeStringBuff), "%T", &timeinfo); //What time: what minutes: how many seconds
strftime(WeekStringBuff, sizeof(WeekStringBuff), "%w", &timeinfo); //Sunday is 0 (0-6)
strftime(hhStringBuff, sizeof(hhStringBuff), "%H", &timeinfo); //hour
strftime(mmStringBuff, sizeof(mmStringBuff), "%M", &timeinfo); //minute
strftime(ssStringBuff, sizeof(ssStringBuff), "%S", &timeinfo); //second
String HH, MM, SS;
HH = hhStringBuff;
MM = mmStringBuff;
SS = ssStringBuff;
hh = HH.toInt();
mm = MM.toInt();
ss = SS.toInt();
//---------------For simulation--------------
if (targetTime < millis()) {
targetTime = millis() + 1000;
//tft.setTextSize(3);
tft.setCursor(30, 50); // locate cursor
tft.setTextColor(ILI9341_BLUE );
tft.println(dateStringBuff); // Overwrite the text to clear it
//tft.setTextSize(3);
tft.setCursor(30, 100); // locate cursor
tft.setTextColor(0x39C4, ILI9341_BLACK);
tft.println("88:88:88"); // Overwrite the text to clear it
tft.setCursor(30, 100); // locate cursor
tft.setTextColor(ILI9341_RED);
tft.println(timeStringBuff); // Overwrite the text to clear it
//tft.setTextSize(3);
tft.setCursor(30, 150); // locate cursor
tft.setTextColor(ILI9341_PURPLE );
tft.println("Week:" + String(WeekStringBuff)); // Overwrite the text to clear it
Serial.println(timeStringBuff);
}
//------Actually use this------TFT_eSPI tft = TFT_eSPI();
// if (targetTime < millis()) {
// targetTime = millis() + 1000;
// tft.setTextColor(0x39C4, TFT_BLACK);
// tft.drawString("88:88:88", 0, 120, 7); // Overwrite the text to clear it
// tft.setTextColor(0xFBE0);
// tft.drawString(timeStringBuff, 0, 120, 7); // Overwrite the text to clear it
// }
}