dynamic bms endpoint

This commit is contained in:
Siwat Sirichai 2023-09-12 01:11:49 +07:00
parent 9520802f7b
commit fb6484bdf2
1 changed files with 82 additions and 34 deletions

View File

@ -20,8 +20,10 @@ IPAddress DNS(0, 0, 0, 0);
IPAddress MQTT_SERVER(0, 0, 0, 0);
uint16_t MQTT_PORT = 0;
bool standalone = true;
#define MQTT_BASE_TOPIC "/espmega/ProR3"
char STATE_REQUEST_TOPIC[75] = MQTT_BASE_TOPIC "/requeststate";
// #define MQTT_BASE_TOPIC "/espmega/ProR3"
char MQTT_BASE_TOPIC[20];
uint8_t base_topic_length = 0;
char STATE_REQUEST_TOPIC[40];
// #define MQTT_USE_AUTH
#ifdef MQTT_USE_AUTH
const char MQTT_USERNAME[] = "username";
@ -47,8 +49,8 @@ const float pwm_linear_scaling_m[PWM_COUNT] = {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};
#define PWM_CYCLE_VALUES_COUNT 3
const int PWM_CYCLE_VALUES[PWM_CYCLE_VALUES_COUNT] = {50, 125, 255};
char PWM_SET_STATE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/set/state";
char PWM_SET_VALUE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/set/value";
char PWM_SET_STATE_TOPIC[70];
char PWM_SET_VALUE_TOPIC[70];
// Infrared Transciever
#define IR_RECIEVE_PIN 32
@ -72,14 +74,14 @@ uint8_t ac_fan_speed = 0;
uint8_t ac_temperature = 25;
#define AC_MAX_TEMPERATURE 30
#define AC_MIN_TEMPERATURE 18
char AC_SET_MODE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/mode";
char AC_SET_FAN_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/fan_speed";
char AC_SET_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/temperature";
char AC_MODE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/mode";
char AC_FAN_TOPIC[75] = MQTT_BASE_TOPIC "/ac/fan_speed";
char AC_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/temperature";
char AC_ROOM_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/room_temperature";
char AC_HUMIDITY_TOPIC[75] = MQTT_BASE_TOPIC "/ac/humidity";
char AC_SET_MODE_TOPIC[75];
char AC_SET_FAN_TOPIC[75];
char AC_SET_TEMPERATURE_TOPIC[75];
char AC_MODE_TOPIC[75];
char AC_FAN_TOPIC[75];
char AC_TEMPERATURE_TOPIC[75];
char AC_ROOM_TEMPERATURE_TOPIC[75];
char AC_HUMIDITY_TOPIC[75];
// EEPROM ADDRESS
#define EEPROM_ADDRESS_AC_MODE 0 // 01bytes
@ -162,14 +164,14 @@ void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_
IPAddress eeprom_ip_retrieve(uint16_t rom_address);
void set_hostname(String hostname);
void eeprom_hostname_retrieve();
void set_topic(String topic);
void eeprom_topic_retrieve();
void set_basetopic(String topic);
void eeprom_basetopic_retrieve();
void mqtt_port_set(uint16_t port);
void eeprom_mqtt_port_retrieve();
char PWM_STATE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/state";
char PWM_VALUE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/value";
char INPUTS_TOPIC[75] = MQTT_BASE_TOPIC "/input/00";
char PWM_STATE_TOPIC[75];
char PWM_VALUE_TOPIC[75];
char INPUTS_TOPIC[75];
WiFiClient eth;
PubSubClient mqtt_client(MQTT_SERVER, 1883, eth);
@ -275,6 +277,42 @@ void eeprom_retrieve_init()
eeprom_hostname_retrieve();
eeprom_mqtt_port_retrieve();
mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
eeprom_basetopic_retrieve();
base_topic_length = strlen(MQTT_BASE_TOPIC)+1;
memcpy(STATE_REQUEST_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(STATE_REQUEST_TOPIC, "/requeststate");
memcpy(PWM_SET_STATE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(PWM_SET_STATE_TOPIC, "/pwm/00/set/state");
memcpy(PWM_SET_VALUE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(PWM_SET_VALUE_TOPIC, "/pwm/00/set/value");
memcpy(AC_SET_MODE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_SET_MODE_TOPIC, "/ac/set/mode");
memcpy(AC_SET_FAN_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_SET_FAN_TOPIC, "/ac/set/fan_speed");
memcpy(AC_SET_TEMPERATURE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_SET_TEMPERATURE_TOPIC, "/ac/set/temperature");
memcpy(AC_MODE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_MODE_TOPIC, "/ac/mode");
memcpy(AC_FAN_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_FAN_TOPIC, "/ac/fan_speed");
memcpy(AC_TEMPERATURE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_TEMPERATURE_TOPIC, "/ac/temperature");
memcpy(AC_ROOM_TEMPERATURE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_ROOM_TEMPERATURE_TOPIC, "/ac/room_temperature");
memcpy(AC_HUMIDITY_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_HUMIDITY_TOPIC, "/ac/humidity");
memcpy(PWM_STATE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(PWM_STATE_TOPIC, "/pwm/00/state");
memcpy(PWM_VALUE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(PWM_VALUE_TOPIC, "/pwm/00/value");
memcpy(INPUTS_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(INPUTS_TOPIC, "/input/00");
Serial.println(PWM_STATE_TOPIC);
Serial.println(PWM_VALUE_TOPIC);
Serial.println(PWM_SET_STATE_TOPIC);
Serial.println(PWM_SET_VALUE_TOPIC);
Serial.print("BTLEN: ");
Serial.println(base_topic_length);
}
void io_begin()
@ -335,10 +373,10 @@ void mqtt_subscribe()
{
for (int i = 0; i < PWM_COUNT; i++)
{
PWM_SET_VALUE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 4] = ((i - i % 10) / 10) + '0';
PWM_SET_VALUE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 5] = (i % 10) + '0';
PWM_SET_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 4] = ((i - i % 10) / 10) + '0';
PWM_SET_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 5] = (i % 10) + '0';
PWM_SET_VALUE_TOPIC[base_topic_length + 4] = ((i - i % 10) / 10) + '0';
PWM_SET_VALUE_TOPIC[base_topic_length + 5] = (i % 10) + '0';
PWM_SET_STATE_TOPIC[base_topic_length + 4] = ((i - i % 10) / 10) + '0';
PWM_SET_STATE_TOPIC[base_topic_length + 5] = (i % 10) + '0';
mqtt.subscribe(PWM_SET_STATE_TOPIC, pwm_state_callback);
mqtt.subscribe(PWM_SET_VALUE_TOPIC, pwm_value_callback);
}
@ -362,8 +400,8 @@ void thread_initialization()
void pwm_state_callback(String topic, String message)
{
int a = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 4) - '0';
int b = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 5) - '0';
int a = topic.charAt(base_topic_length + 4) - '0';
int b = topic.charAt(base_topic_length + 5) - '0';
int id = 10 * a + b;
if (message.compareTo("on") == 0)
{
@ -377,8 +415,8 @@ void pwm_state_callback(String topic, String message)
void pwm_value_callback(String topic, String message)
{
int a = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 4) - '0';
int b = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 5) - '0';
int a = topic.charAt(base_topic_length + 4) - '0';
int b = topic.charAt(base_topic_length + 5) - '0';
int id = 10 * a + b;
int value = message.toInt();
pwm_set_value(id, value);
@ -430,10 +468,10 @@ void publish_pwm_state(int id)
{
int state = pwm_states[id];
int value = pwm_values[id];
PWM_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 4] = ((id - id % 10) / 10) + '0';
PWM_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 5] = (id % 10) + '0';
PWM_VALUE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 4] = ((id - id % 10) / 10) + '0';
PWM_VALUE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 5] = (id % 10) + '0';
PWM_STATE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0';
PWM_STATE_TOPIC[base_topic_length + 5] = (id % 10) + '0';
PWM_VALUE_TOPIC[base_topic_length + 4] = ((id - id % 10) / 10) + '0';
PWM_VALUE_TOPIC[base_topic_length + 5] = (id % 10) + '0';
if (state == 1)
{
mqtt_client.publish(PWM_STATE_TOPIC, "on");
@ -541,9 +579,8 @@ void publish_input_state(int id)
void publish_input_state(int id, int state)
{
char INPUTS_TOPIC[75] = MQTT_BASE_TOPIC "/input/00";
INPUTS_TOPIC[sizeof(MQTT_BASE_TOPIC) + 6] = ((id - id % 10) / 10) + '0';
INPUTS_TOPIC[sizeof(MQTT_BASE_TOPIC) + 7] = (id % 10) + '0';
INPUTS_TOPIC[base_topic_length + 6] = ((id - id % 10) / 10) + '0';
INPUTS_TOPIC[base_topic_length + 7] = (id % 10) + '0';
mqtt.publish(String(INPUTS_TOPIC), state ? "1" : "0");
}
@ -773,6 +810,7 @@ void lcd_refresh()
panel.writeStr("mqttsv_set.txt", MQTT_SERVER.toString());
panel.writeStr("host_set.txt", HOSTNAME);
panel.writeNum("port_set.val", MQTT_PORT);
panel.writeStr("topic_set.txt", MQTT_BASE_TOPIC);
break;
default:
break;
@ -887,6 +925,7 @@ void trigger13()
set_dns(panel.readStr("dns_set.txt"));
set_mqtt_server(panel.readStr("mqttsv_set.txt"));
set_hostname(panel.readStr("host_set.txt"));
set_basetopic(panel.readStr("topic_set.txt"));
uint16_t port = panel.readNumber("port_set.val");
mqtt_port_set(port);
Serial.println("NEWPORT:");
@ -960,8 +999,17 @@ void set_hostname(String hostname)
void eeprom_hostname_retrieve()
{
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_HOSTNAME, (uint8_t *)HOSTNAME, 15);
Serial.print("HOSTNAMEREAD:");
Serial.println(HOSTNAME);
}
void set_basetopic(String topic)
{
topic.toCharArray(MQTT_BASE_TOPIC, 20);
ESPMega_EEPROM.writeBlock(EEPROM_ADDRESS_TOPIC, (uint8_t *)MQTT_BASE_TOPIC, 20);
}
void eeprom_basetopic_retrieve()
{
ESPMega_EEPROM.readBlock(EEPROM_ADDRESS_TOPIC, (uint8_t *)MQTT_BASE_TOPIC, 20);
}
void mqtt_port_set(uint16_t port)