Skip to content

Commit

Permalink
mqtt: enhancements
Browse files Browse the repository at this point in the history
Added last will topic/msg
Implemented MQTT messaging similar to Tasmota
Added "power on state"
Added more wifi led settings (relay, mqtt topic)
  • Loading branch information
ljalves committed Jan 22, 2022
1 parent 8762b9f commit bead487
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 60 deletions.
61 changes: 44 additions & 17 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SOFTWARE.

#include "hfeasy.h"

#define CONFIG_MAGIC_VER1 0xd4
#define CONFIG_MAGIC_VER1 0xd3
#define CONFIG_OFFSET 0x00
#define CONFIG_SIZE (sizeof(struct hfeasy_config))

Expand Down Expand Up @@ -64,6 +64,26 @@ void USER_FUNC reboot(void)
hftimer_start(reset_timer);
}

static const char *config_page_save =
"<!DOCTYPE html><html><head>"\
"<meta http-equiv=\"refresh\" content=\"5;url=/config\" />"\
"<title>HFeasy config v%d.%d</title><link rel=\"stylesheet\" href=\"styles.css\"></head><body>"\
"Saving config to flash. Please wait...</body></html>";

static void USER_FUNC httpd_page_save(char *url, char *rsp)
{
char tmp[50];
int ret;

ret = httpd_arg_find(url, "save", tmp);
if (ret > 0) {
config_save();
reboot();
}
sprintf(rsp, config_page_save, HFEASY_VERSION_MAJOR, HFEASY_VERSION_MINOR);
}


static const char *config_page =
"<!DOCTYPE html><html><head><title>HFeasy config v%d.%d</title><link rel=\"stylesheet\" href=\"styles.css\"></head><body>"\
"<h1>HFeasy config page</h1><hr>"\
Expand All @@ -72,6 +92,7 @@ static const char *config_page =
"<td><a href=\"config_device\">Device</a>"\
"<td><a href=\"config_gpio\">GPIO</a>"\
"<td><a href=\"status\">Status</a>"\
"<td><a href=\"iweb.html\">Upgrade</a>"\
"</table>"\
"<form action=\"/config\" method=\"GET\"><table>"\
"<th colspan=\"2\">Module"\
Expand All @@ -81,11 +102,14 @@ static const char *config_page =
"<option value=\"0\"%s>Off</option>"\
"<option value=\"1\"%s>MQTT</option>"\
"<option value=\"2\"%s>HTTP</option>"\
"<option value=\"3\"%s>All</option>"\
"<option value=\"4\"%s>Find</option>"\
"<option value=\"3\"%s>MQTT+HTTP</option>"\
"<option value=\"4\"%s>Relay</option>"\
"<option value=\"5\"%s>MQTT topic</option>"\
"<option value=\"6\"%s>Find</option>"\
"</select>"\
"<tr><td>Power on state<td><input type=\"text\" name=\"pwron\" value=\"%d\"> (relay: 0=off, >0=on; dimmer: level)"\
"</table><input type=\"submit\" value=\"Apply\"></form>"\
"<hr><form action=\"/config\" method=\"GET\"><input type=\"submit\" value=\"Save changes to flash and reboot\" name=\"save\"></form>"\
"<hr><form action=\"/save\" method=\"GET\"><input type=\"submit\" value=\"Save changes to flash and reboot\" name=\"save\"></form>"\
"</body></html>";

static void USER_FUNC httpd_page_config(char *url, char *rsp)
Expand All @@ -109,14 +133,13 @@ static void USER_FUNC httpd_page_config(char *url, char *rsp)
ret = httpd_arg_find(url, "wifi_led", tmp);
if (ret > 0) {
state.cfg.wifi_led = atoi(tmp);
if (state.cfg.wifi_led > 4)
if (state.cfg.wifi_led >= LED_CONFIG_END)
state.cfg.wifi_led = 0;
}

ret = httpd_arg_find(url, "save", tmp);
ret = httpd_arg_find(url, "pwron", tmp);
if (ret > 0) {
config_save();
reboot();
state.cfg.pwron_state = atoi(tmp);
}

