From 423511bffc95e28a82ebd44cbb5e172c8d82afee Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Sun, 28 Jan 2018 22:18:01 +0100 Subject: [PATCH] Added mqttHandler --- src/main.c | 47 ++------------------------- src/mqttHandler.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++ src/mqttHandler.h | 11 +++++++ src/relayHandler.c | 8 +++-- src/relayHandler.h | 1 + 5 files changed, 100 insertions(+), 47 deletions(-) create mode 100644 src/mqttHandler.c create mode 100644 src/mqttHandler.h diff --git a/src/main.c b/src/main.c index 88a8a8d..599e875 100644 --- a/src/main.c +++ b/src/main.c @@ -1,55 +1,12 @@ -#include -#include - #include "mgos.h" #include "mgos_app.h" -#include "mgos_gpio.h" #include "mgos_sys_config.h" #include "mgos_timers.h" -#include "mgos_mqtt.h" -#include "common/platform.h" -#include "common/cs_file.h" #include "buttonHandler.h" #include "ledHandler.h" #include "relayHandler.h" - -#define RELAY_PIN 12 - -bool mqtt_conn_flag = false; - -int mqtt_connected(void) { - return (int) mqtt_conn_flag; -} - -//static void pub(struct mg_connection *c, const char *fmt, ...) { - //char msg[200]; - //struct json_out jmo = JSON_OUT_BUF(msg, sizeof(msg)); - //va_list ap; - //int n; - //va_start(ap, fmt); - //n = json_vprintf(&jmo, fmt, ap); - //va_end(ap); - //mg_mqtt_publish(c, get_cfg()->mqtt.pub, 0, MG_MQTT_QOS(0), msg, n); - //LOG(LL_INFO, ("%s -> %s", get_cfg()->mqtt.pub, msg)); -//} - -static void mqtt_ev_handler(struct mg_connection *c, int ev, void *p, void *user_data) { - struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p; - if (ev == MG_EV_MQTT_CONNACK) { - LOG(LL_INFO, ("SonoffApp: MQTT connected: %d", msg->connack_ret_code)); - mqtt_conn_flag = true; - //if (get_cfg()->mqtt.pub == NULL) { - //LOG(LL_ERROR, ("Run 'mos config-set mqtt.pub=... '")); - //} else { - //pub(c, "{timestamp:%.3lf, mem_free:%d}", mg_time(), mgos_get_free_heap_size() ); /* post uptime */ - //} - } else if (ev == MG_EV_CLOSE) { - mqtt_conn_flag = false; - } - (void) user_data; - (void) c; -} +#include "mqttHandler.h" static void buttonPressOne(int pressCount) { LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", pressCount)); @@ -71,7 +28,7 @@ enum mgos_app_init_result mgos_app_init(void) { add_button_press_callback(1, buttonPressOne); add_button_press_callback(2, buttonPressTwo); add_button_press_callback(3, buttonPressThree); - mgos_mqtt_add_global_handler(mqtt_ev_handler, NULL); + init_mqtt_handler(); LOG(LL_INFO, ("SONOFF app initialized")); return MGOS_APP_INIT_SUCCESS; } diff --git a/src/mqttHandler.c b/src/mqttHandler.c new file mode 100644 index 0000000..f1ad9ae --- /dev/null +++ b/src/mqttHandler.c @@ -0,0 +1,80 @@ +#include "mgos.h" +#include "mgos_mqtt.h" +#include "mqttHandler.h" +#include "relayHandler.h" + +static bool mqtt_conn_flag = false; +static bool enablePub = false; +static const char *pubTopic = NULL; +static bool enableSub = false; +static const char *subTopic = NULL; +static struct mg_connection *mqttConnection = NULL; + +bool mqtt_connected(void) { + return mqtt_conn_flag; +} + +//static void pub(struct mg_connection *c, const char *fmt, ...) { + //char msg[200]; + //struct json_out jmo = JSON_OUT_BUF(msg, sizeof(msg)); + //va_list ap; + //int n; + //va_start(ap, fmt); + //n = json_vprintf(&jmo, fmt, ap); + //va_end(ap); + //mg_mqtt_publish(c, get_cfg()->mqtt.pub, 0, MG_MQTT_QOS(0), msg, n); + //LOG(LL_INFO, ("%s -> %s", get_cfg()->mqtt.pub, msg)); +//} + +static void mqtt_ev_handler(struct mg_connection *c, int ev, void *p, void *user_data) { + struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p; + mqttConnection = c; + if (ev == MG_EV_MQTT_CONNACK) { + LOG(LL_INFO, ("SonoffApp: MQTT connected: %d", msg->connack_ret_code)); + mqtt_conn_flag = true; + //if (get_cfg()->mqtt.pub == NULL) { + //LOG(LL_ERROR, ("Run 'mos config-set mqtt.pub=... '")); + //} else { + //pub(c, "{timestamp:%.3lf, mem_free:%d}", mg_time(), mgos_get_free_heap_size() ); /* post uptime */ + //} + // subscribe command topic + (void) subTopic; + } else if (ev == MG_EV_CLOSE) { + mqtt_conn_flag = false; + } + (void) user_data; + (void) c; +} + +static void pubMqttStatus(char *event) { + if (!enablePub) return; + if (!mqtt_conn_flag) return; + char msg[255]; + snprintf(msg, sizeof(msg), + "{timestamp:%.3lf, event: \"%s\", mem_free:%d, switch: \"%s\"}", + mg_time(), + event, + mgos_get_free_heap_size(), + relay_is_on() ? "true" : "false"); + mgos_mqtt_pub(pubTopic, msg, strlen(msg), MG_MQTT_QOS(0)); + //mg_mqtt_publish(c, get_cfg()->mqtt.pub, 0, MG_MQTT_QOS(0), msg, n); + LOG(LL_DEBUG, ("MQTT pub topic=%s -> %s", pubTopic, msg)); +} + + +void init_mqtt_handler() { + mgos_mqtt_add_global_handler(mqtt_ev_handler, NULL); + pubTopic = mgos_sys_config_get_sonoff_switch_status_pub_topic(); + if (pubTopic != NULL) enablePub = true; + subTopic = mgos_sys_config_get_sonoff_command_sub_topic(); + if (subTopic != NULL) enableSub = true; + if (pubTopic == NULL) { + LOG(LL_WARN, ("Missing configuration for sonoff.switch_status_pub_topic")); + enablePub = false; + } else { + pubMqttStatus("DeviceInit"); + } + LOG(LL_INFO, ("MQTT handler initialized")); + + (void) subTopic; +} \ No newline at end of file diff --git a/src/mqttHandler.h b/src/mqttHandler.h new file mode 100644 index 0000000..d7b91f3 --- /dev/null +++ b/src/mqttHandler.h @@ -0,0 +1,11 @@ +// Author: Dirk Jahnke +// January 24, 2018 +// +// Manage MQTT interface +// +#ifndef _mqttHandler_h_included +#define _mqttHandler_h_included + +extern void init_mqtt_handler(); + +#endif \ No newline at end of file diff --git a/src/relayHandler.c b/src/relayHandler.c index 2936301..538571c 100644 --- a/src/relayHandler.c +++ b/src/relayHandler.c @@ -2,8 +2,7 @@ #include "mgos_gpio.h" #include "relayHandler.h" -#define ON_BOARD_RELAY_PIN 8 - +#define ON_BOARD_RELAY_PIN 12 void set_relay(enum RelayAction rm) { switch (rm) { @@ -23,7 +22,12 @@ void set_relay(enum RelayAction rm) { LOG(LL_DEBUG, ("set_relay mode=%d", rm)); } +bool relay_is_on() { + return !mgos_gpio_read_out(ON_BOARD_RELAY_PIN); +} + void init_relay_handler() { mgos_gpio_set_mode(ON_BOARD_RELAY_PIN, MGOS_GPIO_MODE_OUTPUT); mgos_gpio_write(ON_BOARD_RELAY_PIN, true); + LOG(LL_INFO, ("Relay handler initialized")); } \ No newline at end of file diff --git a/src/relayHandler.h b/src/relayHandler.h index 4d7a52a..1c178af 100644 --- a/src/relayHandler.h +++ b/src/relayHandler.h @@ -9,5 +9,6 @@ enum RelayAction { }; extern void set_relay(enum RelayAction newStatus); extern void init_relay_handler(); +extern bool relay_is_on(); #endif