I have a small NNTP clock with auto TIMEZONE adjustments which runs on an ESP8266,.. I modified the code to split the different parts of the code into their own functions and files (.ino, .h), so that the code is more logically laid out.
However, since splitting I cannot globally access individual time variables for Hours or minutes etc.
I am familiar with creating variables within a function,.. and at the start to make them global or local ( to a function),.. But something beyond my knowledge is in action here.
Could someone with greater knowledge than myself explain what is going on...
Many tx....
#include "Init_and_Define.h"
void loop() {
do {
delay(50);
// Do Nothing
} while ( ( millis() - PreviousMillis ) < 1000 );
/*
This creates a 1 second second timer,...
*/
PreviousMillis = millis();
getime();
Serial.println("Hello World");
delay(1000);
t += second(local); THIS CAUSES COMPILE FAILURES
Serial.println(t); WITH:- error: 'local' was not declared in this scope
}
clockfunc.ino
void getime() {
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
date = ""; // clear the variables
t = "";
// update the NTP client and get the UNIX UTC timestamp
timeClient.update();
epochTime = timeClient.getEpochTime();
// convert received time stamp to time_t object
time_t local, utc;
utc = epochTime;
// Then convert the UTC UNIX timestamp to local time
TimeChangeRule ukBST = {"BST", Last, Sun, Mar, 1, 60}; //
TimeChangeRule ukGMT = {"GMT", Last, Sun, Oct, 2, 0}; //
Timezone ukLondon(ukBST, ukGMT);
local = ukLondon.toLocal(utc);
// now format the Time variables into strings with proper names for month, day etc
date += days[weekday(local) - 1];
date += ", ";
date += months[month(local) - 1];
date += " ";
date += day(local);
date += ", ";
date += year(local);
// format the time to 12-hour format with AM/PM and no seconds
if (hour(local) < 10) // add a zero if minute is under 10
t += "0";
t += hour(local);
t += ":";
if (minute(local) < 10) // add a zero if minute is under 10
t += "0";
t += minute(local);
t += ":";
if (second(local) < 10) // add a zero if minute is under 10
t += "0";
t += second(local);
// t += " ";
// t += ampm[isPM(local)];
// Display the date and time
snprintf(TimeDateSTR, BUFF_MAX, "%s %d %s %02d:%02d",
days[weekday(local) - 1], day(local), months[month(local) - 1], hour(local), minute(local) );
Serial.println(TimeDateSTR);
Serial.println(date);
Serial.println(t);
}
else // attempt to connect to wifi again if disconnected
{
delay(1000);
}
}
// --------------------------------------
Setup.ino
void setup() {
Serial.begin(57600);
delay(10);
Serial.println();
Serial.println();
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
timeClient.begin(); // Start the NTP UDP client
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(1000);
}
Serial.println("Got Time OK");
}
Init_and_Define.h
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <time.h> // time() ctime()
#include <sys/time.h> // struct timeval
#include <coredecls.h> // settimeofday_cb()
#include <string.h>
#include <WifiUDP.h>
#include <NTPClient.h>
#include <Time.h>
#include <TimeLib.h>
#include <Timezone.h>
const char* ssid = "somethn"; // wifi ssid
const char* password = "fhlkfjhlkjgfl"; // wifi password
String auth = "bWFya0BoYJLS1hHVQ==";
// Authentication credentials Create a string from <email_address>:<API_Password> and encode it base64
// The sample: https://www.base64encode.org/
// String auth = "dXNlcjpzd29yZA=="
// is the encoding for "user:password" 567rujhdgfjh5
const char* host = "www.ic2pro.com";
const int httpPort = 80;
String devId = "dc79761b-33fd5f";
// Device ID. CREATE YOUR OWN GUID; Use this http://www.guidgenerator.com/
#define Chk_Bit(var,pos) (((var)>>(pos)) & 1)
#define Set_Bit(var,pos) ((var) |= (1<<(pos)))
#define Clr_Bit(var,pos) ((var) &= ~(1<<(pos)))
#define Tog_Bit(var,pos) ((var) ^= (1<<(pos)))
byte BitTestFlags = 0; // Initialise flags to all zero and cleared
// Name Bit No.
#define TempTooHigh 0 // Temperature to High
#define LastReadCore 1 // Time between probe reads too long
#define LastReadDoor 2
#define LastReadRad 3
#define LastReadExtrn 4
#define OneShotTimer 5
#define RDInProgress 6 // Flag Set if Read in Progress
#define WRInProgress 7 // Flag set if Write in progress
#define LEDoutput D7 // Set Output pin for LED
#define FANoutput D3 // Set Output pin for FAN
#define BUFF_MAX 32
// Define NTP properties
#define NTP_OFFSET 0 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "0.uk.pool.ntp.org" // change this to whatever pool is closest (see ntp.org)
// Set up the NTP UDP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
String date;
String t;
const char * days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"} ;
const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} ;
const char * ampm[] = {"AM", "PM"} ;
unsigned long currentMillis, PreviousMillis = millis(), time_now = 0, epochTime;
char TimeDateSTR[BUFF_MAX];