76 lines
2.2 KiB
C
76 lines
2.2 KiB
C
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
#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"
|
|
|
|
#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) {
|
|
LOG(LL_DEBUG, ("buttonPressOne called with pressCount=%d", pressCount));
|
|
}
|
|
|
|
static void buttonPressTwo(int pressCount) {
|
|
LOG(LL_DEBUG, ("buttonPressTwo called with pressCount=%d", pressCount));
|
|
}
|
|
|
|
static void buttonPressThree(int pressCount) {
|
|
LOG(LL_DEBUG, ("buttonPressThree called with pressCount=%d", pressCount));
|
|
}
|
|
|
|
enum mgos_app_init_result mgos_app_init(void) {
|
|
init_led_handler();
|
|
set_led_status(LED_BLINK_FAST);
|
|
init_button_handler();
|
|
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);
|
|
LOG(LL_INFO, ("SONOFF app initialized"));
|
|
return MGOS_APP_INIT_SUCCESS;
|
|
}
|