Development: Web RD Simple Clock Example

This example is an Arduino Sketch that puts up a simple Web based clock.

Set the Arduino Board type to “Generic ESP8266 Module”

Create  the Sketch:

#define STASSID “SSID”
#define STAPSK “PASSWORD”

#include <ESP8266WiFi.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 0 // or SCK aka GPIO0
#define CS_PIN 2 // GPIO2
#define DATA_PIN 3 // or MOSI aka GPIO3 or U0RXD
#define MAX_MESG 50
static char szMesg[MAX_MESG+1];
#define SPEED_TIME 300
static struct tm timeinfo;
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

#define BAUD_SERIAL 115200

const char* ntpServer = “pool.ntp.org”;
const int daylightOffset_sec = 3600;
const long gmtOffset_sec = 0;

void setup() {

Serial.begin(BAUD_SERIAL);
delay(30000); //wait 30 secs

WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
Serial.print(“\nConnecting to “);
Serial.println(STASSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(‘.’);
delay(500);
}

P.begin(/*numZones*/ 1);
P.setInvert(false);
P.setZone(/*zone number*/ 0,
/*moduleStart*/ 0,
/*moduleEnd */ 3);
P.displayZoneText(0, szMesg, PA_CENTER, SPEED_TIME, 0, PA_PRINT, PA_NO_EFFECT);

Serial.println(“Ready”);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

}

bool getLocalTime(struct tm * info, uint32_t ms) {
uint32_t count = ms / 10;
time_t now;

time(&now);
localtime_r(&now, info);

if (info->tm_year > (2016 – 1900)) {
return true;
}

while (count–) {
delay(10);
time(&now);
localtime_r(&now, info);
if (info->tm_year > (2016 – 1900)) {
return true;
}
}
return false;
}
uint32_t last_minute=0;

void loop() {
if(getLocalTime(&timeinfo,5000))
{ // if got local time
if (timeinfo.tm_min!=last_minute)
{
last_minute=timeinfo.tm_min;
memset(szMesg,0,50); // blank it
strftime(szMesg, sizeof(szMesg), “%2H:%2M”, &timeinfo);
Serial.printf(“%s\n”,szMesg);
P.displayReset(0);
P.displayClear();
}
else
{
P.displayAnimate();
delay(500);
}
}
}

This build requires the Libraries MD_Parola and MD_MAX72xx