kopia lustrzana https://github.com/martin-ger/esp_mqtt
Add published event, resolved #10
rodzic
2accdb20e6
commit
3ca9908267
3
Makefile
3
Makefile
|
@ -93,6 +93,7 @@ MODULE_INCDIR := $(addsuffix /include,$(INCDIR))
|
||||||
|
|
||||||
FW_FILE_1 := $(addprefix $(FW_BASE)/,$(FW_FILE_1).bin)
|
FW_FILE_1 := $(addprefix $(FW_BASE)/,$(FW_FILE_1).bin)
|
||||||
FW_FILE_2 := $(addprefix $(FW_BASE)/,$(FW_FILE_2).bin)
|
FW_FILE_2 := $(addprefix $(FW_BASE)/,$(FW_FILE_2).bin)
|
||||||
|
BLANKER := $(addprefix $(SDK_BASE)/,bin/blank.bin)
|
||||||
|
|
||||||
V ?= $(VERBOSE)
|
V ?= $(VERBOSE)
|
||||||
ifeq ("$(V)","1")
|
ifeq ("$(V)","1")
|
||||||
|
@ -140,7 +141,7 @@ firmware:
|
||||||
$(Q) mkdir -p $@
|
$(Q) mkdir -p $@
|
||||||
|
|
||||||
flash: firmware/0x00000.bin firmware/0x40000.bin
|
flash: firmware/0x00000.bin firmware/0x40000.bin
|
||||||
$(PYTHON) $(ESPTOOL) -p $(ESPPORT) write_flash 0x00000 firmware/0x00000.bin 0x40000 firmware/0x40000.bin
|
$(PYTHON) $(ESPTOOL) -p $(ESPPORT) write_flash 0x00000 firmware/0x00000.bin 0x3C000 $(BLANKER) 0x40000 firmware/0x40000.bin
|
||||||
|
|
||||||
test: flash
|
test: flash
|
||||||
screen $(ESPPORT) 115200
|
screen $(ESPPORT) 115200
|
||||||
|
|
37
README.md
37
README.md
|
@ -47,9 +47,35 @@ void mqttDisconnectedCb(uint32_t *args)
|
||||||
MQTT_Client* client = (MQTT_Client*)args;
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
INFO("MQTT: Disconnected\r\n");
|
INFO("MQTT: Disconnected\r\n");
|
||||||
}
|
}
|
||||||
void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh)
|
|
||||||
|
void mqttPublishedCb(uint32_t *args)
|
||||||
{
|
{
|
||||||
INFO("MQTT topic: %s, data: %s \r\n", topic, data);
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
|
INFO("MQTT: Published\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
|
||||||
|
{
|
||||||
|
char topicBuf[64], dataBuf[64];
|
||||||
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
|
|
||||||
|
os_memcpy(dataBuf, topic, topic_len);
|
||||||
|
topicBuf[topic_len] = 0;
|
||||||
|
|
||||||
|
os_memcpy(dataBuf, data, data_len);
|
||||||
|
dataBuf[data_len] = 0;
|
||||||
|
|
||||||
|
INFO("MQTT topic: %s, data: %s \r\n", topicBuf, dataBuf);
|
||||||
|
|
||||||
|
/* Echo back to /echo channel*/
|
||||||
|
MQTT_Publish(client, "/echo", dataBuf, data_len, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mqttPublishedCb(uint32_t *args)
|
||||||
|
{
|
||||||
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
|
INFO("MQTT: Published\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,6 +89,7 @@ void user_init(void)
|
||||||
MQTT_InitConnection(&mqttClient, sysCfg.mqtt_host, sysCfg.mqtt_port, SEC_SSL);
|
MQTT_InitConnection(&mqttClient, sysCfg.mqtt_host, sysCfg.mqtt_port, SEC_SSL);
|
||||||
MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive);
|
MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive);
|
||||||
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
|
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
|
||||||
|
MQTT_OnPublished(&mqttClient, mqttPublishedCb);
|
||||||
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
|
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
|
||||||
MQTT_OnData(&mqttClient, mqttDataCb);
|
MQTT_OnData(&mqttClient, mqttDataCb);
|
||||||
|
|
||||||
|
@ -86,6 +113,12 @@ void MQTT_Publish( MQTT_Client *client,
|
||||||
**Default configuration**
|
**Default configuration**
|
||||||
See: *include/user_config.h* and *include/config.c*
|
See: *include/user_config.h* and *include/config.c*
|
||||||
If you want to load new default configurations, just change the value of CFG_HOLDER in ***include/user_config.h***
|
If you want to load new default configurations, just change the value of CFG_HOLDER in ***include/user_config.h***
|
||||||
|
Now in the Makefile, it will erase section hold the user configuration at 0x3C000
|
||||||
|
|
||||||
|
```bash
|
||||||
|
flash: firmware/0x00000.bin firmware/0x40000.bin
|
||||||
|
$(PYTHON) $(ESPTOOL) -p $(ESPPORT) write_flash 0x00000 firmware/0x00000.bin 0x3C000 $(BLANKER) 0x40000 firmware/0x40000.bin
|
||||||
|
```
|
||||||
|
|
||||||
**Create SSL Self sign**
|
**Create SSL Self sign**
|
||||||
```
|
```
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
#define _USER_CONFIG_H_
|
#define _USER_CONFIG_H_
|
||||||
#include "user_interface.h"
|
#include "user_interface.h"
|
||||||
|
|
||||||
#define CFG_HOLDER 0x00FF55A2
|
#define CFG_HOLDER 0x00FF55A4
|
||||||
#define CFG_LOCATION 0x3C
|
#define CFG_LOCATION 0x3C /* Please don't change or if you know what you doing */
|
||||||
|
|
||||||
/*DEFAULT CONFIGURATIONS*/
|
/*DEFAULT CONFIGURATIONS*/
|
||||||
|
|
||||||
#define MQTT_HOST "mqtt.yourserver.com" //or "192.168.11.1"
|
#define MQTT_HOST "mqtt.yourdomain.com" //or "192.168.11.1"
|
||||||
#define MQTT_PORT 8443
|
#define MQTT_PORT 8443
|
||||||
#define MQTT_BUF_SIZE 1024
|
#define MQTT_BUF_SIZE 1024
|
||||||
#define MQTT_KEEPALIVE 120 /*second*/
|
#define MQTT_KEEPALIVE 120 /*second*/
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
#define MQTT_PASS "DVES_PASS"
|
#define MQTT_PASS "DVES_PASS"
|
||||||
|
|
||||||
#define STA_SSID "DVES_HOME"
|
#define STA_SSID "DVES_HOME"
|
||||||
#define STA_PASS "dvespassword"
|
#define STA_PASS "wifipassword"
|
||||||
#define STA_TYPE AUTH_WPA2_PSK
|
#define STA_TYPE AUTH_WPA2_PSK
|
||||||
|
|
||||||
#define MQTT_RECONNECT_TIMEOUT 5 /*second*/
|
#define MQTT_RECONNECT_TIMEOUT 5 /*second*/
|
||||||
|
|
11
user/mqtt.c
11
user/mqtt.c
|
@ -168,6 +168,8 @@ mqtt_tcpclient_recv(void *arg, char *pdata, unsigned short len)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
INFO("MQTT: Connected to %s:%d\r\n", client->host, client->port);
|
INFO("MQTT: Connected to %s:%d\r\n", client->host, client->port);
|
||||||
|
if(client->connectedCb)
|
||||||
|
client->connectedCb((uint32_t*)client);
|
||||||
client->connState = MQTT_SUBSCIBE_SEND;
|
client->connState = MQTT_SUBSCIBE_SEND;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -346,11 +348,6 @@ mqtt_tcpclient_connect_cb(void *arg)
|
||||||
espconn_regist_sentcb(client->pCon, mqtt_tcpclient_sent_cb);///////
|
espconn_regist_sentcb(client->pCon, mqtt_tcpclient_sent_cb);///////
|
||||||
INFO("MQTT: Connected to broker %s:%d\r\n", client->host, client->port);
|
INFO("MQTT: Connected to broker %s:%d\r\n", client->host, client->port);
|
||||||
client->connState = MQTT_CONNECT_SEND;
|
client->connState = MQTT_CONNECT_SEND;
|
||||||
if(client->connectedCb)
|
|
||||||
client->connectedCb((uint32_t*)client);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
|
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -562,3 +559,7 @@ void MQTT_OnData(MQTT_Client *mqttClient, MqttDataCallback dataCb)
|
||||||
{
|
{
|
||||||
mqttClient->dataCb = dataCb;
|
mqttClient->dataCb = dataCb;
|
||||||
}
|
}
|
||||||
|
void MQTT_OnPublished(MQTT_Client *mqttClient, MqttCallback publishedCb)
|
||||||
|
{
|
||||||
|
mqttClient->publishedCb = publishedCb;
|
||||||
|
}
|
||||||
|
|
|
@ -92,6 +92,7 @@ typedef struct {
|
||||||
mqtt_connect_info_t connect_info;
|
mqtt_connect_info_t connect_info;
|
||||||
MqttCallback connectedCb;
|
MqttCallback connectedCb;
|
||||||
MqttCallback disconnectedCb;
|
MqttCallback disconnectedCb;
|
||||||
|
MqttCallback publishedCb;
|
||||||
MqttDataCallback dataCb;
|
MqttDataCallback dataCb;
|
||||||
ETSTimer mqttTimer;
|
ETSTimer mqttTimer;
|
||||||
uint32_t keepAliveTick;
|
uint32_t keepAliveTick;
|
||||||
|
@ -121,6 +122,7 @@ void MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32 port, ui
|
||||||
void MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime);
|
void MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime);
|
||||||
void MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb);
|
void MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb);
|
||||||
void MQTT_OnDisconnected(MQTT_Client *mqttClient, MqttCallback disconnectedCb);
|
void MQTT_OnDisconnected(MQTT_Client *mqttClient, MqttCallback disconnectedCb);
|
||||||
|
void MQTT_OnPublished(MQTT_Client *mqttClient, MqttCallback publishedCb);
|
||||||
void MQTT_OnData(MQTT_Client *mqttClient, MqttDataCallback dataCb);
|
void MQTT_OnData(MQTT_Client *mqttClient, MqttDataCallback dataCb);
|
||||||
void MQTT_Subscribe(MQTT_Client *client, char* topic);
|
void MQTT_Subscribe(MQTT_Client *client, char* topic);
|
||||||
void MQTT_Connect(MQTT_Client *mqttClient);
|
void MQTT_Connect(MQTT_Client *mqttClient);
|
||||||
|
|
|
@ -54,6 +54,7 @@ void mqttConnectedCb(uint32_t *args)
|
||||||
{
|
{
|
||||||
MQTT_Client* client = (MQTT_Client*)args;
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
INFO("MQTT: Connected\r\n");
|
INFO("MQTT: Connected\r\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqttDisconnectedCb(uint32_t *args)
|
void mqttDisconnectedCb(uint32_t *args)
|
||||||
|
@ -61,9 +62,28 @@ void mqttDisconnectedCb(uint32_t *args)
|
||||||
MQTT_Client* client = (MQTT_Client*)args;
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
INFO("MQTT: Disconnected\r\n");
|
INFO("MQTT: Disconnected\r\n");
|
||||||
}
|
}
|
||||||
void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh)
|
|
||||||
|
void mqttPublishedCb(uint32_t *args)
|
||||||
{
|
{
|
||||||
INFO("MQTT topic: %s, data: %s \r\n", topic, data);
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
|
INFO("MQTT: Published\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)
|
||||||
|
{
|
||||||
|
char topicBuf[64], dataBuf[64];
|
||||||
|
MQTT_Client* client = (MQTT_Client*)args;
|
||||||
|
|
||||||
|
os_memcpy(dataBuf, topic, topic_len);
|
||||||
|
topicBuf[topic_len] = 0;
|
||||||
|
|
||||||
|
os_memcpy(dataBuf, data, data_len);
|
||||||
|
dataBuf[data_len] = 0;
|
||||||
|
|
||||||
|
INFO("MQTT topic: %s, data: %s \r\n", topicBuf, dataBuf);
|
||||||
|
|
||||||
|
/* Echo back to /echo channel*/
|
||||||
|
MQTT_Publish(client, "/echo", dataBuf, data_len, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -78,6 +98,7 @@ void user_init(void)
|
||||||
MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive);
|
MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive);
|
||||||
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
|
MQTT_OnConnected(&mqttClient, mqttConnectedCb);
|
||||||
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
|
MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb);
|
||||||
|
MQTT_OnPublished(&mqttClient, mqttPublishedCb);
|
||||||
MQTT_OnData(&mqttClient, mqttDataCb);
|
MQTT_OnData(&mqttClient, mqttDataCb);
|
||||||
|
|
||||||
WIFI_Connect(sysCfg.sta_ssid, sysCfg.sta_pwd, wifiConnectCb);
|
WIFI_Connect(sysCfg.sta_ssid, sysCfg.sta_pwd, wifiConnectCb);
|
||||||
|
|
Ładowanie…
Reference in New Issue