sprintf(rsp, config_page, HFEASY_VERSION_MAJOR, HFEASY_VERSION_MINOR,
Expand All @@ -125,7 +148,10 @@ static void USER_FUNC httpd_page_config(char *url, char *rsp)
state.cfg.wifi_led == 1 ? "selected" : "",
state.cfg.wifi_led == 2 ? "selected" : "",
state.cfg.wifi_led == 3 ? "selected" : "",
state.cfg.wifi_led == 4 ? "selected" : ""
state.cfg.wifi_led == 4 ? "selected" : "",
state.cfg.wifi_led == 5 ? "selected" : "",
state.cfg.wifi_led == 6 ? "selected" : "",
state.cfg.pwron_state
);

if (state.cfg.wifi_led == LED_CONFIG_FIND) {
Expand All @@ -148,8 +174,8 @@ static const char *config_page_mqtt =
"<TR><TD>Server port (0=disabled)<TD><input type=\"text\" name=\"port\" value=\"%d\">"\
"<TR><TD>Username<TD><input type=\"text\" name=\"user\" value=\"%s\">"\
"<TR><TD>Password<TD><input type=\"password\" name=\"pass\" value=\"%s\">"\
"<TR><TD>Subscribe topic<TD><input type=\"text\" name=\"sub_topic\" value=\"%s\">"\
"<TR><TD>Publish topic<TD><input type=\"text\" name=\"pub_topic\" value=\"%s\">"\
"<TR><TD>Topic (%%topic%%)<TD><input type=\"text\" name=\"topic\" value=\"%s\">"\
"<TR><TD>Full topic<TD><input type=\"text\" name=\"full_topic\" value=\"%s\">"\
"<TR><TD>QOS<TD><input type=\"text\" name=\"qos\" value=\"%d\">"\
"<TR><TD>ON value<TD><input type=\"text\" name=\"on_val\" value=\"%s\">"\
"<TR><TD>OFF value<TD><input type=\"text\" name=\"off_val\" value=\"%s\">"\
Expand Down Expand Up @@ -177,13 +203,13 @@ static void USER_FUNC httpd_page_config_mqtt(char *url, char *rsp)
if (ret > 0)
strcpy(state.cfg.mqtt_server_pass, tmp);

ret = httpd_arg_find(url, "sub_topic", tmp);
ret = httpd_arg_find(url, "topic", tmp);
if (ret > 0)
strcpy(state.cfg.mqtt_sub_topic, tmp);
strcpy(state.cfg.mqtt_topic, tmp);

ret = httpd_arg_find(url, "pub_topic", tmp);
ret = httpd_arg_find(url, "full_topic", tmp);
if (ret > 0)
strcpy(state.cfg.mqtt_pub_topic, tmp);
strcpy(state.cfg.mqtt_full_topic, tmp);

ret = httpd_arg_find(url, "qos", tmp);
if (ret > 0)
Expand All @@ -207,7 +233,7 @@ static void USER_FUNC httpd_page_config_mqtt(char *url, char *rsp)
sprintf(rsp, config_page_mqtt, HFEASY_VERSION_MAJOR, HFEASY_VERSION_MINOR,
state.cfg.mqtt_server_hostname, state.cfg.mqtt_server_port,
state.cfg.mqtt_server_user, state.cfg.mqtt_server_pass,
state.cfg.mqtt_sub_topic, state.cfg.mqtt_pub_topic, state.cfg.mqtt_qos,
state.cfg.mqtt_topic, state.cfg.mqtt_full_topic, state.cfg.mqtt_qos,
state.cfg.mqtt_on_value, state.cfg.mqtt_off_value);

u_printf("page_size=%d\r\n", strlen(rsp));
Expand Down Expand Up @@ -430,7 +456,7 @@ static const char *status_page =
"</body></html>";


const char *relay_change_src[] = { "HTTP", "MQTT", "TIMER", "SWITCH" };
const char *relay_change_src[] = { "HTTP", "MQTT", "TIMER", "SWITCH", "SWUP", "SWDOWN", "POWER ON" };

static void USER_FUNC httpd_page_status(char *url, char *rsp)
{
Expand Down Expand Up @@ -704,4 +730,5 @@ void USER_FUNC config_init(void)
httpd_add_page("/config_gpio", httpd_page_config_gpio);
httpd_add_page("/status", httpd_page_status);
httpd_add_page("/log", httpd_page_log);
httpd_add_page("/save", httpd_page_save);
}
8 changes: 4 additions & 4 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ struct hfeasy_config {
uint16_t mqtt_server_port;
char mqtt_server_user[30];
char mqtt_server_pass[30];
char mqtt_sub_topic[30];
char mqtt_pub_topic[30];
char mqtt_topic[30];
char mqtt_full_topic[50];
uint8_t mqtt_qos;
char mqtt_on_value[10];
char mqtt_off_value[10];
Expand All @@ -59,6 +59,8 @@ struct hfeasy_config {

//time_t timer_on[CONFIG_MAX_TIMERS], timer_off[CONFIG_MAX_TIMERS];

uint8_t pwron_state;

uint32_t log_ptr;
};

Expand Down Expand Up @@ -89,8 +91,6 @@ enum {
DEVICE_CUSTOM
};



void USER_FUNC config_init(void);
void USER_FUNC config_save(void);
void USER_FUNC reboot(void);
Expand Down
18 changes: 16 additions & 2 deletions src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ int USER_FUNC gpio_i2c_recv(uint8_t addr, uint8_t *data)
#endif


void USER_FUNC dimmer_publish_state(void)
{
struct hfeasy_state *state = config_get_state();
char buf[10];
uint8_t lvl;

lvl = state->dimmer_level;

sprintf(buf, "%d", lvl);
mqttcli_publish(buf, "dimmer");
}

void USER_FUNC gpio_set_dimmer(uint8_t lvl, uint8_t publish, uint8_t source)
{
struct hfeasy_state *state = config_get_state();
Expand All @@ -294,7 +306,6 @@ void USER_FUNC gpio_set_dimmer(uint8_t lvl, uint8_t publish, uint8_t source)
state->relay_state = 1;



changed = (old_rs != state->relay_state) || ((lvl > 0) && (lvl != state->dimmer_level));

if (state->relay_state == 0) {
Expand All @@ -314,7 +325,7 @@ void USER_FUNC gpio_set_dimmer(uint8_t lvl, uint8_t publish, uint8_t source)

if (publish && changed) {
sprintf(buf, "%d", lvl);
mqttcli_publish(buf);
mqttcli_publish(buf, "dimmer");
}
}

Expand Down Expand Up @@ -625,6 +636,9 @@ void USER_FUNC gpio_init(void)
hfgpio_configure_fpin(GPIO_I2C_SDA, HFPIO_DEFAULT | HFM_IO_TYPE_INPUT);

state->dimmer_level = 0x80;

gpio_set_dimmer(state->cfg.pwron_state, 1, RELAY_SRC_POWERON);

}

debounce_timer = hftimer_create("debouncer", 50, false, HFTIMER_ID_DEBOUNCE, debounce_timer_handler, 0);
Expand Down
5 changes: 3 additions & 2 deletions src/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ enum {


void USER_FUNC gpio_init(void);
void USER_FUNC gpio_set_dimmer(uint8_t lvl, uint8_t publish, uint8_t source);
int USER_FUNC gpio_get_state(int fid);

int *gpio_pin(int n);

void USER_FUNC gpio_set_dimmer(uint8_t lvl, uint8_t publish, uint8_t source);
void USER_FUNC dimmer_publish_state(void);

#endif
2 changes: 1 addition & 1 deletion src/hfeasy.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


#define HFEASY_VERSION_MAJOR 0
#define HFEASY_VERSION_MINOR 8
#define HFEASY_VERSION_MINOR 9


enum {
Expand Down
13 changes: 8 additions & 5 deletions src/led.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include <hsf.h>

enum {
LED_CONFIG_OFF = 0,
LED_CONFIG_MQTT = 1,
LED_CONFIG_HTTP = 2,
LED_CONFIG_ALL = 3,
LED_CONFIG_FIND = 4,
LED_CONFIG_OFF = 0,
LED_CONFIG_MQTT = 1,
LED_CONFIG_HTTP = 2,
LED_CONFIG_ALL = 3,
LED_CONFIG_RELAY = 4,
LED_CONFIG_TOPIC = 5,
LED_CONFIG_FIND = 6,
LED_CONFIG_END
};

enum {
Expand Down
Loading

0 comments on commit bead487

Please sign in to comment.