migrate from PubSubClientTools

This commit is contained in:
Siwat Sirichai 2023-11-12 21:25:08 +07:00
parent d7b8af258a
commit a6206cd881
3 changed files with 42 additions and 30 deletions

View File

@ -12,6 +12,7 @@
platform = espressif32 platform = espressif32
board = wt32-eth01 board = wt32-eth01
framework = arduino framework = arduino
board_build.f_cpu = 240000000L
lib_deps = siwats/ESPMegaPROR3@^1.3.0 lib_deps = siwats/ESPMegaPROR3@^1.3.0
knolleary/PubSubClient@^2.8 knolleary/PubSubClient@^2.8
ivanseidel/ArduinoThread@^2.1.1 ivanseidel/ArduinoThread@^2.1.1

View File

@ -159,6 +159,7 @@ void setup()
lcd_send_command("boot_state.txt=\"IoT Core Initializing . . .\""); lcd_send_command("boot_state.txt=\"IoT Core Initializing . . .\"");
mqtt.setSocketTimeout(1000); mqtt.setSocketTimeout(1000);
eth.setTimeout(1); eth.setTimeout(1);
mqtt.setCallback(&mqtt_callback);
mqtt_connect(); mqtt_connect();
lcd_send_command("boot_state.txt=\"Threads Initializing . . .\""); lcd_send_command("boot_state.txt=\"Threads Initializing . . .\"");
thread_initialization(); thread_initialization();
@ -518,18 +519,29 @@ void mqtt_subscribe()
mqtt.subscribe(STATE_REQUEST_TOPIC); mqtt.subscribe(STATE_REQUEST_TOPIC);
} }
void mqtt_callback(char* topic, char* payload, unsigned int length) { void mqtt_callback(char *topic, byte *payload, unsigned int length)
{
uint8_t topic_length = strlen(topic); uint8_t topic_length = strlen(topic);
char topic_trim[50]; char topic_trim[50];
strncpy(topic_trim, topic+topic_length, topic_length); char payload_nt[length + 1];
// PWM Callback memcpy(payload_nt, payload, length);
if((!strncmp(topic_trim,"/pwm/",5)) && !strncmp(topic_trim+7,"/set/state",10)) { payload_nt[length] = NULL;
pwm_state_callback(topic_trim, topic_length,payload,length); strncpy(topic_trim, topic + base_topic_length - 1, topic_length);
if ((!strncmp(topic_trim, "/pwm/", 5)) && !strncmp(topic_trim + 7, "/set/state", 10))
{
pwm_state_callback(topic_trim, topic_length, payload_nt, length);
} }
else if((!strncmp(topic_trim,"/pwm/",5)) && !strncmp(topic_trim+7,"/set/value",10)) { else if ((!strncmp(topic_trim, "/pwm/", 5)) && !strncmp(topic_trim + 7, "/set/value", 10))
pwm_value_callback(topic_trim, topic_length,payload,length); {
pwm_value_callback(topic_trim, topic_length, payload_nt, length);
} else if(!strcmp(topic,STATE_REQUEST_TOPIC)) {
state_request_callback();
} }
user_mqtt_callback(topic_trim,topic_length, payload, length); else
{
ac_state_callback(topic, topic_length, payload_nt, length);
}
user_mqtt_callback(topic_trim, topic_length, payload_nt, length);
} }
void thread_initialization() void thread_initialization()
@ -548,22 +560,22 @@ void thread_initialization()
user_timer_tick.setInterval(15000); user_timer_tick.setInterval(15000);
} }
void pwm_state_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length) void pwm_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length)
{ {
int a = topic[5] - '0'; int a = topic[5] - '0';
int b = topic[6] - '0'; int b = topic[6] - '0';
int id = 10 * a + b; int id = 10 * a + b;
if (!strcmp(payload,"on")) if (!strcmp(payload, "on"))
{ {
pwm_set_state(id, true); pwm_set_state(id, true);
} }
else if (!strcmp(payload,"off")) else if (!strcmp(payload, "off"))
{ {
pwm_set_state(id, false); pwm_set_state(id, false);
} }
} }
void pwm_value_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length) void pwm_value_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length)
{ {
int a = topic[5] - '0'; int a = topic[5] - '0';
int b = topic[6] - '0'; int b = topic[6] - '0';
@ -629,7 +641,7 @@ void publish_pwm_state(int id)
mqtt.publish(PWM_STATE_TOPIC, "off"); mqtt.publish(PWM_STATE_TOPIC, "off");
} }
char temp[6]; char temp[6];
itoa(value,temp,6); itoa(value, temp, DEC);
mqtt.publish(PWM_VALUE_TOPIC, temp); mqtt.publish(PWM_VALUE_TOPIC, temp);
} }
@ -740,7 +752,7 @@ void publish_input_state(int id, int state)
mqtt.publish(INPUTS_TOPIC, state ? "1" : "0"); mqtt.publish(INPUTS_TOPIC, state ? "1" : "0");
} }
void state_request_callback(String topic, String message) void state_request_callback()
{ {
publish_input_states(); publish_input_states();
publish_pwm_states(); publish_pwm_states();
@ -777,7 +789,7 @@ void publish_ac_state()
break; break;
} }
char temp[5]; char temp[5];
itoa(ac_temperature,temp,DEC); itoa(ac_temperature, temp, DEC);
mqtt.publish(AC_TEMPERATURE_TOPIC, temp); mqtt.publish(AC_TEMPERATURE_TOPIC, temp);
switch (ac_fan_speed) switch (ac_fan_speed)
{ {
@ -794,49 +806,48 @@ void publish_ac_state()
mqtt.publish(AC_FAN_TOPIC, "low"); mqtt.publish(AC_FAN_TOPIC, "low");
break; break;
} }
} }
void ac_state_callback(String topic, String message) void ac_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length)
{ {
if (topic.compareTo(String(AC_SET_TEMPERATURE_TOPIC)) == 0) if (!strcmp(topic, AC_SET_TEMPERATURE_TOPIC))
{ {
int new_temp = message.toInt(); int new_temp = atoi(payload);
if (new_temp >= AC_MIN_TEMPERATURE && new_temp <= AC_MAX_TEMPERATURE) if (new_temp >= AC_MIN_TEMPERATURE && new_temp <= AC_MAX_TEMPERATURE)
{ {
ac_set_state(ac_mode, new_temp, ac_fan_speed); ac_set_state(ac_mode, new_temp, ac_fan_speed);
} }
} }
else if (topic.compareTo(String(AC_SET_MODE_TOPIC)) == 0) else if (!strcmp(topic, AC_SET_MODE_TOPIC))
{ {
if (message.compareTo("off") == 0) if (!strcmp(payload, "off"))
{ {
ac_set_state(0, ac_temperature, ac_fan_speed); ac_set_state(0, ac_temperature, ac_fan_speed);
} }
else if (message.compareTo("cool") == 0) else if (!strcmp(payload, "cool"))
{ {
ac_set_state(1, ac_temperature, ac_fan_speed); ac_set_state(1, ac_temperature, ac_fan_speed);
} }
else if (message.compareTo("fan_only") == 0) else if (!strcmp(payload, "fan_only"))
{ {
ac_set_state(2, ac_temperature, ac_fan_speed); ac_set_state(2, ac_temperature, ac_fan_speed);
} }
} }
else if (topic.compareTo(String(AC_SET_FAN_TOPIC)) == 0) else if (!strcmp(topic, AC_SET_FAN_TOPIC))
{ {
if (message.compareTo("auto") == 0) if (!strcmp(payload, "auto"))
{ {
ac_set_state(ac_mode, ac_temperature, 0); ac_set_state(ac_mode, ac_temperature, 0);
} }
else if (message.compareTo("low") == 0) else if (!strcmp(payload, "low"))
{ {
ac_set_state(ac_mode, ac_temperature, 1); ac_set_state(ac_mode, ac_temperature, 1);
} }
else if (message.compareTo("med") == 0) else if (!strcmp(payload, "med"))
{ {
ac_set_state(ac_mode, ac_temperature, 2); ac_set_state(ac_mode, ac_temperature, 2);
} }
else if (message.compareTo("high") == 0) else if (!strcmp(payload, "high"))
{ {
ac_set_state(ac_mode, ac_temperature, 3); ac_set_state(ac_mode, ac_temperature, 3);
} }

View File

@ -43,7 +43,7 @@ void mqtt_subscribe();
void thread_initialization(); void thread_initialization();
void pwm_state_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length); void pwm_state_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length);
void pwm_value_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length); void pwm_value_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length);
void state_request_callback(String topic, String message); void state_request_callback();
void io_begin(); void io_begin();
void ir_loop(); void ir_loop();
@ -66,7 +66,7 @@ void publish_env_state();
uint8_t ac_get_temperature(); uint8_t ac_get_temperature();
uint8_t ac_get_mode(); uint8_t ac_get_mode();
uint8_t ac_get_fan_speed(); uint8_t ac_get_fan_speed();
void ac_state_callback(String topic, String message); void ac_state_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length);
void ac_set_state(int mode, int temperature, int fan_speed); void ac_set_state(int mode, int temperature, int fan_speed);
void publish_input_states(); void publish_input_states();