Added mqttHandler

This commit is contained in:
Dirk Jahnke 2018-01-28 22:18:01 +01:00
parent 922420a6fd
commit 423511bffc
5 changed files with 100 additions and 47 deletions

View File

@ -1,55 +1,12 @@
#include <stdio.h>
#include <time.h>
#include "mgos.h" #include "mgos.h"
#include "mgos_app.h" #include "mgos_app.h"
#include "mgos_gpio.h"
#include "mgos_sys_config.h" #include "mgos_sys_config.h"
#include "mgos_timers.h" #include "mgos_timers.h"
#include "mgos_mqtt.h"
#include "common/platform.h"
#include "common/cs_file.h"
#include "buttonHandler.h" #include "buttonHandler.h"
#include "ledHandler.h" #include "ledHandler.h"
#include "relayHandler.h" #include "relayHandler.h"
#include "mqttHandler.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;
}
static void buttonPressOne(int pressCount) { static void buttonPressOne(int pressCount) {
LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", 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(1, buttonPressOne);
add_button_press_callback(2, buttonPressTwo); add_button_press_callback(2, buttonPressTwo);
add_button_press_callback(3, buttonPressThree); add_button_press_callback(3, buttonPressThree);
mgos_mqtt_add_global_handler(mqtt_ev_handler, NULL); init_mqtt_handler();
LOG(LL_INFO, ("SONOFF app initialized")); LOG(LL_INFO, ("SONOFF app initialized"));
return MGOS_APP_INIT_SUCCESS; return MGOS_APP_INIT_SUCCESS;
} }

80
src/mqttHandler.c Normal file
View File

@ -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;
}

11
src/mqttHandler.h Normal file
View File

@ -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

View File

@ -2,8 +2,7 @@
#include "mgos_gpio.h" #include "mgos_gpio.h"
#include "relayHandler.h" #include "relayHandler.h"
#define ON_BOARD_RELAY_PIN 8 #define ON_BOARD_RELAY_PIN 12
void set_relay(enum RelayAction rm) { void set_relay(enum RelayAction rm) {
switch (rm) { switch (rm) {
@ -23,7 +22,12 @@ void set_relay(enum RelayAction rm) {
LOG(LL_DEBUG, ("set_relay mode=%d", 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() { void init_relay_handler() {
mgos_gpio_set_mode(ON_BOARD_RELAY_PIN, MGOS_GPIO_MODE_OUTPUT); mgos_gpio_set_mode(ON_BOARD_RELAY_PIN, MGOS_GPIO_MODE_OUTPUT);
mgos_gpio_write(ON_BOARD_RELAY_PIN, true); mgos_gpio_write(ON_BOARD_RELAY_PIN, true);
LOG(LL_INFO, ("Relay handler initialized"));
} }

View File

@ -9,5 +9,6 @@ enum RelayAction {
}; };
extern void set_relay(enum RelayAction newStatus); extern void set_relay(enum RelayAction newStatus);
extern void init_relay_handler(); extern void init_relay_handler();
extern bool relay_is_on();
#endif #endif