state storage

This commit is contained in:
Siwat Sirichai 2023-09-11 16:13:09 +07:00
parent f1299071c1
commit 82be4f0342
2 changed files with 71 additions and 9 deletions

View File

@ -12,7 +12,7 @@
platform = espressif32 platform = espressif32
board = wt32-eth01 board = wt32-eth01
framework = arduino framework = arduino
lib_deps = siwats/ESPMegaPROR3@^1.1.2 lib_deps = siwats/ESPMegaPROR3@^1.1.5
knolleary/PubSubClient@^2.8 knolleary/PubSubClient@^2.8
ivanseidel/ArduinoThread@^2.1.1 ivanseidel/ArduinoThread@^2.1.1
arduino-libraries/Arduino_BuiltIn@^1.0.0 arduino-libraries/Arduino_BuiltIn@^1.0.0

View File

@ -37,9 +37,11 @@ unsigned long virtual_interupt_timer[VINT_COUNT];
// Outputs // Outputs
#define PWM_COUNT 16 #define PWM_COUNT 16
const int pwm_pins[PWM_COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; const uint8_t pwm_pins[PWM_COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int pwm_states[PWM_COUNT]; bool pwm_states[PWM_COUNT];
int pwm_values[PWM_COUNT]; uint8_t pwm_states_eeprom[PWM_COUNT];
uint16_t pwm_values[PWM_COUNT];
uint8_t pwm_values_eeprom[PWM_COUNT*2];
// output = m*input+c // output = m*input+c
const float pwm_linear_scaling_m[PWM_COUNT] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; const float pwm_linear_scaling_m[PWM_COUNT] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
const float pwm_linear_scaling_c[PWM_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; const float pwm_linear_scaling_c[PWM_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
@ -65,9 +67,9 @@ Mode 0: Off, 1: Cool, 2: Fan
Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low
*/ */
#define DHT22_PIN 17 #define DHT22_PIN 17
int ac_mode = 0; uint8_t ac_mode = 0;
int ac_fan_speed = 0; uint8_t ac_fan_speed = 0;
int ac_temperature = 25; uint8_t ac_temperature = 25;
#define AC_MAX_TEMPERATURE 30 #define AC_MAX_TEMPERATURE 30
#define AC_MIN_TEMPERATURE 18 #define AC_MIN_TEMPERATURE 18
char AC_SET_MODE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/mode"; char AC_SET_MODE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/mode";
@ -130,6 +132,9 @@ void trigger11();
void trigger12(); void trigger12();
void trigger13(); void trigger13();
void eeprom_retrieve_init();
void eeprom_pwm_update();
void lcd_ac_refresh_fan(); void lcd_ac_refresh_fan();
void lcd_ac_refresh_mode(); void lcd_ac_refresh_mode();
@ -145,7 +150,8 @@ DHTNEW env_sensor(DHT22_PIN);
Thread mqtt_reconnector = Thread(); Thread mqtt_reconnector = Thread();
Thread environment_reporter = Thread(); Thread environment_reporter = Thread();
StaticThreadController<2> thread_controller(&mqtt_reconnector, &environment_reporter); Thread eeprom_pwm_updater = Thread();
StaticThreadController<3> thread_controller(&mqtt_reconnector, &environment_reporter, &eeprom_pwm_updater);
Thread top_bar_updater = Thread(); Thread top_bar_updater = Thread();
Thread page_updater = Thread(); Thread page_updater = Thread();
@ -161,6 +167,7 @@ void setup()
Serial.println("ESPMega R3 Initializing . . ."); Serial.println("ESPMega R3 Initializing . . .");
ESPMega_begin(); ESPMega_begin();
io_begin(); io_begin();
eeprom_retrieve_init();
Serial.println("Initializing Infrared . . ."); Serial.println("Initializing Infrared . . .");
lcd_send_command("boot_state.txt=\"Infrared Initializing . . .\""); lcd_send_command("boot_state.txt=\"Infrared Initializing . . .\"");
IrReceiver.begin(IR_RECIEVE_PIN); IrReceiver.begin(IR_RECIEVE_PIN);
@ -192,6 +199,39 @@ void loop()
lcd_loop(); lcd_loop();
} }
void eeprom_retrieve_init()
{
// EEPROM Data Retrival
ac_mode = ESPMega_EEPROM.readByte(0);
ac_temperature = ESPMega_EEPROM.readByte(1);
ac_fan_speed = ESPMega_EEPROM.readByte(2);
// EEPROM Data Retrival Validation
if (ac_mode > 2)
{
ac_mode = 0;
ESPMega_EEPROM.writeByte(0, ac_mode);
}
if (ac_temperature > AC_MAX_TEMPERATURE || ac_temperature < AC_MIN_TEMPERATURE)
{
ac_temperature = AC_MAX_TEMPERATURE;
ESPMega_EEPROM.writeByte(1, ac_temperature);
}
if (ac_fan_speed > 3)
{
ac_fan_speed = 0;
ESPMega_EEPROM.writeByte(2, ac_fan_speed);
}
ac_set_state(ac_mode,ac_temperature,ac_fan_speed);
ESPMega_EEPROM.readBlock(3, pwm_states_eeprom, 16);
memcpy(pwm_states, pwm_states_eeprom, 16);
ESPMega_EEPROM.readBlock(19, pwm_values_eeprom, 32);
memcpy(pwm_values, pwm_values_eeprom, 32);
for(int i =0;i<15;i++) {
pwm_set_state(i,pwm_states[i]);
pwm_set_value(i,pwm_values[i]);
}
}
void io_begin() void io_begin()
{ {
Serial.println("Initializing I/O . . ."); Serial.println("Initializing I/O . . .");
@ -276,6 +316,8 @@ void thread_initialization()
mqtt_reconnector.setInterval(15000); mqtt_reconnector.setInterval(15000);
environment_reporter.onRun(publish_env_state); environment_reporter.onRun(publish_env_state);
environment_reporter.setInterval(5000); environment_reporter.setInterval(5000);
eeprom_pwm_updater.onRun(eeprom_pwm_update);
eeprom_pwm_updater.setInterval(5000);
} }
void pwm_state_callback(String topic, String message) void pwm_state_callback(String topic, String message)
@ -583,6 +625,8 @@ void ac_set_state(int mode, int temperature, int fan_speed)
panel.writeStr("temp.txt", "--C"); panel.writeStr("temp.txt", "--C");
} }
publish_ac_state(); publish_ac_state();
uint8_t ac_datablock[3] = {ac_mode, ac_temperature, ac_fan_speed};
ESPMega_EEPROM.writeBlock(0, ac_datablock, 3);
switch (mode) switch (mode)
{ {
case 0: case 0:
@ -688,7 +732,7 @@ void lcd_refresh()
} }
void lcd_top_bar_update() void lcd_top_bar_update()
{ {
String time_str = String(rtc.getHour())+":"+String(rtc.getMinute()); String time_str = String(rtc.getHour()) + ":" + String(rtc.getMinute());
panel.writeStr("time.txt", time_str); panel.writeStr("time.txt", time_str);
panel.writeNum("server.pic", standalone ? 4 : 5); panel.writeNum("server.pic", standalone ? 4 : 5);
panel.writeNum("lan.pic", ETH.linkUp() ? 3 : 2); panel.writeNum("lan.pic", ETH.linkUp() ? 3 : 2);
@ -789,4 +833,22 @@ void trigger12()
void trigger13() void trigger13()
{ {
}
void eeprom_pwm_update()
{
if (memcmp(pwm_states, pwm_states_eeprom, 16))
{
memcpy(pwm_states_eeprom, pwm_states, 16);
ESPMega_EEPROM.writeBlock(3, pwm_states_eeprom, 16);
Serial.println("EEPROM PWM UPDATE STATE!");
lcd_send_stop_bit();
}
if (memcmp(pwm_values, pwm_values_eeprom, 32))
{
memcpy(pwm_values_eeprom, pwm_values, 32);
ESPMega_EEPROM.writeBlock(19, pwm_values_eeprom, 32);
Serial.println("EEPROM PWM UPDATE VALUE!");
lcd_send_stop_bit();
}
} }