mirror of https://github.com/jeelabs/esp-link.git
commit
b912c4babe
@ -1,8 +1,9 @@ |
||||
#ifdef MQTT |
||||
#ifndef CGIMQTT_H |
||||
#define CGIMQTT_H |
||||
|
||||
#include "httpd.h" |
||||
|
||||
int cgiMqtt(HttpdConnData *connData); |
||||
|
||||
#endif |
||||
#endif // CGIMQTT_H
|
||||
#endif // MQTT
|
||||
|
@ -1,40 +1,145 @@ |
||||
#ifdef MQTT |
||||
#include <esp8266.h> |
||||
#include "cgiwifi.h" |
||||
#include "config.h" |
||||
#include "mqtt.h" |
||||
|
||||
#ifdef MQTTCLIENT_DBG |
||||
#define DBG_MQTTCLIENT(format, ...) os_printf(format, ## __VA_ARGS__) |
||||
#else |
||||
#define DBG_MQTTCLIENT(format, ...) do { } while(0) |
||||
#endif |
||||
|
||||
MQTT_Client mqttClient; |
||||
char* statusTopicStr; |
||||
static char* onlineMsgStr; |
||||
|
||||
static ETSTimer mqttTimer; |
||||
static MqttCallback connected_cb; |
||||
static MqttCallback disconnected_cb; |
||||
static MqttCallback published_cb; |
||||
static MqttDataCallback data_cb; |
||||
|
||||
static int once = 0; |
||||
static void ICACHE_FLASH_ATTR |
||||
mqttTimerCb(void *arg) |
||||
{ |
||||
if (once++ > 0) return; |
||||
if (flashConfig.mqtt_enable) |
||||
MQTT_Connect(&mqttClient); |
||||
//MQTT_Subscribe(&mqttClient, "system/time", 0);
|
||||
void ICACHE_FLASH_ATTR |
||||
mqttConnectedCb(uint32_t *args) { |
||||
MQTT_Client* client = (MQTT_Client*)args; |
||||
DBG_MQTTCLIENT("MQTT Client: Connected\n"); |
||||
MQTT_Subscribe(client, "system/time", 0); |
||||
MQTT_Publish(client, "announce/all", onlineMsgStr, 0, 0); |
||||
if (connected_cb) |
||||
connected_cb(args); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
mqttDisconnectedCb(uint32_t *args) { |
||||
// MQTT_Client* client = (MQTT_Client*)args;
|
||||
DBG_MQTTCLIENT("MQTT Client: Disconnected\n"); |
||||
if (disconnected_cb) |
||||
disconnected_cb(args); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
mqttPublishedCb(uint32_t *args) { |
||||
// MQTT_Client* client = (MQTT_Client*)args;
|
||||
DBG_MQTTCLIENT("MQTT Client: Published\n"); |
||||
if (published_cb) |
||||
published_cb(args); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) { |
||||
// MQTT_Client* client = (MQTT_Client*)args;
|
||||
|
||||
char *topicBuf = (char*)os_zalloc(topic_len + 1); |
||||
char *dataBuf = (char*)os_zalloc(data_len + 1); |
||||
|
||||
os_memcpy(topicBuf, topic, topic_len); |
||||
topicBuf[topic_len] = 0; |
||||
|
||||
os_memcpy(dataBuf, data, data_len); |
||||
dataBuf[data_len] = 0; |
||||
|
||||
DBG_MQTTCLIENT("MQTT Client: Received topic: %s, data: %s\n", topicBuf, dataBuf); |
||||
os_free(topicBuf); |
||||
os_free(dataBuf); |
||||
|
||||
if (data_cb) |
||||
data_cb(args, topic, topic_len, data, data_len); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
wifiStateChangeCb(uint8_t status) |
||||
{ |
||||
if (status == wifiGotIP) { |
||||
os_timer_disarm(&mqttTimer); |
||||
os_timer_setfn(&mqttTimer, mqttTimerCb, NULL); |
||||
os_timer_arm(&mqttTimer, 200, 0); |
||||
if (flashConfig.mqtt_enable) { |
||||
if (status == wifiGotIP && mqttClient.connState != TCP_CONNECTING) { |
||||
MQTT_Connect(&mqttClient); |
||||
} |
||||
else if (status == wifiIsDisconnected && mqttClient.connState == TCP_CONNECTING) { |
||||
MQTT_Disconnect(&mqttClient); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
// initialize the custom stuff that goes beyond esp-link
|
||||
void ICACHE_FLASH_ATTR |
||||
mqtt_client_init() |
||||
{ |
||||
MQTT_Init(&mqttClient, flashConfig.mqtt_host, flashConfig.mqtt_port, 0, |
||||
flashConfig.mqtt_timeout, flashConfig.mqtt_clientid, |
||||
flashConfig.mqtt_username, flashConfig.mqtt_password, |
||||
if (flashConfig.mqtt_enable) { |
||||
MQTT_Init(&mqttClient, flashConfig.mqtt_host, flashConfig.mqtt_port, 0, flashConfig.mqtt_timeout, |
||||
flashConfig.mqtt_clientid, flashConfig.mqtt_username, flashConfig.mqtt_password, |
||||
flashConfig.mqtt_keepalive); |
||||
|
||||
if (flashConfig.mqtt_status_enable) { |
||||
// removed client_id concat for now until a better solution is devised
|
||||
// statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(flashConfig.mqtt_status_topic) + 2);
|
||||
// os_strcpy(statusTopicStr, flashConfig.mqtt_clientid);
|
||||
// os_strcat(statusTopicStr, "/");
|
||||
|
||||
statusTopicStr = (char*)os_zalloc(strlen(flashConfig.mqtt_status_topic) + 1); |
||||
os_strcpy(statusTopicStr, flashConfig.mqtt_status_topic); |
||||
} |
||||
|
||||
char* onlineMsg = " is online"; |
||||
onlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(onlineMsg) + 1); |
||||
os_strcpy(onlineMsgStr, flashConfig.mqtt_clientid); |
||||
os_strcat(onlineMsgStr, onlineMsg); |
||||
|
||||
char* offlineMsg = " is offline"; |
||||
char* offlineMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(offlineMsg) + 1); |
||||
os_strcpy(offlineMsgStr, flashConfig.mqtt_clientid); |
||||
os_strcat(offlineMsgStr, offlineMsg); |
||||
|
||||
char* lwt = "/lwt"; |
||||
char *lwtMsgStr = (char*)os_zalloc(strlen(flashConfig.mqtt_clientid) + strlen(lwt) + 1); |
||||
os_strcpy(lwtMsgStr, flashConfig.mqtt_clientid); |
||||
os_strcat(lwtMsgStr, lwt); |
||||
MQTT_InitLWT(&mqttClient, lwtMsgStr, offlineMsg, 0, 0); |
||||
|
||||
MQTT_OnConnected(&mqttClient, mqttConnectedCb); |
||||
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); |
||||
MQTT_OnPublished(&mqttClient, mqttPublishedCb); |
||||
MQTT_OnData(&mqttClient, mqttDataCb); |
||||
} |
||||
|
||||
wifiAddStateChangeCb(wifiStateChangeCb); |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
mqtt_client_on_connected(MqttCallback connectedCb) { |
||||
connected_cb = connectedCb; |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
mqtt_client_on_disconnected(MqttCallback disconnectedCb) { |
||||
disconnected_cb = disconnectedCb; |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
mqtt_client_on_published(MqttCallback publishedCb) { |
||||
published_cb = publishedCb; |
||||
} |
||||
|
||||
void ICACHE_FLASH_ATTR |
||||
mqtt_client_on_data(MqttDataCallback dataCb) { |
||||
data_cb = dataCb; |
||||
} |
||||
|
||||
#endif // MQTT
|
||||
|
@ -1,9 +1,15 @@ |
||||
#ifdef MQTT |
||||
#ifndef MQTT_CLIENT_H |
||||
#define MQTT_CLIENT_H |
||||
|
||||
#include "mqtt.h" |
||||
|
||||
extern MQTT_Client mqttClient; |
||||
extern char* statusTopicStr; |
||||
void mqtt_client_init(); |
||||
void mqtt_client_on_connected(MqttCallback connectedCb); |
||||
void mqtt_client_on_disconnected(MqttCallback disconnectedCb); |
||||
void mqtt_client_on_published(MqttCallback publishedCb); |
||||
void mqtt_client_on_data(MqttDataCallback dataCb); |
||||
|
||||
#endif |
||||
#endif //MQTT_CLIENT_H
|
||||
#endif // MQTT
|
||||
|
@ -0,0 +1,76 @@ |
||||
//===== MQTT cards
|
||||
|
||||
function changeMqtt(e) { |
||||
e.preventDefault(); |
||||
var url = "mqtt?1=1"; |
||||
var i, inputs = document.querySelectorAll('#mqtt-form input'); |
||||
for (i = 0; i < inputs.length; i++) { |
||||
if (inputs[i].type != "checkbox") |
||||
url += "&" + inputs[i].name + "=" + inputs[i].value; |
||||
}; |
||||
|
||||
hideWarning(); |
||||
var cb = $("#mqtt-button"); |
||||
addClass(cb, 'pure-button-disabled'); |
||||
ajaxSpin("POST", url, function (resp) { |
||||
showNotification("MQTT updated"); |
||||
removeClass(cb, 'pure-button-disabled'); |
||||
}, function (s, st) { |
||||
showWarning("Error: " + st); |
||||
removeClass(cb, 'pure-button-disabled'); |
||||
window.setTimeout(fetchMqtt, 100); |
||||
}); |
||||
} |
||||
|
||||
function displayMqtt(data) { |
||||
Object.keys(data).forEach(function (v) { |
||||
el = $("#" + v); |
||||
if (el != null) { |
||||
if (el.nodeName === "INPUT") el.value = data[v]; |
||||
else el.innerHTML = data[v]; |
||||
return; |
||||
} |
||||
el = document.querySelector('input[name="' + v + '"]'); |
||||
if (el != null) { |
||||
if (el.type == "checkbox") el.checked = data[v] > 0; |
||||
else el.value = data[v]; |
||||
} |
||||
}); |
||||
$("#mqtt-spinner").setAttribute("hidden", ""); |
||||
$("#mqtt-status-spinner").setAttribute("hidden", ""); |
||||
$("#mqtt-form").removeAttribute("hidden"); |
||||
$("#mqtt-status-form").removeAttribute("hidden"); |
||||
|
||||
var i, inputs = $("input"); |
||||
for (i = 0; i < inputs.length; i++) { |
||||
if (inputs[i].type == "checkbox") |
||||
inputs[i].onclick = function () { console.log(this); setMqtt(this.name, this.checked) }; |
||||
} |
||||
} |
||||
|
||||
function fetchMqtt() { |
||||
ajaxJson("GET", "/mqtt", displayMqtt, function () { |
||||
window.setTimeout(fetchMqtt, 1000); |
||||
}); |
||||
} |
||||
|
||||
function changeMqttStatus(e) { |
||||
e.preventDefault(); |
||||
var v = document.querySelector('input[name="mqtt-status-topic"]').value; |
||||
ajaxSpin("POST", "/mqtt?mqtt-status-topic=" + v, function () { |
||||
showNotification("MQTT status settings updated"); |
||||
}, function (s, st) { |
||||
showWarning("Error: " + st); |
||||
window.setTimeout(fetchMqtt, 100); |
||||
}); |
||||
} |
||||
|
||||
function setMqtt(name, v) { |
||||
ajaxSpin("POST", "/mqtt?" + name + "=" + (v ? 1 : 0), function () { |
||||
var n = name.replace("-enable", ""); |
||||
showNotification(n + " is now " + (v ? "enabled" : "disabled")); |
||||
}, function () { |
||||
showWarning("Enable/disable failed"); |
||||
window.setTimeout(fetchMqtt, 100); |
||||
}); |
||||
} |
Loading…
Reference in new issue