#include <TimeLib.h>
#include <TimeAlarms.h>
#include <ESP8266WiFi.h>
#include <WifiUDP.h>
#include <NTPClient.h>
#include <Timezone.h>
AlarmId id;
//******* Lights definition ** //
const int CHL_1 = 13; //Blue1
const int CHL_2 = 12; //Blue1
const int CHL_3 = 5; //White
const int CHL_4 = 2; //Red
const int CHL_5 = 10; //Uv
// Define NTP properties
#define NTP_OFFSET 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "ca.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);
const char* ssid = "Aa123123"; // insert your own ssid
const char* password = "123456789"; // and password
void setup() {
pinMode(CHL_1, OUTPUT);
pinMode(CHL_2, OUTPUT);
pinMode(CHL_3, OUTPUT);
pinMode(CHL_4, OUTPUT);
pinMode(CHL_5, OUTPUT);
Serial.begin(115200); // most ESP-01's use 115200 but this could vary
timeClient.begin(); // Start the NTP UDP client
while (!Serial) ; // wait for Arduino Serial Monitor
// Connect to wifi
Serial.println("");
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi at ");
Serial.print(WiFi.localIP());
Serial.println("");
delay(1000);
// setTime(17,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
setTime(syncProvider()); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms for Lights
Alarm.alarmRepeat(1,02,00, Lights_SunRaise); // 7:00am every day
Alarm.alarmRepeat(1,03,30,Lights_Day); // 8:00am every day
Alarm.alarmRepeat(13,00,00,Lights_Night); // 8:00am every day
Alarm.alarmRepeat(20,00,10,Lights_Evening); // 20:30pm every day
Alarm.alarmRepeat(23,30,00,Lights_Night); // 22:00pm every day
// create timers, to trigger relative to when they're created
Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
id = Alarm.timerRepeat(2, Repeats2); // timer for every 2 seconds
Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
}
void loop() {
// update the NTP client and get the UNIX UTC timestamp
timeClient.update();
unsigned long 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 usEDT = {"EDT", Second, Sun, Mar, 2, +60}; //UTC - 5 hours - change this as needed
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, +60}; //UTC - 6 hours - change this as needed
Timezone usEastern(usEDT, usEST);
local = usEastern.toLocal(utc);
//setTime(hour(local),minute(local),second(local),day(local),month(local),year(local));
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
void Lights_SunRaise(){
analogWrite(CHL_1, 16); //Ch1- Blue1
analogWrite(CHL_2, 16); //Ch2- blue2
analogWrite(CHL_3, 16); //Ch3- White
analogWrite(CHL_4, 15); //Ch4- Red
analogWrite(CHL_5, 125); //Ch5- Uv
Serial.println("Lights_SunRaise");
delay(1000);
}
void Lights_Day(){
analogWrite(CHL_1, 150); //Ch1- Blue1
analogWrite(CHL_2, 150); //Ch2- Blue2
analogWrite(CHL_3, 200); //Ch3- White
analogWrite(CHL_4, 64); //Ch4- Red
analogWrite(CHL_5, 125); //Ch5- Uv
Serial.println("Lights_Day");
delay(1000);
}
void Lights_Evening(){
analogWrite(CHL_1, 16); //Ch1- Blue1
analogWrite(CHL_2, 16); //Ch2- Blue2
analogWrite(CHL_3, 05); //Ch2- White
analogWrite(CHL_4, 10); //Ch4- Red
analogWrite(CHL_5, 125); //Ch5- Uv
Serial.println("Lights_Evening");
delay(1000);
}
void Lights_Night(){
analogWrite(CHL_1, 0); //Ch1- Blue1
analogWrite(CHL_2, 0); //Ch3- llue2
analogWrite(CHL_3, 0); //Ch3- White
analogWrite(CHL_4, 0); //Ch4- Red
analogWrite(CHL_5, 0); //Ch5- Uv
Serial.println("Lights_Night");
delay(1000);
}
void Repeats() {
Serial.println("15 second timer");
}
void Repeats2() {
Serial.println("2 second timer");
}
void OnceOnly() {
Serial.println("This timer only triggers once, stop the 2 second timer");
// use Alarm.free() to disable a timer and recycle its memory.
Alarm.free(id);
// optional, but safest to "forget" the ID after memory recycled
id = dtINVALID_ALARM_ID;
// you can also use Alarm.disable() to turn the timer off, but keep
// it in memory, to turn back on later with Alarm.enable().
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
// time_t syncProvider()
//{
// return ntpClient.getNtpTime();
//}
Please read the sticky post at the top of the forum about how to properly post your code using code tags. Then, go back and edit your post and put them in. It will help people help you.
What is the "issue"?
i have the time of the wifi and i want to use it for the TimeAlarm and it does not work :-/
Getting the time code
// update the NTP client and get the UNIX UTC timestamp
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
// convert received time stamp to time_t object
time_t local, utc;
// Then convert the UTC UNIX timestamp to local time
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, +60}; //UTC - 5 hours - change this as needed
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, +60}; //UTC - 6 hours - change this as needed
Timezone usEastern(usEDT, usEST);
local = usEastern.toLocal(utc);
utc = epochTime;
Setting the time via settime function :
setTime(hour(local),minute(local),second(local),day(local),month(local),year(local));
1.Alarms does not triggered
2. im setting the wifitime inside the loop over and over and i want to to it only once
Thanks !!