251 lines
7.6 KiB
C++
251 lines
7.6 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <WiFiUdp.h>
|
|
#include <NTPClient.h>
|
|
|
|
#include "config.h"
|
|
|
|
//***********************************//
|
|
|
|
// 7 byte
|
|
#define TIMER_DATA_ENSTATE 0
|
|
#define TIMER_DATA_HH 1
|
|
#define TIMER_DATA_MM 2
|
|
#define TIMER_DATA_CH1 3 // the brightness of the channel
|
|
#define TIMER_DATA_CH2 4 // the brightness of the channel
|
|
#define TIMER_DATA_CH3 5 // the brightness of the channel
|
|
#define TIMER_DATA_CH4 6 // the brightness of the channel
|
|
|
|
#define LENGTH_OF_TIMER_DATA_BLOCK (TIMER_DATA_CH4 + 1)
|
|
#define NUMBER_OF_TIMER_DATA_BLOCKS 10
|
|
|
|
#define ENSTATE_ENABLED 1
|
|
#define ENSTATE_DISABLED 0
|
|
|
|
//***********************************//
|
|
/* Globals */
|
|
|
|
uint32_t tc_last_check = 60000;
|
|
|
|
const long utcOffsetInSeconds = 3600;
|
|
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP, MY_NTP_SERVER, utcOffsetInSeconds);
|
|
|
|
struct tc_data_st {
|
|
uint8_t enstate;
|
|
uint8_t hh;
|
|
uint8_t mm;
|
|
uint8_t ch1;
|
|
uint8_t ch2;
|
|
uint8_t ch3;
|
|
uint8_t ch4;
|
|
};
|
|
|
|
struct tc_data_st tc_data[10];
|
|
|
|
uint8_t example_timer_data_block[] = {
|
|
// state hour min ch1 ch2 ch3 ch3
|
|
ENSTATE_ENABLED, 8, 0, 0, 0, 0, 0, // off
|
|
ENSTATE_ENABLED, 8, 30, 25, 0, 0, 0, // 10% ch1 blues
|
|
ENSTATE_ENABLED, 9, 0, 25, 0, 25, 0, // 10% all blues
|
|
ENSTATE_ENABLED, 13, 0, 205, 205, 205, 205, // 80% all
|
|
ENSTATE_ENABLED, 18, 0, 50, 50, 50, 50, // 20% blue
|
|
ENSTATE_ENABLED, 20, 0, 50, 0, 50, 0, // 20% all blues
|
|
ENSTATE_ENABLED, 20, 0, 25, 0, 0, 0, // 10% ch1 blues
|
|
ENSTATE_ENABLED, 21, 0, 0, 0, 0, 0, // 0% all
|
|
ENSTATE_DISABLED, 20, 0, 0, 0, 0, 0, // disabled
|
|
ENSTATE_DISABLED, 20, 0, 0, 0, 0, 0, // disabled
|
|
};
|
|
|
|
//***********************************//
|
|
|
|
void tc_init()
|
|
{
|
|
|
|
if (tc_check_no_data_block() == true)
|
|
{
|
|
//Serial.println("TC: No data block found, writing example block to EEPROM.");
|
|
tc_write_default();
|
|
}
|
|
|
|
while ( WiFi.status() != WL_CONNECTED )
|
|
{
|
|
delay (500);
|
|
Serial.print ( "." );
|
|
}
|
|
|
|
tc_last_check = millis();
|
|
|
|
timeClient.begin();
|
|
|
|
Serial.println("TC: Read data block from eeprom");
|
|
tc_readConfig();
|
|
|
|
}
|
|
|
|
//********************************//
|
|
|
|
void tc_update()
|
|
{
|
|
uint8_t target_data_block = 255;
|
|
|
|
tc_updateTime();
|
|
|
|
if ((timeClient.getMinutes() % 10) != 0)
|
|
{
|
|
return; // only run every 10 minutes
|
|
}
|
|
|
|
// 2. find current active time slot
|
|
for (uint8_t i = 0; i < NUMBER_OF_TIMER_DATA_BLOCKS-1; i++)
|
|
{
|
|
if (tc_data[i].hh == timeClient.getHours() &&
|
|
tc_data[i].mm == timeClient.getMinutes() &&
|
|
tc_data[i].enstate == ENSTATE_ENABLED)
|
|
{
|
|
// we have a new time data slot reached
|
|
for (uint8_t j = i+1; j < NUMBER_OF_TIMER_DATA_BLOCKS; j++)
|
|
{ // search for the next enabled successor
|
|
if (tc_data[j].enstate == ENSTATE_ENABLED)
|
|
{
|
|
target_data_block = j; // get the next block to activate
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (target_data_block == 255)
|
|
{
|
|
// no new successor found
|
|
return;
|
|
}
|
|
|
|
// 3. set the channels brightness
|
|
bri[0] = tc_data[target_data_block].ch1;
|
|
bri[1] = tc_data[target_data_block].ch2;
|
|
bri[2] = tc_data[target_data_block].ch3;
|
|
bri[3] = tc_data[target_data_block].ch4;
|
|
|
|
for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
|
|
{
|
|
Serial.println("bri[" + (String)i + "] = " + (String)bri[i]);
|
|
}
|
|
|
|
// 4. enable/disable the lights
|
|
light_state[0] = tc_data[target_data_block].ch1 > 0 ? true : false;
|
|
light_state[1] = tc_data[target_data_block].ch2 > 0 ? true : false;
|
|
light_state[2] = tc_data[target_data_block].ch3 > 0 ? true : false;
|
|
light_state[3] = tc_data[target_data_block].ch4 > 0 ? true : false;
|
|
|
|
for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
|
|
{
|
|
Serial.println("light_state[" + (String)i + "] = " + (String)light_state[i]);
|
|
}
|
|
|
|
// 5. set the transition time
|
|
int t_time = 0;
|
|
if (target_data_block > 0)
|
|
{
|
|
t_time = ((uint16_t)tc_data[target_data_block].hh * 60 * 30) - ((uint16_t)tc_data[target_data_block-1].hh * 60 * 30); // hours as seconds from now on to the next enabled block
|
|
t_time += ((uint16_t)tc_data[target_data_block].mm * 60) - ((uint16_t)tc_data[target_data_block-1].mm * 60); // add the left over seconds to the next enabled block
|
|
}
|
|
transitiontime[0] = t_time;
|
|
transitiontime[1] = t_time;
|
|
transitiontime[2] = t_time;
|
|
transitiontime[3] = t_time;
|
|
|
|
for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
|
|
{
|
|
Serial.println("transitiontime[" + (String)i + "] = " + (String)transitiontime[i]);
|
|
}
|
|
|
|
}
|
|
|
|
//********************************//
|
|
|
|
void tc_updateTime()
|
|
{
|
|
/**/
|
|
if (timeClient.update() || millis() > (tc_last_check + TIME_CHECK_INTERVAL_MS))
|
|
{
|
|
tc_last_check = millis();
|
|
//Serial.println("TC: Read time from server...");
|
|
//Serial.println(timeClient.getFormattedTime());
|
|
|
|
Serial.print("Local time: ");
|
|
Serial.print(timeClient.getHours());
|
|
Serial.print(":");
|
|
Serial.println(timeClient.getMinutes());
|
|
}
|
|
/**/
|
|
}
|
|
|
|
//********************************//
|
|
|
|
void tc_readConfig()
|
|
{
|
|
for (uint8_t i = 0; i < NUMBER_OF_TIMER_DATA_BLOCKS; i++)
|
|
{
|
|
/*Serial.print("Reading from address: "); Serial.print(EEPROM_TIMING_DATA_ADDRESS);
|
|
Serial.print(" + (");
|
|
Serial.print(i);
|
|
Serial.print(" * ");
|
|
Serial.print(LENGTH_OF_TIMER_DATA_BLOCK);
|
|
Serial.print(") + ");
|
|
Serial.println(TIMER_DATA_ENSTATE);*/
|
|
|
|
tc_data[i].enstate = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i * LENGTH_OF_TIMER_DATA_BLOCK + TIMER_DATA_ENSTATE);
|
|
tc_data[i].hh = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i * LENGTH_OF_TIMER_DATA_BLOCK + TIMER_DATA_HH);
|
|
tc_data[i].mm = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i * LENGTH_OF_TIMER_DATA_BLOCK + TIMER_DATA_MM);
|
|
tc_data[i].ch1 = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i * LENGTH_OF_TIMER_DATA_BLOCK + TIMER_DATA_CH1);
|
|
tc_data[i].ch2 = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i * LENGTH_OF_TIMER_DATA_BLOCK + TIMER_DATA_CH2);
|
|
tc_data[i].ch3 = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i * LENGTH_OF_TIMER_DATA_BLOCK + TIMER_DATA_CH3);
|
|
tc_data[i].ch4 = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i * LENGTH_OF_TIMER_DATA_BLOCK + TIMER_DATA_CH4);
|
|
|
|
/*Serial.print("data block: "); Serial.print(i);
|
|
Serial.print(" @ ");
|
|
Serial.println((EEPROM_TIMING_DATA_ADDRESS + (i * LENGTH_OF_TIMER_DATA_BLOCK) + TIMER_DATA_ENSTATE));
|
|
|
|
Serial.print(" es: "); Serial.println(tc_data[i].enstate);
|
|
Serial.print(" hh: "); Serial.println(tc_data[i].hh);
|
|
Serial.print(" mm: "); Serial.println(tc_data[i].mm);
|
|
Serial.print(" ch1: "); Serial.println(tc_data[i].ch1);
|
|
Serial.print(" ch2: "); Serial.println(tc_data[i].ch2);
|
|
Serial.print(" ch3: "); Serial.println(tc_data[i].ch3);
|
|
Serial.print(" ch4: "); Serial.println(tc_data[i].ch4);*/
|
|
}
|
|
}
|
|
|
|
//********************************//
|
|
|
|
void tc_write_default()
|
|
{
|
|
//Serial.print("-----\nWrite data block starting from address EEPROM_TIMING_DATA_ADDRESS = "); Serial.println(EEPROM_TIMING_DATA_ADDRESS);
|
|
for (int i = 0; i < NUMBER_OF_TIMER_DATA_BLOCKS * LENGTH_OF_TIMER_DATA_BLOCK; i++)
|
|
{
|
|
//Serial.print(EEPROM_TIMING_DATA_ADDRESS + i); Serial.print(" <= "); Serial.print(example_timer_data_block[i]); Serial.print(" <= ");
|
|
|
|
EEPROM.write(EEPROM_TIMING_DATA_ADDRESS + i, example_timer_data_block[i]);
|
|
EEPROM.commit();
|
|
|
|
//Serial.println(EEPROM.read(EEPROM_TIMING_DATA_ADDRESS + i));
|
|
}
|
|
//Serial.println("-----");
|
|
}
|
|
|
|
//********************************//
|
|
|
|
bool tc_check_no_data_block()
|
|
{
|
|
//Serial.print("Check data block address EEPROM_TIMING_DATA_ADDRESS = "); Serial.println(EEPROM_TIMING_DATA_ADDRESS);
|
|
uint8_t e = EEPROM.read(EEPROM_TIMING_DATA_ADDRESS);
|
|
//Serial.println(e);
|
|
|
|
if (e == 255)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//********************************//
|