lumini_p30_control/firmware/timing_control.ino

235 lines
7.1 KiB
Arduino
Raw Normal View History

2023-04-25 14:32:42 +02:00
#include <ESP8266WiFi.h>
2023-04-24 20:26:49 +02:00
#include <WiFiUdp.h>
2023-04-25 14:32:42 +02:00
#include <NTPClient.h>
2023-04-24 20:26:49 +02:00
#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
//***********************************//
2023-04-25 14:32:42 +02:00
/* Globals */
2023-04-24 20:26:49 +02:00
2023-04-25 14:32:42 +02:00
uint32_t tc_last_check = 0;
2023-04-24 20:26:49 +02:00
2023-04-25 14:32:42 +02:00
const long updateInterval = 60000;
const long utcOffsetInSeconds = 3600;
2023-04-24 20:26:49 +02:00
2023-04-25 14:32:42 +02:00
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, MY_NTP_SERVER, utcOffsetInSeconds, updateInterval);
2023-04-24 20:26:49 +02:00
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[LIGHTS_COUNT];
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();
}
2023-04-25 14:32:42 +02:00
while ( WiFi.status() != WL_CONNECTED )
2023-04-24 20:26:49 +02:00
{
2023-04-25 14:32:42 +02:00
delay (500);
Serial.print ( "." );
2023-04-24 20:26:49 +02:00
}
2023-04-25 14:32:42 +02:00
tc_last_check = millis();
2023-04-24 20:26:49 +02:00
2023-04-25 14:32:42 +02:00
timeClient.begin();
Serial.println("TC: Read data block from eeprom");
tc_readConfig();
2023-04-24 20:26:49 +02:00
}
2023-04-25 14:32:42 +02:00
//********************************//
2023-04-24 20:26:49 +02:00
void tc_update()
{
uint8_t target_data_block = 255;
2023-04-25 14:32:42 +02:00
tc_updateTime();
2023-04-24 20:26:49 +02:00
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++)
{
2023-04-25 14:32:42 +02:00
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
2023-04-24 20:26:49 +02:00
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;
// 4. enable/disable the lights
light_state[0] = tc_data[target_data_block].ch1 > 0 ? true : false;
light_state[0] = tc_data[target_data_block].ch2 > 0 ? true : false;
light_state[0] = tc_data[target_data_block].ch3 > 0 ? true : false;
light_state[0] = tc_data[target_data_block].ch4 > 0 ? true : false;
// 5. set the transition time
int t_time = 0;
if (target_data_block > 0)
{
t_time = (tc_data[target_data_block].hh * 60 * 30) - (tc_data[target_data_block-1].hh * 60 * 30); // hours as seconds from now on to the next enabled block
t_time += (tc_data[target_data_block].mm * 60) - (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;
}
2023-04-25 14:32:42 +02:00
//********************************//
void tc_updateTime()
{
if (timeClient.update())
{
Serial.println("TC: Reading 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);*/
}
}
//********************************//
2023-04-24 20:26:49 +02:00
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("-----");
}
2023-04-25 14:32:42 +02:00
//********************************//
2023-04-24 20:26:49 +02:00
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;
}
2023-04-25 14:32:42 +02:00
//********************************//